Integer详解

Integer

简介
Integer 类在对象中包装了一个基本类型 int 的值。Integer 类对象包含一个 int 类型的字段。此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
数据类型占用字节数位数取值范围
Byte18-2的7次方到2的7次方-1
Short216-2的15次方到2的15次方-1
Int432-2的31次方到2的31次方-1
char216
Float4323.402823e+38 ~ 1.401298e-45
Long864-2的63次方到2的63次方-1
Double8641.797693e+308~ 4.9000000e-324
boolean1(前7位是0,即1/8bit)80/1
方法
static int	bitCount(int i)
// 以整数值的二进制补码表示形式返回one-bits的数量计数。有时将此功能称为人口计数。
byte	byteValue()
//在缩小原始转换后,将给定的Integer转换为字节,并返回它
static int	compare(int x, int y)
// 作为参数给出的两个整数值(x,y),如果(x == y)则返回零,如果(x <y)则返回小于零,如果(x> y),则返回大于零的值。
int	compareTo(Integer anotherInteger)
// 数字比较两个 Integer对象。
static int	compareUnsigned(int x, int y)
// 比较两个 int值,以数值方式将值视为无符号。
static Integer	decode(String nm)
// 将 String解码成 Integer 。
static int	divideUnsigned(int dividend, int divisor)
// 返回将第一个参数除以秒的无符号商,其中每个参数和结果被解释为无符号值。
double	doubleValue()
// 在类型转换后将调用对象指定的值返回为double
boolean	equals(Object obj)
// 将此对象与指定的对象进行比较。
float	floatValue()
// 在类型转换后将调用对象指定的值返回为float
static Integer	getInteger(String nm)
// 确定具有指定名称的系统属性的整数值。
static Integer	getInteger(String nm, int val)
// 确定具有指定名称的系统属性的整数值。
static Integer	getInteger(String nm, Integer val)
// 返回具有指定名称的系统属性的整数值。
int	hashCode()
// 返回这个 Integer的哈希码。
static int	hashCode(int value)
// 返回值为int的哈希码; 兼容Integer.hashCode() 。
static int	highestOneBit(int i)
// 返回一个 int值与至多一个单个1位,在最高阶(“最左侧”)的位置在指定的一个位 int值。
int	intValue()
// 将 Integer的值作为 int 。
long	longValue()
// 在类型转换后将调用对象指定的值返回为long
static int	lowestOneBit(int i)
// 在指定的 int值中,以最低位(最右边)为1位返回一个最多为单个1位的 int值。
static int	max(int a, int b)
// 返回两个 int的较大值,就像调用 Math.max一样 。
static int	min(int a, int b)
// 返回两个 int的较小值,就像调用 Math.min一样 。
static int	numberOfLeadingZeros(int i)
// 返回的最高阶的(“最左边的”)中所指定的二进制补码表示的一个位前述零个比特的数量 int值。
static int	numberOfTrailingZeros(int i)
// 返回零位以下最低阶(“最右边的”)的数量在指定的二进制补码表示的一个位 int值。
static int	parseInt(String s)
// 将字符串参数解析为带符号的十进制整数。
static int	parseInt(String s, int radix)
// 将字符串参数解析为第二个参数指定的基数中的有符号整数。
static int	parseUnsignedInt(String s)
// 将字符串参数解析为无符号十进制整数。
static int	parseUnsignedInt(String s, int radix)
// 将字符串参数解析为第二个参数指定的基数中的无符号整数。
static int	remainderUnsigned(int dividend, int divisor)
// 返回无符号余数,将第一个参数除以秒,其中每个参数和结果被解释为无符号值。
static int	reverse(int i)
// 返回由指定的二进制补码表示反转位的顺序而获得的值 int值。
static int	reverseBytes(int i)
// 返回反转指定的二进制补码表示的字节顺序而获得的值 int值。
static int	rotateLeft(int i, int distance)
// 返回通过旋转指定的二的补码的二进制表示而得到的值 int由位指定数目的左值。
static int	rotateRight(int i, int distance)
// 返回通过旋转指定的二的补码的二进制表示而得到的值 int右移位的指定数值。
short	shortValue()
// 返回此值 Integer为 short的基本收缩转换后。
static int	signum(int i)
// 返回指定的 int值的 int函数。
static int	sum(int a, int b)
// 根据+运算符将两个整数相加。
static String	toBinaryString(int i)
// 在基数2中返回整数参数的字符串表示形式为无符号整数。
static String	toHexString(int i)
// 返回整数参数的字符串表示形式,作为16位中的无符号整数。
static String	toOctalString(int i)
// 在基数8中返回整数参数的字符串表示形式为无符号整数。
// String	toString()
返回 String表示此对象 Integer的价值。
// static String	toString(int i)
返回一个 String指定整数的 String对象。
// static String	toString(int i, int radix)
返回由第二个参数指定的基数中的第一个参数的字符串表示形式。
static long	toUnsignedLong(int x)
// 参数给转换 long由无符号转换。
static String	toUnsignedString(int i)
// 将参数的字符串表示形式返回为无符号十进制值。
static String	toUnsignedString(int i, int radix)
// 以第二个参数指定的基数中的无符号整数值返回第一个参数的字符串表示形式。
static Integer	valueOf(int i)
// 返回一个 Integer指定的 int值的 Integer实例。
static Integer	valueOf(String s)
// 返回一个 Integer对象,保存指定的值为 String 。
static Integer	valueOf(String s, int radix)
// 返回一个 Integer对象,保存从指定的String中 String的值,当用第二个参数给出的基数进行解析时。

