Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

concat

 
Pratiti Naphade
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
why??
these things really confuse me...
 
Bala Arul
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratiti,
False.
i) String s = "Hello" + "Java"; yields "HelloJava" as the value of s.
ii) String s = "Hello";
String s2= "Java";
s.concat( s2 );
After all the above 3 statements executed, s still refer to the value "Hello". s.concat(s2) does not change the s's value, but return new String object whose value is "HelloJava"
Bala.
 
kapil apshankar
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratiti,
The '+' operator is inherently overloaded by Java for String objects.
Also the Java API defines a concat method for String objects which concatenates the specified string to the end of the first string.
The effect of both these is bound to be the same.
I guess because the overloaded '+' operator implicitly calls the concat() method of String objects.
So you can use either of these, as long as you are comfprtable with it.
Please correct me if I am wrong.
Cheers
Kapil
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic