java Long==Long有趣的现象详解

前言

long数据类型是64位有符号的Java原始数据类型。当对整数的计算结果可能超出int数据类型的范围时使用。

long数据类型范围是-9,223,372,036,854,775,808至9,223,372,036,854,775,807(-2^63至2^63-1)。

long数据类型范围内的所有整数称为long类型的整数字面量。long类型的整数常数总是以大写L或小写l结尾。

1. 描述

首先,这里应该声明一下,Long等对象类型判断是否相等,采用Long == Long这种形式是错误的。

可以采用以下方式:

Long left = 100L;
Long right = 100L;

// 方法1:
System.out.println(left.doubleValue() == right.doubleValue());

// 方法2:
System.out.println(left.compareTo(right));

输出结果:

true
0

1.1 实验1

如果你亲自操刀,动手做了以下实验,也许你会突然得到一个让你困惑的结果:

Long left = 100L;
Long right = 100L;

System.out.println(left == right);

输出结果:

true

没错,采用 == 竟然也能得出true,也许这时候你可能会误以为jvm在自动拆箱(我以前也是这样以为,有点小白!!!)。

1.2 实验2

那么请你再做下面这个实验:

Long left = 1000L;
Long right = 1000L;

// 方法1:
System.out.println(left.doubleValue() == right.doubleValue());

// 方法2:
System.out.println(left.compareTo(right));

// 错误的方法:
System.out.println(left == right);

输出结果

true
0
false

等会,什么情况,怎么又为false?

1.3 实验3

不急,再做一个实验。

for (int i = -200; i < 200; i++) {
      Long left = Long.valueOf(i);
      Long right = Long.valueOf(i);

      System.out.println("Long left = " + i + ", Long right = " + i
          + ", left == right :" + (left == right));
    }

输出结果

