看板java
標 題Re: [無聊] string, ==, equals
發信站神秘之旅 (Thu Jun 29 14:34:29 2006)
轉信站ptt!ctu-reader!ctu-gate!news.nctu!news.ncu!news.cycu!news.ice.cycu!BBS
我猜是 x == z != y
所以output 是
x, z are same obj.
same content
※ 引述《[email protected] (永遠的黑眼圈)》之銘言:
> 最近遇到一位小朋友,他一直弄不懂java裡頭string,於是給了他一個題目玩玩。
> java初心者也可以來試試看。猜猜結果會是什麼。
> public class Test {
> public static void main(String args[]){
> String y = "ab";
> y = y.concat("c");
> String x = "abc";
> String z = "abc";
> System.out.println(x.hashCode() + " " + y.hashCode()
> + " " + z.hashCode());
> // this line print: 96354 96354 96354
> if(x == z){
> System.out.println("x, z are same obj.");
> }
> if(y == z){
> System.out.println("y, z are same obj.");
> }
> if(x == y){
> System.out.println("x, y are same obj.");
> }
> if(x.equals(y)){
> System.out.println("same content");
> }
> }
> }
--
\\ ▎ \\ ▎
\ \ ▎ \ \ ▎ \\
▎ ▎ \☆/
\● ●看....流星 ▎ \● ● 咦...... ▎ ● ●
█\/█\ ▎ █\/█ 在哪裡ㄚ ▎/█\/█\ 哎呀!!!
/\ /\ ▎ /\ /\ ▎ /\ /\
--
╭┬╮
│
┴│╭─﹏﹏──────────────
Web bbs http://bbs.ice.cycu.edu.tw
└┬┘│
中原資工神秘之旅 Post by ega │
╰﹏╯
From:140.135.5.37 ╰﹏﹏
bbs.ice.cycu.edu.tw﹏﹏
Ω