字符串類為JAVA中的特殊類
阿新 • • 發佈:2018-08-04
mon main pan clas [] pri fin 找到 print
字符串類為JAVA中的特殊類,String中為final類,一個字符串的值不可重復。因此在JAVA VM(虛擬機)中有一個字符串池,專門用來存儲字符串。如果遇到String a=”hello”時(註意沒有NEW,不是創建新串),系統在字符串池中尋找是否有”hello”,此時字符串池中沒有”hello”,那麽系統將此字符串存到字符串池中,然後將”hello”在字符串池中的地址返回a。如果系統再遇到String b=”hello”,此時系統可以在字符串池中找到 “hello”。則會把地址返回b,此時a與b為相同。
String a=”hello”;
System.out.println(a==”hello”);
系統的返回值為true。
1 package TomText; 2 3 public class TomText_36 { 4 private int day; 5 private int month; 6 private int year; 7 public TomText_36(int d,int m,int y){ 8 day=d; 9 month=m; 10 year=y; 11 } 12 public void setDate(int d,int m,inty){ 13 day=d; 14 month=m; 15 year=y; 16 } 17 public void printDate( ){ 18 System.out.println("今天是"+year+"年"+month+"月"+day+"日"); 19 } 20 21 public static void main(String[] args){ 22 TomText_36 t=new TomText_36(3,4,2004); 23 t.printDate();24 t.setDate(4, 5, 2018); 25 t.printDate(); 26 27 } 28 29 }
字符串類為JAVA中的特殊類