In Java, does an object variable contain the address of an object?

I spoke with my teacher, and she mentioned that the object variable (it means an instance of the object) does not contain the object itself, but rather an address in memory.

I heard that in Java, an instance of an object does indeed contain a reference to the object in memory. Am I mistaken? Is the link the same as the address in memory, or something else?

+4
source share
7 answers

The object variable does not match the instance of the object. It is very important to distinguish between variables, their values ​​and objects.

For instance:

String x = "hello"; 

Variable x . It is like a piece of paper on which a value can be written.

The value of the variable is a reference — some data that the VM can use to access the object itself as a string. It doesn’t have to be an address - it’s just a “way to get to the object’s data” (For more on this, read Eric Lippert's blog, “Links are not addresses” - this is talking about C #, not Java, but it's the same principle.)

The object itself is a separate object.

To use a real-life example, imagine that I have a piece of paper with my home address written on it. There are three things here:

  • Paper (as a variable). The paper itself is not my home address, and it is not a home. It is just that which can hold value.
  • My home address is not a piece of paper and is not a home. This is just a value that allows someone to get to my house.
  • My home is neither a sheet of paper nor an address in itself. This is an object.

This becomes important if you consider things like passing parameters and assigning variables. For instance:

 House x = new House("Jon"); House y = x; 

Here we have two variables, x and y , like two sheets of paper. We build a house and write it on x . Then we copy the value that is written in x to y . Note that they are still completely separate pieces of paper - but they currently have the same meaning written on them. There is only one object - we built only one house, but now it has the same directions as on two sheets.

If one person followed the directions on sheet of paper x and painted the front door in red, then the second person followed the directions on sheet of paper y , they will find a house with a red front door.

On the other hand, if one person scribbled directions on sheet of paper x , this would not affect the directions written on sheet of paper y at all.

+11
source

Well, not exactly the memory address, but something that can be translated into the JVM, yes.

Thing Foo foo = new Foo() is a reference only to Foo , and not to the entire structure of Foo .

+5
source

A link is the same as an in-memory address of an object. “Memory” is actually more like a piece of space for creating things that are clearly not related to execution (call frames, etc. - this is what is actually used to execute).

+1
source

Yes sorting. When you use a new operator to highlight an object. It stands out on the heap, and a link to that location is returned. When you declare a variable like: Object foo. foo and does not stand out on the heap. Variables are not objects, but a reference to an object. They are created on the stack for local variables or in an object if it is an instance variable. foo might refer to something on the heap. Its very similar to a pointer to C, but the main difference is that its not just a memory address (the details of why are a bit complicated, and for all intensive purposes you can think of a link as a pointer). The big difference you have in Java than C is that you cannot manipulate the pointer. You cannot determine its meaning, and you cannot do any math. This is mainly for your own and security programs. You can change what foo points to by assigning it to another link, or null, as shown in the figure.

0
source

Consider

  A a1 = new A(); 

here A is a class a1 is a reference variable indicating somewhere in HEAP memory, Area in Heap memory (aka object) Holds attributes and methods (methods are used to send messages and are stored in the method table)

Now point convention is not required if the object is called from the class in which the object is created, otherwise we must go for the point

as

  class A { static int age; static int email; mehtod1() { } mehtod2() { } ... ... ... A a1 = new A(); A a2 = new A(); A a3 = a1; } 

now a1 and a3 are one and the same, each a1, a2, a3 have access to only one copy of static global attributes (not one per object) and email;

now

  class B { some attributes; ... ... ... a1.method1();// will pass method 1 to object1 } 

In java calls, objects are similar to working with math (with an expression between two variables 2 + 4 = 6) and English

Like ROBOT MOVES LEFT BY 5

  robot.movesleft(5); // robot is object and movesleft is method or function which tells robot what to do can be anything like eat(banana), sleep(50) blah blah 

replace + point

  Objects are purely handled at JVM and Computer doesnot know what objects are(hardware only knows subroutines(sub-functions) and attributes) 

IF Somewhere I'm wrong, please correct me

0
source

Car c = new car ();

In the above example, c is a reference variable that contains a reference identifier that is generated by a new statement that contains the location of an object in the form of hashcode due to a security purpose. The java link identifier is like a pointer to a pointer to C ++, because it is a memory cell that contains the memory location of the object where it is actually present. The reference identifier always gets memory on the Stack, and Object always gets the memory on the heap.

0
source

The instance does not contain a memory address, instead it contains some address that only the JVM can understand, in the example below h1 contains " com.mmm.examples.Hippo@3d4eac69 " , which is " packageName.ClassName@someaddress "

Example:

 Hippo h1 = new Hippo(); System.out.println(h1); System.out.println("h1.size = " + h1.size);// com.mmm.examples.Hippo@3d4eac69 
-one
source

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


All Articles