缓存(可配置)
-128~127
-XX:AutoBoxCacheMax=1000
其他概念
无符号数据类型
Java不支持无符号数据类型。
byte,short,int和long都是有符号数据类型。
对于有符号数据类型,值范围的一半存储正数,一半用于负数,因为一个位用于存储值的符号。
例如,一个字节需要8位;其范围是-128到127.如果您只在一个字节中存储正数,则其范围将为0到255。
Java在包装器类中有一些静态方法,以支持处理带符号值中的位的操作,就像它们是无符号整数一样。
字节类包含两个静态方法:
int  toUnsignedInt(byte x)
long  toUnsignedLong(byte x)
这些方法将指定的字节参数转换为int和long,就像该字节存储无符号值一样。
如果指定的字节为零或正,则转换的int和long值将相同。
如果参数是负数,则转换的数字将是28 + x。
例如,对于-10的输入,返回的值将是28 +(-10),这是246。
负数以2的补码形式存储。值-10将被存储为11110110.最高有效位1表示它是一个负数。
前7位(1110110)的2的补码将是001010,十进制为10。
如果将实际位11110110视为无符号整数,其值为246(128 + 64 + 32 + 16 + 0 + 4 + 2 + 0)。

Integer类包含以下静态方法以支持无符号运算和转换:

int compareUnsigned(int x, int y)
int  divideUnsigned(int dividend, int divisor)
int  parseUnsignedInt(String s)
int  parseUnsignedInt(String s, int radix)
int  remainderUnsigned(int dividend,  int divisor)
long  toUnsignedLong(int x)
String toUnsignedString(int i)
String toUnsignedString(int i, int radix)

以下代码显示了对两个int变量的除法运算,如同它们的位表示无符号值一样:

public class Main {
  public static void main(String[] args) {
    // Two negative integer values
    int x = -1;
    int y = -2;

    // Performs signed division
    System.out.println("Signed x = " + x);
    System.out.println("Signed y = " + y);
    System.out.println("Signed x/y  = " + (x / y));

    // Performs unsigned division by treating x and y holding unsigned values
    long ux = Integer.toUnsignedLong(x);
    long uy = Integer.toUnsignedLong(y);
    int uQuotient = Integer.divideUnsigned(x, y);
    System.out.println("Unsigned x  = " + ux);
    System.out.println("Unsigned y  = " + uy);
    System.out.println("Unsigned x/y  = " + uQuotient);
  }
}

上面的代码生成以下结果。

img

