深入學習java原始碼之StringBuilder.indexOf()與StringBuilder.reverse()
阿新 • • 發佈:2019-01-13
深入學習java原始碼之StringBuilder.indexOf()與StringBuilder.reverse()
java的toString()方法
在Java中每個類都預設繼承Object類,除非宣告繼承某個類。而Object類中有一個叫做toString的方法。該方法返回的是該Java物件的記憶體地址經過雜湊演算法得出的int型別的值在轉換成十六進位制。這個輸出的結果可以等同的看作Java物件在堆中的記憶體地址。
我們知道,在輸出一個類的時候,如果沒有重寫父類的toString方法的話,列印的將會是[email protected],即
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
所以只有子類重寫父類的toString()方法之後,才會列印你想輸出的資訊。
System.out.println的原始碼:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
String.valueOf(),
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
Modifier and Type | Method and Description |
---|---|
int |
capacity() 返回當前容量。 |
char |
charAt(int index) 返回 |
void |
ensureCapacity(int minimumCapacity) 確保容量至少等於規定的最小值。 |
void |
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 字元從該序列複製到目標字元陣列 |
int |
indexOf(String str) 返回指定子字串第一次出現的字串內的索引。 |
int |
indexOf(String str, int fromIndex) 返回指定子串的第一次出現的字串中的索引,從指定的索引開始。 |
int |
lastIndexOf(String str) 返回指定子字串最右邊出現的字串內的索引。 |
int |
lastIndexOf(String str, int fromIndex) 返回指定子字串最後一次出現的字串中的索引。 |
int |
length() 返回長度(字元數)。 |
int |
offsetByCodePoints(int index, int codePointOffset) 返回此序列中與 |
StringBuilder |
reverse() 導致該字元序列被序列的相反代替。 |
void |
setCharAt(int index, char ch) 指定索引處的字元設定為 |
void |
setLength(int newLength) 設定字元序列的長度。 |
CharSequence |
subSequence(int start, int end) 返回一個新的字元序列,該序列是該序列的子序列。 |
String |
substring(int start) 返回一個新的 |
String |
substring(int start, int end) 返回一個新的 |
String |
toString() 返回表示此順序中的資料的字串。 |
void |
trimToSize() 嘗試減少用於字元序列的儲存。 |
java原始碼
package java.lang;
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
static final long serialVersionUID = 4383685877147921099L;
public StringBuilder() {
super(16);
}
public StringBuilder(int capacity) {
super(capacity);
}
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
@Override
public int indexOf(String str) {
return super.indexOf(str);
}
@Override
public int indexOf(String str, int fromIndex) {
return super.indexOf(str, fromIndex);
}
@Override
public int lastIndexOf(String str) {
return super.lastIndexOf(str);
}
@Override
public int lastIndexOf(String str, int fromIndex) {
return super.lastIndexOf(str, fromIndex);
}
@Override
public StringBuilder reverse() {
super.reverse();
return this;
}
@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
s.defaultWriteObject();
s.writeInt(count);
s.writeObject(value);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
value = (char[]) s.readObject();
}
}
package java.lang;
import sun.misc.FloatingDecimal;
import java.util.Arrays;
abstract class AbstractStringBuilder implements Appendable, CharSequence {
char[] value;
int count;
AbstractStringBuilder() {
}
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
@Override
public int length() {
return count;
}
public int capacity() {
return value.length;
}
private int newCapacity(int minCapacity) {
// overflow-conscious code
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
? hugeCapacity(minCapacity)
: newCapacity;
}
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0) {
value = Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
{
if (srcBegin < 0)
throw new StringIndexOutOfBoundsException(srcBegin);
if ((srcEnd < 0) || (srcEnd > count))
throw new StringIndexOutOfBoundsException(srcEnd);
if (srcBegin > srcEnd)
throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
public int indexOf(String str) {
return indexOf(str, 0);
}
public int indexOf(String str, int fromIndex) {
return String.indexOf(value, 0, count, str, fromIndex);
}
public int lastIndexOf(String str) {
return lastIndexOf(str, count);
}
public int lastIndexOf(String str, int fromIndex) {
return String.lastIndexOf(value, 0, count, str, fromIndex);
}
public AbstractStringBuilder reverse() {
boolean hasSurrogates = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
char cj = value[j];
char ck = value[k];
value[j] = ck;
value[k] = cj;
if (Character.isSurrogate(cj) ||
Character.isSurrogate(ck)) {
hasSurrogates = true;
}
}
if (hasSurrogates) {
reverseAllValidSurrogatePairs();
}
return this;
}
private void reverseAllValidSurrogatePairs() {
for (int i = 0; i < count - 1; i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
char c1 = value[i + 1];
if (Character.isHighSurrogate(c1)) {
value[i++] = c1;
value[i] = c2;
}
}
}
}
public String substring(int start) {
return substring(start, count);
}
@Override
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}
public String substring(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
throw new StringIndexOutOfBoundsException(end);
if (start > end)
throw new StringIndexOutOfBoundsException(end - start);
return new String(value, start, end - start);
}
@Override
public abstract String toString();
final char[] getValue() {
return value;
}
}