Bhaxy
Bhaxy

Reputation: 5574

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

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

I've heard that in Java, an instance of an object really contains a reference to an object in memory. Am I wrong? Is a reference the same thing as containing the address in memory, or something else?

Upvotes: 5

Views: 4997

Answers (7)

Mohammed shakeel M
Mohammed shakeel M

Reputation: 1

The instance does not hold memory address instead it holds some address which only JVM can understand, in an example below h1 holds "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

Upvotes: -1

Deepak Rawat
Deepak Rawat

Reputation: 1

Car c=new car();

In above example c is a reference variable it contains the reference ID that is generated by new operator that contains the location of the object in the hashcode form because of security purpose. Reference ID in java is like a pointer to pointer in C++ because it is a memory location which contains the memory location of object where it is actually present. Reference ID always gets memory in Stack and Object are always gets memory in Heap.

Upvotes: 0

atul
atul

Reputation: 1

Consider

 A a1 = new A();

here A is class a1 is reference variable pointing to somewhere in HEAP Memory, Area in Heap memory(aka object) Holds attributes and methods(methods are used for message passing and stored in method table)

Now dot convention is not required if object is called from the class in which object is created else we have to go for dot

like

 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) age and email;

now

 class B
 {

 some attributes;
...
 ...
 ...

 a1.method1();// will pass method 1 to object1

}

IN java calls on objects are nearly like working with Mathematics(with expression in 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 + by dot

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

IF Somewhere I am wrong please correct me

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500385

An object variable isn't the same as an instance of an object. It's really important that you distinguish between variables, their values, and objects.

For example:

String x = "hello";

The variable is x. It's like a piece of paper, which can have a value written on it.

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

The object itself is a separate entity.

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

  • The piece of paper (like a variable). The piece of paper itself isn't my home address, nor is it a house. It's just something which can store a value.
  • My home address isn't a piece of paper, nor is it a house. It's just a value which lets someone get to my house.
  • My house is neither a piece of paper, nor an address in itself. It's an object.

This becomes important when you consider things like parameter passing and variable assignment. For example:

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

Here we have two variables, x and y, like two pieces of paper. We build a house, and write down the directions to it on x. We then copy the value that's written on x into y. Note that they're still completely separate bits of paper - but they currently have the same value written on them. There's only a single object - we've only built one house - but now two pieces of paper have the same directions on them.

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

On the other hand, if one person scribbled out the directions on piece of paper x, that wouldn't affect the directions written on piece of paper y at all.

Upvotes: 13

chubbsondubs
chubbsondubs

Reputation: 38696

Yes sorta. When you use the new operator to allocate an object. It's allocated on the heap, and the reference to that location is returned. When you declare a variable like: Object foo. foo variable and is not allocated on the heap. Variables aren't objects, but a reference to an object. They are created on the stack for local variables or in the object if its an instance variable. foo can refer to something on the heap. Its very much like a pointer from C, but the main difference is that its not simply a memory address (the details of why are a little complex, and for all intensive purposes you can think of a reference as a pointer). The big difference you have in Java, than C, is you can't manipulate pointer. You can't determine its value, and you can't do any sort of math on it. These are mostly for your own and the programs safety. You CAN change what foo points to by assigning it to another reference, or null as in pointing at nothing.

Upvotes: 0

cdeszaq
cdeszaq

Reputation: 31280

A reference is the same thing as the address of the object "in memory". The "memory" is really more like a chunk of space to create things not related explicitly to execution (call frames, etc. are what actually is used for execution)

Upvotes: 1

Bozho
Bozho

Reputation: 597076

Well, not exactly a memory address, but something translatable to that in the JVM, yes.

The thing is Foo foo = new Foo() is only a reference to Foo, rather than the whole structure of Foo.

Upvotes: 5

Related Questions