进制
二进制变量的声明以0b为前缀;
八进制变量的声明以0为前缀;
十六进制变量的声明以0x为前缀。
二进制、八进制、十六进制数值在运用时候自动转为对应的十进制的值
ASCII
目前计算机中用得最广泛的 字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Standard Code for Information Interchange,美国标准信息交换码),它已被国际标准化组织(ISO)定为国际标准,称为ISO 646标准。适用于所有拉丁文字字母,ASCII码有7位码和8位码两种形式。
位运算
Java 定义的位运算直接对整数类型的位进行操作,这些整数类型包括 long,int,short,char 和 byte。
位运算符主要用来对操作数二进制的位进行运算。按位运算表示按每个二进制位(bit)进行计算,其操作数和运算结果都是整型值。
原码
原码比较好理解,也就是该数字不进行其他操作时数字最原始的二进制表示,在Java中我们有熟悉的byte,short,int,long的整数型基本数据类型以及float,double的浮点型基本数据类型。
在Java中,对于整数而言,其原码格式为最高位为符号位,该位上1表示负数而0表示正数,剩余位数为该数字的二进制表示。在这里我们简单用int型的整数5及-5看下例子:
5的原码:00000000 00000000 00000000 00000101
-5的原码:10000000 00000000 00000000 00000101
因此,对于原码来说,绝对值相等的正数和负数只有符号位不同。
反码
正数的原码即为其反码,对于负数而言只需要将原码除符号位以外的位数取反(如果为0改为1,如果为1改为0),便得到了当前负数的反码。
我们依旧拿数字5举例:
5的反码:00000000 00000000 00000000 00000101
-5的反码:11111111 11111111 11111111 11111010
补码
明白了反码的规则,补码的表示方法就很简单了,正数的原码即为其补码,负数的补码是上面取得的反码加1。
5的补码:00000000 00000000 00000000 00000101
-5的补码:11111111 11111111 11111111 11111011

原文链接:https://blog.csdn.net/weixin_37870009/article/details/79775926

BigInteger

BigInteger不是基本数据类型之一,它其实更像String,是Java里的一个类,然而它的初始化方式却没有String那么方便可以直接赋值,而是跟其他自定义的类一样,要调用它的构造器进行初始化。这个类的取值范围原则上是没有上限的,取决于你的计算机的内存

BigInteger abs()  返回大整数的绝对值
BigInteger add(BigInteger val) 返回两个大整数的和
BigInteger and(BigInteger val)  返回两个大整数的按位与的结果
BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
BigInteger divide(BigInteger val)  返回两个大整数的商
double doubleValue()   返回大整数的double类型的值
float floatValue()   返回大整数的float类型的值
BigInteger gcd(BigInteger val)  返回大整数的最大公约数
int intValue() 返回大整数的整型值
long longValue() 返回大整数的long型值
BigInteger max(BigInteger val) 返回两个大整数的最大者
BigInteger min(BigInteger val) 返回两个大整数的最小者
BigInteger mod(BigInteger val) 用当前大整数对val求模
BigInteger multiply(BigInteger val) 返回两个大整数的积
BigInteger negate() 返回当前大整数的相反数
BigInteger not() 返回当前大整数的非
BigInteger or(BigInteger val) 返回两个大整数的按位或
BigInteger pow(int exponent) 返回当前大整数的exponent次方
BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
BigInteger leftShift(int n) 将当前大整数左移n位后返回
BigInteger rightShift(int n) 将当前大整数右移n位后返回
BigInteger subtract(BigInteger val)返回两个大整数相减的结果
byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
String toString() 将当前大整数转换成十进制的字符串形式
BigInteger xor(BigInteger val) 返回两个大整数的异或

package com.hongshi.demo;


import java.lang.reflect.Field;
import java.math.BigInteger;

/**
 * Integer演示
 *
 * @author 白白白
 * @date 2021/10/31
 */
public class IntegerDemo {

    public static void main(String[] args) throws InterruptedException, NoSuchFieldException, IllegalAccessException {
//        buildInteger();
//        parseInt();
//        valueOf();
        getInteger();
//        compare();
//        basic();
//        toStrints();
//        parseUnsignedInt();
//        decode();
//        other();

//        printResult0();
//        printResult1();
//        printResult2();
//        printResult3();
//        printResult4();
//        printResult5();
        printResult6();
    }


