java基本类型与其对应的封装类型应用实例源码



java基本类型与其对应的封装类型应用实例源码。数据类型的操作源码。

基本类型与其封装类型对应的关系如下:
byte——Byte; short——Short; int——Integet; long——Long; float——Float; double——Double; boolean——Boolean; char——Character
基本类型的值转化成封装类对象时,有两种方法:
v 直接用new方法创建封装类对象,参数为基本类型的值。
v 使用封装类的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();
}
}