boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Java String concatenation
相关主题
Q in C/C++
regular expression mathinc --Java写竟然超时了/。
question 2: o(1) euque and dequeue?
求DEBUG Substring with Concatenation of All Words
storm8 online test 讨论
问大牛们一个Leetcode上的题
帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "
Substring with Concatenation of All Words 还有更简洁的解法吗?
请问Substring with Concatenation of All Words?
Leetcode的Substring with Concatenation of All Words超时。
相关话题的讨论汇总
话题: string话题: java话题: s1话题: s2
进入JobHunting版参与讨论
1 (共1页)
y*******7
发帖数: 99
1
String s="abc";
String s1=s+"";
String s2="ab"+"c";
s==s1 false
s和s1为什么不等
s==s2 true
谢谢
w****r
发帖数: 15252
2
string 在Java里面是 immutable data type, 意思就是一旦建立就不能改了, 所以s1
就会在string pool里面再建一个string,两个reference不一样,而是s2建立的过程
Java回去string pool里面找是否有ABC,如果存在就引用,不存在再新建,所以s和s2是
一个reference

【在 y*******7 的大作中提到】
: String s="abc";
: String s1=s+"";
: String s2="ab"+"c";
: s==s1 false
: s和s1为什么不等
: s==s2 true
: 谢谢

y*******7
发帖数: 99
3

s1
s1建立在pool上是什么样? "abc"??

【在 w****r 的大作中提到】
: string 在Java里面是 immutable data type, 意思就是一旦建立就不能改了, 所以s1
: 就会在string pool里面再建一个string,两个reference不一样,而是s2建立的过程
: Java回去string pool里面找是否有ABC,如果存在就引用,不存在再新建,所以s和s2是
: 一个reference

n*******s
发帖数: 17267
4
It is one of java's stupid implementations, but you have to live with it and
you have to agree with people who think they have grasped this core, lol.

【在 y*******7 的大作中提到】
: String s="abc";
: String s1=s+"";
: String s2="ab"+"c";
: s==s1 false
: s和s1为什么不等
: s==s2 true
: 谢谢

n*******s
发帖数: 17267
5
In general, + always create new string unless it happens at compiler time,
so
in s1=s+"" a new string is created, but in s2="ab"+"c", no new string is
created, based on my poor memory.

【在 y*******7 的大作中提到】
:
: s1
: s1建立在pool上是什么样? "abc"??

l****r
发帖数: 637
6
nm

s1

【在 w****r 的大作中提到】
: string 在Java里面是 immutable data type, 意思就是一旦建立就不能改了, 所以s1
: 就会在string pool里面再建一个string,两个reference不一样,而是s2建立的过程
: Java回去string pool里面找是否有ABC,如果存在就引用,不存在再新建,所以s和s2是
: 一个reference

f********x
发帖数: 2086
7

s1
s1查找的时候为什么确定 "abc" 不存在?

【在 w****r 的大作中提到】
: string 在Java里面是 immutable data type, 意思就是一旦建立就不能改了, 所以s1
: 就会在string pool里面再建一个string,两个reference不一样,而是s2建立的过程
: Java回去string pool里面找是否有ABC,如果存在就引用,不存在再新建,所以s和s2是
: 一个reference

p*****p
发帖数: 379
8
JLS规定的:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jl
The String object is newly created (§12.5) unless the expression is a
compile-time constant expression (§15.28).
String s1=s+"";
不是一个 compile-time constant expression,因为s是个变量
同理
String s3 = "bc";
s == "a" + s3 -> false
f********x
发帖数: 2086
9
哦,明白了
关键在于compile time

【在 p*****p 的大作中提到】
: JLS规定的:
: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jl
: The String object is newly created (§12.5) unless the expression is a
: compile-time constant expression (§15.28).
: String s1=s+"";
: 不是一个 compile-time constant expression,因为s是个变量
: 同理
: String s3 = "bc";
: s == "a" + s3 -> false

1 (共1页)
进入JobHunting版参与讨论
相关主题
Leetcode的Substring with Concatenation of All Words超时。
Leetcode第30题真心不容易
一道design题目
怎么sort inside a string itself in python
Leetcode OJ的编译器是?
问一道uber onsite题目
Java parking lot 编程题
Java programming question
[合集] 没人回复,直接上板上讨论吧。如果违反版规希望斑竹删掉就是
刚和Amazon电话面试完
相关话题的讨论汇总
话题: string话题: java话题: s1话题: s2