    public static void buildInteger() {
        Integer int_1 = new Integer(10);
        Integer int_2 = new Integer("10");
        Integer int_3 = new Integer('a');
//        Integer int_4 = new Integer("null");
        Integer int_5 = 10;
        System.out.println(int_1);
        System.out.println(int_2);
        System.out.println(int_3);
//        System.out.println(int_4);
        System.out.println(int_5);
    }

    public static void parseInt() {
        // 输出十进制数值
        // 输入值最小2进制,最大32进制
        int int_1 = Integer.parseInt("100");
        System.out.println("Integer.parseInt(\"100\");  ==> " + int_1);

        int int_2 = Integer.parseInt("Kona", 26);
        System.out.println("Integer.parseInt(\"Kona\", 26);  ==> " + int_2);

        int int_3 = Integer.parseInt("111", 2);
        System.out.println("Integer.parseInt(\"111\", 2);  ==> " + int_3);
    }

    public static void valueOf() {
        // 会优先从缓存中获取
        Integer int_2 = Integer.valueOf("Kona", 26);
        System.out.println("Integer.valueOf(\"Kona\", 26);  ==> " + int_2);
//        Integer int_1 = Integer.valueOf("Kona", 2);
//        System.out.println("Integer.valueOf(\"Kona\", 2);  ==> " + int_1);
        Integer int_4 = Integer.valueOf('a');
        System.out.println("Integer.valueOf('a');  ==> " + int_4);
        Integer int_3 = Integer.valueOf("2", 2);
        System.out.println("Integer.valueOf(\"2\", 2)  =>" + int_3);

    }

    public static void getInteger() {
        //根据指定的名称得到系统属性的整数值。第一个参数将被认为是系统属性的名称
//        System.setProperty("Kona", "100");
//        String property = System.getProperty("");

        Integer int_1 = Integer.getInteger("Kona");
        System.out.println("Integer.getInteger(\"Kona\"); => " + int_1);

        Integer int_2 = Integer.getInteger("Kona", 2);
        System.out.println("Integer.getInteger(\"Kona\",2); => " + int_2);

//        Integer int_3 = Integer.getInteger("Kona", new Integer(null));
//        System.out.println("Integer.getInteger(\"Kona\",new Integer(null)); => " + int_3);
    }

    public static void compare() {
        // 比较大小
        int int_1 = new Integer(1).compareTo(2);
        System.out.println("new Integer(1).compareTo(2) => " + int_1);

        int int_2 = new Integer(2).compareTo(2);
        System.out.println("new Integer(2).compareTo(2) => " + int_2);

        int int_3 = new Integer(3).compareTo(2);
        System.out.println("new Integer(3).compareTo(2) => " + int_3);

//        int int_4 = new Integer(3).compareTo(null);
//        System.out.println("new Integer(3).compareTo(null) => " + int_4);

        int int_5 = Integer.compare(1, 2);
        System.out.println("Integer.compare(1,2); => " + int_5);

        int int_6 = Integer.compare(2, 2);
        System.out.println("Integer.compare(2,2) => " + int_6);

        int int_7 = Integer.compare(3, 2);
        System.out.println("Integer.compare(3,2); => " + int_7);


        // 用于将两个整形参数转化为无符号数值后比较大小
        int int_8 = Integer.compareUnsigned(1, -2);
        System.out.println("Integer.compareUnsigned(1,2); => " + int_8);
        int int_9 = Integer.compareUnsigned(2, -2);
        System.out.println("Integer.compareUnsigned(2,2) => " + int_9);
        int int_10 = Integer.compareUnsigned(3, -2);
        System.out.println("Integer.compareUnsigned(3,-2); => " + int_10);

    }

    public static void basic() {
        int int_1 = Integer.sum(1, 2);
        System.out.println("Integer.sum(1,2); =>" + int_1);
        int int_2 = Integer.max(1, 2);
        System.out.println("Integer.max(1,2); =>" + int_2);
        int int_3 = Integer.min(1, 2);
        System.out.println("Integer.min(1,2); =>" + int_3);


        // 返回指定整数值的正负号函数。对于正值,负值和零,该方法分别返回1,-1和0。
        int int_12 = Integer.signum(100);
        System.out.println("Integer.signum(1); => " + int_12);
        int int_13 = Integer.signum(-100);
        System.out.println("Integer.signum(-1); => " + int_13);
        int int_14 = Integer.signum(0);
        System.out.println("Integer.signum(0); => " + int_14);

    }


