In-depth analysis of some Java interview questions

Source: Internet
Author: User

In-depth analysis of some Java interview questions

I. What is the difference between & and?


& And & can both be used as operators of logic and, indicating logic and (and). When both expressions return true, the entire operation result is true. Otherwise, if either of them is false, the result is false.

& AlsoWithShort-circuit function, that is, if the first expression is false, the second expression is not calculated. For example, if (str! = Null &&! Str. equals ("") expression. When str is null, the following expression will not be executed.NoIf NullPointerException is returned and & is changed to &, an NullPointerException is thrown. If (x = 33 & ++ y> 0) y will increase, If (x = 33 & ++ y> 0) will not increase

& Does not haveShort-circuit. For if (str! = Null &&! Str. equals ("") expression. When str is null, the following expression Still execute, So NullPointerException,In addition, & can be used as bitwise operators.


2. Can the switch statement be applied to byte, long, or String?

In switch (expr1), expr1 can be byte, char, short, int, and their packaging classes such as Byte, Character, Short, Integer, and enum (enumeration ),String (Added by JavaSE 7)

Reference official http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html


3. When a variable is modified using the final keyword, can the reference not be changed, or can the referenced object not be changed?

When you use the final keyword to modify a variable, it is suggested that the variable cannot be changed, and the content in the object to which the referenced variable points can still be changed. For example, for the following statement:

Final StringBuffer a = new StringBuffer ("immutable ");
Run the following statement to report compilation errors:

A = new StringBuffer ("");
However, the following statement can be compiled:

A. append ("broken! ");

 

When defining the parameters of a method, someone may want to use the following form to prevent the method from modifying the passed parameter object:

Public void method (FinalStringBuffer param)

{

}

Actually, this isNoIn this method, you can add the following code to modify the parameter object:

Param. append ("");


4. How many String objects are created when String str = new String ("abc"); is executed?

If you search for answers online, the answer is almost the same:

If the String pool does not have the abc String, create an abc object in the String pool, and then create an abc object in the heap, returns the abc object address in the heap to str. In this case, two objects are created. Otherwise, only one abc object is created in the heap.


However, I saw a video explaining the problem today. He said that only one String object is created in any situation. In no case will abc objects be created in the string pool. argument:

String s1 = "hjk"; String s2 = s1.intern (); // intern () is used if the s1 String value "hjk" exists in the constant pool ", // returns the string object. Otherwise, add "hjk" to the constant pool and return the string object. System. out. println (s2 = s1); // trueString s3 = new String ("abc"); String s4 = s3.intern (); System. out. println (s3 = s4); // falseString s5 = s3.intern (); System. out. println (s5 = s4); // true


Another can refer to: Java interview video to explain http://study.163.com/course/courseMain.htm? CourseId = 519065 #/courseMain

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.