Common Java classes - Integer class basic types of packaging

1 Overview

  • The Integer class wraps a base object type in an int
  • Integer class provides a number of methods that can convert between types of type int and String
package zhengshu;

public class IntegerDm {
    public static void main(String[] args){
        System.out.println(Integer.toBinaryString(100));//转换成二进制数:1100100
        System.out.println(Integer.toOctalString(100));//转换成八进制数:144
        System.out.println(Integer.toHexString(100));//转换成十六进制数:64
        System.out.println("---------------------------");
        System.out.println(Integer.MAX_VALUE);//代表int所能表示的最大值
        System.out.println(Integer.MIN_VALUE);//代表int所能表示的最小值
    }
}

String type is converted to int type:

package zhengshu;

public class IntegerDm {
    public static void main(String[] args){
        int i1 = 100;
        System.out.println("i1的值为:"+i1);
        Integer i2 = new Integer(i1);
        System.out.println("i2的值为:"+i2);

        String s1 = "100";
        Integer i3 = new Integer(s1);//字符串转换成数字,前提是这个字符串是由数组字符组成
        System.out.println("i3的值为:"+i3);
    }
}

2, the configuration method

  • public Integer(int value)
  • public Integer(String s)

3, member methods

  • public int intValue()
  • public static int parseInt(String s)
  • public static String toString(int i)
  • public static Integer valueOf(int i)
  • public static Integer valueOf(String s)
package zhengshu;

public class IntegerDm {
    public static void main(String[] args){
        //int -> Stirng
        int i1 = 100;
        String s1 = ""+ i1;//int -> Stirng
        System.out.println(s1);//输出字符串

        String s2 = String.valueOf(i1);//int -> Stirng,返回给定参数的原生 Number 对象值
        System.out.println(s2);

        Integer i2 = new Integer(i1);
        System.out.println(i2);

        String s3 = Integer.toString(i1);
        System.out.println(s3);
        System.out.println("------------------------");

        //String -> int
        String st1 = "200";
        Integer in1 = new Integer(st1);
        System.out.println(in1);

        int in2 = in1.intValue();
        System.out.println(in2);

        System.out.println(Integer.parseInt(st1));//parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析
    }
}

4, common basic hex conversion

  • public static String toBinaryString(int i)
  • public static String toOctalString(int i)
  • public static String toHexString(int i)

5, decimal to hexadecimal other

  • public static String toString(int i,int radix)

6, other binary to decimal

  • public static int parseInt(String s,int radix)
package zhengshu;

public class IntegerDm {
    public static void main(String[] args){
        System.out.println(Integer.toBinaryString(100));//十进制转二进制
        System.out.println(Integer.toOctalString(100));//十进制转八进制
        System.out.println(Integer.toHexString(100));//十进制转十六进制
        System.out.println("---------------------------");

        //十进制转到其他进制
        System.out.println(Integer.toString(100,2));//十进制转二进制
        System.out.println(Integer.toString(100,8));//十进制转八进制
        System.out.println(Integer.toString(100,16));//十进制转十六进制
        System.out.println(Integer.toString(100,7));//十进制转七进制
        System.out.println(Integer.toString(100,30));//十进制转三十进制
        System.out.println("============================");

        //其他进制转到十进制
        System.out.println(Integer.parseInt("100",10));//那个数字的10进制是100,100
        System.out.println(Integer.parseInt("100",2));//那个数字的2进制是100,4
        System.out.println(Integer.parseInt("100",8));//那个数字的8进制是100,64
        System.out.println(Integer.parseInt("100",16));//那个数字的16进制是100,256
    }
}

7, the automatic boxing and unboxing

  • Integer x = new Integer(4);Can be written Integer x = 4;(autoboxing)
  • x = x + 5;(Automatic unpacking), by the method intValue
  • New features JDK 1.5 ( autoboxing ): The basic types to wrapper class type
  • JDK 1.5 new features ( automatic unpacking ): to convert the base type packaging type
package zhengshu;

public class IntegerDm {
    public static void main(String[] args){
        byte b1 = 100;
        byte b2 = b1;

        Integer i1 = new Integer(200);//定义一个 int 类型的包装类型变量i1
        Integer i2 = 300;
        i2 = i2 + 400;
        System.out.println("i2的值为:"+i2);//输出:700

        Integer i3 = Integer.valueOf(500);//自动装箱
        i3 = Integer.valueOf(i3.intValue() + 60);//自动拆箱,再自动装箱
        System.out.println((new StringBuilder("i3的值为:")).append(i3).toString());//输出:560
    }
}
package zhengshu;

public class IntegerDm {
    public static void main(String[] args){
        Integer i1 = new Integer(128);
        Integer i2 = new Integer(128);
        System.out.println(i1 == i2);
        System.out.println(i1.equals(i2));
        System.out.println("-----------------------");

        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);
        System.out.println(i3.equals(i4));
        System.out.println("=========================");

        Integer i5 = 127;
        Integer i6 = 127;
        System.out.println(i5 == i6);
        System.out.println(i5.equals(i6));
        System.out.println("+++++++++++++++++++++++++");

        int i7 = 1280;
        Integer i8 = 1280;
        System.out.println(i7 == i8);
        // 针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
    }
}

note:

  • Integer data direct assignment, if between -128 to 127, retrieves data directly from the buffer pool
  • In use, Integer x = null;the above code will be a NullPointerException (null pointer exception)

Guess you like

Origin blog.csdn.net/weixin_43860260/article/details/91356141