Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from MindQ Mock Exam 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 "question from MindQ Mock Exam " Watch "question from MindQ Mock Exam " New topic
Author

question from MindQ Mock Exam

Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
11. The statement ...
String s = "Hello" + "Java";
yields the same value for s as ...
String s = "Hello";
String s2= "Java";
s.concat( s2 );
True
False

I thought + here is overloaded to concatenate the strings, but I thought it's a easier way for concat(). so my answer was true. but the correct answer is false. Is it correct?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
String functions do not actually change the String that invokes them. In your second example, what actually happened was that s.concat(s2) created a new String with value equivalent to "HelloJava" and returned it, but since no String reference received this return value, the change was not apparent. Note that the String s is still equivalent to "Hello". Try this:
String newString = s.concat(s2);
System.out.println("String s is " + s);
System.out.println("String newString is " + newString);
Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
I know the s.concat("java");
gives no change to s.
but does
String s="Hello" + "java";
give us the same as
String s="Hellojava" ?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Yes, of course. Besides, you can see it for yourself by compiling it...
Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
thank you very much.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
you're welcome too
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from MindQ Mock Exam
 
Similar Threads
concat
MindQ's question no. 11
String Concat
String Question
Java String pool problem