What is the difference between an Object , Reference ID , and Reference Variable in Java?

前端 未结 8 862
余生分开走
余生分开走 2020-11-30 13:24

What is difference between the following in java :

  1. Object

  2. Reference ID

  3. Reference Variable

When I see sta

8条回答
  •  情话喂你
    2020-11-30 13:53

    Emp{
    int salary;
    int name;
    }
    

    New new Emp(); Emp is a class, new is used to reserve memory area in heap.New returns the address of memory it reserved. As soon as you write ,new Emp(),a memory buffer gets reserved in heap.Size of this reserved area depends upon size of data members of a class(Here it is 2 bytes since int salary takes 1 byte and int name takes 1 byte.)

    Reference Id Reference id of an object is the address of location where object is stored. new Emp(); creates the object but its address is not stored and catched anywhere.

    Reference Variable Now,Suppose you want to use the same object again and again,then you must have its address location(reference Id). For storing its addressid(ReferenceId) you can use reference variable. Reference variable always take 4 bytes and they are just variable which stores the address(reference of object).

    Creation of reference variable Emp e2;

    Assigning reference Id to a reference variable Emp e2=new Emp();

提交回复
热议问题