string和string new之间的区别 [英] difference between string and string new
问题描述
字符串s =abc和字符串s =新字符串(abc)之间有什么区别。
我搜索了它,有些人说没有区别,有人说是。
任何人都可以解释我或给这些网站链接。
谢谢。
Hi ,
What is the difference between string s="abc" and string s =new string("abc").
I searched for it,some say no difference some say yes .
can anybody explain me or give links to the sites.
Thanks.
推荐答案
例如:
For Example:
String s = "Marcus";
这是一个字符串文字。
It is a string literal.
String s2 = new String("Marcus");
创建一个新实例,因为你用新的创建了一个字符串运营商。
但是如果你创建了一个新的字符串,例如
String s1 =Marcus
首先它搜索字符串池中是否存在字符串Marcus(在这种情况下,因为它已经存在),现在它只是将它重新分配给s1。
A new instance is created as you have created the string with the new operator.
But again if you have created a new string like
String s1 = "Marcus"
First it searches if the string "Marcus" is there in the string pool,(in this case,as it is already there),now it just reassigns the same to s1.
1-字符串对象池问题
字符串对象是不可变的;这就是说当你说
1- String objects pool issue
String object is immutable; thats mean when you say
String s = "Mohammed";
s += "El-Adawi";
运行时这意味着创建了一个新的字符串Mohammed El-adawi
并分配给引用s
和字符串Mohammed现在是合格的for gc
为什么sun必须这样做?,因为String文字
(之间的任何东西都被编译器重新识别为String literal )
由编译器放入字符串池中,然后如果compilar再次找到相同的文字,例如
on runtime this means that a new String "Mohammed El-adawi" is created
and assigned to reference s
and String "Mohammed" now is eligable for gc
why sun have to do that ?, because String literals
(any thing between " " is reccognized by compiler as String literal)
are put by compiler in String pool, then if compilar found the same literal again for example
String s = "abc";
String m = "abc"; // compiler now found the same String literal
编译器将有一个abc副本,并将s,m分配给相同的字符串文字,(在运行时)构造并赋予s的一个对象,m)
如果m改变字符串,s将指向错误的东西,这就是为什么String是不可变的。
2-回答你的问题
compiler will have one copy of "abc" and will assign both s,m to the same String literal, (at run time one object constructed and asigned to s,m)
if m change the String, s will point to the wrong thing, thats why String is immutable.
2- answer to your question
String s = "Marcus"; // One String object is built and assigned to s.
String s2 = new String("Marcus");
/* now compiler found "Marcus" in the pool
so it will not create a new object
new operator creates new object contains
Marcus at runtime
*/
String s3 = new String("Mohammed Not Marcus");
// compiler adds "Mohammed Not Marcus" to String pool but with no
// reference assignment, and String object created and assigned to S3 // at runtime
3-另一件事
3- Another thing
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); // true
但
but
String s1 = "abc";
String s2 = new String ("abc");
System.out.println(s1 == s2); // false
我希望这是有帮助的
I hope that is helpfull
访问链接
很好的解释与数字将明确的疑惑:)
主题: 2.1字符串文字与字符串对象
http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html [ ^ ]
Happy编码!
:)
visit link
nice explanation with figure that will clear doubts :)
Topic : 2.1 String Literal vs. String Object
http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html[^]
Happy Coding!
:)
这篇关于string和string new之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!