• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Concat

 
Santosh Jaiswal
Greenhorn
Posts: 26
  • 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 );
1. True 2.False
How the answer is False

 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santosh Jaiswal:
The statement ...
String s = "Hello" + "Java";
yields the same value for s as ...
String s = "Hello";
String s2= "Java";
s.concat( s2 );
1. True 2.False
How the answer is False


Santosh,
Answer will be false coz after this statement:
String s = "Hello" + "Java";
a String pool will be created which will hold "HelloJava",and variable s will point to that...and when you say
String s = "Hello";
String s2= "Java";
s.concat( s2 );
a new pool of String will be created ie HelloJava,but s is still pointing to the other pool of string...now if you say
s=s.concat( s2 );
then the answer will be true coz now your variable s is being assigned to a new string which contains "HelloJava"...
Hope it clears your doubt....
thanks,
Harpal...
 
Santosh Jaiswal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! I got it, I was just skipping the assignment part.
Thanks Harpal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic