File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Reference - Doubt

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not if you give the same variable a different value. You'd have to use 2 different variables.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(See Strings, Literally.)
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Friends.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic