JDK_实例(数字和数字封装类)

package book.number;

/**
 * 数字的封装类
 * 为了满足用户可能会需要以对象的方式操作基本类型,因此,
 * Java为每种基本数据类型都定义了相应的封装类。
 * byte --> Byte; short --> Short; int --> Integer
 * long --> Long; float --> Float; double --> Double
 * boolean --> Boolean; char --> Character 
 */
public class NumberClass {
	
	/** 基本类型到封装类型的转换:以基本类型的数据为参数new一个相应封装类的对象
	 * 封装类型到基本类型的转换:返回封装类型对象的相应的value值。 */
	
	/**
	 * byte类型数字转换成Byte类型对象
	 */
	public static Byte byte2Byte(byte b){
		//return Byte.valueOf(b);
		return new Byte(b);
	}
	/**
	 * Byte类型对象转换成byte类型数字
	 */
	public static byte Byte2byte(Byte B){
		if (B == null) {
			return 0;
		} else {
			return B.byteValue();
		}
	}
	/**
	 * int类型数字转换成Integer类型对象
	 */
	public static Integer int2Integer(int i){
		// return Integer.valueOf(i);
		return new Integer(i);
	}
	/**
	 * Integer类型对象转换成int类型数字
	 */
	public static int Integer2int(Integer integer){
		if (integer == null) {
			return 0;
		} else {
			return integer.intValue();
		}
	}
	//其他基本类型与封装类型的相互转换都符合这个规则,这里就不一一列出了
	
	public static void main(String[] args) {
		int i = 5;
		Integer I = int2Integer(i);
		//将int类型转换成Integer之后,可以变成字符串
		String iStr = I.toString();//将int类型转换成Integer之后,可以变成字符串
		Integer a = new Integer(5);
		Integer b = new Integer(10);
		//Integer对象本身不能进行加减乘除的运算,必须使用它的int值进行运算
		int sum = a.intValue() + b.intValue();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值