Long left = -200, Long right = -200, left == right :false
Long left = -199, Long right = -199, left == right :false
Long left = -198, Long right = -198, left == right :false
Long left = -197, Long right = -197, left == right :false
Long left = -196, Long right = -196, left == right :false
Long left = -195, Long right = -195, left == right :false
Long left = -194, Long right = -194, left == right :false
Long left = -193, Long right = -193, left == right :false
Long left = -192, Long right = -192, left == right :false
Long left = -191, Long right = -191, left == right :false
Long left = -190, Long right = -190, left == right :false
Long left = -189, Long right = -189, left == right :false
Long left = -188, Long right = -188, left == right :false
Long left = -187, Long right = -187, left == right :false
Long left = -186, Long right = -186, left == right :false
Long left = -185, Long right = -185, left == right :false
Long left = -184, Long right = -184, left == right :false
Long left = -183, Long right = -183, left == right :false
Long left = -182, Long right = -182, left == right :false
Long left = -181, Long right = -181, left == right :false
Long left = -180, Long right = -180, left == right :false
Long left = -179, Long right = -179, left == right :false
Long left = -178, Long right = -178, left == right :false
Long left = -177, Long right = -177, left == right :false
Long left = -176, Long right = -176, left == right :false
Long left = -175, Long right = -175, left == right :false
Long left = -174, Long right = -174, left == right :false
Long left = -173, Long right = -173, left == right :false
Long left = -172, Long right = -172, left == right :false
Long left = -171, Long right = -171, left == right :false
Long left = -170, Long right = -170, left == right :false
Long left = -169, Long right = -169, left == right :false
Long left = -168, Long right = -168, left == right :false
Long left = -167, Long right = -167, left == right :false
Long left = -166, Long right = -166, left == right :false
Long left = -165, Long right = -165, left == right :false
Long left = -164, Long right = -164, left == right :false
Long left = -163, Long right = -163, left == right :false
Long left = -162, Long right = -162, left == right :false
Long left = -161, Long right = -161, left == right :false
Long left = -160, Long right = -160, left == right :false
Long left = -159, Long right = -159, left == right :false
Long left = -158, Long right = -158, left == right :false
Long left = -157, Long right = -157, left == right :false
Long left = -156, Long right = -156, left == right :false
Long left = -155, Long right = -155, left == right :false
Long left = -154, Long right = -154, left == right :false
Long left = -153, Long right = -153, left == right :false
Long left = -152, Long right = -152, left == right :false
Long left = -151, Long right = -151, left == right :false
Long left = -150, Long right = -150, left == right :false
Long left = -149, Long right = -149, left == right :false
Long left = -148, Long right = -148, left == right :false
Long left = -147, Long right = -147, left == right :false
Long left = -146, Long right = -146, left == right :false
Long left = -145, Long right = -145, left == right :false
Long left = -144, Long right = -144, left == right :false
Long left = -143, Long right = -143, left == right :false
Long left = -142, Long right = -142, left == right :false
Long left = -141, Long right = -141, left == right :false
Long left = -140, Long right = -140, left == right :false
Long left = -139, Long right = -139, left == right :false
Long left = -138, Long right = -138, left == right :false
Long left = -137, Long right = -137, left == right :false
Long left = -136, Long right = -136, left == right :false
Long left = -135, Long right = -135, left == right :false
Long left = -134, Long right = -134, left == right :false
Long left = -133, Long right = -133, left == right :false
Long left = -132, Long right = -132, left == right :false
Long left = -131, Long right = -131, left == right :false
Long left = -130, Long right = -130, left == right :false
Long left = -129, Long right = -129, left == right :false
Long left = -128, Long right = -128, left == right :true
Long left = -127, Long right = -127, left == right :true
Long left = -126, Long right = -126, left == right :true
Long left = -125, Long right = -125, left == right :true
Long left = -124, Long right = -124, left == right :true
Long left = -123, Long right = -123, left == right :true
Long left = -122, Long right = -122, left == right :true
Long left = -121, Long right = -121, left == right :true
Long left = -120, Long right = -120, left == right :true
Long left = -119, Long right = -119, left == right :true
Long left = -118, Long right = -118, left == right :true
Long left = -117, Long right = -117, left == right :true
Long left = -116, Long right = -116, left == right :true
Long left = -115, Long right = -115, left == right :true
Long left = -114, Long right = -114, left == right :true
Long left = -113, Long right = -113, left == right :true
Long left = -112, Long right = -112, left == right :true
Long left = -111, Long right = -111, left == right :true
Long left = -110, Long right = -110, left == right :true
Long left = -109, Long right = -109, left == right :true
Long left = -108, Long right = -108, left == right :true
Long left = -107, Long right = -107, left == right :true
Long left = -106, Long right = -106, left == right :true
Long left = -105, Long right = -105, left == right :true
Long left = -104, Long right = -104, left == right :true
Long left = -103, Long right = -103, left == right :true
Long left = -102, Long right = -102, left == right :true
Long left = -101, Long right = -101, left == right :true
Long left = -100, Long right = -100, left == right :true
Long left = -99, Long right = -99, left == right :true
Long left = -98, Long right = -98, left == right :true
Long left = -97, Long right = -97, left == right :true
Long left = -96, Long right = -96, left == right :true
Long left = -95, Long right = -95, left == right :true
Long left = -94, Long right = -94, left == right :true
Long left = -93, Long right = -93, left == right :true
Long left = -92, Long right = -92, left == right :true
Long left = -91, Long right = -91, left == right :true
Long left = -90, Long right = -90, left == right :true
Long left = -89, Long right = -89, left == right :true
Long left = -88, Long right = -88, left == right :true
Long left = -87, Long right = -87, left == right :true
Long left = -86, Long right = -86, left == right :true
Long left = -85, Long right = -85, left == right :true
Long left = -84, Long right = -84, left == right :true
Long left = -83, Long right = -83, left == right :true
Long left = -82, Long right = -82, left == right :true
Long left = -81, Long right = -81, left == right :true
Long left = -80, Long right = -80, left == right :true
Long left = -79, Long right = -79, left == right :true
Long left = -78, Long right = -78, left == right :true
Long left = -77, Long right = -77, left == right :true
Long left = -76, Long right = -76, left == right :true
Long left = -75, Long right = -75, left == right :true
Long left = -74, Long right = -74, left == right :true
Long left = -73, Long right = -73, left == right :true
Long left = -72, Long right = -72, left == right :true
Long left = -71, Long right = -71, left == right :true
Long left = -70, Long right = -70, left == right :true
Long left = -69, Long right = -69, left == right :true
Long left = -68, Long right = -68, left == right :true
Long left = -67, Long right = -67, left == right :true
Long left = -66, Long right = -66, left == right :true
Long left = -65, Long right = -65, left == right :true
Long left = -64, Long right = -64, left == right :true
Long left = -63, Long right = -63, left == right :true
Long left = -62, Long right = -62, left == right :true
Long left = -61, Long right = -61, left == right :true
Long left = -60, Long right = -60, left == right :true
Long left = -59, Long right = -59, left == right :true
Long left = -58, Long right = -58, left == right :true
Long left = -57, Long right = -57, left == right :true
Long left = -56, Long right = -56, left == right :true
Long left = -55, Long right = -55, left == right :true
Long left = -54, Long right = -54, left == right :true
Long left = -53, Long right = -53, left == right :true
Long left = -52, Long right = -52, left == right :true
Long left = -51, Long right = -51, left == right :true
Long left = -50, Long right = -50, left == right :true
Long left = -49, Long right = -49, left == right :true
Long left = -48, Long right = -48, left == right :true
Long left = -47, Long right = -47, left == right :true
Long left = -46, Long right = -46, left == right :true
Long left = -45, Long right = -45, left == right :true
Long left = -44, Long right = -44, left == right :true
Long left = -43, Long right = -43, left == right :true
Long left = -42, Long right = -42, left == right :true
Long left = -41, Long right = -41, left == right :true
Long left = -40, Long right = -40, left == right :true
Long left = -39, Long right = -39, left == right :true
Long left = -38, Long right = -38, left == right :true
Long left = -37, Long right = -37, left == right :true
Long left = -36, Long right = -36, left == right :true
Long left = -35, Long right = -35, left == right :true
Long left = -34, Long right = -34, left == right :true
Long left = -33, Long right = -33, left == right :true
Long left = -32, Long right = -32, left == right :true
Long left = -31, Long right = -31, left == right :true
Long left = -30, Long right = -30, left == right :true
Long left = -29, Long right = -29, left == right :true
Long left = -28, Long right = -28, left == right :true
Long left = -27, Long right = -27, left == right :true
Long left = -26, Long right = -26, left == right :true
Long left = -25, Long right = -25, left == right :true
Long left = -24, Long right = -24, left == right :true
Long left = -23, Long right = -23, left == right :true
Long left = -22, Long right = -22, left == right :true
Long left = -21, Long right = -21, left == right :true
Long left = -20, Long right = -20, left == right :true
Long left = -19, Long right = -19, left == right :true
Long left = -18, Long right = -18, left == right :true
Long left = -17, Long right = -17, left == right :true
Long left = -16, Long right = -16, left == right :true
Long left = -15, Long right = -15, left == right :true
Long left = -14, Long right = -14, left == right :true
Long left = -13, Long right = -13, left == right :true
Long left = -12, Long right = -12, left == right :true
Long left = -11, Long right = -11, left == right :true
Long left = -10, Long right = -10, left == right :true
Long left = -9, Long right = -9, left == right :true
Long left = -8, Long right = -8, left == right :true
Long left = -7, Long right = -7, left == right :true
Long left = -6, Long right = -6, left == right :true
Long left = -5, Long right = -5, left == right :true
Long left = -4, Long right = -4, left == right :true
Long left = -3, Long right = -3, left == right :true
Long left = -2, Long right = -2, left == right :true
Long left = -1, Long right = -1, left == right :true
Long left = 0, Long right = 0, left == right :true
Long left = 1, Long right = 1, left == right :true
Long left = 2, Long right = 2, left == right :true
Long left = 3, Long right = 3, left == right :true
Long left = 4, Long right = 4, left == right :true
Long left = 5, Long right = 5, left == right :true
Long left = 6, Long right = 6, left == right :true
Long left = 7, Long right = 7, left == right :true
Long left = 8, Long right = 8, left == right :true
Long left = 9, Long right = 9, left == right :true
Long left = 10, Long right = 10, left == right :true
Long left = 11, Long right = 11, left == right :true
Long left = 12, Long right = 12, left == right :true
Long left = 13, Long right = 13, left == right :true
Long left = 14, Long right = 14, left == right :true
Long left = 15, Long right = 15, left == right :true
Long left = 16, Long right = 16, left == right :true
Long left = 17, Long right = 17, left == right :true
Long left = 18, Long right = 18, left == right :true
Long left = 19, Long right = 19, left == right :true
Long left = 20, Long right = 20, left == right :true
Long left = 21, Long right = 21, left == right :true
Long left = 22, Long right = 22, left == right :true
Long left = 23, Long right = 23, left == right :true
Long left = 24, Long right = 24, left == right :true
Long left = 25, Long right = 25, left == right :true
Long left = 26, Long right = 26, left == right :true
Long left = 27, Long right = 27, left == right :true
Long left = 28, Long right = 28, left == right :true
Long left = 29, Long right = 29, left == right :true
Long left = 30, Long right = 30, left == right :true
Long left = 31, Long right = 31, left == right :true
Long left = 32, Long right = 32, left == right :true
Long left = 33, Long right = 33, left == right :true
Long left = 34, Long right = 34, left == right :true
Long left = 35, Long right = 35, left == right :true
Long left = 36, Long right = 36, left == right :true
Long left = 37, Long right = 37, left == right :true
Long left = 38, Long right = 38, left == right :true
Long left = 39, Long right = 39, left == right :true
Long left = 40, Long right = 40, left == right :true
Long left = 41, Long right = 41, left == right :true
Long left = 42, Long right = 42, left == right :true
Long left = 43, Long right = 43, left == right :true
Long left = 44, Long right = 44, left == right :true
Long left = 45, Long right = 45, left == right :true
Long left = 46, Long right = 46, left == right :true
Long left = 47, Long right = 47, left == right :true
Long left = 48, Long right = 48, left == right :true
Long left = 49, Long right = 49, left == right :true
Long left = 50, Long right = 50, left == right :true
Long left = 51, Long right = 51, left == right :true
Long left = 52, Long right = 52, left == right :true
Long left = 53, Long right = 53, left == right :true
Long left = 54, Long right = 54, left == right :true
Long left = 55, Long right = 55, left == right :true
Long left = 56, Long right = 56, left == right :true
Long left = 57, Long right = 57, left == right :true
Long left = 58, Long right = 58, left == right :true
Long left = 59, Long right = 59, left == right :true
Long left = 60, Long right = 60, left == right :true
Long left = 61, Long right = 61, left == right :true
Long left = 62, Long right = 62, left == right :true
Long left = 63, Long right = 63, left == right :true
Long left = 64, Long right = 64, left == right :true
Long left = 65, Long right = 65, left == right :true
Long left = 66, Long right = 66, left == right :true
Long left = 67, Long right = 67, left == right :true
Long left = 68, Long right = 68, left == right :true
Long left = 69, Long right = 69, left == right :true
Long left = 70, Long right = 70, left == right :true
Long left = 71, Long right = 71, left == right :true
Long left = 72, Long right = 72, left == right :true
Long left = 73, Long right = 73, left == right :true
Long left = 74, Long right = 74, left == right :true
Long left = 75, Long right = 75, left == right :true
Long left = 76, Long right = 76, left == right :true
Long left = 77, Long right = 77, left == right :true
Long left = 78, Long right = 78, left == right :true
Long left = 79, Long right = 79, left == right :true
Long left = 80, Long right = 80, left == right :true
Long left = 81, Long right = 81, left == right :true
Long left = 82, Long right = 82, left == right :true
Long left = 83, Long right = 83, left == right :true
Long left = 84, Long right = 84, left == right :true
Long left = 85, Long right = 85, left == right :true
Long left = 86, Long right = 86, left == right :true
Long left = 87, Long right = 87, left == right :true
Long left = 88, Long right = 88, left == right :true
Long left = 89, Long right = 89, left == right :true
Long left = 90, Long right = 90, left == right :true
Long left = 91, Long right = 91, left == right :true
Long left = 92, Long right = 92, left == right :true
Long left = 93, Long right = 93, left == right :true
Long left = 94, Long right = 94, left == right :true
Long left = 95, Long right = 95, left == right :true
Long left = 96, Long right = 96, left == right :true
Long left = 97, Long right = 97, left == right :true
Long left = 98, Long right = 98, left == right :true
Long left = 99, Long right = 99, left == right :true
Long left = 100, Long right = 100, left == right :true
Long left = 101, Long right = 101, left == right :true
Long left = 102, Long right = 102, left == right :true
Long left = 103, Long right = 103, left == right :true
Long left = 104, Long right = 104, left == right :true
Long left = 105, Long right = 105, left == right :true
Long left = 106, Long right = 106, left == right :true
Long left = 107, Long right = 107, left == right :true
Long left = 108, Long right = 108, left == right :true
Long left = 109, Long right = 109, left == right :true
Long left = 110, Long right = 110, left == right :true
Long left = 111, Long right = 111, left == right :true
Long left = 112, Long right = 112, left == right :true
Long left = 113, Long right = 113, left == right :true
Long left = 114, Long right = 114, left == right :true
Long left = 115, Long right = 115, left == right :true
Long left = 116, Long right = 116, left == right :true
Long left = 117, Long right = 117, left == right :true
Long left = 118, Long right = 118, left == right :true
Long left = 119, Long right = 119, left == right :true
Long left = 120, Long right = 120, left == right :true
Long left = 121, Long right = 121, left == right :true
Long left = 122, Long right = 122, left == right :true
Long left = 123, Long right = 123, left == right :true
Long left = 124, Long right = 124, left == right :true
Long left = 125, Long right = 125, left == right :true
Long left = 126, Long right = 126, left == right :true
Long left = 127, Long right = 127, left == right :true
Long left = 128, Long right = 128, left == right :false
Long left = 129, Long right = 129, left == right :false
Long left = 130, Long right = 130, left == right :false
Long left = 131, Long right = 131, left == right :false
Long left = 132, Long right = 132, left == right :false
Long left = 133, Long right = 133, left == right :false
Long left = 134, Long right = 134, left == right :false
Long left = 135, Long right = 135, left == right :false
Long left = 136, Long right = 136, left == right :false
Long left = 137, Long right = 137, left == right :false
Long left = 138, Long right = 138, left == right :false
Long left = 139, Long right = 139, left == right :false
Long left = 140, Long right = 140, left == right :false
Long left = 141, Long right = 141, left == right :false
Long left = 142, Long right = 142, left == right :false
Long left = 143, Long right = 143, left == right :false
Long left = 144, Long right = 144, left == right :false
Long left = 145, Long right = 145, left == right :false
Long left = 146, Long right = 146, left == right :false
Long left = 147, Long right = 147, left == right :false
Long left = 148, Long right = 148, left == right :false
Long left = 149, Long right = 149, left == right :false
Long left = 150, Long right = 150, left == right :false
Long left = 151, Long right = 151, left == right :false
Long left = 152, Long right = 152, left == right :false
Long left = 153, Long right = 153, left == right :false
Long left = 154, Long right = 154, left == right :false
Long left = 155, Long right = 155, left == right :false
Long left = 156, Long right = 156, left == right :false
Long left = 157, Long right = 157, left == right :false
Long left = 158, Long right = 158, left == right :false
Long left = 159, Long right = 159, left == right :false
Long left = 160, Long right = 160, left == right :false
Long left = 161, Long right = 161, left == right :false
Long left = 162, Long right = 162, left == right :false
Long left = 163, Long right = 163, left == right :false
Long left = 164, Long right = 164, left == right :false
Long left = 165, Long right = 165, left == right :false
Long left = 166, Long right = 166, left == right :false
Long left = 167, Long right = 167, left == right :false
Long left = 168, Long right = 168, left == right :false
Long left = 169, Long right = 169, left == right :false
Long left = 170, Long right = 170, left == right :false
Long left = 171, Long right = 171, left == right :false
Long left = 172, Long right = 172, left == right :false
Long left = 173, Long right = 173, left == right :false
Long left = 174, Long right = 174, left == right :false
Long left = 175, Long right = 175, left == right :false
Long left = 176, Long right = 176, left == right :false
Long left = 177, Long right = 177, left == right :false
Long left = 178, Long right = 178, left == right :false
Long left = 179, Long right = 179, left == right :false
Long left = 180, Long right = 180, left == right :false
Long left = 181, Long right = 181, left == right :false
Long left = 182, Long right = 182, left == right :false
Long left = 183, Long right = 183, left == right :false
Long left = 184, Long right = 184, left == right :false
Long left = 185, Long right = 185, left == right :false
Long left = 186, Long right = 186, left == right :false
Long left = 187, Long right = 187, left == right :false
Long left = 188, Long right = 188, left == right :false
Long left = 189, Long right = 189, left == right :false
Long left = 190, Long right = 190, left == right :false
Long left = 191, Long right = 191, left == right :false
Long left = 192, Long right = 192, left == right :false
Long left = 193, Long right = 193, left == right :false
Long left = 194, Long right = 194, left == right :false
Long left = 195, Long right = 195, left == right :false
Long left = 196, Long right = 196, left == right :false
Long left = 197, Long right = 197, left == right :false
Long left = 198, Long right = 198, left == right :false
Long left = 199, Long right = 199, left == right :false

