• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String concatination

 
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code which print "hello".

Need to know why?
and how about the memory allocation for this.

String s="hello";
s.concat("world");
System.out.println(s)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aneek Banerjee wrote:I have the following code which print "hello".

Need to know why?
and how about the memory allocation for this.

String s="hello";
s.concat("world");
System.out.println(s)



Strings are immutable. So if you want to change the s reference to point to an object that is "hello world", you will need to actually set it to the reference that is returned from the concat() method.

Henry
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,
I am new to all this.can you please explain with any example.It will be very helpful for me then...

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can change a reference variable that points to a String through reassignment.


The following lesson on strings may help with understanding The String class
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aneek Banerjee wrote:I have the following code which print "hello".

Need to know why?
and how about the memory allocation for this.

String s="hello";
s.concat("world");
System.out.println(s)



I will give a very small and simple example, in case you're still lost (which I doubt, as Kevin's answer was very good):

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...

I got it....
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic