differet way to creat String
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