数字和数字封装

 1 package com.jdk7.chapter3;
 2 /**
 3  * 数字和数字封装
 4  * Byte是byte的封装,byte没有任何功能,要想使用byte的功能,就必须将byte转换为Byte,通过调用Byte的对象操作byte类型的值
 5  * @author Administrator
 6  *
 7  */
 8 public class NumberClass {
 9     //Byte继承抽象类Number,是对基本数据类型byte相关操作的封装类
10     public static Byte byte2Byte(byte b){
11         return new Byte(b);
12     }
13     
14     public static byte Byte2byte(Byte B){
15         if(B==null){
16             return 0;
17         }else{
18             return B.byteValue();
19         }
20     }
21     
22     public static void main(String[] args) {
23         NumberClass numberClass = new NumberClass();
24         byte bb = 88;
25         System.out.println(Byte.valueOf(bb));
26         System.out.println(numberClass.byte2Byte(bb));
27         
28         Byte B = new Byte("66");
29         Byte B1 = new Byte((byte)44);
30         System.out.println(numberClass.Byte2byte(B));
31         System.out.println(numberClass.Byte2byte(B1));
32     }
33 }
34 
35 执行结果:
36 88
37 88
38 66
39 44

 基本数据类型与其封装类的对应关系如下:

 ♦字节(byte)的封装类是字节类(Byte)。

 ♦短整型(short)的封装类是短整型类(Short)。

♦整型(int)的封装类是整型类(Int)。

♦长整型(long)的封装类是长整型类(Long)。

♦单精度浮点型(float)的封装类是单精度浮点型类(Float)。

♦双精度浮点型(double)的封装类是双精度浮点型类(Double)。

♦布尔型(boolean)的封装类是布尔型类(Boolean)。

♦字符型(char)的封装类是字符型类(Character)。

基本类型值转换为封装类对象有两种方法:

1. 直接使用new方法创建封装类对象,参数使用基本类型值。

2. 使用封装类名加上valueOf静态方法,参数使用基本类型值,返回封装类型对象。

封装类对象转换为基本类型值方法:

封装类对象+xxValue实例方法,返回xx的基本类型值。

原文地址:https://www.cnblogs.com/celine/p/8298741.html