前言
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)阿瓦提,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
long數(shù)據(jù)類型是64位有符號(hào)的Java原始數(shù)據(jù)類型。當(dāng)對(duì)整數(shù)的計(jì)算結(jié)果可能超出int數(shù)據(jù)類型的范圍時(shí)使用。
long數(shù)據(jù)類型范圍是-9,223,372,036,854,775,808至9,223,372,036,854,775,807(-2^63至2^63-1)。
long數(shù)據(jù)類型范圍內(nèi)的所有整數(shù)稱為long類型的整數(shù)字面量。long類型的整數(shù)常數(shù)總是以大寫(xiě)L或小寫(xiě)l結(jié)尾。
1. 描述
首先,這里應(yīng)該聲明一下,Long等對(duì)象類型判斷是否相等,采用Long == Long這種形式是錯(cuò)誤的。
可以采用以下方式:
Long left = 100L; Long right = 100L; // 方法1: System.out.println(left.doubleValue() == right.doubleValue()); // 方法2: System.out.println(left.compareTo(right));
輸出結(jié)果:
true
0
1.1 實(shí)驗(yàn)1
如果你親自操刀,動(dòng)手做了以下實(shí)驗(yàn),也許你會(huì)突然得到一個(gè)讓你困惑的結(jié)果:
Long left = 100L; Long right = 100L; System.out.println(left == right);
輸出結(jié)果:
true
沒(méi)錯(cuò),采用 == 竟然也能得出true,也許這時(shí)候你可能會(huì)誤以為jvm在自動(dòng)拆箱(我以前也是這樣以為,有點(diǎn)小白!?。。?。
1.2 實(shí)驗(yàn)2
那么請(qǐng)你再做下面這個(gè)實(shí)驗(yàn):
Long left = 1000L; Long right = 1000L; // 方法1: System.out.println(left.doubleValue() == right.doubleValue()); // 方法2: System.out.println(left.compareTo(right)); // 錯(cuò)誤的方法: System.out.println(left == right);
輸出結(jié)果
true
0
false
等會(huì),什么情況,怎么又為false?
1.3 實(shí)驗(yàn)3
不急,再做一個(gè)實(shí)驗(yàn)。
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)); }
輸出結(jié)果
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
通過(guò)這個(gè)超長(zhǎng)的輸出結(jié)果,你會(huì)發(fā)現(xiàn),存在一個(gè)區(qū)間[-128, 127],在這個(gè)區(qū)間里通過(guò) == 這種方式返回的是true ,其他返回的都是false。
2. 總結(jié)
其實(shí),通過(guò)源碼分析:
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這些數(shù)的對(duì)象在內(nèi)存中被緩存起來(lái),在內(nèi)存中提供重用,這些數(shù)獲取的時(shí)候就會(huì)自動(dòng)獲取緩存中的數(shù),而不是new出來(lái)的對(duì)象。
所以,這與自動(dòng)打包拆包無(wú)關(guān)。
再所以…,判讀Long對(duì)象是否相等的時(shí)候建議不要采用 == 的形式,程序中無(wú)法絕對(duì)的判斷參數(shù)值是否會(huì)在這個(gè)區(qū)間內(nèi),如果不在,那就出現(xiàn)讓你摸不著頭腦的問(wèn)題。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
分享文章:javaLong==Long有趣的現(xiàn)象詳解
網(wǎng)頁(yè)網(wǎng)址:http://www.rwnh.cn/article40/jipgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、企業(yè)建站、網(wǎng)站制作、網(wǎng)站營(yíng)銷、靜態(tài)網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)