• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

concat

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic