This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Concat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Concat" Watch "String Concat" New topic
Author

String Concat

Santosh Jaiswal
Greenhorn

Joined: Oct 04, 2000
Posts: 26
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

Joined: Oct 10, 2000
Posts: 229
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

Joined: Oct 04, 2000
Posts: 26
Oh! I got it, I was just skipping the assignment part.
Thanks Harpal.
 
Consider Paul's rocket mass heater.
 
subject: String Concat
 
Similar Threads
Strings ?
concat
MindQ's question no. 11
question from MindQ Mock Exam
String Question