基本类型与其封装类型

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

byte------Byte;  short------Short;  int------Integet;   long------Long;   float------Float;  double------Double;   boolean------Boolean;   char------Character

基本类型的值转化成封装类对象时,有两种方法:

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

使用封装类的valueOf静态方法,参数为基本类型的值,返回封装类对象。

封装类转换为基本类型的值时,有一种方法:

使用封装类对象的xxxValue实例方法,返回它的基本类型值。

 

应用例子如下所示:

public class TestNumber {
 // 基本类型byte转换为包装类型的方法
 private Byte byte2Byte(byte b) {
  Byte by = new Byte(b);
  // Byte by = Byte.valueOf(b);
  return by;
 }

 // 包装类型Byte装换位基本类型
 private byte Byte2byte(Byte b) {
  if (b == null) {
   return 0;
  } else {
   return b.byteValue();
  }
 }
 // short的
 private Short short2Short(short s) {
  return Short.valueOf(s);
//  return new Short(s);
 }
 private short Short2short(Short s) {
  return s.shortValue();
 }
 
 // 基本类型int转换为包装类型的方法
 private Integer int2Integer(int a) {
  // return new Integer(a);
  return Integer.valueOf(a);
 }
 // 包装类型Integet装换位基本类型
 private int Integet2int(Integer a) {
  return a.intValue();
 }
 
 // 基本类型long转换为包装类型的方法
 private long long2Long(long a) {
//   return new Long(a);
  return Long.valueOf(a);
 }
 // 包装类型Integet装换位基本类型
 private long Long2long(Long a) {
  return a.longValue();
 }
 
 //float
 private Float float2Float(float f) {
  return Float.valueOf(f);
//  return new Float(f);
 }
 private float Float2float(Float f) {
  return f.floatValue();
 }
 
 //double
 private Double double2Double(double d) {
  return Double.valueOf(d);
  //return new Double(d);
 }
 private double Double2double(Double d) {
  return d.doubleValue();
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值