/*
* Copyright (c) 2006, 2023, webrx.cn All rights reserved.
*
*/
package cn.webrx;
/**
* <p>Project: wxbili2mp4 - Test
* <p>Powered by webrx On 2023-11-14 20:28:46
* <p>描述:<p>
*
* @author webrx [webrx@126.com]
* @version 1.0
* @since 17
*/
@SuppressWarnings("all")
public class Test {
public static void main(String[] args) {
Integer a1 = 100;
Integer a2 = 100;
System.out.println(a1 == a2); //true
System.out.println(a1.equals(a2));//true
Integer a3 = Integer.valueOf(100);
Integer a4 = Integer.valueOf(100);
System.out.println(a3 == a4);//true
System.out.println(a3.equals(a4));//true
//java 9 不建议使用new Integer()实例化对象
Integer a5 = new Integer(100);
Integer a6 = new Integer(100);
System.out.println(a5 == a6);//false
System.out.println(a5.equals(a6));//true
}
}
执行结果如下:

对象实例的equals方法是判断对象的内容是不是一样,在java程序中,如果使用的new就会给对象重新分配内存地址。==主要是用来判断对象内存是不是一样,如果一样就为true。
本文解释了Java中的equals()和==方法在比较对象时的区别,指出equals用于内容比较,而==用于内存地址判断。特别提到Java9不推荐使用newInteger()创建对象实例。
1138

被折叠的 条评论
为什么被折叠?



