This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Object Reference Variable Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Object Reference Variable Question " Watch "Java Object Reference Variable Question " New topic
Author

Java Object Reference Variable Question

lee tierra
Greenhorn

Joined: Sep 26, 2009
Posts: 5
Hi can you please explain the areas I have crossed blue (in the attached image) as Im not quite confident in my understanding of this ...heres what I understand it to be...

These numerics (memory address #) represent the reference to the instance (object) called Shirt with the Myshirt object variable holding the reference to this object, this is used to access the object attributes using the dot notation. the rest I understand 100% just this I keep getting confused on for some reason..

Have I got that right ?

Thanks

Jef



[Thumbnail for java_q.GIF]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Welcome to JavaRanch

I am afraid I can't read the text on your screenshot. Please copy and paste the text.
lee tierra
Greenhorn

Joined: Sep 26, 2009
Posts: 5
Campbell Ritchie wrote:Welcome to JavaRanch

I am afraid I can't read the text on your screenshot. Please copy and paste the text.


Hey Cambell thanks for the welcome ...you can download this rite ? its a gif file..

thanks

Jef
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Yes, I managed to read it on the download, but it would be better to copy the text too.
I presume those are the memory locations. Not sure how you managed to find them; locations are not usually easily available in Java. We don't consider you actually need to know a memory location.

Does the Shirt class have an overridden hashCode method? Does it have an overridden toString method? If neither method is overridden, try . . . and see whether there are any similarities between the numbers displayed after the @
lee tierra
Greenhorn

Joined: Sep 26, 2009
Posts: 5
Campbell Ritchie wrote:Yes, I managed to read it on the download, but it would be better to copy the text too.
I presume those are the memory locations. Not sure how you managed to find them; locations are not usually easily available in Java. We don't consider you actually need to know a memory location.

Does the Shirt class have an overridden hashCode method? Does it have an overridden toString method? If neither method is overridden, try . . . and see whether there are any similarities between the numbers displayed after the @


Thanks Cambell they are actually screenshots from my college IT tutorial class, they are teaching us what the difference between an object and an object reference variable is and how these 2 correlate with each other.so its rather an example than actual code I have. I just needed a little help with my understanding of the concept as Im a little unsure.

Thanks

Jef ;-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
You don't usually see the actual memory locations.

Each class object has a space for the methods. When you call a method, you pass back the memory location of the Shirt object (myShirt or yourShirt) as well as any other information required (= method parameters).
Each Shirt object has a memory location for its class object in, as well as locations for all its fields. So a Shirt object at 0x123abc will have data in maybe like thisWhen you call myShirt.price = 123.45 the . allows the JVM to find the location of the price field from the location of the Shirt object.

When you write myShirt the JVM will note the memory location, in this example 0x123abc. Note we do not usually see that location, so I am not sure how your teacher found it (that would be interesting to find out), and the location varies from one run of the application to another. In fact the memory location may be maintained as a handle ("if you look in location 0x234bcd you will find the memory location of the Shirt object") and the contents of the handle and the location of the Shirt object may be changed while the application is running.

If you don't have overridden hashCode and toString methods, then printing the object (as I suggested earlier) will give you a guess at its memory location. Note this is only a guess and its location might change later.

So I think your interpretation is correct.

I hope I have clarified things more than confusing them
lee tierra
Greenhorn

Joined: Sep 26, 2009
Posts: 5
Hi Cambell thanks for the amount of knowledge you sent me Im still absorbing this thanks.

I have only been studying Java programming for 6 weeks and as a newb think my particular answer will probably be a little less complex than this ..lol ...Im still unsure and wanted to know wether my understanding was correct as shown in my inital question and also to know what the alpha numeric code was, how it relates and wether I was correct on this?...thank you again Jef
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
That's not alphanumeric; it's a hexadecimal number. It is customary to prefix hexadecimal numbers with 0x.
lee tierra
Greenhorn

Joined: Sep 26, 2009
Posts: 5
ok got it ....so is my understanding of this process correct ? was my question.

This Hexadecimal notation represent the reference to the instance (object) called Shirt with the Myshirt object variable holding this reference to this object, this is used to reference the objects attributes and can access these attributes using the dot notation. the rest I understand 100% just this I keep getting confused on for some reason.

thanks ....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Object Reference Variable Question
 
Similar Threads
Class Casting
K&B question
reference variable doubt
Interface instance?
eligible for garbage collection