Java 数值比较

Integer a1 = 128;
Integer b1 = 128;
System.out.println("Integer Integer equals "+a1.equals(b1));
System.out.println("Integer Integer == "+ (a1 == b1));

Byte a2 = 1;
Byte b2 = 1;
System.out.println("Byte Byte equals "+a2.equals(b2));
System.out.println("Byte Byte == "+(a2 == b2));

Integer a3 = 128;
System.out.println("Integer num equals "+a3.equals(128));
System.out.println("Integer num == "+(a3 == 128));

Byte a4 = 1;
System.out.println("Byte num equals "+a4.equals(1));
System.out.println("Byte num == "+(a4 == 1));

运行结果:
image.png

结论

数值型包装类型与数值型包装类型比较,使用equals
数值型包装类型与常量比较,使用==
字符串型包装类型与字符串型包装类型比较,使用equals
字符串型包装类型与常量比较,使用equals


点墨
26 声望3 粉丝

全栈前端开发工程师