String类 and StringBuffer类

1.equals()

==:

  a)对于原生数据类型来说,比较的是左右两边的值是否相等。

  b)对于引用类型来说,比较左右两边的引用是否指向同一个对象,或者说左右两边的引用地址是否相同。

equals()方法,该方法定义在Object类当中,因此Java中的每个类都具有该方法,对于Object类的equals()方法来说,它是判断调用equals()方法的引用与传进来的引用是否一

致,即这两个引用是否指向的是同一个对象。对于Object类的 equals()方法来、说,它等价于==。

对于 String 类的 equals()方法来说,它是判断当前字符串与传进来的字符串的内容是否一致。对于 String 对象的相等性判断来说,请使用 equals()方法,而不要使用==。

String 是常量,其对象一旦创建完毕就无法改变。

当使用+拼接字符串时,会生成新的 String 对象,而不是向原有的 String 对象追加内容。

String s = aaa;(采用字面值方式赋值)

  查找 String Pool 中是否存在“aaa”这个对象,如果不存在,则在 String Pool 中创建一个aaa对象,然后将 String Pool中的这个“aaa”对象的地址返回来,赋给引用变量 s,

这样s 会指String Pool 中的这个aaa字符串对象如果存在,则不创建任何对象,直接将 String Pool 中的这个aaa对象地址返回来,赋给 s 引用。

String s = new String(“aaa”);

  a)首先在 String Pool 中查找有没有“aaa”这个字符串对象,如果有,则不在 String Pool中再去创建aaa这个对象了,直接在堆中(heap)中创建一个aaa字符串对象,然后

将堆中的这个“aaa”对象的地址返回来,赋给 s 引用,导致 s 指向了堆中创建的这个aaa字符串对象。

  b)如果没有,则首先在 String Pool 中创建一个“aaa“对象,然后再在堆中(heap)创建一个aaa对象,然后将堆中的这个aaa对象的地址返回来,赋给 s 引用,导致 s 指向了

堆中所创建的这个”aaa“对象。

public class ObjectTest2
{
    public static void main(String[] args)
    {
        Object object = new Object();
        Object object2 = new Object();

        System.out.println(object == object2);

        System.out.println("----------------");


        String str = new String("aaa");
        String str2 = new String("aaa");

        System.out.println(str == str2);

        System.out.println("----------------");

        String str3 = "bbb";
        String str4 = "bbb";

        System.out.println(str3 == str4);
        
        System.out.println("----------------");

        String str5 = new String("ccc");
        String str6 = "ccc";

        System.out.println(str5 == str6);

        System.out.println("----------------");

        String s = "hello";
        String s1 = "hel";
        String s2 = "lo";

        System.out.println(s == s1 + s2);

        System.out.println("----------------");

        System.out.println(s == "hel" + "lo");


    }
}
结果为:
false ---------------- false ---------------- true ---------------- false ---------------- false ---------------- true

 2.StringBuffer

   StringBuffer可以创建一个动态的String,而不是一个常量。

   

 1 package com.li;
 2 
 3 public class StringBufferTest
 4 {
 5     public static void main(String[] args)
 6     {
 7         StringBuffer buffer = new StringBuffer();
 8 
 9         buffer.append("hello").append(" world").append(" welcome").append(100).append(false);        
10         String result = buffer.toString();
11         
12         System.out.println(result);
13         
14         System.out.println("----------------------------------------");
15         
16         buffer = buffer.append("hello");
17         buffer.append(" world");
18         buffer.append(" welcome");
19         String result1 = buffer.toString();
20         
21         System.out.println(result1);
22     }
23 }
hello world welcome100false
----------------------------------------
hello world welcome100falsehello world welcome
原文地址:https://www.cnblogs.com/daneres/p/4445124.html