This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes String Reference - Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Reference - Doubt" Watch "String Reference - Doubt" New topic
Author

String Reference - Doubt

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Hi

I have one doubt.

1 String str = "Hello";
2 System.out.println(str); //Prints Hello
3 str = "GoodBye";
4 System.out.println(str); //Prints GoodBye.

In Line 3 , the string reference is created to new String Object and GoodBye is placed in the String reference str.
And the Original Object str Hello is Unchanged.

Is there any Way to access or print the Original Object Value (Hello) of str after the Line 4.






N.Senthil Kumar
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

No, not if you give the same variable a different value. You'd have to use 2 different variables.


GenRocket - Experts at Building Test Data
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

With most objects, the answer is no. But String literals are a special case due to the String pool, so I think you actually could reference that original object again.

But this seems academic. I'm not sure what the practical implication might be.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

(See Strings, Literally.)
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Thank You Friends.
 
Don't get me started about those stupid light bulbs.
 
subject: String Reference - Doubt