java-String(二)

来源:互联网 发布:二手手机平台 知乎 编辑:程序博客网 时间:2024/06/08 12:15
package demo;/** * String容易错的地方总结 * 1.String类是final修饰的,String类对象称为不可变字符串. * 不能修改其中任意一个字符,我们一般是修改的变量,让他引用另外一个字符串.method_1(); *  * 2.String是对象,真实值是存放在堆内存里面的。自己看到的都只是一个引用. *   intern()方法 *   public String intern();返回字符串对象的规范化表现形式. *   一个初始化时为空的字符串池,他由String私有的维护 *   当调用intern方法时,如果池已经包含一个等于 此String对象的字符串(该对象由equals(object)方法确定) *   则返回池中的字符串,否则,将此String对象添加到池中,并且返回此String对象的引用. *   method_2(); *    * 3.字符串的常量池是共享的,位于方法区中. *   字符串==的比较只是引用的比较,不是内容的比较 *   method_3(); *    * 4.String到底创建了几个对象 *    String str = new String("Hello") *    String创建了两个对象,一个存在堆中,一个存在字符常量池中 *    如果字符常量池中有hello那么就只会在堆中产生一个对象 *  *  *    *  **/public class StringDemo2 {public static void method_1(){String s = "abc";System.out.println("s:" + s);  // 输出s:abcs = "def";System.out.println("s:" + s);  // 输出s:def   /*此时,初看上去,输出的结果变了,发现s的值发生了变化,* 那么这与上面的说法——String类是不可变类是否矛盾呢?答案是否定的,* 因为s只是指向堆内存中的引用,存储的是对象在堆中的地址,而非对象本身,* s本身存储在栈内存中。实际上,* 此时堆内存中依然存在着"abc"和"def"对象。对于"abc"对象本身而言,* 对象的状态是没有发生任何变化的(对象的状态是指对象的属性,包括属性的类型及属性值)。*/}public static void method_2(){    String s = "2";      System.out.println("aa" == "ab");                               //false      System.out.println("aa" == "aa");                               //true  因为都是指向堆内存的同一地址,也就是说堆数据区就那一个      System.out.println("aa" == new String("aa"));                   //false 这又new一个出来,地址就不一样啦,==比的不是值,而是地址      System.out.println(new String("aa").equals(new String("aa")));  //true  这个比的才是值,所以在写一些实体对象如Person的时候若                                                                      //      要比较是不是同一个人,就得重写hashCode()和equal()      System.out.println(new String("aa") == new String("aa"));       //false  new 出来的很明显是2个对象,地址当然不一样。      System.out.println(new String("aa").intern() == new String("aa").intern());//true 这就参照这个方法的说明      System.out.println(s.lastIndexOf("x")); //-1  }/*字符常量池验证*/public static void method_3(){ String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo"; String s4 = "Hel" + new String("lo"); String s5 = new String("Hello"); String s6 = s5.intern(); String s7 = "H"; String s8 = "ello"; String s9 = s7 + s8;            System.out.println(s1 == s2);  // true System.out.println(s1 == s3);  // true System.out.println(s1 == s4);  // false System.out.println(s1 == s9);  // false System.out.println(s4 == s5);  // false System.out.println(s1 == s6);  // true}public static void method_4(){   String a = "ab";// 创建了一个对象,并加入字符串池中      System.out.println("String a = 'ab';");      String b = "cd";// 创建了一个对象,并加入字符串池中      System.out.println("String b = 'cd';");      String c = "abcd";// 创建了一个对象,并加入字符串池中      System.out.println("String c = 'abcd'");       String d = "ab" + "cd";      // 如果d和c指向了同一个对象,则说明d也被加入了字符串池      if (d == c) {          System.out.println("'ab'+'cd' 创建的对象 '加入了' 字符串池中");      }      // 如果d和c没有指向了同一个对象,则说明d没有被加入字符串池      else {          System.out.println("'ab'+'cd' 创建的对象 '没加入' 字符串池中");      }         String e = a + "cd";      // 如果e和c指向了同一个对象,则说明e也被加入了字符串池      if (e == c) {          System.out.println(" a  +'cd' 创建的对象 '加入了' 字符串池中");      }      // 如果e和c没有指向了同一个对象,则说明e没有被加入字符串池      else {          System.out.println(" a  +'cd' 创建的对象 '没加入' 字符串池中");          }           String f = "ab" + b;      // 如果f和c指向了同一个对象,则说明f也被加入了字符串池      if (f == c) {          System.out.println("'ab'+ b   创建的对象 '加入了' 字符串池中");      }      // 如果f和c没有指向了同一个对象,则说明f没有被加入字符串池      else {          System.out.println("'ab'+ b   创建的对象 '没加入' 字符串池中");          }           String g = a + b;          // 如果g和c指向了同一个对象,则说明g也被加入了字符串池      if (g == c) {          System.out.println(" a + b 创建的对象 '加入了' 字符串池中");      }      // 如果g和c没有指向了同一个对象,则说明g没有被加入字符串池      else {          System.out.println(" a + b创建的对象 '没加入' 字符串池中");      }       String a1 = new String(new char[] { 'a', 'b', 'c', 'd' });      String b1 = a1.intern();      if (b1 == a1) {          System.out.println("b1被加入了字符串池中,没有新建对象");      } else {          System.out.println("b1没被加入字符串池中,新建了对象");      }    String h = a+b;   if (h == c) {          System.out.println("h创建的对象 '加入了' 字符串池中");      }      // 如果d和c没有指向了同一个对象,则说明d没有被加入字符串池      else {          System.out.println("h创建的对象 '没加入' 字符串池中");      } }   public static void main(String[] args){  method_1();  method_2();      method_3();      method_4();   }}/*public String(String original) {    int size = original.count;    char[] originalValue = original.value;    char[] v;      if (originalValue.length > size) {         // The array representing the String is bigger than the new         // String itself.  Perhaps this constructor is being called         // in order to trim the baggage, so make a copy of the array.            int off = original.offset;            v = Arrays.copyOfRange(originalValue, off, off+size);     } else {         // The array representing the String is the same         // size as the String, so no point in making a copy.        v = originalValue;     }    this.offset = 0;    this.count = size;    this.value = v;    }  */

原创粉丝点击