“==” 、equals 、instance of区别

操作符“==”

用来比较两个操作元是否相等,是否引用同一个对象,这两个操作元既可以是基本类型,也可以是引用类型。

  /**
     * <pre>
     *     当操作符“==”两边都是引用类型时,这两个引用变量必须都引用同一个对象,结果才为true。
     * </pre>
     
*/
    
public   static   void  f2() {
        Integer int1 
=   new  Integer( 1 );
        Integer int2 
=   new  Integer( 1 );
        Integer int3 
=  int1;  //  int3和int1引用同一个对象

        
int [] array1  =   new   int [ 1 ];
        
int [] array2  =   new   int [ 1 ];
        
int [] array3  =  array1;  //  array3和array1引用同一个对象

        System.out.println(
" int1==int2 is  "   +  (int1  ==  int2));
        System.out.println(
" int1==int3 is  "   +  (int1  ==  int3));
        System.out.println(
" array1==array2 is  "   +  (array1  ==  array2));
        System.out.println(
" array1==array3 is  "   +  (array1  ==  array3));
    }

    
/**
     * 
@param  args
     
*/
    
public   static   void  main(String[] args) {
        System.out.println(
" -----------f1()----------- " );
        Demo01.f1();
        System.out.println(
" -----------f2()----------- " );
        Demo01.f2();
    }

}

输出结果:
----------- f1() -----------
false
false
----------- f2() -----------
int1
== int2 is  false
int1
== int3 is  true
array1
== array2 is  false
array1
== array3 is  true

equals() 方法

quals() 方法是在 Object 类中定义的方法,它的声明格式如下:

    public   boolean  equals(Object obj)
Object 类的 equals() 方法的比较规则为:当参数 obj 引用的对象与当前对象为同一个对象时,就返回true,否则返回false。 


    /**
     * <pre>
     *         1、a1和a2变量引用不同的对象,因此用“==”或 equals() 方法比较的结果都为 false;
     *         2、a1和a3变量都引用同一个Dog对象,因此用“==”或equals()方法比较的结果都为true;
     * </pre>
     
*/
    
public static void f1() {
        Animal a1 
= new Dog();
        Animal a2 
= new Cat();
        Animal a3 
= a1;

        System.out.println(a1 
== a2); // 打印false
        System.out.println(a1.equals(a2)); // 打印false

        System.out.println(a1 
== a3); // 打印true
        System.out.println(a1.equals(a3)); // 打印true
    }

    
/**
     * <pre>
     *     在JDK中有一些类覆盖了 Object 类的equal()方法,它们的比较规则为:
     *         如果两个对象的类型一致,并且内容一致,则返回true。
     *     这些类包括:java.io.File、java.util.Date、java.lang.String、
     *         包装类(如java.lang.Integer和java.lang.Double类)。 
     * </pre>
     
*/
    
public static void f2() {
        Integer int1 
= new Integer(1);
        Integer int2 
= new Integer(1);

        String str1 
= new String("Hello");
        String str2 
= new String("Hello");

        System.out.println(int1 
== int2); // 打印false
        System.out.println(int1.equals(int2)); // 打印true

        System.out.println(str1 
== str2); // 打印false
        System.out.println(str1.equals(str2)); // 打印true
    }

instanceof操作符

 instanceof操作符用于判断一个引用类型所引用的对象是否是一个类的实例。
 instanceof操作符左边的操作元是一个引用类型,右边的操作元是一个类名或接口名。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值