标签:详细介绍 value 显示 private bool 方法 内存 object weight
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = char v2[] = anotherString. int i = offset;
int j = anotherString.offset;
while (n-- != 0) { //直接比较的是每个字符
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2); //false,s1+s2是一个新的String的地址的引用
s3.equals((s1 + s2));
System.out.println(s3 == "hello" + "world");//true,拼接起来的是常量池中找到的
s3.equals("hello" + "world");
String a = "ab";
final String bb = "b"; /编译时已经放入常量池
String b = "a" + bb;
System.out.println((a == b)); //result = true
String a = "ab";
final String bb = getBB();
String b = "a" + bb;
System.out.println((a == b)); //result = false
private static String getBB() { return "b"; }
[源码]String StringBuffer StringBudlider(1String部分)
标签:详细介绍 value 显示 private bool 方法 内存 object weight
原文地址:http://www.cnblogs.com/zongjihengfei/p/5995385.html