Re: [無聊] string, ==, equals

看板java作者時間19年前 (2006/06/29 15:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
我猜是 x == z != y 所以output 是 x, z are same obj. same content ※ 引述《hougzou.bbs@ptt.cc (永遠的黑眼圈)》之銘言: > 最近遇到一位小朋友,他一直弄不懂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﹏﹏Ω
文章代碼(AID): #14etgl00 (java)
文章代碼(AID): #14etgl00 (java)