Re: [問題] 初學者的幾點小疑問

看板java作者時間19年前 (2007/01/21 21:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/7 (看更多)
※ war3rd.bbs@ptt.cc (war3rd) wrote: > 以下這二支小程式.... > 程式一: > public static void main (String[] args){ > Integer n1 = new Integer(47); > Integer n2 = new Integer(47); > System.out.println(n1==n2); > Integer i1 = 47; > Integer i2 = 47; > System.out.println(i1==i2); > Integer p1 = 147; > Integer p2 = 147; > System.out.println(p1==p2); > } > } > 因為小弟剛接觸java(自修) 如果覺得問題太蠢還請見諒 > 問題1:為何n1==n2(結果:false)而i1==i2(結果:true) 他們的值不是都相等嗎? > 還是Integer oo = new Integer (xx) 與 Integer oo = xx 二種方式有哪不同嗎? > 問題2: 那又為啥同樣是 Integer oo = xx 的方式 > i1==i2(ture) 而p1==p2(false) 這又是為啥?我只不過數字修正了一下 基本上...Integer是個物件,物件的比較要用.equals 例如:i1.equals(i2); > 程式二: > class Letter { > char c; > } > public class test{ > static void f(Letter y) { > y.c='Z'; > } > public static void main (String[] args){ > Letter x= new Letter(); > x.c= 'a'; > System.out.println("1:x.c: "+x.c); > f(x); > System.out.println("2:x.c: "+x.c); > } > } > 問題3: 為何第二次的輸出 x.c的字元是Z 而不是a 呢? > ps: 如果有有違反板規請告知 我會自d的(因為不知算不算是作業文^^u) 因為方法f是call by reference (?) -- だれかとだれかつなぐ 赤い糸 -- ███ ███ Post by Pistachi ◢█ ▉▉█ █▇█ From 220-134-160-28.HINET-IP.hinet.net ◥█◤ ﹉ ﹍ ﹋ 月光海洋BBS 《mobbs.stut.edu.tw》 163.26.220.130
文章代碼(AID): #15isjM00 (java)
文章代碼(AID): #15isjM00 (java)