b*******p 发帖数: 44 | 1 一直认为java中String对象如果用new来创建,即使内容一样(identical character
sequ
ences),也还是不同对象,当然在内存中不同地方。
只有用quoted character string(字符串常量?)初始化时,如果内容一样,就会指
向之
前创建过的String对象,而不是新建一个。如: String a="a"; String b="a";那此时
a,
b两个reference都是指向同一个对象。记得有人写过java编译会把这些字符串常量放进一
个pool里,每次加入新的时候都保证不重复创建同样内容的对象。
但前两天看TIJ 3rd上,p540说"if a program has several String objects that
conta
in identical character sequences, then those String objects ALL MAP to the
sam
e memory.这里没有区别String对象怎样创建。紧接着后面提到So it makes sense
that
the hashCOde() |
g*****g 发帖数: 34805 | 2 应该不同。str1 != str2,但是str1.equals(str2) == true
hashCode应该override了。
进一
【在 b*******p 的大作中提到】 : 一直认为java中String对象如果用new来创建,即使内容一样(identical character : sequ : ences),也还是不同对象,当然在内存中不同地方。 : 只有用quoted character string(字符串常量?)初始化时,如果内容一样,就会指 : 向之 : 前创建过的String对象,而不是新建一个。如: String a="a"; String b="a";那此时 : a, : b两个reference都是指向同一个对象。记得有人写过java编译会把这些字符串常量放进一 : 个pool里,每次加入新的时候都保证不重复创建同样内容的对象。 : 但前两天看TIJ 3rd上,p540说"if a program has several String objects that
|
b*******p 发帖数: 44 | 3 那是TIJ上那句话我理解错了?还是他写错了,可是第三版啊
【在 g*****g 的大作中提到】 : 应该不同。str1 != str2,但是str1.equals(str2) == true : hashCode应该override了。 : : 进一
|
m******t 发帖数: 2416 | 4 Can you repost the quote below? The part following "several String objects
that..." is kind of messed up, which I think could actually be critical in
understanding what the author is trying to say.
【在 b*******p 的大作中提到】 : 那是TIJ上那句话我理解错了?还是他写错了,可是第三版啊
|
b*******p 发帖数: 44 | 5 3x.
Strings have the special characteristic that if a program has several String
objects that contain identical character sequences, then those String
objects all map to the same memory (the mechanism for this is described in
Appendix A). So it makes sense that the hashCode( ) produced by two separate
instances of new String(“hello”) should be identical.
objects
【在 m******t 的大作中提到】 : Can you repost the quote below? The part following "several String objects : that..." is kind of messed up, which I think could actually be critical in : understanding what the author is trying to say.
|
m******t 发帖数: 2416 | 6
String
Well I know this is definitely true for the simple cases, e.g., new String("
Hello") or s = "Hello" would always map to the same entry in the constant
pool.
What I'm not sure is cases like:
String s1 = "Hello World";
String s2 = foo1.bar1() + foo2.bar2();
If s2 ends up having exactly "Hello World", would that be the same entry in
the pool? To achieve that, the JVM would have to do a lookup after _every_
concatenation, which is probably more inefficient than allocating a new
buffer.
The
【在 b*******p 的大作中提到】 : 3x. : Strings have the special characteristic that if a program has several String : objects that contain identical character sequences, then those String : objects all map to the same memory (the mechanism for this is described in : Appendix A). So it makes sense that the hashCode( ) produced by two separate : instances of new String(“hello”) should be identical. : : objects
|
b*******p 发帖数: 44 | 7 But
String a = "a";
String b = new String("a");
a == b will return false, how could they map to the same instance?
("
in
_
【在 m******t 的大作中提到】 : : String : Well I know this is definitely true for the simple cases, e.g., new String(" : Hello") or s = "Hello" would always map to the same entry in the constant : pool. : What I'm not sure is cases like: : String s1 = "Hello World"; : String s2 = foo1.bar1() + foo2.bar2(); : If s2 ends up having exactly "Hello World", would that be the same entry in : the pool? To achieve that, the JVM would have to do a lookup after _every_
|
l****u 发帖数: 2166 | 8 string by new String("aaa") are not pooled.
they are just staying at eden or young gen
while pooled strings are staying at perm gen.
so they are not pointing to same memory address.
【在 b*******p 的大作中提到】 : But : String a = "a"; : String b = new String("a"); : a == b will return false, how could they map to the same instance? : : (" : in : _
|
b*******p 发帖数: 44 | 9 That's what I always think. So you're saying TIJ is wrong about this?
btw, what're eden,young gen and perm gen here. Thanks.
【在 l****u 的大作中提到】 : string by new String("aaa") are not pooled. : they are just staying at eden or young gen : while pooled strings are staying at perm gen. : so they are not pointing to same memory address.
|
m******t 发帖数: 2416 | 10
It's always a != b. But that's not what the TIJ author meant. He is
talking about the constant string pool, which contains all the literal
strings in the application, and both a and b would be backed by the same
entry in that pool. See the javadoc for String.intern().
【在 b*******p 的大作中提到】 : But : String a = "a"; : String b = new String("a"); : a == b will return false, how could they map to the same instance? : : (" : in : _
|
|
|
b*******p 发帖数: 44 | 11 well, that's my point. I think he consider those String objects created
using new the same as literal strings. So you also think he is wrong about
this.
【在 m******t 的大作中提到】 : : It's always a != b. But that's not what the TIJ author meant. He is : talking about the constant string pool, which contains all the literal : strings in the application, and both a and b would be backed by the same : entry in that pool. See the javadoc for String.intern().
|
m******t 发帖数: 2416 | 12
Not by what you quoted. He said "all map to the same memory," that's
referring to the underlying buffer, not the string instances.
Considering it's the 3rd edition of TIJ, I would tend to trust the author
not to make a mistake like that. 8-)
【在 b*******p 的大作中提到】 : well, that's my point. I think he consider those String objects created : using new the same as literal strings. So you also think he is wrong about : this.
|
b*******p 发帖数: 44 | 13 There is a buffer? Don't think so. If Java can keep track of all String
objects with the same content, then why not do the same thing as those
initialized by string literals since String objects are immutable. And wots
the buffer for here?
【在 m******t 的大作中提到】 : : Not by what you quoted. He said "all map to the same memory," that's : referring to the underlying buffer, not the string instances. : Considering it's the 3rd edition of TIJ, I would tend to trust the author : not to make a mistake like that. 8-)
|
m******t 发帖数: 2416 | 14
wots
Just go and read the javadoc for String.intern(), will you? 8-)
Oh, and the JLS 3.10.5.
【在 b*******p 的大作中提到】 : There is a buffer? Don't think so. If Java can keep track of all String : objects with the same content, then why not do the same thing as those : initialized by string literals since String objects are immutable. And wots : the buffer for here?
|
b*******p 发帖数: 44 | 15 I read those before.
Actually, according to the doc comments of String.intern, not every string
will be in that pool. So his words "all map to the same memory" shouldn't
mean mapping into the same string in the pool.
【在 m******t 的大作中提到】 : : wots : Just go and read the javadoc for String.intern(), will you? 8-) : Oh, and the JLS 3.10.5.
|
g*****g 发帖数: 34805 | 16 Why are you guys beating the dead horse.
For all non-JVM developers, it's more than enough to know
you always compare string by "equals".
And I never had the need to use new String() constructor.
【在 b*******p 的大作中提到】 : I read those before. : Actually, according to the doc comments of String.intern, not every string : will be in that pool. So his words "all map to the same memory" shouldn't : mean mapping into the same string in the pool.
|
b*******p 发帖数: 44 | 17 I don't need that when I coding, just out of curiosity whether I understood
it wrong or TIJ was wrong... :)
【在 g*****g 的大作中提到】 : Why are you guys beating the dead horse. : For all non-JVM developers, it's more than enough to know : you always compare string by "equals". : And I never had the need to use new String() constructor.
|
A**o 发帖数: 1550 | 18 so tij sucks by wasting people's time on such stuff.
understood
【在 b*******p 的大作中提到】 : I don't need that when I coding, just out of curiosity whether I understood : it wrong or TIJ was wrong... :)
|
g*****g 发帖数: 34805 | 19 It's like the 1 ++++ 1 question you'll see once a while on
programming board. You don't care, whoever write some code
that way should be fired. Program is written to be readable
without 2nd guess.
understood
【在 b*******p 的大作中提到】 : I don't need that when I coding, just out of curiosity whether I understood : it wrong or TIJ was wrong... :)
|
l****u 发帖数: 2166 | 20 regions of how jvm divides heap mah
【在 b*******p 的大作中提到】 : That's what I always think. So you're saying TIJ is wrong about this? : btw, what're eden,young gen and perm gen here. Thanks.
|
|
|
m******t 发帖数: 2416 | 21
intern() isn't the actual underlying buffer. Note the word "canonical" in
there. 8-)
【在 b*******p 的大作中提到】 : I read those before. : Actually, according to the doc comments of String.intern, not every string : will be in that pool. So his words "all map to the same memory" shouldn't : mean mapping into the same string in the pool.
|
m******t 发帖数: 2416 | 22
Yeah, until you hit the bug where you changed a constant string value, but
forgot to rebuild all the classes depending on it.
【在 g*****g 的大作中提到】 : Why are you guys beating the dead horse. : For all non-JVM developers, it's more than enough to know : you always compare string by "equals". : And I never had the need to use new String() constructor.
|
m******t 发帖数: 2416 | 23
That I agree. For TIJ's intended audience, this stuff isn't necessary.
【在 A**o 的大作中提到】 : so tij sucks by wasting people's time on such stuff. : : understood
|
l****u 发帖数: 2166 | 24 what is tij a? //blush
【在 m******t 的大作中提到】 : : That I agree. For TIJ's intended audience, this stuff isn't necessary.
|
g*****g 发帖数: 34805 | 25 And why would I do something like that? I am relying on ant script
to do the job, and I always uses a clean build for deployment.
【在 m******t 的大作中提到】 : : That I agree. For TIJ's intended audience, this stuff isn't necessary.
|
m******t 发帖数: 2416 | 26
It gets trickier if you have multiple modules built separately, e.g.,
through maven. Or if your code relies on some 3rd party libraries.
【在 g*****g 的大作中提到】 : And why would I do something like that? I am relying on ant script : to do the job, and I always uses a clean build for deployment.
|
m******t 发帖数: 2416 | 27
I believe we are talking about Thinking In Java, the book.
【在 l****u 的大作中提到】 : what is tij a? //blush
|
l****u 发帖数: 2166 | 28 o. I have the book. let's me dig dig tonite
mine seemly first edition tho
【在 m******t 的大作中提到】 : : I believe we are talking about Thinking In Java, the book.
|
r*****l 发帖数: 2859 | 29 JVM definitely does not do 'lookup'. At least it can calculate
the hash of String literal and store the literal according to
the hash value. That's constant time operation. And that may
be just the JVM has to do anyway.
"If s2 ends up having exactly "Hello World", would that be the same entry in
the pool? To achieve that, the JVM would have to do a lookup after _every_
concatenation, which is probably more inefficient than allocating a new
buffer."
【在 m******t 的大作中提到】 : : I believe we are talking about Thinking In Java, the book.
|
r*****l 发帖数: 2859 | 30 I think people confuse the concept of "String literal" and
"String object". My understanding is:
"String literal" is the actual string.
"String object" is the object wrap around "String literal"
After the following code
String a = "abc";
String b = new String("abc");
a and b are two object since a==b is not true. While a and b
use the same string literal.
I think TIJ means the latter. |
|
|
m******t 发帖数: 2416 | 31 This is pretty much what I was trying to explain to the OP - except you put
it in a clearer way.
【在 r*****l 的大作中提到】 : I think people confuse the concept of "String literal" and : "String object". My understanding is: : "String literal" is the actual string. : "String object" is the object wrap around "String literal" : After the following code : String a = "abc"; : String b = new String("abc"); : a and b are two object since a==b is not true. While a and b : use the same string literal. : I think TIJ means the latter.
|