Site Search:

Differentiate between object reference variables and primitive variables

Back OCAJP

Java only have reference variables and primitive variables.



Primitive variables stores data with primitive data types. Java has 8 primitive data types.


OCAJP>cat test.java 
class test {
boolean a = false;
byte b = 0;
short s = 0;
long l = 0;
float f = 0.0f;
double d = 0.0;
char c = '\u0000';
}
OCAJP>javac test.java

OCAJP>


The numeric primitives can be initialized with corresponding numeric literals.

By default, numeric digits literals (123) is of type int. When postfixing with L (123L), it is of type long.
By default, numeric decimal literals (123.123) is of type double. When postfixing with f (123.123f), it is of type float.
Assign numeric literal with lower precision type to higher precision primitive type is allowed (long a = 123). Assign higher precision literal to lower precision primitive type won't work (int a = 123L), the code won't compile.



OCAJP>cat test.java
class test {
  int i1 = 1;
  int i2 = 1L;  //error: incompatible types: possible lossy conversion from long to int

  short s1 = 1;
  short s2 = 1L;  //error: incompatible types: possible lossy conversion from long to short
  short s3 = 1.1; //error: incompatible types: possible lossy conversion from double to short

  long l1 = 1;
  long l2 = 1L;

  float f1 = 1.1f;
  float f2 = 1.1; //error: incompatible types: possible lossy conversion from double to float

  double d1 = 1.1;
  double d2 = 1.1f;
}
OCAJP>javac test.java 

OCAJP>


Numeric literal can also be expressed in the following way:
octal (base 8), with 0 as prefix.
hexadecimal (base 16), with 0x or 0X as prefix.
binary (base 2), with 0b or 0B as prefix.

There is a Java 7 feature for numeric literals: underscores can be added anywhere except at the beginning and end of the literal, right before and after the decimal point.

OCAJP>cat test.java
class test {
  int i1 = 077;
  //int i2 = 081; //error: integer number too large: 081
  int i3 = 013___4;
  int i4 = 13_____0_9__5;

  short s1 = 0XFF;
  short s2 = 0x09;
  //short s3 = 0xAA_A_A;  //incompatible types: possible lossy conversion from int to short
  //short s4 = 0XG1;  //hexadecimal numbers must contain at least one hexadecimal digit
  
  long l1 = 0B111;
  long l2 = 0b001;
  long l3 = 0b11_01_1;
  //long l4 = 0b_11__0_1;  //error: illegal underscore

  float f1 = 07;
  float f2 = 00____7;
  //float f3 = 0b31;  //binary numbers must contain at least one binary digit

  double d1 = 0xA0;
  double d2 = 100_3_2.00_6f;
  //double d4 = 0XAAA_AA.FF_F;  //malformed floating point literal
}
OCAJP>javac test.java 
OCAJP>


These 8 primitive data types are the building blocks of java objects. Object reference variables are pointers that refers to the memory block storing the objects.  Object reference can be assigned another object of the same type. It can be assigned to a new object with new keyword. It can also be assigned to null, so that it refers to nothing.

OCAJP>cat test.java 
class test {
  public static void main(String[] args){
    test t1 = new test();
    test t2; //default null;
    t2 = t1;
    t2 = null;
    t1 = null;
  }
}
OCAJP>javac test.java
Back OCAJP