看板java
标 题Re: [问题] 初学者的几点小疑问
发信站月光海洋BBS (Sun Jan 21 21:18:00 2007)
转信站ptt!ctu-reader!news.nctu!mobbs
※ [email protected] (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