Integer和int的比较大小

来源:互联网 发布:线切割五角星编程 编辑:程序博客网 时间:2024/06/05 08:28

1.Int和Integer比较大小

public static void main(String[] args) {
int i = 10;
Integer i1 = new Integer(10);
System.out.println(i == i1);
}

true

Integer和int比较会进行自动拆箱,比较的是大小

2.Integer和Integer比较大小

public static void main(String[] args) {
Integer i =new Integer(10);
Integer i1 = new Integer(10);
System.out.println(i == i1);
}

false

    Integer 直接等于数字如果在-128到127之间会保存到常量池,而直接new出来的是对象,所以不相等

public static void main(String[] args) {
Integer i = 10;
Integer i1 = 10;
System.out.println(i == i1);
}

true

public static void main(String[] args) {
Integer i = 128;
Integer i1 = 128;
System.out.println(i == i1);
}

false


2 0
原创粉丝点击