通过这个超长的输出结果,你会发现,存在一个区间[-128, 127],在这个区间里通过 == 这种方式返回的是true ,其他返回的都是false。

2. 总结

其实,通过源码分析:

public static Long valueOf(long l) {
    final int offset = 128;
    if (l >= -128 && l <= 127) { // will cache
      return LongCache.cache[(int)l + offset];
    }
    return new Long(l);
  }

在128~127这些数的对象在内存中被缓存起来,在内存中提供重用,这些数获取的时候就会自动获取缓存中的数,而不是new出来的对象。

所以,这与自动打包拆包无关。

再所以…,判读Long对象是否相等的时候建议不要采用 == 的形式,程序中无法绝对的判断参数值是否会在这个区间内,如果不在,那就出现让你摸不着头脑的问题。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

(0)

相关推荐

  • java byte数组与int,long,short,byte的转换实现方法

    实例如下: public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return convert result */ public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } /** * 将一个单字节的Byte转换成十六进制的数 * * @param b * byte * @return conv

  • Java编程long数据类型的使用问题

    在Java编程中经常遇到一些整数类型的使用问题,下面我们来看看长整形数据使用问题. 今天在写一个java线程的时候,想让线程休眠720小时候继续执行,代码如下: long runSperiod = 720 * 60 * 60 * 1000; Thread.sleep(runSperiod) 启动线程测试的时候,发现线程并没有按照预想的720小时候后执行,而是不停在执行,最后跟踪代码发现 runSperiod 的值时负数.仔细研究后发现,java在处理几个数相乘时,如果几个数都是 int 类型,那

  • 谈谈Java中整数类型(short int long)的存储方式

    在java中的整数类型有四种,分别是 byte  short int long 其中byte只有一个字节 0或1,在此不详细讲解. 其他的三种类型如下: 1. 基本类型:short 二进制位数:16 包装类:java.lang.Short 最小值:Short.MIN_VALUE=-32768 (-2的15此方) 最大值:Short.MAX_VALUE=32767 (2的15次方-1) 2. 基本类型:int 二进制位数:32 包装类:java.lang.Integer 最小值:Integer.M

  • java中long数据类型转换为int类型

    由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参考: 一.强制类型转换 long ll = 300000; int ii = (int)ll; 二.调用intValue()方法 long ll = 300000; int ii= new Long(ll).intValue(); 三.先把long转换成字符串String,然后在转行成Integer long ll = 300000; i

  • java int转byte和long转byte的方法

    在网络编程中,出于节约带宽或者编码的需要,通常需要以原生方式处理long和int,而不是转换为string. public class ByteOrderUtils { public static byte[] int2byte(int res) { byte[] targets = new byte[4]; targets[3] = (byte) (res & 0xff);// 最低位 targets[2] = (byte) ((res >> 8) & 0xff);// 次低

  • Java中byte、byte数组与int、long的转换详解

    一.Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; } public static int byteToInt(byte b) { //Java 总是把 byte 当做有符处理:我们可以通过将其和 0xFF 进行二进制与得到它的无符值 return b & 0xFF; } 测试代码: //测试 int 转 byte int int0 = 234

  • 细数java中Long与Integer比较容易犯的错误总结

    今天使用findbugs扫描项目后发现很多高危漏洞,其中非常常见的一个是比较两个Long或Integer时直接使用的==来比较. 其实这样是错误的. 因为Long与Ineger都是包装类型,是对象. 而不是普通类型long与int , 所以它们在比较时必须都应该用equals,或者先使用longValue()或intValue()方法来得到他们的基本类型的值然后使用==比较也是可以的. 但是有一种特殊情况, 其实Long与Integer都将 -128~127 这些对象缓存了. 可以看看Long类

  • Java中long类型与Long类型的区别和大小比较详解

    前言 最近在学习java,接触到了long类型,发现网上这方面的资料较乱,所以决定总结一下,在Java中我们经常要做一些判断,而对于判断的话,用的最多的便是">"."=="."<"的比较,这里我们进行一个Long类型数据和long类型数据的比较大小的讲解.话不多说了,来一起看看详细的介绍吧. Java中Long和long的区别 Java的数据类型分为两种: 1.基本类型:byte(8),short(16),int(32),long(6

  • java Long==Long有趣的现象详解

    前言 long数据类型是64位有符号的Java原始数据类型.当对整数的计算结果可能超出int数据类型的范围时使用. long数据类型范围是-9,223,372,036,854,775,808至9,223,372,036,854,775,807(-2^63至2^63-1). long数据类型范围内的所有整数称为long类型的整数字面量.long类型的整数常数总是以大写L或小写l结尾. 1. 描述 首先,这里应该声明一下,Long等对象类型判断是否相等,采用Long == Long这种形式是错误的.

  • java HashMap,TreeMap与LinkedHashMap的详解

     java HashMap,TreeMap与LinkedHashMap的详解 今天上午面试的时候 问到了Java,Map相关的事情,我记错了HashMap和TreeMap相关的内容,回来赶紧尝试了几个demo理解下 package Map; import java.util.*; public class HashMaps { public static void main(String[] args) { Map map = new HashMap(); map.put("a", &

  • java 中死锁问题的实例详解

    java 中死锁问题的实例详解 先看代码在做解释 public class DeadLock implements Runnable{ String a; String b; boolean flag; public DeadLock(String a,String b,boolean flag){ this.a=a; this.b=b; this.flag=flag; } public void run(){ if(flag){ // while(true){ synchronized(a){

  • Java并发编程预防死锁过程详解

    这篇文章主要介绍了Java并发编程预防死锁过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在java并发编程领域已经有技术大咖总结出了发生死锁的条件,只有四个条件都发生时才会出现死锁: 1.互斥,共享资源X和Y只能被一个线程占用 2.占有且等待,线程T1已经取得共享资源X,在等待共享资源Y的时候,不释放共享资源X 3.不可抢占,其他线程不能强行抢占线程T1占有的资源 4.循环等待,线程T1等待线程T2占有的资源,线程T2等待线程T1占有

  • Java CAS底层实现原理实例详解

    这篇文章主要介绍了Java CAS底层实现原理实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.CAS(compareAndSwap)的概念 CAS,全称Compare And Swap(比较与交换),解决多线程并行情况下使用锁造成性能损耗的一种机制. CAS(V, A, B),V为内存地址.A为预期原值,B为新值.如果内存地址的值与预期原值相匹配,那么将该位置值更新为新值.否则,说明已经被其他线程更新,处理器不做任何操作:无论哪种情

  • Java设计模式之23种设计模式详解

    一.什么是设计模式 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样.项目中合理的运用设计模式可以完美的解决很多问题,每种模式在现在中都有相应的原理来与之对应,每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的核心解决方案,这也是

  • Java多线程之哲学家就餐问题详解

    一.题目 教材提供一个哲学家就餐问题的解决方案的框架.本问题要求通过pthreads 互斥锁来实现这个解决方案. 哲学家 首先创建 5 个哲学家,每个用数字 0~4 来标识.每个哲学家作为一个单独的 线程运行. 可使用 Pthreads 创建线程.哲学家在思考和吃饭之间交替.为了模拟这两种活动,请让线程休眠 1 到 3 秒钟.当哲学家想要吃饭时,他调用函数: pickup_forks(int philosopher _number) 其中,philosopher _number 为想吃饭哲学家的

  • java之assert关键字用法案例详解

    Java2在1.4中新增了一个关键字:assert.在程序开发过程中使用它创建一个断言(assertion).,它的语法形式有如下所示的两种形式: 1.assert condition; 这里condition是一个必须为真(true)的表达式.如果表达式的结果为true,那么断言为真,并且无任何行动 如果表达式为false,则断言失败,则会抛出一个AssertionError对象.这个AssertionError继承于Error对象, 而Error继承于Throwable,Error是和Exc

  • Java多线程之搞定最后一公里详解

    目录 绪论 一:线程安全问题 1.1 提出问题 1.2 不安全的原因 1.2.1 原子性 1.2.2 代码"优化" 二:如何解决线程不安全的问题 2.1 通过synchronized关键字 2.2 volatile 三:wait和notify关键字 3.1 wait方法 3.2 notify方法 3.3 wait和sleep对比(面试常考) 四:多线程案例 4.1 饿汉模式单线程 4.2 懒汉模式单线程 4.3 懒汉模式多线程低性能版 4.4懒汉模式-多线程版-二次判断-性能高 总结

  • Java异步编程工具Twitter Future详解

    目录 异步编程(Twitter Future) 为啥要异步 基本用法 1.封装计算逻辑,异步返回. 2.异步计算结果串联异步处理 3.并行多个异步任务,统一等待结果 4.异步错误处理 Twitter包装 pom依赖 1.封装计算逻辑,异步返回 2.异步计算结果串联异步处理 3.并行多个异步任务 4.错误处理 其他用法 其他工具 异步编程(Twitter Future) 为啥要异步 异步编程有点难以理解,这东西感觉不符合常理,因为我们思考都是按照串行的逻辑,事都是一件一件办.但在异步计算的情况下,

随机推荐