4

What is difference between the following in java :

  1. Object

  2. Reference ID

  3. Reference Variable

When I see statements like this:

Emp e = new Emp();

Here Emp is class, but e is not its object? If that is so .. somewhere else I have seen this:

cos if it is so then ..somewhere is see like

Emp e = new Local(); 

Where local is a child class of Emp. So what does e mean in this case? What does it hold?

Leigh
  • 28,765
  • 10
  • 55
  • 103
Ratan Kumar
  • 1,640
  • 3
  • 25
  • 52

8 Answers8

3

An object is, essentially, a chunk of memory living in the heap. (Part of the memory structure of objects includes a reference to the class of that object.)

Object variables in Java (like e, in this example) contain references to objects living in the heap.

Classes are completely different from all of these; they might describe the structure of objects of that type, and have method implementations and the like, but classes live in an entirely different area of memory from other objects.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • not clear from the above explanation about , reference ID , and Reference Variable . – Ratan Kumar May 26 '12 at 13:38
  • 1
    By "reference variable," you probably mean object variables. There's no such thing as "reference IDs." – Louis Wasserman May 26 '12 at 13:59
  • @LouisWasserman -- In early JNI documentation the JNI (vs Java) reference value for an object was referred to as an "ID". They've apparently gotten away from that notation and now only use "ID" to denote fields and methods. – Hot Licks May 26 '12 at 18:00
  • @HotLicks In what early version? Since at least 1997 when I started using JNI, JNI references have always been of type jobject, jclass, jstring, etc., ultimately being pointers. Method and field references have always been IDs, ultimately being ints. – user207421 May 27 '12 at 00:46
  • The references are not pointers to the objects, but are, directly or indirectly, IDs. – Hot Licks May 27 '12 at 01:17
  • @HotLicks So you said, but you didn't answer my question. – user207421 May 29 '12 at 23:33
  • We got the pre-released documentation from Sun. – Hot Licks May 30 '12 at 00:20
3
Emp e

This statement creates a reference variable 'e' in the stack.

    new Emp()

This statement creates an object in the heap. An object is just a buffer or we can say "a chunk of memory". Hence , a buffer gets reserved in the heap. Thus the statement,

   Emp e=new Emp() 

passes the reference id of that object created in the heap to the reference variable 'e'.

1
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();

Swati Ghai
  • 41
  • 1
  • 5
1

Here:

Emp e=new Emp();

e is reference variable which holds the address of an object which is created in heap area.

Reference id gets generated in form of a hash code, with help of magic method of object (object has total 9 methods) that is toString() method; internally, using the toString method of the object, it automatically generates Ref id for each object created at run time.

Memory for object always gets reserved in Heap area of RAM. bcz of there is no explicit pointer available in java. Here, ref variable points to the stack to give reference to object to reserve memory in Heap; bcz object also don't know where the memory is.

Stack segment is also part of memory: when we give ref variable then ref variable must be already stored in stack then ref variable knows which part of memory of head is free and it points to object reserve place and it hold in reference variable.

'new' is a textable operator which helps to create object.

Emp(): is child class of class; if there no constructor provided in java program by programmer explicitly, then the compiler adds a default constructor implicitly, and reason to give the child class name as same class name is that so where "new" can easily knows how much memory is required for non static data members.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

It's a simple question...

emp e=new emp();

Here, e is reference id to the object. emp is the reference variable to the class and your object id different its the combination of state and behaviour..

zero323
  • 322,348
  • 103
  • 959
  • 935
0

I just made a program for displaying reference ID of an object.

class abc
{

   int a=10;
   int b;
}

class t extends abc
{

   public static void main(String args[])
   {
     abc A=new abc();
     System.out.println(""+A);
   }
}

output : shockingly a hex string :

"abc@52e922"

Java maps the actual location of an object at a separate place in form of a hex string which is known as reference ID. It never displays actual location of it's object stored in the memory.

Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
Jane
  • 43
  • 1
  • 8
0

Car c=new Car();

Object is nothing it is just a buffer or memory in heap where non static data members gets the memory.

Reference ID is generated by new operator in the stack and it is a memory location which contains the memory location of an object in hashcode form. Reference ID is the only way to reach the object. Reference ID is generated because there is a rule in java that memory allocated at run time does not have any name and we all know that objects are created at run time so they also have no name and we need a unique ID to perform operation on that object that's why there is a unique Reference ID in java for each object.

In above example c is a reference variable which store the reference ID.

-1

Object is nothing it is just a memory area or buffer in a heap area where all the instance data members of a class are getting a memory.

Emp e = new Emp();

in the above statement e is a reference variable which hold the reference id of the object, however in for security purpose java is not allowing anyone to get the id of actual object it also can be self explanatory as we are using word reference id which redirects us that we are not getting the actual id of our object instead just a reference of it.

also reference id would be nomenclature as classname@hexadecimal representation of the # code of object.