1 Object
Object类比较重要,代码比较少,这里直接全部粘出来了。
public class Object {
private static native void registerNatives();
static {
registerNatives();
}
// 返回此Object运行时的类
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object obj) {
return (this == obj);
}
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
timeout++;
}
wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}
protected void finalize() throws Throwable { }
总结:
- registerNatives()方法是为了让JVM找到您的本机函数,就是和本地方法相关(这里没去详细看…)
- getClass()方法是获取运行时此Object的类,能够获取到当前对象的信息。
- hashCode()使用与支持与hash相关的集合类,不同对象可以有同一个hash码,但是为了提高性能最好重复较少
- equals() 方法是为了比较对象,从Object类中看,他最开始也是比较的对象引用。其他类要比较内容则需要重写该方法。该方法主要用于比较和排序。
- clone()用于拷贝一个对象,但是要注意由于clone()方法是protected修饰的,因此需要实现Cloneable接口才能调用,同时需要覆写clone()方法才能调用。同时该方法存在浅拷贝和深拷贝问题探讨,参看关于Java的Object.clone()方法与对象的深浅拷贝
- toString()方法用于打印类信息,默认为ClassName@hashCode(),System.out.print一个对象时会自动调用该方法。
- notify()、notifyAll()、wait()等方法和线程相关,notify唤醒等待该对象的单个线程,notifyAll唤醒所有线程,wait方法就是使当前线程等待该对象的锁。
- finalize()该方法用于释放资源。它的工作原理是:一旦垃圾回收器准备好释放对象占用的存储空间,将首先调用其finalize()方法。并且在下一次垃圾回收动作发生时,才会真正回收对象占用的内存。
其他链接Java常用类(一)之Object类详解
2 String
- 类接口信息
public final class String implements java.io.Serializable, Comparable<String>, CharSequence{}
String类是final类型的,并且实现了上述3个接口。 - 底层数据结构 String类内部维护的是一个char[] value数组
private final char value[];
,同时为了提高效率维护了一个哈希缓存private int hash;
- 构造器 String类构造方法很多有能接收,字符数组,字节数组,StringBuffer,StringBuilder等构造参数。后面很多方法比如subString()一类的方法大多通过new String()的方法构造一个新的String类型。常用构造函数如:
public String() // 空构造
public String(byte[] bytes) // 把字节数组转成字符串
public String(byte[] bytes,int index,int length) // 把从某索引开始的一定长度的字节数组转成字符串
public String(char[] value) // 把字符数组转成字符串
public String(char[] value,int index,int count) // 把字符数组的一部分转成字符串
public String(String original) // 把字符串常量值转成字符串
- String类重写了equals方法,首先比地址,如果相同则直接返回,其次检测anObject是不是一个String类型,在检查两个长度是否相等,最后才用while循环挨个比较他们的value字段中的字符。
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
- hashCode产生方法是长度为零则hash = 0, 否则迭代计算
hash = 31*hash + value[i]
,返回最终结果。
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
- 搜索和匹配方法 主要用maches(正则式匹配)、indexOf、lastIndexOf、charAt、startWith、endWith、contains。
- 字符串操作 subString、concat、replace、replaceAll、split、toLowerCase、toUpperCase、trim。
- format是static方法,用于构造一个格式化的字符串。
- valueOf系列重载方法用于放回对应类型的字符串表示。
- getbytes用于将字符串转化成字节数组,可以指定编码方式。
- intern方法