JDK.8自动拆装箱
1.Integer构造方法
package ContainerLoading;
/**
* Integer应用类型的构造方法
* public Integer(int value) 构造新分配的 Integer对象,该对象表示指定的 int值。
* public Integer(String s) 构造一个新分配Integer对象,表示int由指示值String参数。 字符串将转换为int值,正好与基数为parseInt方法一样。
*/
public class ContainerLoading {
public static void main(String[] args) {
//通过构造方法将整数转换为引用型并且赋值
Integer num = new Integer(10);
Integer num1 = new Integer("20");
System.out.println(num);
System.out.println(num1);
}
}
运行结果:
10
20
2.Integer自动拆装箱
package ContainerLoading;
/*
* 自动拆装性:
* 为了方便操作基本类型和引用类型之间可以快速灵活转换
* 自动装箱:隐式的将基本类型转为引用类型
* 自动拆箱:就是将引用类型转为基本类型
* */
public class Loading {
public static void main(String[] args) {
int num = 100;
//自动装箱:隐式的将基本类型转为引用类型(int转换为Integer类型)
//等价于 Integer num1 = Integer.valueOf(num);
Integer num1 = num;
System.out.println(num1);
//自动拆箱:就是将引用类型转为基本类型(Integer类型转换为int类型)
//本质是将对象中的int值拿到
Integer num2 = new Integer(100);
int i = num2.intValue();
System.out.println(i);
}
}
运行结果;
100
100
3.Integer基本方法
package ContainerLoading;
/**
* Integer类型的基本方法
* Integer的最大值 : Integer.MAX_VALUE
* Integer的最小值 : Integer.MIN_VALUE
* Integer的字符长度 : Integer.SIZE
* Integer类型转换为byte类型 : byteValue()
* Integer类型转换为long类型 : longValue()
* String类型的数字转换为int : Integer.parseInt(String s)
* int类型转换为String : Integer.toString(int i)
* int转换为二进制的结果(String类型) : Integer.toBinaryString(int i)
*
*/
public class BasicFunction {
public static void main(String[] args) {
Integer i = 130;// 自动装箱,引用类型
// Integer类型的基本常识
System.out.println("Integer的最大值---->"+Integer.MAX_VALUE);
System.out.println("Integer的最小值---->"+Integer.MIN_VALUE);
System.out.println("Integer的字符长度---->"+Integer.SIZE);
// Integer类型转换为byte类型
byte b = i.byteValue();
System.out.println("Integer类型转换为byte类型--->"+b);
// Integer类型转换为long类型
long l = i.longValue();
System.out.println("Integer类型转换为long类型--->"+l);
// 把String类型的数字转换为int
int n1 = Integer.parseInt("30");
System.out.println("String类型的数字转换为int-->"+n1);
// 把int类型转换为String
String str = Integer.toString(n1);
System.out.println("将int类型转换为String类型并调用length()-->"+str.length());
// 扩展方法(十进制整数转为二进制) 以下代码输出4转换为二进制的结果
System.out.println("输出4转换为二进制的结果(String类型)--->"+Integer.toBinaryString(4));
}
}
运行结果;
Integer的最大值---->2147483647
Integer的最小值---->-2147483648
Integer的字符长度---->32
Integer类型转换为byte类型--->-126
Integer类型转换为long类型--->130
String类型的数字转换为int-->30
将int类型转换为String类型并调用length()-->2
输出4转换为二进制的结果(String类型)--->100
4.Integer与int之间的==关系
package ContainerLoading;
/**
* 测试Integer与int之间的==关系
*/
public class IntergerDemo {
public static void main(String[] args) {
int num = 200;
int num1 = 200;
// 基本类型 == 基本类型 比较值
System.out.println("num == num1---->"+(num == num1));
// 自动装箱:隐式的将基本类型转为引用类型
// 为了方便操作基本类型和引用类型之间可以快速灵活转换
Integer num2 = 10;
Integer num3 = 10;
// 引用类型 == 引用类型 比较的是对象在内存中地址值
System.out.println("num2 == num3---->"+(num2 == num3));
Integer num4 = 128;
Integer num5 = 128;
// Integer一旦超出缓存范围[-128,127],就会每次都创建一个新的对象
System.out.println("num4 == num5---->"+(num4 == num5));
Integer num6 = new Integer(20);
Integer num7 = new Integer(20);
// 只要出现new 那么肯定是创建一个独一无二的对象
System.out.println("num6 == num7---->"+(num6 == num7));
int num8 = 10000;
Integer num9 = 10000;
// 基本类型 == 引用类型 会将引用类型拆箱为基本类型
System.out.println("num8 == num9---->"+(num8 == num9));
}
}
运行结果:
num == num1---->true
num2 == num3---->true
num4 == num5---->false
num6 == num7---->false
num8 == num9---->true
对于Integer与int之间的==关系,微信公众号:Java高效学习(为什么Java中1000 == 1000为false而100 == 100为true?),当我看到的时候我有点好奇就点击去阅读了,因为学Java时间不长,记得不熟,所以这就促使我索性整理一下基础知识。可能整理的不到位,还望阅读者提出建议。