aspose file tools*
The moose likes Beginning Java and the fly likes Basic reference question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic reference question" Watch "Basic reference question" New topic
Author

Basic reference question

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Hi All, I am new to this forum. And new to java. And I have some basic questions about object reference. I find it difficult to read the program. So if you can walk me through line by line, it'd be so helpful! Please look at the example below:

source is from http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#12028
In the end, the program produces the output:
i1==3 but i2==4
v1.val==6 and v2.val==6 // how come v1.val also become 6? So the line Value v2=v1, it's not v2 points at v1, it's v1 points at v2?
I think the point of this program is to show you both primitive and object reference.
Thank you!
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
We are dealing with primitives here, so it means i2 is now 3 as well?

Yes.
so i2 is now 4??? then what's the point of the line above?

Yes and you tell us. It's your code. BTW, If you did not write this code yourself, you are obligated to cite your source.
What does + i1 mean? 1+i1??

When referring to Strings, the + operator becomes a concatenator of strings.
Now we are dealing with Object.

Yes.
is val a method here? since it's written that way.

val is an instance variable of the class. You can tell the difference because methods end with parentheses.
v2 also points at v1, refer to the same object? or v1 points at v2?

v2 does not point at v1. v1 and v2 both reference the same object. BTW, we use the term reference in Java - not pointer - because we're not supposed to know how Java does what it does (point variables at objects)
Then I don't get why it's 6 instead of 5?

Because you are only dealing with one object. The member variable val used to hold the value 5. Then you set it to 6.
how come v1.val also become 6? So the line Value v2=v1, it's not v2 points at v1, it's v1 points at v2?

v1 and v2 are references to the same object. When you change the val variable, you change it in the object - not the reference. Again, v1 does not point at v2 and v2 does not point at v1. v1 and v2 reference object instances. In this case, they reference the same object instance.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Thanks, Tom. This is not my code, so I just cited the source. Sorry about that. Here are my following comments.

is val a method here? since it's written that way. val is an instance variable of the class. You can tell the difference because methods end with parentheses.


I don't think I've yet encountered that an object reference followed by an instance variable. What's the purpose of it? Does it just mean that v1 is an object reference, and the actual Object has the content of 5. I assume int val=5? And when we have: Value v1 = new Value(). Does it just mean we create an object reference, not the actual object then?

v2 also points at v1, refer to the same object? or v1 points at v2? v2 does not point at v1. v1 and v2 both reference the same object.

So v2 and v1 now reference to the same object that has the value 5?

Thanks again!
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Note that I'm trying to be specific and verbose in my language here.
I don't think I've yet encountered that an object reference followed by an instance variable. What's the purpose of it? Does it just mean that v1 is an object reference, and the actual Object has the content of 5. I assume int val=5?
Classes can have both methods and variables. You have two classes defined: Value and Test. You've defined a method in Test (main). Test has no variables. Your Value class does not define any methods but it does define a variable of type int called val. If I understand your question correctly, the "actual Object" does not have a value of 5. The "actual Object" has a member variable of type int that has a value of 5 and the value changes to 6 when you change it:
And when we have: Value v1 = new Value(). Does it just mean we create an object reference, not the actual object then?
On that line, you are:
1. Defining a reference variable to a Value object, called v1 (the "Value v1" part)
2. Creating an instance of a Value object (the "new Value()" part)
3. Assigning a reference to the Value object that you created to the variable v1 (the "=" part).
So v2 and v1 now reference to the same object that has the value 5?

Yes, until you change the instance variable, Value.val, to the value 6. Then both v2 and v1 will see 6 because the references don't change - the int value of the object changes.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Welcome to the Ranch Cheryl!

It seems Tom has your answers all cleared up rather nicely but I just wanted to comment on something which is almost off-topic. I read Java forums a lot, and I must say I really appreciated how you setup your question. You wanted a breakdown line by line, so you commented in concise and specific questions directly with your code in question. I hope more people catch on to your thoughtful approach, seems like half the posters in this section don't even post with code tags! Keep up the good work and once again, welcome to the Ranch!


