Java字串面試(二)

來源:互聯網
上載者:User

標籤:chm   mon   append   details   存在   start   第一個   cli   end   

先看下面2個程式

 

[java] view plain copy
  1. public static void main(String[] args) {  
  2.         String a = "a1";    
  3.         String b = "a" + 1;    
  4.         System.out.println(a == b);   
  5.     }  
[java] view plain copy
  1. public static void main(String[] args) {  
  2.     String a = "a1";    
  3.     String b = "1";    
  4.     String c = "a" + b;  
  5.     System.out.println(a == c);   
  6. }  

第一個程式輸出是true,由於“a” 和1算是字串常量,所以在編譯期b的值就確定了,在運行期不會產生StringBuilder對象,所以在運行期,由於“a1“已經在String Pool中存在, 所以對象”a1“的引用同時指向a和b。

 

第二個程式中輸出是false。因為在運行期才能確定"a"+b的值,所以為了提高效率,在運行期會產生一個StringBuilder對象,對它調用append方法,最後調用toString()方法,返回一個String對象的引用。

 

下面開始說幾個面試題:

1.

 

[java] view plain copy
  1. public static void main(String[] args) {  
  2.         //列印true,原因見上面分析  
  3.         String a = "a1";    
  4.         String b = "a" + 1;    
  5.         System.out.println(a == b);  
  6.     }  

 

2

 

[java] view plain copy
  1. public static void main(String[] args) {  
  2.         //列印false,原因見上面分析  
  3.         String a = "ab";    
  4.         String bb = "b";    
  5.         String b = "a" + bb;    
  6.         System.out.println(a == b);    
  7.     }  

3.

 

 

[java] view plain copy
  1. public static void main(String[] args) {  
  2.     //列印true,final說明bb始終指向”b“,不能把其他對象的引用給bb,所以,在編譯期,bb的值是確定的,即  
  3.     //"a" + bb的值也是確定的,所以和上面1同理  
  4.     String a = "ab";    
  5.     final String bb = "b";   
  6.     String b = "a" + bb;    
  7.     System.out.println(a == b);    
  8. }  

4

 

 

[java] view plain copy
  1. public static void main(String[] args) {  
  2.     //列印false, 簡言之,就是編譯期不能確定,在運行期才能確定,因此會產生StringBulder對象,通過toSring()返回一個String  
  3.     //的引用,肯定他a和b的記憶體位址是不同的。  
  4.     String a = "ab";    
  5.     final String bb = getBB();    
  6.     String b = "a" + bb;    
  7.     System.out.println(a == b);    
  8. }    
  9.   
  10. private static String getBB() {    
  11.     return "b";    
  12. }  

5

 

 

[java] view plain copy
  1. public class Test {  
  2.     private static String a = "ab";    
  3.       
  4.     public static void main(String[] args) {   
  5.         //列印false,true, static資料放在方法區中,其他和不是static的變臉一樣  
  6.         String s1 = "a";    
  7.         String s2 = "b";    
  8.         String s = s1 + s2;    
  9.         System.out.println(s == a);    
  10.         System.out.println(s.intern() == a);    
  11.     }   
  12. }  

 

分析圖如下:

 

6

 

[java] view plain copy
  1. public class Test {  
  2.     private static String a = new String("ab");      
  3.       
  4.     public static void main(String[] args) {  
  5.         //列印false,false,true。分析看後面的分析圖  
  6.         String s1 = "a";    
  7.         String s2 = "b";    
  8.         String s = s1 + s2;    
  9.         System.out.println(s == a);    
  10.         System.out.println(s.intern() == a);    
  11.         System.out.println(s.intern() == a.intern());    
  12.           
  13.     }    
  14. }  

 

Java字串面試(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.