404 Brain Not Found
404 Brain Not Found

Reputation: 605

Where is the new Object of String created when we concat using + operator

I know this might be quite basic and probably pretty straight forward but i cannot clearly understand what will happen in this situation, so, here it goes.

In the following code:

String str1 = "Hello";
String str2 = "World";
String str3 = new String("HelloWorld");
String str4 = str1 + str2;

i know that str1 and str2 will create an object "Hello" and "World" respectively inside the String Constant Pool. While for str3 a new object is created outside the String Constant Pool which is pointing to "HelloWorld" that is created inside String Constant Pool.

My question is, what will happen if i concat 2 or more string (using '+' or concat() method)?

Will a new object be created outside the pool just like in the case of String str3 or will str4 point directly at the object "HelloWorld" inside the String Constant Pool

PS : And IF it is the case similar as creation of new object outside the pool, then how does it happen without using the new keyword?

Upvotes: 9

Views: 7653

Answers (5)

Utpal Kumar
Utpal Kumar

Reputation: 445

Two points to keep in mind before reading through my comments here.

  1. Result of any runtime operation during concatenation will create object in heap area.
  2. intern() method gets a reference from string constant pool (scp) area.

Now,

String s = "you";       // s in scp
String s1 = s + "me";   // "me" in scp and runtime operation here will put s1 in heap
String s2 = "youme";    // s2 in scp

To ascertain my point above, see below code.

System.out.println(s1 == s2);          // false , since s1 is in heap and s2 is in scp
System.out.println(s1.intern() == s2); // true , since s2 in scp and s1.intern() in scp

Had there been like below.

String s3 = "you" + "me"; //operation is compile time now, s3 in scp

so,

System.out.println(s3 == s2); // true, s3 in scp and s2 in scp

Note: "abc" value of String is compile time constant

Upvotes: 2

ajay tomar
ajay tomar

Reputation: 382

First of all String s = new String("abs"); It will create two objects, one object in the pool area and another one in the non-pool area because you are using new and as well as a string literal as a parameter.

String str1 = "Hello";
String str2 = "World";
String str3 = new String("HelloWorld");
String str4 = str1 + str2;

Till now you have five String objects, four in String Constant Pool and one in Heap. So your str4 is a new object altogether inside the String Pool, Please check the below code also,

 String str5="HelloWorld"; //This line will create one more String Constant Pool object because we are using the variable name as str5.
 String str6="HelloWorld";////This line will not create any object, this will refer the same object str5.

For test

System.out.println(str3==str4); //false
System.out.println(str4==str5);//false
System.out.println(str5==str6);//true

Upvotes: 3

To answer your question, str4 object is created out side the pool.

This is my explanation:

    String s = "abc";
    String s1="xyz";

    String s3 = "abcxyz";
    String s2=s+s1;

    if(s2 == s3){
        System.out.println("true");
    }else{
        System.out.println("false");
    }

This will print false. That means s2 is not referring to the pool object created by s3.

Upvotes: 1

Sunil Kanzar
Sunil Kanzar

Reputation: 1260

First of all, This is not an answer, this is the way to get answer by your self or explanation of situation.
Concatenation of two strings is always creating a new object of the string.
To conform this thing you can do one thing that how it managed in memory heap and pool.

1: Go to the NetBeans:
2: Write program like this:

public class StringTest {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "World";
        String str3 = new String("HelloWorld");
        String str4 = str1 + str2;
        String str5 = str1 + str2;
        String str6 = str1.concat(str2);

        String str7 = "HelloWorld";
        String str8 = "HelloWorld";

        System.out.println("");
    }
}

3: Just put a break point at System.out.println(""); this line.
4: Now debug this code.
5: Goto the Variable window (Window->Debugging->Variables), which looks like:

enter image description here

6: Now Right Click on str8 and select Mark Object... and give some tag to that object like Same Object.

Now you can see that same tag line is also appear on str7 like:

enter image description here

Which shows that both references str7' andstr8` are refer same object.

7: Now check this thing for str3-4-5-6 all references by marking them with a different tag lines like:

enter image description here

And for further more internal management of that object just look into Show Refereances option by right clicking on the variable name in variables windows like:

enter image description here enter image description here

Update:
- Concatenation creates objects in the heap.
- To make sure this statement look which says that pool can not contain multiple string with the same value
- And here str7-8 are referred object from the pool which is different from the str4-5-6 as depicted in the screenshot of point 7.
- And you also can confirm it by comparing str5 to str7 by using == operator if it returns true, the concatenation creates objects in pool because str7 refer to the pool and both are referred the same object, but it will returns false because both are not same.

Upvotes: 3

Dildeep Singh
Dildeep Singh

Reputation: 108

        **str4 is stored in heap.**        
                String str1 = "Hello";
                String str2 = "World";
                String str3 = new String("HelloWorld");
                String str4 = str1 + str2;
                String str5="HelloWorld";
            if str4 is stored on string pool then str5 will point to same object where str4 is pointing.
            but this statement giving me false output-:
                **System.out.println(str4 == str5);**
            ***************output****************
            false

                 String str4 = (str1 + str2).intern();
                 String str5="HelloWorld";
                but if you are using  String.intern then this method will try to find a String with the same sequence of characters in the pool.
    **System.out.println(str4 == str5);**
    ***************output******************
    true


  [1]: https://i.sstatic.net/IjMhP.png

Upvotes: 0

Related Questions