Everyday in every way, we get a little better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Cheryl Scodario wrote:Thanks, Tom. This is not my code, so I just cited the source. . . .
You have misunderstood our requirement. We need you to tell us where the code came from.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Campbell Ritchie wrote:
Cheryl Scodario wrote:Thanks, Tom. This is not my code, so I just cited the source. . . .
You have misunderstood our requirement. We need you to tell us where the code came from.


Cheryl has added the link to the source code in question on her original post
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Classes can have both methods and variables. You have two classes defined: Value and Test. You've defined a method in Test (main). Test has no variables. Your Value class does not define any methods but it does define a variable of type int called val. If I understand your question correctly, the "actual Object" does not have a value of 5. The "actual Object" has a member variable of type int that has a value of 5.


When you say "member variable" of an object, do you mean it is like a trait of the Value Object. Like it is part of it, but not THE object? But the object includes the value 5?

The point of this code is to show that Object is passed also by value, not by reference. But I am still a little confused because what would the result look like if Object is passed by reference? This is something that a lot of people argue about, but I am still not so clear at that... Thanks!


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

Cheryl Scodario wrote:When you say "member variable" of an object, do you mean it is like a trait of the Value Object. Like it is part of it, but not THE object? But the object includes the value 5?

When you write a class, you generally have two things - methods and member variables. Methods are what the object can do, and member variables hold the states. For example, if you wrote a Car class, you might have methods like startEngine(), speedUp(), setReverseGear(), etc. But you need member variables to hold things like color, or modelYear or licensePlateNumber.

No matter how many Car objects you make, they can all share the startEngine() method, since that is basically the same thing for any car. However, each time you make a new car, you need a new variable to hold that specific car's color.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
fred rosenberger wrote: Methods are what the object can do, and member variables hold the states. For example, if you wrote a Car class, you might have methods like startEngine(), speedUp(), setReverseGear(), etc. But you need member variables to hold things like color, or modelYear or licensePlateNumber.


Thanks, fred. So member variables that you guys were talking about are just instance variables? I have always been sort of confused at the definition of state. So state is just like a characteristic of the Object and it is always stored in instance variable?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
mike ryan wrote: . . . Cheryl has added the link to the source code in question on her original post
Thank you. I missed that. Sorry for my mistake.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
I have a question that is related to what I asked above. What does it mean by "Objects themselves are never passed as parameters"? I am always confused at the meaning of 'passing by parameters'.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
It means that instead of the actual object, a reference to that object is passed to a method. So the address of the object is given to a method, and not an actual entire copy of the object.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Thanks! One other question that is related to an unanswered question above. What does it mean by "a Java method can update an object's state, but it cannot replace the contents of an object reference." So the contents of an object reference is just the Object itself? Also, I have always been sort of confused at the definition of state. So state is just like a characteristic/trait of an Object?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

An example:
The reason clearWrongly doen't work is because the sb on lines 13 and 15 is not the same as the sb from lines 5, 6, and 7. They are referencing the same object (until line 15) but they are not 100% the same.

Note that in languages like Visual Basic this is allowed (pass-by-reference is default). Other languages like Pascal/Delphi and C/C++ allow this with a minor change in code. Java doesn't, in any way.

You may also want to read this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
Cheryl, the state of an object is all the information about that object that is visible to the outside world.

Think of a traffic light. When it's green, that's the state of the traffic light. When it's red, it's in a different state.

Normally, we're not very interested in the wiring inside the traffic light. So that's not really part of the traffic light's state. Unless it becomes visible to us! Let's say that the wiring in the traffic light has corroded, and the lights aren't working any more. We say the traffic light is in a poor state
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Stephan van Hulst wrote:Cheryl, the state of an object is all the information about that object that is visible to the outside world.

Also the information that is not visible but influences its behaviour. Using your traffic light example, the time until the next colour switch is not visible but it's part of the state since it influences the behaviour (when the next colour changes).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

Cheryl Scodario wrote:What does it mean by "a Java method can update an object's state, but it cannot replace the contents of an object reference." So the contents of an object reference is just the Object itself? Also, I have always been sort of confused at the definition of state. So state is just like a characteristic/trait of an Object?

Something else to consider:

