JavaのString理解
Javaでは、文字列の特殊なオブジェクトがある---継承することができない、不変、直接割り当てを作成
継承することができない、不変
- 文字列型がマークされ、最終的な変更のキーワードを、私たちは継承できません
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
// ....
}
- スティングクラスに提供最終それが初期化されると値スティングタイプを格納するために、それは変更することができない、文字配列の改変。
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
private final char value[];
public String() {
this.value = "".value;
}
}
創造の文字列
Javaは、最も一般的に使用されるオブジェクト型であるスティング、2通りの方法で作成することができます
- 直接割り当て
、それがより一般的に使用されるので、JVMは、String型のために最適化された、文字列型は、直接、直接参照を容易にするために一定のプールに文字列型を作成するために割り当てることができ
String型の直接割り当てのためのJVM一定のストレージプールを作成する意志がない場合、クエリ文字列定数プールは、参照に戻り、次に、種別に対応する開始します。
// 直接赋值
String s = "llll";
新しいStringオブジェクトキーの作成
ヒープメモリに初期化され、その後、String型の定数プールに対応する負荷されるStringオブジェクトを作成するために、新しいキーワードの違いを。参照するために、ヒープメモリアドレスの値を返します。二つのアプローチの違い
文字列メソッドの紹介
コンストラクタ
- 財団工法
public String() {
this.value = "".value;
}
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
- 文字配列コンストラクタ
// 根据整个字符数组创建字符串
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
//截取部分字符数组创建字符串
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
- Unicodeのエンコーディングに応じて文字列を作成しました
// 没有使用过。。。
public String(int[] codePoints, int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= codePoints.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > codePoints.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
final int end = offset + count;
// Pass 1: Compute precise size of char[]
int n = count;
for (int i = offset; i < end; i++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
continue;
else if (Character.isValidCodePoint(c))
n++;
else throw new IllegalArgumentException(Integer.toString(c));
}
// Pass 2: Allocate and fill in char[]
final char[] v = new char[n];
for (int i = offset, j = 0; i < end; i++, j++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
v[j] = (char)c;
else
Character.toSurrogates(c, v, j++);
}
this.value = v;
}
- バイト配列には、文字列を作成します
// 需要指定编码集,根据传递的编码集解码
public String(byte bytes[], int offset, int length, String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException("charsetName");
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charsetName, bytes, offset, length);
}
public String(byte bytes[], int offset, int length, Charset charset) {
if (charset == null)
throw new NullPointerException("charset");
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charset, bytes, offset, length);
}
// 根据传入的字节数组和其长度创建字符串
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
- StringBufferとStringBuilderのを使用して文字列を作成しますが、一般的に使用されていない、あなたがより効率的に、文字列を作成する方法をToStringメソッドこれらの2つのオブジェクトを使用することができます
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
- 予約方法、デフォルトのアクセスタイプは、直接割り当てて文字列を作成します
// 预留方法,默认访问类型,通过直接赋值创建字符串
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
staticメソッド
- valueOf()
toString()メソッドを呼び出します
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
public static String valueOf(int i) {
return Integer.toString(i);
}
public static String valueOf(long l) {
return Long.toString(l);
}
public static String valueOf(float f) {
return Float.toString(f);
}
public static String valueOf(double d) {
return Double.toString(d);
}
コンストラクタを呼び出します
public static String valueOf(char data[]) {
return new String(data);
}
public static String valueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
public static String copyValueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
public static String copyValueOf(char data[]) {
return new String(data);
}
ブーリアン
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
- フォーマット
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
public static String format(Locale l, String format, Object... args) {
return new Formatter(l).format(format, args).toString();
}
一般的な方法
インターン()メソッドは、
オブジェクトのメソッドを呼び出すことによって、定数プールに向けることができます
public native String intern();
参照元