String 啊 String ,让我说你什么好呢?你为我们 Java 程序员带来所有的困扰还不够吗?  看看 String 这一次又怎么闹事儿吧  1. 回顾一下坏脾气的 String 老弟  例程1:class Str {      public static void main(String[] args) {          String s = "Hi!";          String t = "Hi!";          if (s == t)              System.out.println("equals");          else               System.out.println("not equals");      }  }  程序输出什么呢?  如果看客们看看我的《来自 String 的困惑》之一相信你很快会做出正确的判断:程序输出:equals  2. 哦,天哪,它又在搅混水了  例程2:class Str {      public static void main(String[] args) {          String s = "HELLO";          String t = s.toUpperCase();          if (s == t)              System.out.println("equals");          else              System.out.println("not equals");      }  }  那么这个程序有输出什么呢?  慎重!再慎重!不要被 String 这个迷乱的家伙所迷惑!  它输出:equalsWHY!!!  把程序简单的更改一下:  class Str2 {      public static void main(String[] args) {          String s = "Hello";          String t = s.toUpperCase();          if (s == t)              System.out.println("equals");          else              System.out.println("not equals");      }  }  你可能会说:不是一样吗?不!千真万确的,不一样!这一次输出:  not equalsOh MyGOD!!!  谁来教训一下这个 String 啊!  3. 你了解你的马吗?  “要驯服脱缰的野马,就要了解它的秉性”牛仔们说道。  你 了解 String 吗?解读 String 的 API ,可以看到:toUpperCase() 和 toLowerCase() 方法返回一个新的String对象,它将原字符串表示字符串的大写或小写形势;但是要注意:如果原字符串本身就是大写形式或小写形式,那么返回原始对象。 这就是为什么第二个程序中 s 和 t 纠缠不清的缘故对待这个淘气的、屡教不改的 String ,似乎没有更好的办法了让我们解剖它,看看它到底有什么结构吧:  (1) charAt(int n) 返回字符串内n位置的字符,第一个字符位置为0,最后一个字符的位置为length()-1,访问错误的位置会扔出一块大砖头:StringIndexOutOfBoundsException 真够大的  (2) concat(String str) 在原对象之后连接一个 str ,但是返回一个新的 String 对象  (3) EqualsIgnoreCase(String str) 忽略大小写的 equals 方法这个方法的实质是首先调用静态字符方法toUpperCase() 或者 toLowerCase() 将对比的两个字符转换,然后进行 == 运算  (4) trim() 返回一个新的对象,它将原对象的开头和结尾的空白字符切掉同样的,如果结果与原对象没有差别,则返回原对象  (5) toString() String 类也有 toString() 方法吗?真是一个有趣的问题,可是如果没有它,你的 String 对象说不定真的不能用在System.out.println() 里面啊小心,它返回对象自己String 类还有很多其他方法,掌握他们会带来很多方便也会有很多困惑,所以坚持原则,是最关键的  4.我想买一匹更好的马来购买更驯服温和的 String 的小弟 StringBuffer 吧  这时候会有人反对:  它很好用,它效率很高,它怎么能够是小弟呢?  很 简单,它的交互功能要比 String 少,如果你要编辑字符串它并不方便,你会对它失望但这不意味着它不强大public final class String implements Serializable, Comparable, CharSequencepublic final class StringBuffer implements Serializable, CharSequence很明显的,小弟少了一些东东,不过这不会干扰它的前途StringBuffer 不是由 String 继承来的不过要注意兄弟它也是 final 啊,本是同根生看看他的方法吧,这么多稳定可靠的方法,用起来比顽皮的 String 要有效率的多? Java 为需要改变的字符串对象提供了独立的 StringBuffer 类它的实例不可变(final),之所以要把他们分开是因为,字符串的修改要求系统的开销量增大,占用更多的空间也更复杂,相信当有10000人挤在一个 狭小的游泳池里游泳而岸边又有10000人等待进入游泳池而焦急上火又有10000人在旁边看热闹的时候,你这个 String 游泳池的管理员也会焦头烂额在你无需改变字符串的情况下,简单的 String 类就足够你使唤的了,而当要频繁的更改字符串的内容的时候,就要借助于宰相肚里能撑船的StringBuffer 了  5. 宰相肚里能撑船  (1) length() 与 capacity()String 中的 length() 返回字符串的长度兄弟 StringBuffer 也是如此,他们都由对象包含的字符长度决定capacity()呢?  public class TestCapacity {      public static void main(String[] args){       StringBuffer buf = new StringBuffer("it was the age of wisdom,");         System.out.println("buf = " + buf);          System.out.println("buf.length() = " + buf.length());          System.out.println("buf.capacity() = " + buf.capacity());          String str = buf.toString();          System.out.println("str = " + str);          System.out.println("str.length() = " + str.length());          buf.append(" " + str.substring(0,18)).append("foolishness,");          System.out.println("buf = " + buf);          System.out.println("buf.length() = " + buf.length());          System.out.println("buf.capacity() = " + buf.capacity());         System.out.println("str = " + str);     }  }  程序输出:  buf = it was the age of wisdom.buf.length() = 25  buf.capacity() = 41  str = it was the age of wisdomstr.length() = 25  buf = it was the age of wisdom, it was the age of foolishness,  buf.length() = 56  buf.capacity() = 84  str = it was the age of wisdom,  可 以看到,在内容更改之后,capacity也随之改变了长度随着向字符串添加字符而增加而容量只是在新的长度超过了现在的容量之后才增加 StringBuffer 的容量在操作系统需要的时候是自动改变的程序员们对capacity所能够做的仅仅是可以在初始化 StringBuffer对象的时候。