Say I have a piece of paper with a home address on it. the boss says to me "go to that address and tell me what name is on the mailbox". I do that, and come back with "rosenberger".

Now I give you a photocopy of that piece of paper. then the boss says to you "go to that address and tell me what name is on the mailbox". You would go to the same house, and also come back with "rosenberger".

Now the boss says "erase the address on your piece of paper, and write this NEW address down. then go to THAT address, and give me the name on the mailbox". You may come back with "Scodario". The boss says "OK, you're done, throw your paper away".

Then the boss comes back to me and says "What was that name again?" The address on MY paper never changed, so I still go back and get "rosenberger".

This is how Java works. If you pass an object reference into a method, the method works with a copy of the address. the method can't change the address the calling method has, no matter what.

If we use the same scenario, but the boss says "what color is the bedroom", we may both come back with "blue" the first time. but if the boss tells you to "paint the bedroom green", when he comes back to me, i'm going to go to the house and find a green bedroom.

The state of the bedroom changed from blue to green, and it doesn't matter who has the address of the house - everyone will see the change to to the color.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Rob Prime wrote:An example:
The reason clearWrongly doen't work is because the sb on lines 13 and 15 is not the same as the sb from lines 5, 6, and 7. They are referencing the same object (until line 15) but they are not 100% the same.


Thanks, Rob! Just one follow-up question: Is the reason why sb on lines 13,15 is different from 5,6,7 because the object sb from 5,6,7 starts with "Hello World" (a parameter variable), but from 13,15, it starts with nothing?
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
fred rosenberger wrote:
Say I have a piece of paper with a home address on it. the boss says to me "go to that address and tell me what name is on the mailbox". I do that, and come back with "rosenberger".

Now I give you a photocopy of that piece of paper. then the boss says to you "go to that address and tell me what name is on the mailbox". You would go to the same house, and also come back with "rosenberger".

Now the boss says "erase the address on your piece of paper, and write this NEW address down. then go to THAT address, and give me the name on the mailbox". You may come back with "Scodario". The boss says "OK, you're done, throw your paper away".

This is how Java works. If you pass an object reference into a method, the method works with a copy of the address. the method can't change the address the calling method has, no matter what.



Hi Fred, thanks for your interesting example. Two questions though, so what's your point when the boss says erase the address and write a new one? My second question is in my original question, "the contents of an object reference" is then the Object's address? so it's not the Object itself? I feel like we been talking about the state (what's visible to the outside world and what affects the behavior of the object), so what is the Object really? i know how we can create one and all that. But all this time, we just use object reference and change states. and of course we have methods to dictate the behaviors of the object, but do we really see the Object tangibly?
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi

I will take aq shot at this and say that if you write say



So you only use a reference of an object using "new"= "new Copy of" and a different name referencing the original object.
and can then change the state of that copy using the Methods with the new name you have given it.So it is actually an exact replica of the object with a new address, so in esscence you are seeing the Object Tangibly.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Cheryl Scodario wrote:Thanks, Rob! Just one follow-up question: Is the reason why sb on lines 13,15 is different from 5,6,7 because the object sb from 5,6,7 starts with "Hello World" (a parameter variable), but from 13,15, it starts with nothing?

No, it's different because it starts with "new". Well, actually, it's different because the reference is pointing (I hate that word in this context) to a different object. I could have said "sb = new StringBuilder("Hello World!")" and it still would be different, even though they contain the same characters.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

Cheryl Scodario wrote:what's your point when the boss says erase the address and write a new one?


Suppose you have this method:



Cheryl Scodario wrote:My second question is in my original question, "the contents of an object reference" is then the Object's address? so it's not the Object itself?

correct. Although the jvm knows this, and therefore understand how to get the actual data you want. You really don't have to worry about this 99% of the time.


