Consider statement:
考虑声明:
String s=new String("abc");
String s = new String(“abc”);
Will this statement creates two String objects namely "abc" and the one represented by 's'?
这个语句会创建两个String对象,即“abc”和由's'表示的对象吗?
and if it creates two objects then will "abc" get stored in String pool or just discarded?
如果它创建了两个对象,那么“abc”会被存储在String池中还是只是被丢弃?
EDIT:
编辑:
i am asking this question in reference to Difference between string object and string literal, where in the last two answers , creation of two objects is denied.
我在提到这个问题时参考了字符串对象和字符串文字之间的差异,在最后两个答案中,拒绝创建两个对象。
2 个解决方案
#1
5
Avoid such kind of behavior , because "abc"
is already a String
and by making a new String
, you are creating an unnecessary Object
.
避免这种行为,因为“abc”已经是一个String,并且通过创建一个新的String,您正在创建一个不必要的Object。
Instead go for String s = "abc";
而是去String s =“abc”;
This way, the String
gets interned by the JVM and is added to a pool.
这样,String就会被JVM拦截并添加到池中。
To answer your question, you are just creating an Object s
that is referring to "abc"
.
要回答您的问题,您只需创建一个引用“abc”的Object。
So when you do say String t = new String("abc");
and then do s==t
, will yield in false
. Because they have their separate instances to abc
.
所以当你说String t = new String(“abc”);然后做s == t,将屈服于假。因为他们有各自的abc实例。
#2
0
String s = "HELLO";
String s =“HELLO”;
Here "s" is a object reference variable of type String, which refers to the String literal object "Hello" which is added to the String Literal Pool.
这里“s”是String类型的对象引用变量,它引用字符串文字对象“Hello”,它被添加到String Literal Pool中。
String t = new String("Hello");
String t = new String(“Hello”);
Here t is a object reference variable of type String, which refers to the String object "Hello" which is added to the String Pool.
这里的t是String类型的对象引用变量,它引用String对象“Hello”,它被添加到String Pool中。
Difference Between String Literal and String :
Assume
承担
String s = "Hello";
String s =“Hello”;
String t = new String("Hello");
String t = new String(“Hello”);
Now if following changes are done:
现在,如果进行了以下更改:
s = null;
s = null;
t = null;
t = null;
Hello String object associated with t will be a candidate for Garbage Collector, But Hello String Literal associated with s will NOT BE A CANDIDATE for Garbage Collector, as there will ALWAYS BE A REFERENCE FROM STRING LITERAL POOL to it.
与t关联的Hello String对象将是垃圾收集器的候选对象,但与s关联的Hello String Literal将不会成为垃圾收集器的候选者,因为它始终是STRING LITERAL POOL的参考。
#1
5
Avoid such kind of behavior , because "abc"
is already a String
and by making a new String
, you are creating an unnecessary Object
.
避免这种行为,因为“abc”已经是一个String,并且通过创建一个新的String,您正在创建一个不必要的Object。
Instead go for String s = "abc";
而是去String s =“abc”;
This way, the String
gets interned by the JVM and is added to a pool.
这样,String就会被JVM拦截并添加到池中。
To answer your question, you are just creating an Object s
that is referring to "abc"
.
要回答您的问题,您只需创建一个引用“abc”的Object。
So when you do say String t = new String("abc");
and then do s==t
, will yield in false
. Because they have their separate instances to abc
.
所以当你说String t = new String(“abc”);然后做s == t,将屈服于假。因为他们有各自的abc实例。
#2
0
String s = "HELLO";
String s =“HELLO”;
Here "s" is a object reference variable of type String, which refers to the String literal object "Hello" which is added to the String Literal Pool.
这里“s”是String类型的对象引用变量,它引用字符串文字对象“Hello”,它被添加到String Literal Pool中。
String t = new String("Hello");
String t = new String(“Hello”);
Here t is a object reference variable of type String, which refers to the String object "Hello" which is added to the String Pool.
这里的t是String类型的对象引用变量,它引用String对象“Hello”,它被添加到String Pool中。
Difference Between String Literal and String :
Assume
承担
String s = "Hello";
String s =“Hello”;
String t = new String("Hello");
String t = new String(“Hello”);
Now if following changes are done:
现在,如果进行了以下更改:
s = null;
s = null;
t = null;
t = null;
Hello String object associated with t will be a candidate for Garbage Collector, But Hello String Literal associated with s will NOT BE A CANDIDATE for Garbage Collector, as there will ALWAYS BE A REFERENCE FROM STRING LITERAL POOL to it.
与t关联的Hello String对象将是垃圾收集器的候选对象,但与s关联的Hello String Literal将不会成为垃圾收集器的候选者,因为它始终是STRING LITERAL POOL的参考。