    public static void toStrints() {

        // 返回十进制无符号值
        // 在计算机中,可以区分正负的类型,称为有符号类型,无正负的类型,称为无符号类型
        // https://blog.csdn.net/LANGZI7758521/article/details/51853298
        String s1 = Integer.toUnsignedString(1);
        String s2 = Integer.toUnsignedString(-1);

        System.out.println("Integer.toUnsignedString(1); =>" + s1);
        System.out.println("Integer.toUnsignedString(-1); =>" + s2);
//        // 2,4,8,16,32进制
        String s3 = Integer.toUnsignedString(1, 2);
        String s4 = Integer.toUnsignedString(-1, 2);
        System.out.println("Integer.toUnsignedString(1, 2); =>" + s3);
        System.out.println("Integer.toUnsignedString(-1, 2); =>" + s4);

//        // 十六进制的无符号
        String s5 = Integer.toHexString(1000);
        String s6 = Integer.toUnsignedString(1000, 16);
        System.out.println("Integer.toHexString(1000); =>" + s5);
        System.out.println("Integer.toUnsignedString(1000, 16); =>" + s6);
//
//        // 8进制的无符号
        String s7 = Integer.toOctalString(1000);
        String s8 = Integer.toUnsignedString(1000, 8);
        System.out.println("Integer.toOctalString(1000); =>" + s7);
        System.out.println("Integer.toUnsignedString(1000, 8); =>" + s8);
//
        String s9 = Integer.toBinaryString(1000);
        String s10 = Integer.toUnsignedString(1000, 2);
        System.out.println("Integer.toBinaryString(1000); =>" + s9);
        System.out.println("Integer.toUnsignedString(1000, 2); =>" + s10);


    }

    /**
     * 将String解码为Integer。该方法还接受十进制,十六进制和八进制数。
     */
    private static void decode() {
        Integer int_1 = Integer.decode("010");

        Integer int_2 = Integer.parseInt("010");
        Integer int_3 = Integer.parseInt("010", 8);

        System.out.println("Integer.decode(\"010\") =>" + int_1);
        System.out.println("Integer.parseInt(\"010\") =>" + int_2);
        System.out.println("Integer.parseInt(\"010\",2) =>" + int_3);
    }

    public static void parseUnsignedInt() {
        //Integer.parseUnsignedInt()  此方法将 String 参数解析为无符号十进制整数对象。
        //字符串中的字符必须是十进制数字,但字符串的第一个字符可以是 ASCII 加号 '+' 符号以表示正值。
        //它返回由十进制整数中的参数表示的无符号整数值。


        // Integer.parseUnsignedInt(String s, int radix) 此方法将 String 参数解析为第二个参数指定基数的无符号十进制整数对象。
        // 字符串中的字符必须是指定参数的十进制数字,但第一个字符可以是 ASCII 加号 '+' 表示正值。
        // 将返回结果整数值。
        int int_1 = Integer.parseUnsignedInt("111");
        System.out.println("Integer.parseUnsignedInt(\"111\"); =>" + int_1);
        int int_2 = Integer.parseUnsignedInt("111", 2);
        System.out.println("Integer.parseUnsignedInt(\"111\",2); =>" + int_2);

        int int_3 = Integer.parseUnsignedInt("-1");   // 不可以为负数
        System.out.println("Integer.parseUnsignedInt(\"-1\"); =>" + int_3);
        int int_4 = Integer.parseUnsignedInt("2", 2);
        System.out.println("Integer.parseUnsignedInt(\"2\",2);" + int_4);

    }

