创建String的不同方法

differet way to creat String

1,use new operator:
String s=new String("Hello");
2,a short one:
String s="Hello";
两种方法当用于字符串时有相同的结果,但不用NEW关键字会创建JAVA字符串池中指向同一个字符串的指针。
字符串池是JAVA存储资源的一种方法。
以下程序用于检验:
public class DifferentWayToCreatString
{
   public static void main(String args[])
   {
       String s = "Hello";
       String s2 = "Hello";
       if (s == s2)
       {
           System.out.println("Equal without new operator");
       }
       String t = new String("Hello");
       String u = new String("Hello");
       if (t == u)
       {
           System.out.println("Equal with new operator");
       }
   }
}
//输出结果Equal without new operator
 
发布了26 篇原创文章 · 获赞 1 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章