(5)包装类
java.lang.Number
1、类结构
public abstract class Number implements java.io.Serializable {
2、方法
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
private static final long serialVersionUID = -8742448824652078965L;
包装类
包装类与基本类型是一一对应的。
基本类型的好处是方便存储和计算。而包装类提供对象概念,以及一些方法。
基本类型转为包装类型称为装箱,反之称为拆箱。装箱和拆箱得到编译器的支持,可以自动转换。
Integer a = 3;
int b=10;
setNum1(a);
setNum2(b);
System.out.println(a+b);//13
public static void setNum1(int a){
System.out.println(a);
}
public static void setNum2(Integer b){
System.out.println(b);
}
1、类结构
public final class Byte extends Number implements Comparable<Byte>
public final class Short extends Number implements Comparable<Short>
public final class Integer extends Number implements Comparable<Integer>
public final class Long extends Number implements Comparable<Long>
public final class Float extends Number implements Comparable<Float>
public final class Double extends Number implements Comparable<Double>
public final class Boolean implements java.io.Serializable,Comparable<Boolean>
public final class Character implements java.io.Serializable, Comparable<Character>
2、静态属性
public static final byte MAX_VALUE = 127;
public static final byte MIN_VALUE = -128;
public static final int SIZE = 8;
public static final int BYTES = SIZE / Byte.SIZE;
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
3、非静态属性
private final byte value;
4、构造方法
Byte、Short、Integer、Long、Float、Double和Boolean都有两种构造方法,参数为基本数据类型或者字符串
而Character只有一种,即参数为char
5、其它方法
(1)对Comparable接口的重写
public static int compare(byte x, byte y) {
return x - y;
}
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
(2)xxValue
对Number类方法的重写,返回对应类型的基本类型值
byteValue、shortValue、intValue、longValue、floatValue、longValue
(3)equals和hashCode
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
(4)解析
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
public static byte parseByte(String s, int radix)
throws NumberFormatException {
int i = Integer.parseInt(s, radix);
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:\"" + s + "\" Radix:" + radix);
return (byte)i;
}
(5)取值
valueOf
(6)字符串
toString
toString