    private static void other() {
        // 它通过无符号转换将参数转换为 long。在对 long 的无符号转换中,long 的 high-order 32 位为零,低 32 位等于整数参数的位。
        long long_1 = Integer.toUnsignedLong(1);
        System.out.println("Integer.toUnsignedLong(1); => " + long_1);

        // 返回第一个参数除以第二个参数的无符号商,其中每个参数和结果都被解释为一个无符号值
        int int_1 = Integer.divideUnsigned(4, 2);
        System.out.println("Integer.divideUnsigned(4, 2); => " + int_1);

        // 返回第一个参数除以第二个参数的无符号余数,其中每个参数和结果都被解释为无符号值。
        int int_2 = Integer.remainderUnsigned(5, 2);
        System.out.println("Integer.remainderUnsigned(5, 2); => " + int_2);

        // 返回一个整数值,该整数值最多具有单个one-bit,对于给定的int值,其位于highest-order(即最左侧)one-bit的位置。
        // 如果指定的值在其补码二进制表示形式中不包含one-bits,则其返回零(简单地等于0)。
        // @see https://blog.csdn.net/zhangsai3593/article/details/78963295
        int int_3 = Integer.highestOneBit(3);
        System.out.println("Integer.highestOneBit(3); => " + int_3);

        // 该函数返回一个int值,该int值在指定int值中最低(即最右)的one-bit的位置处最多包含一个one-bit。
        // 如果指定值在其补码二进制表示形式中没有one-bits,则此方法将返回零。如果数字的二进制表示等于零。
        int int_4 = Integer.lowestOneBit(3);
        System.out.println("Integer.lowestOneBit(3); => " + int_4);

        // 此方法返回指定整数值的二进制补码表示中 highest-order ("leftmost") one-bit 之前的零位总数,即将 int 值转换为 Binary 然后考虑最高一位并返回其前面的零位总数。
        // 换句话说,如果指定的整数值没有 one-bits 或在其二进制补码表示中等于 0,则它将返回 32。
        int int_5 = Integer.numberOfLeadingZeros(1);
        System.out.println("Integer.numberOfLeadingZeros(1); => " + int_5);

        // 返回最低位(即,最右或最低有效“1”位)之后的零(0)位总数(one-bit,以指定整数值或二进制数的二进制补码表示)我们可以说这是将int值转换为Binary然后考虑最低位并返回no的函数。后面的零位。
        // 如果指定的整数值在其补码表示形式中不包含one-bits,也就是说,如果该值等于零,则将返回32。
        int int_6 = Integer.numberOfTrailingZeros(1);
        System.out.println("Integer.numberOfTrailingZeros(1); => " + int_6);

        // 以整数值的二进制补码表示形式返回one-bits的数量计数
        int int_7 = Integer.bitCount(1);
        System.out.println("Integer.bitCount(1); => " + int_7);


        // 用于将Integer值的位(以其二进制2的补码形式表示)向左移动指定的位数。 (位向左移或移至更高位)。
        // 移位是一种按位运算,它通过向左或向右移动一定数量的位来对二进制值的所有位执行
        // 100
        int int_8 = Integer.rotateLeft(1, 2);
        System.out.println("Integer.rotateLeft(1); => " + int_8);
        int int_9 = Integer.parseInt("100", 2);
        System.out.println("Integer.parseInt(\"100\", 2); => " + int_9);


        // 通过将指定int值a的二进制补码二进制表示形式向右旋转指定位数来返回获得的值。位向右移,即低位
        int int_10 = Integer.rotateRight(4, 2);
        System.out.println("Integer.rotateRight(100, 2); => " + int_10);
        // 用于返回通过反转给定参数的二进制2的补码表示形式的位顺序生成的值
        int int_11 = Integer.reverse(1);
        System.out.println("Integer.reverse(1); => " + int_11);


        // 用于返回通过反转给定参数的二进制2的补码表示形式的字节顺序而生成的值。
        int int_15 = Integer.reverseBytes(0);
        System.out.println("Integer.reverseBytes(0); => " + int_15);
    }

    public static void printResult0() {
        // ascii码
        Integer int_1 = new Integer('a');
        Integer int_2 = 97;
        Integer int_3 = 97;

        Integer int_4 = new Integer("97");
        Integer int_5 = new Integer(97);

        int int_6 = 97;

        System.out.println(int_1 == int_2);
        System.out.println(int_2 == int_3);
        System.out.println(int_3 == int_4);
        System.out.println(int_5 == int_6);
        System.out.println(int_1 == int_6);
        System.out.println(int_4 == int_5);

    }

