Java string interview (ii)

Source: Internet
Author: User

Take a look at the following 2 programs

[Java]View PlainCopy
    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 PlainCopy
    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. }  

The first program output is true because "a" and 1 are string constants, so the value of compile-time B is determined that the StringBuilder object is not produced at runtime, so at runtime, because "A1" already exists in the string pool, So the reference to the object "A1" also points to A and B.

The output in the second program is false. Because the value of the "a" +b is determined at run time, in order to increase efficiency, a StringBuilder object is generated at run time, the Append method is called on it, and the ToString () method is called, and a reference to a string object is returned.

Let's start with a few interview questions:

1.

[Java]View PlainCopy
    1. Public static void Main (string[] args) {
    2. //Print True for the reasons shown above analysis
    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.         //printing false, for the reasons shown above analysis   
    3.         string a =  "AB";     
    4.         string bb =  "B";     
    5.         string b  =  "a"  + BB;    
    6.          system.out.println (a == b);     
    7.     }  

3.

[Java]View PlainCopy
  1. Public static void Main (string[] args) {
  2. //Print true,final description BB always points to "B" and cannot refer other objects to BB, so, at compile time, BB's value is OK, i.e.
  3. //"a" + BB value is also OK, so the same as above 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 PlainCopy
  1. Public static void Main (string[] args) {
  2. //Print false, in short, the compilation period is not determined, the runtime can be determined, so the Stringbulder object will be generated, through tosring () returns a string
  3. //The reference is certain that the memory address of A and B is different.
  4. String a = "AB";
  5. final String bb = GETBB ();
  6. String B = "a" + BB;
  7. System.out.println (A = = B);
  8. }
  9. Private static String GETBB () {
  10. return "B";
  11. }

5

[Java]View PlainCopy
  1. Public class Test {
  2. private static String a = "AB";
  3. public static void Main (string[] args) {
  4. //Print false,true, static data is placed in the method area, and others are not static.
  5. String S1 = "a";
  6. String s2 = "B";
  7. String s = s1 + s2;
  8. System.out.println (s = = a);
  9. System.out.println (s.intern () = = a);
  10. }
  11. }

The analysis chart is as follows:

6

[Java]View PlainCopy
  1. Public class Test {
  2. private static string a = new string ("AB");
  3. public static void Main (string[] args) {
  4. //Print false,false,true. Analysis look at the back of the analysis diagram
  5. String S1 = "a";
  6. String s2 = "B";
  7. String s = s1 + s2;
  8. System.out.println (s = = a);
  9. System.out.println (s.intern () = = a);
  10. System.out.println (s.intern () = = A.intern ());
  11. }
  12. }

Java string interview (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.