Unlike Primitive data types in java, Reference data type store the address of object in memory which they refer to, than actual value.
lets say, Car class is already defined, and its object is created in memory then variable declared with type Car becomes the reference type which will be holding address of car object to which it refers.
Car car = new Car();
int noOfDoors = 4;
Car myCar = car; //copy the car reference into myCar reference.
Here, variable car actually contains the address and variable noOfDoors will contain actual 4 value.
See below diagram for more explanation:
Available reference types in java:
lets say, Car class is already defined, and its object is created in memory then variable declared with type Car becomes the reference type which will be holding address of car object to which it refers.
Car car = new Car();
int noOfDoors = 4;
Car myCar = car; //copy the car reference into myCar reference.
Here, variable car actually contains the address and variable noOfDoors will contain actual 4 value.
See below diagram for more explanation:
Reference data type holding address of object in heap memory and primitive data type holds actual primitive values |
Available reference types in java:
- Class type
- Interface type
- array type
e.g.
String name = new String("Java");
List listOfNames = new ArrayList();
Students topRankers[] = new Student[10];
Remember though you know now that Reference data type holds the address of object in memory but they are not same as pointers in C/C++ language. java handles it differently, it refers the object by storing address but will not allow pointer operations over declared references.
Consider it just handler to object in heap memory to access the object and do object to object communication.
Comments
Post a Comment