wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String concatination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String concatination" Watch "String concatination" New topic
Author

String concatination

Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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
Sheriff

Joined: Sep 28, 2004
Posts: 18761
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

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

Joined: Jan 06, 2009
Posts: 175
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


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Lucas Henrique Pinto
Greenhorn

Joined: Dec 10, 2012
Posts: 2
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

Joined: Jun 20, 2012
Posts: 61

Thanks...

I got it....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String concatination