aspose file tools*
The moose likes Beginning Java and the fly likes Object and reference accessing for primitives, objecst and collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object and reference accessing for primitives, objecst and collections" Watch "Object and reference accessing for primitives, objecst and collections" New topic
Author

Object and reference accessing for primitives, objecst and collections

Raymond Ong
Ranch Hand

Joined: Jul 17, 2005
Posts: 46
Hi,

I have questions re objects, primitives and collection accessing and references

I made a simple class


the second class uses the first class. running the second class outputs the following...

INSIDE SETTER : same object
INSIDE SETTER : equal values
SampleClassUser MAIN : equal values
INSIDE SETTER : same object
INSIDE SETTER : equal values
INSIDE CONSTRUCTOR : same object
INSIDE CONSTRUCTOR : equal values
INSIDE SETTER : same object
INSIDE SETTER : equal values
sc1 and sc2 : equal values

...i'm just curios why the last 3 lines are the way they are.

INSIDE SETTER : same object
INSIDE SETTER : equal values
sc1 and sc2 : equal values

how come while inside the setter method, the objects are the same object, and after leaving the setter method are not the same objects?

Can anyone point a good book that shows in detail how objects, primitives and collections are referenced, especially when passed to methods. Online reference is preferred since the availability of books can be a problem for me.

Thanks very much
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Hello Raymond,

Welcome to JavaRanch. I'll take a look thorough your code in a few minutes and see if I can give you an answer to your question. In the meantime, here are some recommendations on some books/references that you can look at.

The best source that I, and most people here, can recommend for learning about and understanding variables, references, & constructors, the difference between reference variable equality and object equality, and the difference between == and .equals(),is going to be Head First Java 2nd Edition. You will see a gazillion posts here at JavaRanch singing its praises; and of the hundreds (if not thousands) of intro Java programming books out there, it has consistently been the number one seller. It explains concepts such as this like no other book. After reading HFJ, you truly will understand (i.e. not just "kind of" understand) complex concepts. It is however something you would have to purchase. You can check e-bay or Amazon for a used copy (of either the first or second edition). Also check if your library has a copy available. I know what it is like to be a student, and with the cost of computer books today being what they are... however, if you can find a way to swing it, the purchase of this book will be one of the best investments you'll ever make in your programming education & career.

There is a good opensource and thus free e-book you can download. It is called Thinking in Java. I have never read the book through, but of the excerpts I have read, it does appear to be a good book, and I have seen several positive comments about it here at the Ranch.

Lastly, here are some links to some other online tutorials and books I have in my bookmarks. Some of these I have not looked at for a while, so buyer be aware...

Sun's Online Java Tutorial
Interactive Programming In Java
Introduction to Programming Using Java

You can also do a search of this Java in General (beginners) using something along the lines of online tutorial and book recommendation as a search terms. I know there have been a lot of previous threads of people asking for some recommendations.
[ July 17, 2005: Message edited by: Mark Vedder ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
From what I understand, the code of interest is the last few lines of code in main():

Notice that you are comparing the objects returned by sc2.getAttribute() and sc1.getAttribute() after you have changed the attribute by calling sc2.setAttribute(). This means that the attribute in sc2 is no longer the same object as that in sc1. If you look closely at the setAttribute() method, it is comparing AFTER you change the attribute to refer to the constant String object "test". This means that the setter is ALWAYS going to print the "same object" message. After an assignment, the two references will refer to the same object.

To experiment further, perhaps you should add another set of if statements in main between these two lines:



Doing so should help clear up your doubts about what is happening here.

Keep Coding!

Layne
[ July 17, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Let me add one piece of advice to all the excellent advice you've received above:

Take it one thing at a time.

Your example code conflates at least a dozen different fundamental concepts: object identity, copying objects, member variables, method parameter passing, and the unique behavior of String literals, to name a few. Correctly explaining how every line of this code behaves, and why, would take a few book chapters. It looks like you're anxious to learn, and that's great. But learning things all in a hodgepodge like this very often leads to incorrect or superstitious beliefs about why and how things work. Try to take things slow and learn one step at a time, using a good introductory text (like HFJ, or any of the others you can read about in our bunkhouse.) You'll be glad you did.


[Jess in Action][AskingGoodQuestions]
Raymond Ong
Ranch Hand

Joined: Jul 17, 2005
Posts: 46
Hi Mark,
Thanks, I'll try to get myself a copy. It's the availability of the book here in my country I'm worried about. Nevertheless, I'll try to get a copy and search the forums as you've suggested.

Hi Layne,
I think i've got it. I inserted the set of if's just to check and indeed sc1.getAttribute() was equal to sc2.getAttribute(). Then after sc1.setAttribute("test"), they no longer pointed to the same object due to the immutability of Strings. Thanks.

Hi Ernest,
I think I may have experimented too much too soon. 'Will try to grasp the basic concepts first. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object and reference accessing for primitives, objecst and collections