作者war3rd (war3rd)
看板java
标题[问题] 初学者的几点小疑问
时间Sun Jan 21 20:21:53 2007
以下这二支小程式....
程式一:
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) 这又是为啥?我只不过数字修正了一下
程式二:
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)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.192.32.25