技术文章
当前位置:首页 > Java技术文章 > Java的long和bigint长度比较

Java的long和bigint长度对比详解

  • 发布时间:
  • 作者:码农之家原创
  • 点击:168

这篇文章主要知识点是关于Java、long、bigint、长度对比、java Long==Long有趣的现象详解 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下java相关的电子书

学习JavaScript数据结构与算法
  • 类型:JavaScript大小:13.6 MB格式:PDF出版:人民邮电出版社作者:洛伊安妮
立即下载

更多Java相关的学习资源可以参阅 Java电子书程序设计电子书 等栏目。

Java的long和bigint长度比较

Java 中Long的值大小比较

java.util.Lang 对象吧,而不是基本类型 long  (注意大小写)

Java中如果使用 == 双等于比较对象,等于比较的是两个对象的内存地址,也就是比较两个对象是否是同一个对象

如果比较两个Long对象值是否相等,则不可以使用双等号进行比较,可以采用如下方式:

1. 使用 equals 方法

Long a = new Long(3);
Long b = new Long(3);
System.out.println(a.equals(b));

2. 使用 Long 类型中的 longValue() 方法进行比较,如

Long a = new Long(3);
Long b = new Long(3);
System.out.println(a.longValue()==b.longValue());

因为Long为long的封装对象类型,因此比较大小的时候不能用'=='

用java数组实现BigInt超大整数设计

package com.test;  
  
import org.apache.commons.lang.StringUtils;  
  
/** 
 * @author jsczxy2 
 *  
 */  
public class BigInt {  
      
    public static void main(String[] args) {  
        BigInt a = new BigInt("367892732043217489143432876442367892732043217489143432876442367892732043217489143432876442367892732043217489143432876442");  
        BigInt b = new BigInt("3678927329999999999999994328736789273299999999999999943287367892732043217489143432876442367892732043217489143432876442");  
        System.out.println(a.toString());  
        System.out.println(b.toString());  
        System.out.println(a.add(b));  
    }  
  
    private int[] arrayint = new int[100];  
  
    public BigInt(String num) {  
        //分解数字到int数组中  
        splitNumToArray(num);  
    }  
  
    public void splitNumToArray(String num) {  
        int j = 0;  
        StringBuffer sb = new StringBuffer();  
        //数字全部翻转后分组截取后再翻转回来加入int数组,这里控制数组中每一个int元素恒定为8位不超过int最大长度  
        num = new StringBuffer(num).reverse().toString();  
        for (int i = 0; i <num.length(); i++) {  
            if (i % 8 == 0) {  
                if (sb != null && !sb.toString().equals("")){  
                    arrayint[j] = Integer.valueOf(sb.reverse().toString());  
                    j++;  
                    sb = new StringBuffer();  
                }  
            }  
                sb.append(num.charAt(i));  
              
        }  
        if (sb != null) {  
            arrayint[j] = Integer.valueOf(sb.reverse().toString());  
        }  
    }  
  
    //数组从后开始打印数字,不满8位补齐8位数字用0进行左填充  
    public String printArray(int[] array) {  
        StringBuffer sb = new StringBuffer();  
        boolean isNotFirstInt = false;  
        for (int i = array.length-1; i >=0 ; i--) {  
            if (array[i] != 0) {  
                System.out.println(i+":"+array[i]);  
                if(isNotFirstInt && String.valueOf(array[i]).length()<8){  
                    sb.append(StringUtils.leftPad(String.valueOf(array[i]), 8,"0"));  
                }else{  
                    sb.append(array[i]);  
                    if(!isNotFirstInt)  
                        isNotFirstInt = true;  
                }  
                  
            }  
        }  
        return sb.toString();  
    }  
  
    //BigInt数字进行加法运算  
    public String add(BigInt bigInt) {  
        int[] a = this.arrayint;  
        int[] b = bigInt.arrayint;  
        int[] result = new int[100];  
        //根据各种情况进行结果赋值  
        for(int i=0;i<a.length;i++){  
            if(a[i]==0&&b[i]!=0){  
                result[i]=b[i];  
            }else if(a[i]!=0&&b[i]==0){  
                result[i]=a[i];  
            }else if(a[i]!=0&&b[i]!=0){  
                result[i]=a[i]+b[i];  
            }else{  
                result[i]=0;  
            }  
        }  
        //处理结果数组中超过8位的int元素的进位,该int元素截掉1位后再把其后一个元素值加一  
        for(int i=0;i<result.length;i++){  
            if(String.valueOf(result[i]).length()>8){  
                result[i] = Integer.valueOf(String.valueOf(result[i]).substring(1));  
                result[i+1] = result[i+1] + 1;  
            }  
        }  
        return printArray(result);  
    }  
  
