字符串类为JAVA中的特殊类

字符串类为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,int y){
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 }

猜你喜欢

转载自www.cnblogs.com/borter/p/9419328.html