总述
数据,是一个程序最基本的要素,它的最终形式一定是基本数据类型。java是一种面向对象的语言,不能创建基础数据类型对象,也不能在使用泛型时兼容基本类型。为了解决这个问题,将基本类型视作对象进行处理。Java给每一个基本类型提供了包装类,放在java.lang包下。
所有对应基本类型,其包装类共有八个:
- Integer (int)
- Long (long)
- Short (short)
- Double (double)
- Float (float)
- Byte (byte)
- Character (char)
- Boolean (boolean)
其中非数字类两个,Character , Boolean
其余位数字类,继承数字类的父类Number
关系如下:
分述
分别阐述数字类和非数字类
数字类
在说数字包装类之前,先来看看数字类的父类
Number
看源码:
package java.lang;
/**
* The abstract class <code>Number</code> is the superclass of classes
* <code>BigDecimal</code>, <code>BigInteger</code>,
* <code>Byte</code>, <code>Double</code>, <code>Float</code>,
* <code>Integer</code>, <code>Long</code>, and <code>Short</code>.
* <p>
* Subclasses of <code>Number</code> must provide methods to convert
* the represented numeric value to <code>byte</code>, <code>double</code>,
* <code>float</code>, <code>int</code>, <code>long</code>, and
* <code>short</code>.
*
* @author Lee Boynton
* @author Arthur van Hoff
* @version %I%, %G%
* @see java.lang.Byte
* @see java.lang.Double
* @see java.lang.Float
* @see java.lang.Integer
* @see java.lang.Long
* @see java.lang.Short
* @since JDK1.0
*/
public abstract class Number implements java.io.Serializable
源码的注释部分及类定义,从中可以看出抽象类的性质和继承关系,可以看到,它不仅是六个基础类型包装类的父类,同时也是大整形BigInteger和大浮点型BigDecimal的父类。且由于是抽象类,所有的子类必须重写实现它的所有方法。方法如下:
可以看到,它提供了六种数字基础类型的数值返回的抽象方法 , 所有继承他的子类,必须重写这些方法。
下面的六个包装类,由于实现结构相似,故仅列出具有代表性的两个,双精度浮点型和整型。
需要指出的是,他们都实现了Comparable接口,是可比较对象(数字类很好理解)
Integer
构造方法,常用方法以及常量如下:
构造方法
构造函数是初始化对象的入口,Integer类提供了两种构造方法,实际是规定了两种给对象赋值的方式,一种是直接传入对应基本类型的值,另一种是通过传入数值字符串来赋值。(注意:传入的字符串若不是数值字符串将会报错NumberFormatException)
常用方法
常用方法分为两中:静态方法和成员方法。
静态方法是伴随着类一同加载的,可通过类直接调用而不需要对象,扮演着工具方法的角色。例如其中的parseInt方法作用就是分解字符串为整数。调看源码不难发现,构造函数中利用字符串赋值,调用的就是这个方法。
需要注意的方法是valueOf方法,这个方法是利用传入的数据生成Integer对象,也是Integer对象更变值的方式,同时也表明为Integer类型赋值的几种方式。
最后,所有的静态方法放在这里。剩下三个整数类的静态方法与之类似。
成员方法在类加载时不会被加载,而是在实例化对象时才会产生的专属于对象的方法。因此,这些方法需要通过对象来调用。常用的方法除去实现父类Number的返回值方法外,还有:
- 实现Comparable接口compareTo方法
- 重写equals方法
- 重写toString方法
常量
在Integer类中共定义了四个静态私有常量,分别为:
- 最大值MAX_VALUE
- 最小值MIN_VALUE
- 二进制数值长度SIZE
- 整型类存放TYPE
其余三个整形类与之常量相同
接下来看浮点类型代表
Double
构造方法
同整型一样,构造方法提供了两种初始化对象的方式:直接传入浮点类型值,传入对应的数值字符串
(注:同整型一样,传入的字符串需要时数值字符串,否则会报错NumberFormatException)
常用方法
double类型的常用方法同Integer差不多。
在静态方法中,值得注意的是常用方法增加了比较方法compare()
这个方法的主要作用在于比较两个浮点型,但是浮点类型存在精度问题和未定式问题,所以在实际比较两个浮点类型数据时,需要用到这个类静态方法进行处理。
compare方法代码:
/**
* Compares the two specified <code>double</code> values. The sign
* of the integer value returned is the same as that of the
* integer that would be returned by the call:
* <pre>
* new Double(d1).compareTo(new Double(d2))
* </pre>
*
* @param d1 the first <code>double</code> to compare
* @param d2 the second <code>double</code> to compare
* @return the value <code>0</code> if <code>d1</code> is
* numerically equal to <code>d2</code>; a value less than
* <code>0</code> if <code>d1</code> is numerically less than
* <code>d2</code>; and a value greater than <code>0</code>
* if <code>d1</code> is numerically greater than
* <code>d2</code>.
* @since 1.4
*/
public static int compare(double d1, double d2) {
if (d1 < d2)
return -1; // Neither val is NaN, thisVal is smaller
if (d1 > d2)
return 1; // Neither val is NaN, thisVal is larger
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // Values are equal
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
}
剩余静态方法:
成员方法中,相较于整型,多出了两个专属于浮点类型的判断未定式和无穷的方法:
- isInfinite()
- isNaN()
其余方法与整型类似:
常量
由于浮点型相较于整型更为复杂,需要定义的常量也相对较多。
相较于整型,多出了对无穷、指数的极值、未定式等的定义
非数字类
非数字类仅包含两个类:字符类以及布尔类
Character
构造方法
字符类仅提供一种构造方法,传入字符参数构造。
/**
* Constructs a newly allocated <code>Character</code> object that
* represents the specified <code>char</code> value.
*
* @param value the value to be represented by the
* <code>Character</code> object.
*/
public Character(char value) {
this.value = value;
}
常用方法
对于字符类的常用方法,多是当做工具使用的静态方法,对象独有的成员方法,很少,仅有:
可以发现它重写了hashCode和equals,以及toString方法,实现了Comparable接口的compareTo方法。除此以外,还向外界提供了访问内部字符值的方法。
静态方法非常多,这里常用到的,是转换和判断大小写的方法,以及一些字符类型同其他类型的转化。
(笔者经验水平有限,若有偏误,请多指教)
常量
字符类的常量非常多,不同于数字类,字符类的常量主要是不同标准下的编码,对常量的解释需要对编码规则以及语言特点进行细致了解,这里限于篇幅和笔者水平,就不展开说了。
Boolean
最后一个是布尔类型,这个包装类相较于其余包装类类,是结构最简单的。
构造方法
首先看构造方法,布尔类提供了两种初始化构造方法,提供布尔类型或字符串类型
/**
* Allocates a <code>Boolean</code> object representing the
* <code>value</code> argument.
*
* <p><b>Note: It is rarely appropriate to use this constructor.
* Unless a <i>new</i> instance is required, the static factory
* {@link #valueOf(boolean)} is generally a better choice. It is
* likely to yield significantly better space and time performance.</b>
*
* @param value the value of the <code>Boolean</code>.
*/
public Boolean(boolean value) {
this.value = value;
}
/**
* Allocates a <code>Boolean</code> object representing the value
* <code>true</code> if the string argument is not <code>null</code>
* and is equal, ignoring case, to the string {@code "true"}.
* Otherwise, allocate a <code>Boolean</code> object representing the
* value <code>false</code>. Examples:<p>
* {@code new Boolean("True")} produces a <tt>Boolean</tt> object
* that represents <tt>true</tt>.<br>
* {@code new Boolean("yes")} produces a <tt>Boolean</tt> object
* that represents <tt>false</tt>.
*
* @param s the string to be converted to a <code>Boolean</code>.
*/
public Boolean(String s) {
this(toBoolean(s));
}
值得注意的是,字符串构造方法调用了一个成员方法toBoolean(),这个方法规定了字符串转布尔类型的规则,让我们来看这个方法的代码:
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
可以看到,这是个静态私有方法,所以这个方法是不允许外界调用的,仅在内部使用。同时由于是静态的,它与类一同加载,同时规定着其它函数的字符串转布尔类型。
通过代码不难看出,字符串转布尔类型是否是真的规则就是判断这个字符串忽略大小写后的结果是否是"true" 这个规则在构造方法的注释中可以看到。
常用方法
首先是静态方法。这里提供的静态工具方法主要在于处理字符串同布尔类型之间的转换。
(笔者认为,布尔类型在java程序中表示逻辑,并不作为数值直接参与运算,所以对布尔类型的主要体现在其出现形式及表示上)
接着是成员方法。重写了Object父类的equals,hashCode以及toString方法。同时实现了Comparable接口给的compareTo方法,并且提供了访问对象布尔值的booleanValue方法
值得注意的是它地compareTo方法。这里是源码:
/**
* Compares this <tt>Boolean</tt> instance with another.
*
* @param b the <tt>Boolean</tt> instance to be compared
* @return zero if this object represents the same boolean value as the
* argument; a positive value if this object represents true
* and the argument represents false; and a negative value if
* this object represents false and the argument represents true
* @throws NullPointerException if the argument is <tt>null</tt>
* @see Comparable
* @since 1.5
*/
public int compareTo(Boolean b) {
return (b.value == value ? 0 : (value ? 1 : -1));
}
可以看出比较规则是真大于假,在实现这个规则时,源码写的非常漂亮。
常量
布尔类型的常量组成非常简单。包含表示真假的常量FALSE和TRUE,以及存放基本布尔类型的常量TYPE
后记
知识点的总结就到这里,笔者还是正在学习的新手,难免有认识上的遗漏或是谬误,所以以上内容若有措辞不当或是错误之处,敬请指教。