What is the difference between object, reference identifier and reference variable in Java?

What is the difference between java:

  • An object

  • Link Code

  • Reference variable

When I see the following statements:

Emp e = new Emp(); 

Here Emp is a class, but e not its object? If so .. somewhere else I saw it:

cos, if so, somewhere they see how

 Emp e = new Local(); 

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

+4
source share
8 answers

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

Java object variables (such as e , in this example) contain references to objects that live on the heap.

Classes are completely different from all of these; they can describe the structure of objects of this type and have implementations of methods, etc., but classes live in a completely different memory area from other objects.

+3
source
 Emp e 

This statement creates the reference variable 'e' on the stack.

  new Emp() 

This statement creates an object on the heap. An object is just a buffer, or we can say “piece of memory”. Therefore, the buffer is stored on the heap. Thus, the statement

  Emp e=new Emp() 

passes the reference identifier of this object created on the heap to the reference variable 'e'.

+2
source
 Emp{ int salary; int name; } 

New new Emp (); Emp is a class, new is used to reserve the memory area on the heap. New returns the address of the memory that it has reserved. Once you write the new Emp (), the memory buffer will be reserved on the heap. The size of this reserved area depends on the size of the data members of the class (Here it is 2 bytes, since int salary takes 1 byte, and int name takes 1 byte.)

Link identifier The reference identifier of an object is the address of the place where the object is stored. new Emp(); creates an object, but its address is not stored anywhere and is not intercepted.

Reference variable Now suppose that you want to use the same object over and over, then you should have the address of its location (reference identifier). You can use a reference variable to store its addressid (ReferenceId). The reference variable always takes 4 bytes, and this is just the variable in which the address is stored (object reference).

Creating a reference variable Emp e2;

Assigning a link identifier to a reference variable Emp e2 = new emp ();

+1
source

This is a simple question ...

 emp e=new emp(); 

Here e is the reference identifier of the object. emp is the reference variable for the class, and your object identifier is distinguished by its combination of state and behavior.

0
source

I just made a program to display the reference identifier 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: awesome hexadecimal string:

"a @ 52e922"

Java displays the actual location of an object in a separate place as a hexadecimal string called a reference identifier. It never displays the actual location of its object stored in memory.

0
source

Car c = new car ();

An object is nothing more than a buffer or memory on the heap where non-static data elements receive memory.

The link identifier is generated by the new operator on the stack, and this is a memory cell that contains the location of the object's memory in the form of a hash code. Link identifier is the only way to reach the object. The link identifier is generated due to the fact that in java there is a rule that the memory allocated at runtime does not have any name, and we all know that objects are created at runtime, therefore they also have no name, and we need a unique identifier to perform operations on this object, why there is a unique link identifier in java for each object.

In the above example, c is the reference variable in which the reference identifier is stored.

0
source

Here:

 Emp e=new Emp(); 

e is a reference variable that contains the address of the object that is being created in the heap area.

The reference identifier is generated as a hash code using the object’s magic method (the object has 9 methods in total), that is, the toString() method; internally, using the toString object method, it automatically generates a Ref ID for each object created at runtime.

Memory for an object is always reserved in the heap area of ​​RAM. Bcz does not have an explicit pointer available in Java. Here, the ref variable points to the stack to give a reference to an object to reserve memory in Heap; The bcz object also does not know where the memory is located.

The stack segment is also part of the memory: when we pass the variable ref, then the variable ref must already be stored on the stack, then the variable ref knows which part of the head’s memory is free, and it points to the backup location of the object and is stored in the reference variable.

"new" is a text operator that helps create objects.

Emp() : a child class of the class; if the constructor did not explicitly provide the constructor in the java program, the compiler implicitly adds the default constructor, and the reason for assigning the name of the child class as well as the class name is because where "new" can easily find out how much memory is required for non members of static data.

0
source

An object is nothing, it's just a memory region or a buffer in the heap region, where all the data members of the class instance receive memory.

Emp e = new Emp ();

In the above statement, e is a reference variable that contains the reference identifier of the object, however, for security reasons, java does not allow anyone to get the identifier of the real object, it can also be self-evident, since we use the identifier of the link to the word, which redirects us that we we don’t get the actual identifier of our object, but just a link to it.

also a reference identifier would be a nomenclature like class name @ hexadecimal representation # of object code.

-1
source

Source: https://habr.com/ru/post/1414515/


All Articles