In Java programming language, a variable is a named memory location that stores a value of a specific data type. A variable can be assigned a value, which can be changed during the execution of the program.
In Java, there are three types of variables:
- Primitive data type variables: These variables store simple data types such as integers, floats, booleans, and characters. Examples of primitive data type variables are int, float, boolean, and char.
- Object reference variables: These variables store references to objects in memory. They are used to access the properties and methods of objects. Examples of object reference variables are String, ArrayList, and Object.
- Array variables: These variables store references to arrays. They are used to access the elements of an array. Examples of array variables are int, float, and String.
Primitive Data Type
In Java, a primitive data type is a basic data type that is predefined by the language and is used to define variables that can store simple values. There are eight primitive data types in Java, which are:
- byte: Used to store integer values from -128 to 127.
byte myByte = 42;
- short: Used to store integer values from -32,768 to 32,767.
short myShort = 12345;
- int: Used to store integer values from -2^31 to 2^31-1.
int myInt = 1234567890;
- long: Used to store integer values from -2^63 to 2^63-1.
long myLong = 123456789012345L;
- float: Used to store floating-point values with single-precision.
float myFloat = 3.14f;
- double: Used to store floating-point values with double-precision.
double myDouble = 3.14159265359;
- char: Used to store a single character.
char myChar = 'a';
- boolean: Used to store a true or false value.
boolean myBoolean = true;
Object Reference Variables
In Java, an object reference variable is a variable that holds a reference or memory address of an object in the memory. When an object is created, it is stored in the heap memory and its reference is stored in the reference variable. The reference variable allows us to access and manipulate the properties and methods of the object.
For example, consider the following code snippet:
String myString = new String("Hello World");
In this code, we create a new object of the
String class and initialize it with the value “Hello World”. The
new keyword allocates memory for the object in the heap and returns its reference, which is then assigned to the
myString reference variable.
We can then use the reference variable to access and manipulate the properties and methods of the
String object, such as:
int length = myString.length(); // returns the length of the string (11) String upperCase = myString.toUpperCase(); // returns a new string with all characters in upper case ("HELLO WORLD")
Object reference variables are an important part of object-oriented programming, as they allow us to create and manipulate complex data structures and objects in Java.
In Java, an array variable is a variable that can hold a fixed number of values of the same data type. An array is an object that stores a collection of elements of the same data type, which can be accessed using an index number.
To create an array in Java, we need to specify the data type of the elements and the number of elements in the array. For example, the following code snippet creates an array of integers with 5 elements:
int myArray = new int;
In this code, we create an array of integers with 5 elements, and the
new keyword allocates memory for the array in the heap. The reference to the array is stored in the