由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Java练习题 3
相关主题
Java StringBuilder myth debunked这两个程序哪个更快?
Java练习题 9a simple question
java 的内存分布?请问StringBuffer的OutofMemory问题
问个set和literal String的问题菜鸟问个简单的问题
Java 入门求教我自己编了个Java面试题
请问有谁考过1z0-851?难度怎么样?为什么我这个参数的内容存不下?
两个很基本的JAVA问题初学者code请教 (大牛莫取笑)
int --> String?Java练习题 12
相关话题的讨论汇总
话题: string话题: equal话题: str2话题: java话题: str1
进入Java版参与讨论
1 (共1页)
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
2
not eq
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".
相关主题
请问有谁考过1z0-851?难度怎么样?这两个程序哪个更快?
两个很基本的JAVA问题a simple question
int --> String?请问StringBuffer的OutofMemory问题
进入Java版参与讨论
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的一个老印很不屑的摇了摇头
相关主题
菜鸟问个简单的问题初学者code请教 (大牛莫取笑)
我自己编了个Java面试题Java练习题 12
为什么我这个参数的内容存不下?why doesn't replaceAll work?
进入Java版参与讨论
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^_^

1 (共1页)
进入Java版参与讨论
相关主题
Java练习题 12Java 入门求教
why doesn't replaceAll work?请问有谁考过1z0-851?难度怎么样?
Java大侠们:Hashtable help please!两个很基本的JAVA问题
expression in unicodeint --> String?
Java StringBuilder myth debunked这两个程序哪个更快?
Java练习题 9a simple question
java 的内存分布?请问StringBuffer的OutofMemory问题
问个set和literal String的问题菜鸟问个简单的问题
相关话题的讨论汇总
话题: string话题: equal话题: str2话题: java话题: str1