• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Immutable

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But who created object "two"? pls clarify?
 
Jonathan Jeban
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Jeban.
 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic