Object

  • object 作为超类,是一切类的父类

方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public final native Class<?> getClass()

// o1 == o2
public boolean equals(Object obj)

//用来另存一个当前存在的对象
protected native Object clone() throws CloneNotSupportedException

//让当前线程失去操作权限,进入等待序列
public final void wait(long timeout, int nanos) throws InterruptedException

public String toString()

//随机通知一个持有对象的锁的线程获取操作权限
public final native void notify();

public final native void notifyAll()

//垃圾回收时调用
protected void finalize() throws Throwable

String

1
2
3
4
5
6
7
8
9
10
public final class String implements java.io.Serializable, Comparable<String>, CharSequence
/*
CharSequence是一个只读的字符序列接口,包括:
int length();
char charAt(int index);
CharSequence subSequence(int start, int end);
public String toString();
...
StringBuffer和StringBuilder也实现了这个接口
*/
  • String不是java的基本类型

  • String是const,不能改变:
    private final char value[];

构造器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//常用
public String()
public String(String original)
public String(char value[])
public String(char value[], int offset, int count)
public String(StringBuffer buffer)
public String(StringBuilder builder)
//不常用
public String(int[] codePoints, int offset, int count)
public String(byte ascii[], int hibyte, int offset, int count)
public String(byte ascii[], int hibyte)
public String(byte bytes[], int offset, int length, String charsetName)
public String(byte bytes[], String charsetName)
public String(byte bytes[], Charset charset)
public String(byte bytes[], int offset, int length)
public String(byte bytes[])
String(char[] value, boolean share)

方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
与StringBuffer, StringBuilder作比较时使用
传入参数是一个CharSequence,StringBuffer, StringBuilder均实现了CharSequence接口
*/

public boolean contentEquals(CharSequence cs) {
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// Argument is a String
if (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequence
char v1[] = value;
int n = v1.length;
if (n != cs.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != cs.charAt(i)) {
return false;
}
}
return true;
}
1
2
3
public String concat(String str)  //将str拼接到字符串后