Cheryl Scodario wrote:I feel like we been talking about the state (what's visible to the outside world and what affects the behavior of the object), so what is the Object really? i know how we can create one and all that. But all this time, we just use object reference and change states. and of course we have methods to dictate the behaviors of the object, but do we really see the Object tangibly?


Say you are in construction. You build buildings. You have a set of blueprints - a cottage, a villa, a skyscraper, etc. Those blueprints are like a .java file. They tell your crew (or the JVM) how to build something. You can use a single set of blueprints to make many copies of a given building type, but you need some way to tell them apart.

So, you carry around a rolodex. Your rolodex is filled with cards that say "Mary's house", "Susan's villa", "Fred's Skyscraper". On each card is the address of the house. You can make copies of these cards to give to your workers, who all know that the card isn't the ACTUAL house, but tells them how to get to the house.

When you say "Get me the color of Susan's bedroom", your worker gets a copy of the address, knows how to find the house, knows where the bedroom is, looks at it, and reports back to you that the bedroom is blue.

The object references in your program are the cards in your rolodex. The objects are like the actual houses.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Rob Prime wrote:
No, it's different because it starts with "new". Well, actually, it's different because the reference is pointing (I hate that word in this context) to a different object. I could have said "sb = new StringBuilder("Hello World!")" and it still would be different, even though they contain the same characters.


But can you make a new object reference with the same name sb?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Sure. The old object the reference was pointing to will have its reference count decreased by 1 and if that hits 0 it's eligible for garbage collection. Note that in my example there are two references to the StringBuilder object before line 15 starts - one in the main method and the method parameter.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Rob Prime wrote:Sure. The old object the reference was pointing to will have its reference count decreased by 1 and if that hits 0 it's eligible for garbage collection. Note that in my example there are two references to the StringBuilder object before line 15 starts - one in the main method and the method parameter.


Thanks for your reply. But what does it mean by reference count? I never heard of it. Another question regarding your code, the clearWrongly method on line 13 is basically about creating a new instance? Then the reason why on line 7, clearWrongly won't work is because it's confused about what sb should it use? the one on line 5 or the one on line 15? I am kinda puzzled by the code...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

When you create a new object, a chunk of memory somewhere gets allocated for your use. The JVM maintains a table somewhere that says "starting at memory position 123456, the next 27 bytes are reserved for a Foo object."

It is possible to have any number of reference variables point to that same spot:

Foo myFoo = new Foo(); //1 var refers to that spot
Foo myFoo2 = myFoo; //2 vars both refer to the same spot
Foo myFoo3 = myFoo; //3 now refer to that same spot
etc

If we pass myFoo into a method, there ca be more. We can loose references too. if we continue from the above:

myFoo3 = null; // we now have one less reference pointing to that spot.

It is possible that eventually, ALL the reference are 'nulled' out, drop out of scope, or are changed to point somewhere else. If there are no references (there are some subtleties here, but we'll skip them for now), the memory CANNOT be gotten to anymore. The memory can now be re-used by something else. When needed, Java has something called a 'garbage collector' which re-claims it for use. The chunks that cannot be gotten to are cleared from the table, allowing that space to be used again.

All this is done for you, behind the scenes. You (almost) never need to worry about any of it.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

One of these subtleties I'd like to mention is the so-called "island of isolation". A little explanation with an example:
At point 1), nothing can access the List object anymore. The Object inside the List is still accessible by the List itself, but because that List is inaccessible, the Object itself is also regarded as inaccessible. After all, the doSomething method cannot get to it anymore. That's why it's called an island of isolation - multiple objects can still access each other, but nothing else can access them. They form a little island that is inaccessible by anything outside the island. All of these objects can now be garbage collected.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You can get better sorts of islands with circular references. A has a reference to B and B has a reference to C and C has a reference to A.
The reference-counting algorithms for garbage collection cannot delete such islands, but the mark-and-sweep algorithms the JVM uses can easily find and delete such circular references.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Thanks guys! I have a question about instance variable and object... As I was coding something, I have several different classes. In one class called Player, I have


Then I realize a problem that myPile is an object reference, and bag is a variable whose type is Pile. So can they be equivalent? Secondly, if I take out Pile myPile=...., and instead, declare it as an instance variable, like private Pile myPile. Can I still call method upon myPile? like myPile.play(). Or only an object reference can call a method?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic reference question
 
Similar Threads
Array Behaviour
Array Doubts -
Javabeat query
passing Obj references......plz help.
ARRAY doubt