File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes concat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "concat" Watch "concat" New topic


Pratiti Naphade
Ranch Hand

Joined: Mar 13, 2001
Posts: 39
11. The statement ...
String s = "Hello" + "Java";
yields the same value for s as ...
String s = "Hello";
String s2= "Java";
s.concat( s2 );
these things really confuse me...
Bala Arul

Joined: Feb 09, 2001
Posts: 29
Hi Pratiti,
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"
kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
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.

Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
I agree. Here's the link:
subject: concat
It's not a secret anymore!