    //打印BigInt数字  
    @Override  
    public String toString() {  
        return printArray(arrayint);  
    }  
  
}  

bigint

从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字)。存储大小为 8 个字节。

int

从 -2^31 (-2,147,483,648) 到 2^31 - 1 (2,147,483,647) 的整型数据(所有数字)。存储大小为 4 个字节。int 的 SQL-92 同义字为 integer。

smallint

从 -2^15 (-32,768) 到 2^15 - 1 (32,767) 的整型数据。存储大小为 2 个字节。

tinyint

从 0 到 255 的整型数据。存储大小为 1 字节。

说明:

int(M) 在 integer 数据类型中,M 表示最大显示宽度,只用于在显示,并不影响存储。在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系。和数字位数也无关系 int(3)、int(4)、int(8) 在磁盘上都是占用 4 btyes 的存储空间。

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对象是否相等的时候建议不要采用 == 的形式,程序中无法绝对的判断参数值是否会在这个区间内,如果不在,那就出现让你摸不着头脑的问题。

总结

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

以上就是本次给大家分享的关于Java的全部知识点内容总结,大家还可以在下方相关文章里找到WebUploader实现分片断点上传、 swing组件JScrollPane滚动条实、 Java IO流之字符缓冲流的实、 等java文章进一步学习,感谢大家的阅读和支持。

上一篇:java实现socket连接方法封装详解

下一篇:java获取rabbitmq消息总数是多少

展开 +

收起 -

相关电子书
学习笔记
网友NO.847009

java中申请不定长度数组ArrayList的方法

如下所示: import java.util.ArrayList;//java中申请不定长度数组public class Test01 { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList list=new ArrayList(); list.add("123"); list.add("5"); list.add("5"); //遍历输出list数组 for(int i=0;ilist.size();i++){ //get(index)方法,返回此列表中指定位置上的元素。 System.out.print(list.get(i).toString()+" "); } System.out.println("\n"+"遍历输出使用set方法替换后的数组list"); /*set(index,element)方法,用指定的元素替代此列表中指定位置上的元素。 * index - 要替代的元素的索引 element - 存储在指定位置上的元素 * */ list.set(2,"nihao"); //遍历输出使用set方法替换后的数组list for(int i=0;ilist.size();i++){ //get(index)方法,返回此列表中指定位置上的元素。 System.out.print(list.get(i)+" "); //list.size()取得list数组的长度 System.out.println("\n"+"list数组的长度"+list.size()); } }} 输出结果……

网友NO.248419

Java获得一个数组的指定长度排列组合算法示例

本文实例讲述了Java获得一个数组的指定长度排列组合算法。分享给大家供大家参考,具体如下: package demo;import java.util.Stack;/** * JAVA获得一个数组的指定长度的排列组合。br * * @author JAVA世纪网(java2000.net, laozizhu.com) */public class TestSequenceAll { public static void main(String[] args) { TestSequenceAll t = new TestSequenceAll(); Object[] arr = { 1, 2, 3 }; // 循环获得每个长度的排列组合 for (int num = 1; num = arr.length; num++) { t.getSequence(arr, 0, num); } } // 存储结果的堆栈 private StackObject stack = new StackObject(); /** * 获得指定数组从指定开始的指定数量的数据组合br * * @param arr 指定的数组 * @param begin 开始位置 * @param num 获得的数量 */ public void getSequence(Object[] arr, int begin, int num) { if (num == 0) { System.out.println(stack); // 找到一个结果 } else { // 循环每个可用的元素 for (int i = begin; i arr.length; i++) { // 当前位置……

网友NO.419076

javascript计算对象长度的方法

计算对象的长度,即获取对象属性的个数,具体如下 方法一: 通过for in 遍历对象,并通过hasOwnProperty判断是否是对象自身可枚举的属性 var obj = {"c1":1,"c2":2};function countProperties(obj){ for(var property in obj){ if(Object.prototype.hasOwnProperty.call(obj,property){ count++; }) } return count;}var len = obj.length;console.log(len);//结果为2 方法二: 通过Object.keys()获取对象可枚举属性所组成的数组,并通过length获取对象长度 var obj = {"c1":1,"c2":2};var arr = Object.keys(obj);var len = arr.length;console.log(len);//结果为2 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。 ……

<
1
>

Copyright 2018-2020 xz577.com 码农之家

电子书资源由网友、会员提供上传,本站记录提供者的基本信息及资源来路

鸣谢: “ 码小辫 ” 公众号提供回调API服务、“ 脚本CDN ”提供网站加速(本站寻求更多赞助支持)

版权投诉 / 书籍推广 / 赞助:520161757@qq.com

上传资源(网友、会员均可提供)

查看最新会员资料及资源信息