avatar
Java练习题 3# Java - 爪哇娇娃
u*s
1
public static void main(String [] args)
{
String str1 = "String";
String str2 = "String";
System.out.println(str1 == str2 ? "equal":"not equal");
}
Compile error or runtime error?
What will be printed? equal or not equal?
avatar
s*8
2
not eq
avatar
u*s
3
try to run the code

【在 s***8 的大作中提到】
: not eq
avatar
h*0
4
this is definitely "equal". String literals are automatically optimised.

【在 s***8 的大作中提到】
: not eq
avatar
b*t
5
should be equal.
using debugger you will see both var have same id.
because in jvm, same string are reused, that's one of the reason string is
not mutable.
avatar
n*n
6
Google "String literal pool", it is indeed a very basic knowledge in Java.
avatar
k*d
7
not equal!!!
str1.equal(str2) is true;
str1==str2 is false.
== is a test of address
equal() is a test of value, and it depends on hashCode()
avatar
g*e
8
自己写个程序验证一下

【在 k*******d 的大作中提到】
: not equal!!!
: str1.equal(str2) is true;
: str1==str2 is false.
: == is a test of address
: equal() is a test of value, and it depends on hashCode()

avatar
S*I
9
you need to learn more :); guess you never heard "String literal pool".

【在 k*******d 的大作中提到】
: not equal!!!
: str1.equal(str2) is true;
: str1==str2 is false.
: == is a test of address
: equal() is a test of value, and it depends on hashCode()

avatar
k*d
10
en, you r right
because of the String literal pool, they are equal.

【在 S**I 的大作中提到】
: you need to learn more :); guess you never heard "String literal pool".
avatar
x*p
11
Answer: equal.
Try this one:
String s1 = "ab";
String s2 = s1 + "cd";
String s3 = "abcd";
System.out.println(s2==s3);
What is the output?
avatar
g*e
12
0

【在 x*****p 的大作中提到】
: Answer: equal.
: Try this one:
: String s1 = "ab";
: String s2 = s1 + "cd";
: String s3 = "abcd";
: System.out.println(s2==s3);
: What is the output?

avatar
g*g
13
I think this is a stupid question. Every java developer should be
aware of the String literal pool and know their internal representation
is the same and avoid pitfalls. e.g. we shouldn't use a String as
synchronization lock. At the same time, it's good enough to know
you should always use equals to compare a String.
Why would I care the result of some code that never should be
written.

【在 x*****p 的大作中提到】
: Answer: equal.
: Try this one:
: String s1 = "ab";
: String s2 = s1 + "cd";
: String s3 = "abcd";
: System.out.println(s2==s3);
: What is the output?

avatar
g*e
14
面试最喜欢问这种八股

【在 g*****g 的大作中提到】
: I think this is a stupid question. Every java developer should be
: aware of the String literal pool and know their internal representation
: is the same and avoid pitfalls. e.g. we shouldn't use a String as
: synchronization lock. At the same time, it's good enough to know
: you should always use equals to compare a String.
: Why would I care the result of some code that never should be
: written.

avatar
g*g
15
Unless it's SCJP certificate test, my answer above
is more than enough.

【在 g**e 的大作中提到】
: 面试最喜欢问这种八股
avatar
g*e
16
有一次我说比较string应该用equals,GS的一个老印很不屑的摇了摇头

【在 g*****g 的大作中提到】
: Unless it's SCJP certificate test, my answer above
: is more than enough.

avatar
g*g
17
You can't convince incompetent people, just move on.

【在 g**e 的大作中提到】
: 有一次我说比较string应该用equals,GS的一个老印很不屑的摇了摇头
avatar
x*p
18
No, this is not a "stupid" question. This has very practical application
in real industry. Look at the question from JPMC.
(1) String s = "abc";
s = s + "def";
(2) StringBuffer sb = new StringBuffer("abc");
sb.append("def");
Both of them can get string "abcdef". Who has better performance?

【在 g*****g 的大作中提到】
: I think this is a stupid question. Every java developer should be
: aware of the String literal pool and know their internal representation
: is the same and avoid pitfalls. e.g. we shouldn't use a String as
: synchronization lock. At the same time, it's good enough to know
: you should always use equals to compare a String.
: Why would I care the result of some code that never should be
: written.

avatar
F*n
19
This has nothing to do with the "stupid" question and using StringBuffer is
common sense.

【在 x*****p 的大作中提到】
: No, this is not a "stupid" question. This has very practical application
: in real industry. Look at the question from JPMC.
: (1) String s = "abc";
: s = s + "def";
: (2) StringBuffer sb = new StringBuffer("abc");
: sb.append("def");
: Both of them can get string "abcdef". Who has better performance?

avatar
m*t
20

Some Indians shake heads when they actually agree.
(I know. "Some", that is the confusing part.)

【在 g**e 的大作中提到】
: 有一次我说比较string应该用equals,GS的一个老印很不屑的摇了摇头
avatar
m*t
21

is
Actually using StringBuilder is the new common sense since, you know, Java 5.

【在 F****n 的大作中提到】
: This has nothing to do with the "stupid" question and using StringBuffer is
: common sense.

avatar
v*n
22
要看是左右摆还是左右转。

【在 m******t 的大作中提到】
:
: is
: Actually using StringBuilder is the new common sense since, you know, Java 5.

avatar
c*g
23
老印摇了摇头=yes
you should know that^_^
avatar
g*e
24
这个我是知道滴,不过不是所有老印都这样,我们group的几个老印都是点头=yes的
那个哥们当时一脸不屑的表情,显然不是yes,呵呵。

【在 c********g 的大作中提到】
: 老印摇了摇头=yes
: you should know that^_^

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。