    public static void printResult1() {
        // -XX:AutoBoxCacheMax=10000
        Integer a1 = 1000;
        Integer a2 = 1000;

        Integer b1 = new Integer(1000);
        Integer b2 = new Integer(1000);

        int c1 = 1000;

        System.out.println(a1 == a2);
        System.out.println(b1 == b2);
        System.out.println(a1 == b1);
        System.out.println(c1 == b1);
    }

    private static void printResult2() throws NoSuchFieldException, IllegalAccessException {

        Class cache = Integer.class.getDeclaredClasses()[0];
        Field myCache = cache.getDeclaredField("cache");
        myCache.setAccessible(true);
        Integer[] newCache = (Integer[]) myCache.get(cache);
        newCache[132] = newCache[133];

        int a = 2;
        int b = a + a;
        System.out.printf("%d + %d = %d", a, a, b);
    }

    public static void printResult3() {
        // 基本数据间转换
        Integer int_1 = 100;
        System.out.println("int_1.byteValue() => " + int_1.byteValue());
        System.out.println("int_1.intValue() => " + int_1.intValue());
        System.out.println("int_1.shortValue() => " + int_1.shortValue());
        System.out.println("int_1.longValue() => " + int_1.longValue());
        System.out.println("int_1.doubleValue() => " + int_1.doubleValue());
        System.out.println("int_1.floatValue() => " + int_1.floatValue());
    }

    public static void printResult4() {
        Integer int_1 = 1;
        String s1 = Integer.toString(int_1);
        String s2 = int_1.toString();
        String s3 = int_1 + "";

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }

    private static void printResult5() {
        // 涉及到补码  https://zhuanlan.zhihu.com/p/99254059
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MAX_VALUE + 1);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MIN_VALUE - 1);


        BigInteger bigInteger = new BigInteger(Long.MAX_VALUE + "" + Long.MAX_VALUE);
        System.out.println("bigInteger ==》" + bigInteger);
        BigInteger a = new BigInteger("13");
        BigInteger b = new BigInteger("4");

        //1.加
        BigInteger bigNum1 = a.add(b);            //17
        System.out.println("a.add(b) =>" + bigNum1);
        //2.减
        BigInteger bigNum2 = a.subtract(b);        //9
        System.out.println("a.subtract(b) =>" + bigNum2);
        //3.乘
        BigInteger bigNum3 = a.multiply(b);        //52
        System.out.println("a.multiply(b) =>" + bigNum3);
        //4.除
        BigInteger bigNum4 = a.divide(b);        //3
        System.out.println("a.divide(b) =>" + bigNum4);
        //5.取模(需 b > 0,否则出现异常:ArithmeticException("BigInteger: modulus not positive"))
        BigInteger bigNum5 = a.mod(b);            //1
        System.out.println("a.mod(b) =>" + bigNum5);
        //6.求余
        BigInteger bigNum6 = a.remainder(b);    //1
        System.out.println("a.remainder(b) =>" + bigNum6);
        //7.平方(需 n >= 0,否则出现异常:bigNum6("Negative exponent"))
        BigInteger bigNum7 = a.pow(3);            //2197
        System.out.println("a.pow(3) =>" + bigNum7);
        //8.取绝对值
        BigInteger bigNum8 = a.abs();            //13
        System.out.println("a.abs() =>" + bigNum8);
        //9.取相反数
        BigInteger bigNum9 = a.negate();        //-13
        System.out.println("a.negate() =>" + bigNum9);
    }

    private static void printResult6() {
        Integer int_1 = 1;
        change(int_1);
//
//        Integer int_1 = 1;
//        int_1 = 2;
        System.out.println("-----");
        System.out.println(int_1);
        System.out.println(int_1.intValue());
    }

    private static Integer change(Integer int_1) {
        int_1 = 2;
        System.out.println("int_1 =" + int_1.intValue());
        System.out.println("int_1 =" + int_1);
        return int_1;

    }
}

  • 13
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值