加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

22-包装类型

发布时间:2020-12-15 07:56:15 所属栏目:Java 来源:网络整理
导读:包装类型Java的数据类型分两种:基本类型: byte , short , int , long ,boolean, float , double , char 引用类型:所有class和interface类型引用类型可以赋值为null,表示空,但基本类型不能赋值为null。Java核心库为每种基本类型都提供了对应的包
包装类型
Java的数据类型分两种:
基本类型:byteshortintlong,boolean,floatdoublechar
引用类型:所有class和interface类型

引用类型可以赋值为null,表示空,但基本类型不能赋值为null。
Java核心库为每种基本类型都提供了对应的包装类型

基本类型 对应的引用类型
boolean java.lang.Boolean
byte java.lang.Byte
short java.lang.Short
int java.lang.Integer
long java.lang.Long
float java.lang.Float
double java.lang.Double
char java.lang.Character
可以直接使用
public class Main { public static void main(String[] args) { int i = 100; // 通过new操作符创建Integer实例(不推荐使用,会有编译警告): Integer n1 = new Integer(i); // 通过静态方法valueOf(int)创建Integer实例: Integer n2 = Integer.valueOf(i); // 通过静态方法valueOf(String)创建Integer实例: Integer n3 = Integer.valueOf("100"); System.out.println(n3.intValue()); } } 因为int和Integer可以互相转换: int i = 100; Integer n = Integer.valueOf(i); int x = n.intValue(); 所以,Java编译器可以帮助自动在int和Integer之间转型: Integer n = 100; // 编译器自动使用Integer.valueOf(int) int x = n; // 编译器自动使用Integer.intValue() 这种直接把int变为Integer的赋值写法,称为自动装箱(Auto Boxing),反过来,把Integer变为int的赋值写法,称为自动拆箱(Auto Unboxing)。 注意:自动装箱和自动拆箱只发生在编译阶段,目的是为了少写代码。装箱和拆箱会影响代码的执行效率,因为编译后的class代码是严格区分基本类型和引用类型的。并且,自动拆箱执行时可能会报NullPointerException: public class Main { public static void main(String[] args) { Integer n = null; int i = n; } } 不变类 所有的包装类型都是不变类。我们查看Integer的源码可知,它的核心代码如下: public final class Integer { private final int value; } 注意:一旦创建了Integer对象,该对象就是不变的。对两个Integer实例进行比较绝对不能用==,因为Integer是引用类型,必须使用equals()比较。 进制转换 Integer类本身还提供了大量方法,用于进制转换。例如: //静态方法parseInt()可以把字符串解析成一个整数 int x1 = Integer.parseInt("100"); // 100 int x2 = Integer.parseInt("100",16); // 256,因为按16进制解析 //整数格式化为指定进制的字符串 public class Main { public static void main(String[] args) { System.out.println(Integer.toString(100)); // "100",表示为10进制 System.out.println(Integer.toString(100,36)); // "2s",表示为36进制 System.out.println(Integer.toHexString(100)); // "64",表示为16进制 System.out.println(Integer.toOctalString(100)); // "144",表示为8进制 System.out.println(Integer.toBinaryString(100)); // "1100100",表示为2进制 } } 注意:上述方法的输出都是String,在计算机内存中,只用二进制表示,不存在十进制或十六进制的表示方法。 处理无符号整型 在Java中,并没有无符号整型(Unsigned)的基本数据类型。byteshort、int和long都是带符号整型,最高位是符号位。无符号整型和有符号整型的转换在Java中就需要借助包装类型的静态方法完成。 //把一个负的byte按无符号整型转换为int public class Main { public static void main(String[] args) { byte x = -1; byte y = 127; System.out.println(Byte.toUnsignedInt(x)); // 255 System.out.println(Byte.toUnsignedInt(y)); // 127 } } 因为byte的-1的二进制表示是22222111,以无符号整型转换后的int就是255。 类似的,可以把一个short按unsigned转换为int,把一个int按unsigned转换为long。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读