aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Immutable" Watch "String Immutable" New topic
Author

String Immutable

Balaguru Janarthanan
Ranch Hand

Joined: Nov 01, 2000
Posts: 37
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

Joined: Oct 08, 2000
Posts: 52
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

Joined: Nov 01, 2000
Posts: 37
But who created object "two"? pls clarify?
Jonathan Jeban
Ranch Hand

Joined: Oct 08, 2000
Posts: 52
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

Joined: Nov 01, 2000
Posts: 37
Thank You Jeban.
Balaguru Janarthanan
Ranch Hand

Joined: Nov 01, 2000
Posts: 37
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

Joined: Oct 08, 2000
Posts: 52
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

Joined: Nov 01, 2000
Posts: 37
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
 
subject: String Immutable
 
Similar Threads
GC Question
Strings in Java
Assignment 1.4
Anonymous Array
Insert into String