s*******e 发帖数: 174 | 1 String s1 = "grapefruit";
String s2 = "grapefruit";
请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same
address?
String s3 = "grape"+"fruit";
Does s3 point to same address as s1 and s2?
String s4 = new String("grapefruit");
String s5 = new String("grapefruit");
s4 and s5 should be in heap, s4 and s5 should point to different addresses,
right?
System.out.println(s1+s2);
System.out.println(s4+s5);
which one is faster? Thanks |
j*a 发帖数: 14423 | 2 as far as i know javac translate String s = "abc" to String s = new String("
abc"). u can figure out the rest. if not, just write a 10-liner to test.
heap. no.
no
,
yes
same
【在 s*******e 的大作中提到】 : String s1 = "grapefruit"; : String s2 = "grapefruit"; : 请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same : address? : String s3 = "grape"+"fruit"; : Does s3 point to same address as s1 and s2? : String s4 = new String("grapefruit"); : String s5 = new String("grapefruit"); : s4 and s5 should be in heap, s4 and s5 should point to different addresses, : right?
|
h*****0 发帖数: 4889 | 3 我觉得都是一个地址……
,
【在 s*******e 的大作中提到】 : String s1 = "grapefruit"; : String s2 = "grapefruit"; : 请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same : address? : String s3 = "grape"+"fruit"; : Does s3 point to same address as s1 and s2? : String s4 = new String("grapefruit"); : String s5 = new String("grapefruit"); : s4 and s5 should be in heap, s4 and s5 should point to different addresses, : right?
|
a****i 发帖数: 1182 | 4 "grapefruit" is a string already
new String("grapefruit") means creating a "grapefruit" string
and new String again.......
so, s4+s5 is slower than s1+s2
meanwhile, I belive all the addresses are different
unless you set it like
String s2 = s1;
,
【在 s*******e 的大作中提到】 : String s1 = "grapefruit"; : String s2 = "grapefruit"; : 请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same : address? : String s3 = "grape"+"fruit"; : Does s3 point to same address as s1 and s2? : String s4 = new String("grapefruit"); : String s5 = new String("grapefruit"); : s4 and s5 should be in heap, s4 and s5 should point to different addresses, : right?
|
h*****0 发帖数: 4889 | 5 s1和s2肯定是一样的。我觉得其它的也一样。
【在 a****i 的大作中提到】 : "grapefruit" is a string already : new String("grapefruit") means creating a "grapefruit" string : and new String again....... : so, s4+s5 is slower than s1+s2 : meanwhile, I belive all the addresses are different : unless you set it like : String s2 = s1; : : ,
|
s*******e 发帖数: 174 | 6 faint, 这道题果然引起争论,在网上也找不到标准答案。。
我自己觉得 s1, s2 points to same address in the heap,
s4 and s5 points to different addresses in the heap.
s4 + s5 is slower than s1+s2..
但是C++背景的人会说 s1, s2 are in stack. Java is different, it stores local
variables and active method invocations in stack. |
a****i 发帖数: 1182 | 7 simple, try
s1 == s2 ?
from http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
do it twice, how can s1 has the same address with s2?
local
【在 s*******e 的大作中提到】 : faint, 这道题果然引起争论,在网上也找不到标准答案。。 : 我自己觉得 s1, s2 points to same address in the heap, : s4 and s5 points to different addresses in the heap. : s4 + s5 is slower than s1+s2.. : 但是C++背景的人会说 s1, s2 are in stack. Java is different, it stores local : variables and active method invocations in stack.
|
h*****0 发帖数: 4889 | 8 new String(data) is different from new String("abc"), since the compiler
will not decide the value of the previous case.
s1 = "abc";
s2 = "abc";
System.out.println(s1 == s2);
you will definitely get true
【在 a****i 的大作中提到】 : simple, try : s1 == s2 ? : from http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html : String str = "abc"; : is equivalent to: : char data[] = {'a', 'b', 'c'}; : String str = new String(data); : do it twice, how can s1 has the same address with s2? : : local
|
c*********n 发帖数: 1057 | 9 In my opinion, s1, s2, and s3 are in heap and point to the same address.
s4 and s5 point to different addresses in heap |
g*****g 发帖数: 34805 | 10 They don't have the same address, but have some sort of the
same internal address.
s1 = "abc";
s2 = new String("abc");
while s1!=s2, s1.intern()==s2.intern()
I think there's only one string created in heap, while references may
be on heap or stack depends on where you declare it.
【在 a****i 的大作中提到】 : simple, try : s1 == s2 ? : from http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html : String str = "abc"; : is equivalent to: : char data[] = {'a', 'b', 'c'}; : String str = new String(data); : do it twice, how can s1 has the same address with s2? : : local
|
|
|
s*******e 发帖数: 174 | 11 It says "Because String objects are immutable, they can be shared."
【在 a****i 的大作中提到】 : simple, try : s1 == s2 ? : from http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html : String str = "abc"; : is equivalent to: : char data[] = {'a', 'b', 'c'}; : String str = new String(data); : do it twice, how can s1 has the same address with s2? : : local
|
s*******e 发帖数: 174 | 12 nod nod, s1 == s2 returns true, just tested.
【在 h*****0 的大作中提到】 : new String(data) is different from new String("abc"), since the compiler : will not decide the value of the previous case. : s1 = "abc"; : s2 = "abc"; : System.out.println(s1 == s2); : you will definitely get true
|
h*****0 发帖数: 4889 | 13 right!
【在 s*******e 的大作中提到】 : It says "Because String objects are immutable, they can be shared."
|
m******g 发帖数: 516 | 14 The Java compiler must be very smart now. It create a String object instance
"abc", say S, then s1="abc" interpreted as assign the viarable s1 to
reference to S. The compiler sees another "abc" in the same {} scope (and
its parenting scope?), it must have searched its stack before creating a new
instance using String.equal() function, and found "abc" exists, s2="abc"
assigned viarable s2 to reference same instance S.
It will be really smart (or stupid) for the compiler to say "true".
String s1="
【在 s*******e 的大作中提到】 : nod nod, s1 == s2 returns true, just tested.
|
f*****Q 发帖数: 1912 | |
g******o 发帖数: 208 | 16
unspecified (impl. dependent). Yes.
Yes.
,
Again, it is unspecified. If they point to the same address, the compiler
must have done some optimization.
; System.out.println(s1+s2);
Again, it is unspecified.
Most your questions are unspecified in Java and vary from impl to impl,
except that string literals of the
same value must points to the same instance.
【在 s*******e 的大作中提到】 : String s1 = "grapefruit"; : String s2 = "grapefruit"; : 请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same : address? : String s3 = "grape"+"fruit"; : Does s3 point to same address as s1 and s2? : String s4 = new String("grapefruit"); : String s5 = new String("grapefruit"); : s4 and s5 should be in heap, s4 and s5 should point to different addresses, : right?
|
m**********8 发帖数: 330 | 17 correct!
【在 c*********n 的大作中提到】 : In my opinion, s1, s2, and s3 are in heap and point to the same address. : s4 and s5 point to different addresses in heap
|
c*****t 发帖数: 1879 | 18 受不了。。。
s1 and s2 DEFINITELY WILL BE THE SAME. It is part of the Java spec.
Quote:
The Java programming language requires that identical string
literals (that is, literals that contain the same sequence of characters)
must refer to the same instance of class String. In addition, if the method
String.intern is called on any string, the result is a reference to the same
class instance that would be returned if that string appeared as a literal.
Thus,
("a" + "b" + "c").intern()
【在 s*******e 的大作中提到】 : String s1 = "grapefruit"; : String s2 = "grapefruit"; : 请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same : address? : String s3 = "grape"+"fruit"; : Does s3 point to same address as s1 and s2? : String s4 = new String("grapefruit"); : String s5 = new String("grapefruit"); : s4 and s5 should be in heap, s4 and s5 should point to different addresses, : right?
|
h*****0 发帖数: 4889 | 19 赞大师!
method
same
literal.
【在 c*****t 的大作中提到】 : 受不了。。。 : s1 and s2 DEFINITELY WILL BE THE SAME. It is part of the Java spec. : Quote: : The Java programming language requires that identical string : literals (that is, literals that contain the same sequence of characters) : must refer to the same instance of class String. In addition, if the method : String.intern is called on any string, the result is a reference to the same : class instance that would be returned if that string appeared as a literal. : Thus, : ("a" + "b" + "c").intern()
|
l******e 发帖数: 956 | 20 They are all using different addresses. s1 = "grapefruit" equals to s1 = new
String("grapefruit"), and for such thing, it will create a new String
object in a new address.
To test it, just try s1 = s1.subString(...); see if the value of s2 is
changed. |
|
|
h*****0 发帖数: 4889 | 21 string is immutable...
new
【在 l******e 的大作中提到】 : They are all using different addresses. s1 = "grapefruit" equals to s1 = new : String("grapefruit"), and for such thing, it will create a new String : object in a new address. : To test it, just try s1 = s1.subString(...); see if the value of s2 is : changed.
|
s*******e 发帖数: 174 | 22 s1+s2 should have the exactly the same performance as s4+s5 ??????
method
same
literal.
【在 c*****t 的大作中提到】 : 受不了。。。 : s1 and s2 DEFINITELY WILL BE THE SAME. It is part of the Java spec. : Quote: : The Java programming language requires that identical string : literals (that is, literals that contain the same sequence of characters) : must refer to the same instance of class String. In addition, if the method : String.intern is called on any string, the result is a reference to the same : class instance that would be returned if that string appeared as a literal. : Thus, : ("a" + "b" + "c").intern()
|
h*****0 发帖数: 4889 | 23 i think so. program need to allocate a space for the newly generated String,
then concatenate the two string. the difference in performance should be
ignorable(if not zero).
【在 s*******e 的大作中提到】 : s1+s2 should have the exactly the same performance as s4+s5 ?????? : : method : same : literal.
|