• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String concatination

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • 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)
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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 Java Tomcat Server
  • 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...

 
Kevin Florish
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
 
Lucas Henrique Pinto
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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...

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