Java常用类之Integer类

    科技2022-07-12  143

    Integer整型类

    由于Java语言中的基本数据类型不是面向对象的,所以并不具备对象的性质

    Java在java.lang包中提供了八种基本数据类型对应的包装类

    装箱是指将基本类型的值转换成包装类对象

    拆箱是指将包装类对象转换成基本类型的值

    Integer对应基本类型 int

    Tips:Integer包装类会缓存-128到127的数值,在范围内会复用这个对象(内存分配的是一个地址),所以两个对象无论是用 == 还是equals 比较都是相等的

    Integer类中的常量

    MAX_VALUE:int类型的最大值,为2^32 - 1 MIN_VALUE:int类型的最小值,为 -2^31 SIZE:int类型以二进制补码的形式所占的位数,为32 BYTES:int类型以二进制补码的形式所占的字节数,为4 TYPE:Integer类型的Class对象 代码如下:

    /** * */ package CommonClasses; /** * @Description Integer包装类中的常量 * @author 枳洛淮南 * @version 1.0 * @Date 2020年10月2日下午8:51:43 */ public class IntegerConstant { public static void main(String[] args) { int maxIntValue = Integer.MAX_VALUE; int minIntValue = Integer.MIN_VALUE; int intSize = Integer.SIZE; int intBytes = Integer.BYTES; System.out.println("int类型最大值 :" + maxIntValue); System.out.println("int类型最小值 :" + minIntValue); System.out.println("int类型的二进制补码位数 :" + intSize); System.out.println("int类型的二进制补码字节数:" + intBytes); System.out.println("int类型的类型 :" + Integer.TYPE); } }

    运行结果:

    Integer类构造方法

    Integer类提供了两种构造方法:

    以整型数值为入参创建一个Integer对象 Integer(int number) 以String字符串为入参创建一个Integer对象 Integer(String str) 代码如下:

    /** * */ package CommonClasses; /** * @Description Integer类中的构造函数(以int类型传入和以String传入) * @author 枳洛淮南 * @version 1.0 * @Date 2020年10月2日下午8:59:10 */ public class IntegerConstructor { @SuppressWarnings("deprecation") public static void main(String[] args) { Integer i1 = new Integer(100); Integer i2 = new Integer("100"); System.out.println("以int类型为入参构造Integer对象 :" + i1); System.out.println("以String类型为入参构造Integer对象:" + i2); //自动转换为Integer对象 } }

    运行结果:

    Integer类中的常用方法

    代码如下:

    /** * */ package CommonClasses; /** * @Description Integer类常用方法 * @author 枳洛淮南 * @version 1.0 * @Date 2020年10月2日下午9:05:39 */ public class IntegerMethod { @SuppressWarnings("deprecation") public static void main(String[] args) { Integer i1 = new Integer(1000); Integer i2 = new Integer(1000); Integer i7 = new Integer(200); Integer i8 = new Integer(300); int i3 = new Integer(200); int i4 = new Integer(300); int i5 = new Integer(1000); int i6 = new Integer(1000); System.out.println("比较此对象与指定对象是否值相同:" + i1.equals(i2) + "\t" + i1.equals(i4)); System.out.println("(静态方法):比较数值——>相等为0,大于为1,小于为-1:" + Integer.compare(i5, i6) + "\t" + Integer.compare(i4, i3) + "\t" + Integer.compare(i3, i4)); System.out.println("比较两个Integer对象:" + i1.compareTo(i2) + "\t" + i1.compareTo(i7) + "\t" + i7.compareTo(i1)); System.out.println("(静态方法)返回指定int数值的Integer对象:" + Integer.valueOf(i3)); System.out .println("(静态方法)将指定字符串解析成有符号的十进制整数基本类型:" + Integer.parseInt("-1000") + "\t" + Integer.parseInt("1000")); System.out.println("(静态方法)返回指定String数值的Integer对象:" + Integer.valueOf("-1000")); System.out.println("返回该Integer对象值的String对象:" + i8.toString()); System.out.println("(静态方法)返回指定int数值的String对象:" + Integer.toString(i8)); System.out.println("(静态方法)以二进制无符号整数形式返回String对象:" + Integer.toBinaryString(i8)); System.out.println("(静态方法)以八进制无符号整数形式返回String对象:" + Integer.toOctalString(i8)); System.out.println("(静态方法)以十六进制无符号整数形式返回String对象:" + Integer.toHexString(i8)); System.out.println("(静态方法)返回指定int数值的正负符号:" + Integer.signum(i6) + "\t" + Integer.signum(-i6)); System.out.println("以byte类型返回该Integer对象的值:" + i8.byteValue()); System.out.println("以short类型返回该Integer对象的值:" + i8.shortValue()); System.out.println("以int类型返回该Integer对象的值:" + i8.intValue()); System.out.println("以long类型返回该Integer对象的值:" + i8.longValue()); System.out.println("以float类型返回该Integer对象的值:" + i8.floatValue()); System.out.println("以double类型返回该Integer对象的值:" + i8.doubleValue()); System.out.println("(静态方法)返回两个int数值中的最大值,该方法调用了Math.max方法:" + Integer.max(i6, i5)); System.out.println("(静态方法)返回两个int数值中的最小值,该方法调用了Math.min方法:" + Integer.min(i5, i6)); System.out.println("(静态方法)返回int数值相加的结果:" + Integer.sum(i5, i6)); } }

    运行结果:

    Processed: 0.011, SQL: 8