基本数据类型对应包装类

基本类型包装类
intInteger
charCharacter
doubleDouble
longLong
shortShort
booleanBoolean
floatFloat
byteByte
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java中八种基本数据类型对应包装类的使用示例代码: 1. byte 和 Byte ```java // byte 转 Byte byte b1 = 10; Byte b2 = Byte.valueOf(b1); // 调用Byte类的valueOf方法将byte类型转为Byte类型 System.out.println(b2); // 输出10 // Byte 转 byte Byte b3 = new Byte((byte) 20); byte b4 = b3.byteValue(); // 调用Byte类的byteValue方法将Byte类型转为byte类型 System.out.println(b4); // 输出20 ``` 2. short 和 Short ```java // short 转 Short short s1 = 30; Short s2 = Short.valueOf(s1); // 调用Short类的valueOf方法将short类型转为Short类型 System.out.println(s2); // 输出30 // Short 转 short Short s3 = new Short((short) 40); short s4 = s3.shortValue(); // 调用Short类的shortValue方法将Short类型转为short类型 System.out.println(s4); // 输出40 ``` 3. int 和 Integer ```java // int 转 Integer int i1 = 50; Integer i2 = Integer.valueOf(i1); // 调用Integer类的valueOf方法将int类型转为Integer类型 System.out.println(i2); // 输出50 // Integer 转 int Integer i3 = new Integer(60); int i4 = i3.intValue(); // 调用Integer类的intValue方法将Integer类型转为int类型 System.out.println(i4); // 输出60 ``` 4. long 和 Long ```java // long 转 Long long l1 = 70; Long l2 = Long.valueOf(l1); // 调用Long类的valueOf方法将long类型转为Long类型 System.out.println(l2); // 输出70 // Long 转 long Long l3 = new Long(80L); long l4 = l3.longValue(); // 调用Long类的longValue方法将Long类型转为long类型 System.out.println(l4); // 输出80 ``` 5. float 和 Float ```java // float 转 Float float f1 = 1.23f; Float f2 = Float.valueOf(f1); // 调用Float类的valueOf方法将float类型转为Float类型 System.out.println(f2); // 输出1.23 // Float 转 float Float f3 = new Float(4.56f); float f4 = f3.floatValue(); // 调用Float类的floatValue方法将Float类型转为float类型 System.out.println(f4); // 输出4.56 ``` 6. double 和 Double ```java // double 转 Double double d1 = 7.89; Double d2 = Double.valueOf(d1); // 调用Double类的valueOf方法将double类型转为Double类型 System.out.println(d2); // 输出7.89 // Double 转 double Double d3 = new Double(0.123); double d4 = d3.doubleValue(); // 调用Double类的doubleValue方法将Double类型转为double类型 System.out.println(d4); // 输出0.123 ``` 7. char 和 Character ```java // char 转 Character char c1 = 'A'; Character c2 = Character.valueOf(c1); // 调用Character类的valueOf方法将char类型转为Character类型 System.out.println(c2); // 输出A // Character 转 char Character c3 = new Character('B'); char c4 = c3.charValue(); // 调用Character类的charValue方法将Character类型转为char类型 System.out.println(c4); // 输出B ``` 8. boolean 和 Boolean ```java // boolean 转 Boolean boolean b1 = true; Boolean b2 = Boolean.valueOf(b1); // 调用Boolean类的valueOf方法将boolean类型转为Boolean类型 System.out.println(b2); // 输出true // Boolean 转 boolean Boolean b3 = new Boolean(false); boolean b4 = b3.booleanValue(); // 调用Boolean类的booleanValue方法将Boolean类型转为boolean类型 System.out.println(b4); // 输出false ``` 以上是八种基本数据类型对应包装类的使用示例代码,带有注释说明,希望能够帮助你理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值