java 整型128陷阱

public class XianJing {
	public static void main(String[] ages) {
	Integer vv1 = 127;
	Integer vv2 = 127;
	Integer vv3 = 128;
	Integer vv4 = 128;
	System.out.println( vv1== vv2 );
	System.out.println( vv3 == vv4 );
	
	}
}

执行结果:

true
false

注释:自动装箱规范要求 boolean byte char <=127,介于 -128~127之间的 short 和 int 被包装在固定的对象之中。例如:上面的例子  vv1 = vv2 初始化为127,对他们的比较一定成立。只要大于128 那么就会使得等是不成立 返回 false 这就是所谓的128陷阱

猜你喜欢

转载自blog.csdn.net/weixin_42630877/article/details/81173270