隨筆-9  評論-0  文章-0  trackbacks-0
          代碼如下:
          public class StringTest    
          {   
              
          static final String s1 = "H";   
              
          static final String s2 = "ello";   
              
          static final String s3 = "Hello";   
                 
              
          static String ss1 = "H";   
              
          static String ss2 = "ello";   
              
          static String ss3 = "Hello";   
                 
              
          static final String s11;   
              
          static final String s22;   
                 
              
          static  
              
          {   
                  s11 
          = "H";   
                  s22 
          = "ello";   
              }
             
                 
              
          public static void main(String[] args)   
              
          {   
                  
          //1   
                  String str1 = "H";   
                  String str2 
          = "ello";   
                  String str3 
          = "Hello";   
                  String str4 
          = str1 + str2;   
                  String str5 
          = "H" + "ello";   
                     
                  System.out.println(str4 
          == str3);   
                  System.out.println(str5 
          == str3);   
                     
                  
          //2   
                  String s4 = s1 + s2;   
                     
                  System.out.println(s4 
          == s3);   
                  System.out.println(s4 
          == str3);   
                     
                  
          //3   
                  String ss4 = ss1 + ss2;   
                  System.out.println(ss4 
          == s3);   
                  System.out.println(ss4 
          == ss3);   
                  System.out.println(ss4 
          == str3);   
                     
                  
          //4   
                  String s33 = s11 + s22;   
                  System.out.println(s33 
          == s3);   
                  System.out.println(s33 
          == ss3);   
                  System.out.println(s33 
          == str3);   
              }
             
          }
            

          輸出結果:
           1false  
           2true  
           3true  
           4true  
           5false  
           6false  
           7false  
           8false  
           9false  
          10false 

          static final String的定義使其成為編譯時常量,所以String s4 = s1 + s2;  實際是String s4 = “H” + “ello”,也就是“Hello”,3,4輸出為true。而s11,s22雖然是static final的,但是是在static塊中初始化,編譯時不會成為常量。
          posted on 2009-11-14 13:28 AndyFish 閱讀(101) 評論(0)  編輯  收藏 所屬分類: 基礎知識

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 秦皇岛市| 马边| 苗栗县| 宁强县| 赤城县| 遂昌县| 本溪| 通州市| 婺源县| 贵德县| 台江县| 吉林省| 高淳县| 开化县| 汉沽区| 社会| 宝坻区| 泽库县| 张掖市| 拉萨市| 通江县| 奉节县| 监利县| 苏尼特右旗| 宽城| 富顺县| 诏安县| 涿州市| 鹤山市| 汕头市| 大同市| 田林县| 乾安县| 平昌县| 榆树市| 沾化县| 北碚区| 夏河县| 元朗区| 清丰县| 长宁县|