java基础之"=="操作符

Java代码
public class Test
{
public static void main(String[] args)
{
Test t = new Test();

t.compare();
t.compare2();
}

public void compare()
{
//1
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
Integer i3 = i1;

System.out.println(i1 == i2);
System.out.println(i1 == i3);
System.out.println(i1.equals(i2));
System.out.println();

//2
Boolean b1 = new Boolean(false);
Boolean b2 = new Boolean(false);
Boolean b3 = b1;

System.out.println(b1 == b2);
System.out.println(b1 == b3);
System.out.println(b1.equals(b2));
System.out.println();

//3
int[] a1 = new int[3];
int[] a2 = new int[3];
int[] a3 = a1;

System.out.println(a1 == a2);
System.out.println(a1 == a3);
System.out.println(a1.equals(a2));
System.out.println();
}

public void compare2()
{
TestC tc = new TestC();
TestB tb = new TestB();
TestA ta = new TestA();

System.out.println(tc == tb);
System.out.println(tc == ta);

ta = tb;
System.out.println(ta == tb);

ta = tc;
System.out.println(ta == tc);

ta = new TestC();
System.out.println(ta == tc);
}
}

class TestC extends TestB
{

}

class TestB extends TestA
{

}

class TestA
{

}

public class Test
{
public static void main(String[] args)
{
Test t = new Test();

t.compare();
t.compare2();
}

public void compare()
{
//1
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
Integer i3 = i1;

System.out.println(i1 == i2);
System.out.println(i1 == i3);
System.out.println(i1.equals(i2));
System.out.println();

//2
Boolean b1 = new Boolean(false);
Boolean b2 = new Boolean(false);
Boolean b3 = b1;

System.out.println(b1 == b2);
System.out.println(b1 == b3);
System.out.println(b1.equals(b2));
System.out.println();

//3
int[] a1 = new int[3];
int[] a2 = new int[3];
int[] a3 = a1;

System.out.println(a1 == a2);
System.out.println(a1 == a3);
System.out.println(a1.equals(a2));
System.out.println();
}

public void compare2()
{
TestC tc = new TestC();
TestB tb = new TestB();
TestA ta = new TestA();

System.out.println(tc == tb);
System.out.println(tc == ta);

ta = tb;
System.out.println(ta == tb);

ta = tc;
System.out.println(ta == tc);

ta = new TestC();
System.out.println(ta == tc);
}
}

class TestC extends TestB
{

}

class TestB extends TestA
{

}

class TestA
{

}




输出为:



Java代码
false
true
true

false
true
true

false
true
false

false
false
true
true
false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值