• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

String concatination

 
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • 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: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 182
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks...

I got it....
 
    Bookmark Topic Watch Topic
  • New Topic