x***i 发帖数: 585 | 1 class被load到memory里。所以有一块是code segment
stack 和 heap 分别是低到高,高到低 分享以大块。
请问 string 字符串 常量 会放在哪里?
String str1 = "abcd";
String str2 = new String("abcd");
按我的理解str1 是放在一块特殊的data segment。
而str2 是放在heap里面,不知这样是否理解准确? |
g**e 发帖数: 6127 | 2 string literal is saved in perm generation.
str2 is in heap like any regular class until str2.intern() is called.
【在 x***i 的大作中提到】 : class被load到memory里。所以有一块是code segment : stack 和 heap 分别是低到高,高到低 分享以大块。 : 请问 string 字符串 常量 会放在哪里? : String str1 = "abcd"; : String str2 = new String("abcd"); : 按我的理解str1 是放在一块特殊的data segment。 : 而str2 是放在heap里面,不知这样是否理解准确?
|
x***i 发帖数: 585 | 3 那static变量是不是也在perm generation里? |
g**e 发帖数: 6127 | 4 yes
【在 x***i 的大作中提到】 : 那static变量是不是也在perm generation里?
|
x***i 发帖数: 585 | |
k******p 发帖数: 21 | 6 str1: string pool at perm
str2: heap
str1 != str2
【在 x***i 的大作中提到】 : class被load到memory里。所以有一块是code segment : stack 和 heap 分别是低到高,高到低 分享以大块。 : 请问 string 字符串 常量 会放在哪里? : String str1 = "abcd"; : String str2 = new String("abcd"); : 按我的理解str1 是放在一块特殊的data segment。 : 而str2 是放在heap里面,不知这样是否理解准确?
|
m*****k 发帖数: 731 | 7 was asked this question:
String a = "abc";
String b = "ab";
how many strings are created, the answer from the interviewer is only 1 "abc
" is created, can u guys confirm? |
g*****g 发帖数: 34805 | 8 He's wrong.
abc
【在 m*****k 的大作中提到】 : was asked this question: : String a = "abc"; : String b = "ab"; : how many strings are created, the answer from the interviewer is only 1 "abc : " is created, can u guys confirm?
|
s******e 发帖数: 493 | 9 I think it should be like this:
two string objects in heap (str1 and str 2).
one string literal in string pool (I do not think that spec ahs ever stated
it must be in perm gen, but highly possible. And for hotspot, i think it is)
. |
g***s 发帖数: 3811 | 10 i think the question was:
String a = "abc";
String b = "abc";
how many strings are created, the answer from the interviewer is only 1 "
abc" is created.
abc
【在 m*****k 的大作中提到】 : was asked this question: : String a = "abc"; : String b = "ab"; : how many strings are created, the answer from the interviewer is only 1 "abc : " is created, can u guys confirm?
|
|
|
r*****l 发帖数: 2859 | 11 String b = "ab", not "abc".
【在 g***s 的大作中提到】 : i think the question was: : String a = "abc"; : String b = "abc"; : how many strings are created, the answer from the interviewer is only 1 " : abc" is created. : : abc
|
g***s 发帖数: 3811 | 12 Either madmonk remembered wrong, or the interviewer gave a wrong question.
I think the orignial question should be b="abc". Intervewer wanted to know
if the intervewee knows the "abc" is generated in perm.
【在 r*****l 的大作中提到】 : String b = "ab", not "abc".
|
g***s 发帖数: 3811 | 13 Let's come out another interview question:
boolean f(int x){
Integer a = x;
Integer b = x;
return a==b;
}
1. f(1) should return true or false?
2. f(1000) should return true or false?
【在 g***s 的大作中提到】 : Either madmonk remembered wrong, or the interviewer gave a wrong question. : I think the orignial question should be b="abc". Intervewer wanted to know : if the intervewee knows the "abc" is generated in perm.
|
g*****g 发帖数: 34805 | 14 f(1) should be true, f(1000) not so sure, I know if
the number is big enough, it will be false.
Sync on Integer may lock the system in unexpected way,
that's all it's about.
【在 g***s 的大作中提到】 : Let's come out another interview question: : boolean f(int x){ : Integer a = x; : Integer b = x; : return a==b; : } : 1. f(1) should return true or false? : 2. f(1000) should return true or false?
|
g***s 发帖数: 3811 | 15 for -128 to 127(or bool true/false), when autobox, JVM will reuse the same
object, just like pick a object from a pool.
【在 g*****g 的大作中提到】 : f(1) should be true, f(1000) not so sure, I know if : the number is big enough, it will be false. : Sync on Integer may lock the system in unexpected way, : that's all it's about.
|
g**e 发帖数: 6127 | 16 还好这个我看过,不然又挂了
【在 g***s 的大作中提到】 : Let's come out another interview question: : boolean f(int x){ : Integer a = x; : Integer b = x; : return a==b; : } : 1. f(1) should return true or false? : 2. f(1000) should return true or false?
|
q*****9 发帖数: 85 | 17 re
【在 g***s 的大作中提到】 : i think the question was: : String a = "abc"; : String b = "abc"; : how many strings are created, the answer from the interviewer is only 1 " : abc" is created. : : abc
|