由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 一道 JAVA Stack vs Heap 题
相关主题
一个想不明白的编码问题Which POJO generator is better?
interesting "protect" behavior没有源代码,想替换掉一个class文件里的类
Java Server FacesSpring自学咋学呢?
SOAP over SMTPhtmlunit及多线程问题
Object比较学习J2EE
Design thought.. Sugguestions?Spring JBOSS
The best HashMap Cache solutionEJB ConnectionFactory issue
Question: reflection and genericsSpring MVC多表查询几问
相关话题的讨论汇总
话题: string话题: s1话题: s2话题: same话题: grapefruit
进入Java版参与讨论
1 (共1页)
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

相关主题
Design thought.. Sugguestions?Which POJO generator is better?
The best HashMap Cache solution没有源代码,想替换掉一个class文件里的类
Question: reflection and genericsSpring自学咋学呢?
进入Java版参与讨论
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
15
goodbug也不来管管。
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.
相关主题
htmlunit及多线程问题EJB ConnectionFactory issue
学习J2EESpring MVC多表查询几问
Spring JBOSS一道 JAVA Stack vs Heap 题 (转载)
进入Java版参与讨论
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.

1 (共1页)
进入Java版参与讨论
相关主题
Spring MVC多表查询几问Object比较
一道 JAVA Stack vs Heap 题 (转载)Design thought.. Sugguestions?
java之后,人类就已经不再意计算机语言了The best HashMap Cache solution
C++ 面试题疑问Question: reflection and generics
一个想不明白的编码问题Which POJO generator is better?
interesting "protect" behavior没有源代码,想替换掉一个class文件里的类
Java Server FacesSpring自学咋学呢?
SOAP over SMTPhtmlunit及多线程问题
相关话题的讨论汇总
话题: string话题: s1话题: s2话题: same话题: grapefruit