Variables in Java are classified into primitive and reference variables.
A primitive variable's information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable.
Reference variables are practically always objects in Java.
The value of a primitive variable is concrete, whereas the value of a reference variable is a reference.
When we attempt to print the value of a reference variable, the output contains the type of the variable and an identifier created for it by Java: the string Name@4aa298b7
tells us that the given variable is of type Name
and its identifier is 4aa298b7
. This happens when we try to print the value of a reference type as the reference type object is first converted to string then printed. This conversion to String happens this way.
You can modify the default print by defining the toString
method within the class of the given object, where you specify what the objects print should look like.
Java has eight different primitive variables
boolean
(a truth value: either true
or false
)byte
(a byte containing 8 bits, between the values -128
and 127
)char
(a 16-bit value representing a single character)short
(a 16-bit value that represents a small integer, between the values -32768
and 32767
)int
(a 32-bit value that represents a medium-sized integer, between the values $-2^{31}$ and $2^{31}-1$)long
(a 64-bit value that represents a large integer, between values $-2^{63}$ and $2^{63}-1$)float
(a floating-point number that uses 32 bits)double
(a floating-point number that uses 64 bits)Declaring a primitive variable causes the computer to reserve some memory where the value assigned to the variable can be stored. The size of the storage container reserved depends on type of the primitive.
Any object instanced from a class is a reference variable.
The constructor call returns a value that is a reference to the newly-created object.