This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings ?

 
rahul kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statement ...
String s = "Hello" + "Java";
yields the same value for s as ...
String s = "Hello";
String s2= "Java";
s.concat( s2 );
a) True
b) False
what is the answere with explaination.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is false since the value of s after the concatenation will still be "Hello" and not "HelloJava"
remember that STring objects are immutable anf thus can't be changed with mutator method like concat... A new referecne to a new String object is returned...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
sai murali
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s.concat( s2 );
It will not change the original content of s, will result a new String variable.
So s value will be still "Hello", but the previous
s value is "HelloJava".
Answer is false(b).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic