Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

like image 911
Bhaxy Avatar asked Jan 19 '12 21:01

Bhaxy


People also ask

Can I get address of an object in Java?

The hashCode() method is native because in Java it is impossible to find the address of an object, so it uses native languages like C/C++ to find the address of the object. Use of hashCode() method: It returns a hash value that is used to search objects in a collection.

What does an object variable contain?

An object variable contains a pointer to data that is stored elsewhere. The type of that data can change during run time.

What is an object variable in Java?

An object variable is a container that holds a reference to a specific instance of a class. Otherwise known simply as a "variable" or "member"

What is the address of a variable in Java?

There is no element in the java language that allows you the get the address of anything and more importantly there is no language element ever requiring an address for anything. Thats why there is no address-of operator in java. The whole language is designed to work without.


1 Answers

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.

like image 153
Jon Skeet Avatar answered Oct 13 '22 20:10

Jon Skeet