问题
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?
回答1:
First of all String s = new String("abs");
It will be created two object, one object into pool area and other one in non pool area because you are using new and as well as 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 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
回答2:
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:
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:
Which shows that both references str7' and
str8` 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:
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:
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.
回答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.
回答4:
**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.stack.imgur.com/IjMhP.png
来源:https://stackoverflow.com/questions/50636396/where-is-the-new-object-of-string-created-when-we-concat-using-operator