“==” 、equals 、instance of区别
来源:互联网 发布:如何避开公司网络监控 编辑:程序博客网 时间:2024/06/18 05:41
用来比较两个操作元是否相等,是否引用同一个对象,这两个操作元既可以是基本类型,也可以是引用类型。
* <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();
}
}
输出结果:
false
false
-----------f2()-----------
int1==int2 is false
int1==int3 is true
array1==array2 is false
array1==array3 is true
equals() 方法
quals() 方法是在 Object 类中定义的方法,它的声明格式如下:
/**
* <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操作符左边的操作元是一个引用类型,右边的操作元是一个类名或接口名。
- “==” 、equals 、instance of区别
- java4Android(23):equals 的使用方法 instance 操作符【==与equals方法的区别】
- Effective C# 9:nderstand the Relationships Among ReferenceEquals(), static Equals(), instance Equals(), and operator==
- ReferenceEquals(), static Equals(), instance Equals(), 和运算行符==之间的关系
- equals()和==区别
- equals和==区别
- "=="和equals区别
- ==和equals区别
- equals与==区别
- ==和Equals区别
- ==与equals()区别
- equals与==区别
- Equals()和==区别
- ==与equals()区别
- ==和equals区别
- ==与equals()区别
- equals,==的区别
- ==和equals()区别
- Python官网主页改版 http://www.python.org/
- IOS 协议与代理 的小注
- PLSQLDevelopment字符集与数据库冲突
- cocos2d-x IOS 和Android播放视频(包括网络视频)
- javaweb之Session客户端防表单重复提交(js)和服务端Session防表单重复提交
- “==” 、equals 、instance of区别
- Android版Web服务器实现(四)界面更新
- Minifilter驱动开发指南
- android 手指跟随拖拽 ACTION_MOVE 不能识别的问题
- 链表反转
- 虽然是技术人,但是要注重健康。so,第一篇讲养生 “治疗舒张压偏低”
- STL文件
- iOS开发之CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)
- decorators.xml的用法