This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Immutable

 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just tried and compiled the following works well;
String one="one";
one="two";
System.out.println(one);
it prints two;
how it two, is it not one?
Pls. clarify.
 
Jonathan Jeban
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Balaguru,
one is reference variable.
First it points to "one" object.
Then its assigned to "two" object.
Hence the result.(and there is no need to bring immutablity
concept here for this )
Jeban
Originally posted by c Janarthanan:
Just tried and compiled the following works well;
String one="one";
one="two";
System.out.println(one);
it prints two;
how it two, is it not one?
Pls. clarify.

 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But who created object "two"? pls clarify?
 
Jonathan Jeban
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Balaguru,
This is your code..
String one="one";
one="two";
System.out.println(one);
Lets rewrite it for better understanding...
String one; <-- String reference variable is created
one="one"; <-- "one" String Literal is created in
the pool and it is referenced by
one
one="two"; <-- "two" String Literal is created in
the pool and it is now referenced
by one
System.out.println(one); <-- As currently one points
to String Literal "two" ,it is
printed.
Hope this helps..
Jeban
Originally posted by Balaguru Janarthanan:
But who created object "two"? pls clarify?

 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Jeban.
 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have created two strings containing names. Thus
String fname="John";
String lname="String"
How can you go about changing these strings to take new values within the same block of code?
1)
fname="Fred";
lname="Jones";
2)
String fname=new String("Fred");
String lname=new String("Jones");
3)
StringBuffer fname=new StringBuffer(fname);
StringBuffer lname=new StringBuffer(lname);

4) None of the above

Jeban, I say the correct answer is 1. agree?
 
Jonathan Jeban
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Balaguru,
Yes , u are right.
Option 2 is not acceptable because u are redefining
the variables again.And option 3 is not acceptable because u
r trying redefine it as StringBuffer.

Jeban
Originally posted by Balaguru Janarthanan:
Jeban, I say the correct answer is 1. agree?[/b]


[This message has been edited by Jonathan Jeban (edited November 09, 2000).]
 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeban, But answer given as follows;
4) None of the above
Once created a String is read only and cannot be changed Each one of the options actually creates a new string "behind the scenes" and does not change the original. If that seems to go against your experience and understanding read through information on the immuatbility of strings
pls visit here http://www.jchq.net/tutorial/09_02Tut.htm
thankx
Balaguru
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic