u****s 发帖数: 2186 | 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? |
s***8 发帖数: 1136 | |
u****s 发帖数: 2186 | 3 try to run the code
【在 s***8 的大作中提到】 : not eq
|
h*****0 发帖数: 4889 | 4 this is definitely "equal". String literals are automatically optimised.
【在 s***8 的大作中提到】 : not eq
|
b*****t 发帖数: 1276 | 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. |
n*********n 发帖数: 580 | 6 Google "String literal pool", it is indeed a very basic knowledge in Java. |
k*******d 发帖数: 701 | 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() |
g**e 发帖数: 6127 | 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()
|
S**I 发帖数: 15689 | 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()
|
k*******d 发帖数: 701 | 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".
|
|
|
x*****p 发帖数: 1707 | 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? |
g**e 发帖数: 6127 | 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?
|
g*****g 发帖数: 34805 | 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?
|
g**e 发帖数: 6127 | 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.
|
g*****g 发帖数: 34805 | 15 Unless it's SCJP certificate test, my answer above
is more than enough.
【在 g**e 的大作中提到】 : 面试最喜欢问这种八股
|
g**e 发帖数: 6127 | 16 有一次我说比较string应该用equals,GS的一个老印很不屑的摇了摇头
【在 g*****g 的大作中提到】 : Unless it's SCJP certificate test, my answer above : is more than enough.
|
g*****g 发帖数: 34805 | 17 You can't convince incompetent people, just move on.
【在 g**e 的大作中提到】 : 有一次我说比较string应该用equals,GS的一个老印很不屑的摇了摇头
|
x*****p 发帖数: 1707 | 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.
|
F****n 发帖数: 3271 | 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?
|
m******t 发帖数: 2416 | 20
Some Indians shake heads when they actually agree.
(I know. "Some", that is the confusing part.)
【在 g**e 的大作中提到】 : 有一次我说比较string应该用equals,GS的一个老印很不屑的摇了摇头
|
|
|
m******t 发帖数: 2416 | 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.
|
v*********n 发帖数: 3983 | 22 要看是左右摆还是左右转。
【在 m******t 的大作中提到】 : : is : Actually using StringBuilder is the new common sense since, you know, Java 5.
|
c********g 发帖数: 449 | 23 老印摇了摇头=yes
you should know that^_^ |
g**e 发帖数: 6127 | 24 这个我是知道滴,不过不是所有老印都这样,我们group的几个老印都是点头=yes的
那个哥们当时一脸不屑的表情,显然不是yes,呵呵。
【在 c********g 的大作中提到】 : 老印摇了摇头=yes : you should know that^_^
|