• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about String behavior  RSS feed

 
Marie Kolanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In trying to understand string as a class, I attempted the code below. I thought I understood but I don't understand the output. My understanding was that a string is an object. Therefore, a string variable was a reference variable. THe dog code references a class I created in aanother file.

Here's my code:

Here's the output and where I don't understand.

firstString is Okay this is ORIGINAL firstString.
secondString is Okay this is ORIGINAL firstString.
After I change firstString, secondString reflects the new firsSTring.
Now change first string to: This is REVISED firstString
firstString is now This is REVISED firstString.
secondString is now This is REVISED firstString.


thirdString is This is ORIGINAL third string
fourthString is This is ORIGINAL third string
QUESTION: After I change thirdString, fourthString DOES NOT reflect thirdString. I thought because they were an object and I was pointing the thirdString to fourthString, that they were pointing to the same object. THis does not appear to be behaving like my dog class
thirdString is This is REVISED thirdString.
fourthString is This is ORIGINAL third string

firstDog weight is 15
firstDog name is Buddy
secondDog weight is 15
secondDog name is Buddy

Now change firstDog weight to 25.
25
25
Here the new dog weight is reflected in both objects.
firstDog weight is 25
firstDog name is Buddy
secondDog weight is 25
secondDog name is Buddy

Now change secondDog weight to 84 and name to Napoleon.
Here the new dog weight and name are reflected in both objects.
firstDog weight is 84
firstDog name is Napoleon
secondDog weight is 84
secondDog name is Napoleon


Why if I make a change to the dog class is is reflected in both objects but the string change is only reflected in the string that I made a change to. Isn't fourthString pointed to the same object as thirdString? Or what am I missing?
 
Marie Kolanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore firstString secondString. I made an error in the code.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, String is an object and a String variable is a reference to the String object.

One important thing to keep in mind is that String objects are immutable - that means that after a String object has been initialized, there is no way to change the content of the object. It will always stay the same.

Note that if you change a String variable, which means you make it refer to another String object, then other variables that might be referring to the same String object will not change. In other words, when firstString and secondString both refer to a String object with the content "abc", and you change firstString to refer to a String "xyz", then secondString will still refer to "abc".
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your code...



Specifically, this line...



Question... after this line, how many Dog instances are there?

Henry
 
Marie Kolanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Note that if you change a String variable, which means you make it refer to another String object, then other variables that might be referring to the same String object will not change. In other words, when firstString and secondString both refer to a String object with the content "abc", and you change firstString to refer to a String "xyz", then secondString will still refer to "abc"."

So in other words, by changing firstString, I'm changing the OBJECT it's referring to so that firstString and secondString are no longer pointing to the same object?

Answer to second question:
I would think there are two Dog instances at that point.


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marie Kolanda wrote:
Answer to second question:
I would think there are two Dog instances at that point.



And that is why you are confused with the rest of the code. When you assigned the secondDog reference to the same value as the firstDog reference, both references are now pointing to the same object. The previous instance that was pointed to by the secondDog reference is now eligible for garbage collection.

So, since there is only one Dog instance, and both references are pointing to it, any changes to the one instance seen via both references.

Henry
 
Marie Kolanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I get it now. Misunderstood what my book was saying apparently. Thanks much.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marie Kolanda wrote:So in other words, by changing firstString, I'm changing the OBJECT it's referring to so that firstString and secondString are no longer pointing to the same object?

I think what you're saying is correct, but just to be precise:

If you change the variable firstString, by assigning it a different value, you're making the variable firstString refer to a different object. The other variable, secondString, is still pointing to the other object. So, if you change firstString, then indeed firstString and secondString are no longer pointing to the same object.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question in this thread.. I hope the OP doesnt mind posting that here..

After this line



Two objects are created right??
The object of new String() is created in heap and "This is ORIGINAL third string" string object is created in String constant pool which is pointed by the object in the heap.. right?

Please comment.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:Two objects are created right??

Not necessarily. It all depends on whether the literal has already been put in the pool or not.

You might find the CachedObjects page useful.

And BTW: it's not generally considered good manners to add questions of your own to someone else's thread. It's callled "hijacking".

I've let it go here, but if you want to post any further on this subject, please start your own thread.

Thanks.

Winston
 
Marie Kolanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"If you change the variable firstString, by assigning it a different value, you're making the variable firstString refer to a different object. The other variable, secondString, is still pointing to the other object. So, if you change firstString, then indeed firstString and secondString are no longer pointing to the same object. "


Yup. I didn't say it that way but that's how I was thinking about it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!