隨筆-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)  編輯  收藏 所屬分類: 基礎知識

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


          網站導航:
           
          主站蜘蛛池模板: 黄冈市| 邵阳县| 丹凤县| 马边| 嵩明县| 松原市| 亚东县| 柏乡县| 高州市| 沅江市| 彭山县| 甘洛县| 保山市| 绥阳县| 惠东县| 邓州市| 泰和县| 东乡县| 绥棱县| 保定市| 吐鲁番市| 家居| 永昌县| 铜鼓县| 枣强县| 陇南市| 得荣县| 海兴县| 江北区| 澜沧| 池州市| 重庆市| 南开区| 云霄县| 遵义市| 临夏县| 崇信县| 涿州市| 银川市| 乌苏市| 独山县|