« 1. String Concatenation operator + 3. Using append() method »
concat() method of String class appends the argument string to the string object on which it is called. For example,
System.out.println(”Tom”.concat(“Cat”)); Will print TomCat.
"Go".concat(" to").concat(" hell !") will print "Go to hell !"
Now consider,
String str= "strike";
str.concat("out");
System.out.println(str); // the output will be strike, NOT strike out. WHY?
This is because in Java strings are immutable. Due to which whenever you change the contents of a String object, a new String object is created instead of modifying the original string.
Therefore when you changed the String object str by appending “out “ using str.concat(), it returned a new String object with changed contents instead of modifying original str contents.
str is still pointing to original string with value strike.
That’s why System.out.println(str); printed strike Not strike out.
The newly created String object with changed contents (strike out) will be garbage collected after some time as you have not stored its reference anywhere.
If your code will be:
String str= "strike";
str=str.concat("out");
System.out.println(str); // the output will be strike out, NOT strike.
Here you have stored the reference of newly created String object with changed contents in reference variable str, which was earlier pointing to original string.
Therefore System.out.println(str); printed strike out Not strike.
Now the reference to original string got lost and it will be garbage collected after some time.
Similarly,
String first= "strike";
String sec=first.concat("out");
System.out.println(first==sec); // will be false.
first==sec will print false showing that they are different String objects.
Two cases are there:
1) If the length of argument String is 0 then concat() returns the same string instead of creating a new String object. For example,
String s="James ";
String s1=s.concat("");
System.out.println((s==s1); // will print true.
Here argument of concat() is an empty string (length 0), Therefore instead of creating a new String object it returned reference to same original String object s.This can be confirmed by s==s1 giving true i.e both are pointing to same String object.
2) calling concat() on null reference variable:
Suppose your code is:
String s=null;
String s1=s.concat("Appending on null String variable");
It will give following error:
Exception in thread "main" java.lang.NullPointerException
« 1. String Concatenation operator + 3. Using append() method »
concat() method of String class appends the argument string to the string object on which it is called. For example,
System.out.println(”Tom”.concat(“Cat”)); Will print TomCat.
"Go".concat(" to").concat(" hell !") will print "Go to hell !"
Now consider,
String str= "strike";
str.concat("out");
System.out.println(str); // the output will be strike, NOT strike out. WHY?
This is because in Java strings are immutable. Due to which whenever you change the contents of a String object, a new String object is created instead of modifying the original string.
Therefore when you changed the String object str by appending “out “ using str.concat(), it returned a new String object with changed contents instead of modifying original str contents.
str is still pointing to original string with value strike.
That’s why System.out.println(str); printed strike Not strike out.
The newly created String object with changed contents (strike out) will be garbage collected after some time as you have not stored its reference anywhere.
If your code will be:
String str= "strike";
str=str.concat("out");
System.out.println(str); // the output will be strike out, NOT strike.
Here you have stored the reference of newly created String object with changed contents in reference variable str, which was earlier pointing to original string.
Therefore System.out.println(str); printed strike out Not strike.
Now the reference to original string got lost and it will be garbage collected after some time.
Similarly,
String first= "strike";
String sec=first.concat("out");
System.out.println(first==sec); // will be false.
first==sec will print false showing that they are different String objects.
Two cases are there:
1) If the length of argument String is 0 then concat() returns the same string instead of creating a new String object. For example,
String s="James ";
String s1=s.concat("");
System.out.println((s==s1); // will print true.
Here argument of concat() is an empty string (length 0), Therefore instead of creating a new String object it returned reference to same original String object s.This can be confirmed by s==s1 giving true i.e both are pointing to same String object.
2) calling concat() on null reference variable:
Suppose your code is:
String s=null;
String s1=s.concat("Appending on null String variable");
It will give following error:
Exception in thread "main" java.lang.NullPointerException
« 1. String Concatenation operator + 3. Using append() method »
I would like to know your comments and if you liked the article then please share it on social networking buttons.
While using concat() to create a string like:
ReplyDeleteString s = "hello";
String s2 = "hell";
String s3 = s2.concat("o");
if i try and print (s==s3) i get false
here do the variables s and s3 point to the same literal "hello" in String Constant Pool or does using concat() create a new object with value "hello" like it would do if we used new String("hello");
Though is i use the same example as:
String s = "hello";
String s2 = s.concat("");
if i try and print (s==s3) i get true
Dear anonymous answers are as follows:
DeleteProblem 1:
String s = "hello";
String s2 = "hell";
String s3 = s2.concat("o");
print (s==s3) // will return false why???
Answer:
If u check the code of concat() method in the source code of String class
2016 public String concat(String str) {
2017 int otherLen = str.length();
2018 if (otherLen == 0) {
2019 return this;
2020 }
2021 char buf[] = new char[count + otherLen];
2022 getChars(0, count, buf, 0);
2023 str.getChars(0, otherLen, buf, count);
2024 return new String(0, count + otherLen, buf);
2025 }
Note the line number 2024. It is returning concatenated string as a new string created by "new".
If u read the article String Pool u will get to know that string created by "new" never goes to String pool.Therefore s lies in String pool while s3 does not. So s and s3 are two different strings objects. Hence print s==s3 will give false.
Problem 2:
String s = "hello";
String s2 = s.concat("");
print (s==s2) // will print true. why???
Answer:
If u note the line number 2019 of the above code of String class of concat() method, u will note that when length of string argument passed to concat() is 0 then it is returning "this" which means it is returning same string object which has called the concat(). in your case "this" represents s. So s.concat("") is returning s. Hence s2 and s are same objects. Therefore s==s2 will print true.