Java interview questions compulsory 50 (with answers) 2

11, swtich on whether the role of the byte, whether acting on the long, whether acting on String?

A: Early in the JDK, the switch (expr), expr can be a byte, short, char, int. Starting version 1.5, Java introduced enumerated types (enum), expr can be enumerated, starting with JDK 1.7 version, it can also be a string (String). Long integer (long) is not possible.

 

12, calculated using the most efficient method 2 multiplied by 8?

A: << 2 3 (left three equivalent multiplied by 2 to the third power, the equivalent of three to the right of the cube is divided by 2).

Added: When we rewrite hashCode method, you may see the code as shown below for the preparation of the class, in fact, we do not quite understand why the use of such multiplication to generate a hash code (hash code), and why this number is a prime number, usually select 31 Why this number? The answer to the first two questions you can own Baidu, choose 31 because of a shift and subtraction can be used instead of multiplication, resulting in better performance. Here you may have thought of: 31 * num <==> (num << 5) - num, left the equivalent of 5 by 5 th (32) minus 2 multiplied by itself is equivalent to 31. VM can now automate this optimization.

 

1 package com.loonstudio;  
 2   
 3 public class PhoneNumber {  
 4     private int areaCode;  
 5     private String prefix;  
 6     private String lineNumber;  
 7   
 8     @Override  
 9     public int hashCode() {  
10         final int prime = 31;  
11         int result = 1;  
12         result = prime * result + areaCode;  
13         result = prime * result  
14                 + ((lineNumber == null) ? 0 : lineNumber.hashCode());  
15         result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());  
16         return result;  
17     }  
18   
19     @Override  
20     public boolean equals(Object obj) {  
21         if (this == obj)  
22             return true;  
23         if (obj == null)  
24             return false;  
25         if (getClass() != obj.getClass())  
26             return false;  
27         PhoneNumber other = (PhoneNumber) obj;  
28         if (areaCode != other.areaCode)  
29             return false;  
30         if (lineNumber == null) {  
31             if (other.lineNumber != null)  
32                 return false;  
33         } else if (!lineNumber.equals(other.lineNumber))  
34             return false;  
35         if (prefix == null) {  
36             if (other.prefix != null)  
37                 return false;  
38         } else if (!prefix.equals(other.prefix))  
39             return false;  
40         return true;  
41     }  
42   
43} // Ho asked hovertree.com

 

13, the array has no length () method? String has no length () method?

A: Arrays are not length () method, length attributes. String has a length () method. JavaScript, the length of the string was obtained by the length property, which is easily confused and Java.

 

14, in Java, how to jump out of the current multiple nested loops?

A: A was added as a marker before the outermost loop, then A BREAK; can jump multiple loops. (In Java support tabbed break and continue statements, the role somewhat similar to C and C ++ goto statement, but like to avoid using goto the same, you should avoid using tabbed break and continue, because it will not let you program becomes more elegant, many times even have the opposite effect, so this syntax is not really know better)

 

15, the constructor (constructor) can be rewritten if (override)?

A: The constructor can not be inherited, and therefore can not be rewritten, but can be overloaded.

 

16, two objects the same value (x.equals (y) == true), but it may have different hash code, this sentence right?

A: No, if two objects x and y satisfy x.equals (y) == true, their hash code (hash code) should be the same. (1) If two identical objects (Returns true equals method), then they must be the same hashCode value; (2) if two objects have the same hashCode, they are not: Java for eqauls and hashCode method is predetermined necessarily the same. Of course, you may not want to follow it, but if you violate the above principles will find that when using the container, the same object can appear in the Set collection, while increasing the efficiency of the new elements will be greatly reduced (to use a hash storage the system, if the hash code of frequent conflict will result in a sharp decline in access performance).

Added: about equals and hashCode methods, many Java programs are aware, but many people that is simply knowing it, in Joshua Bloch's masterpiece "Effective Java" (a lot of software companies, "Effective Java", "Java programming ideas" and "heavy structure: improving the quality of existing Code "is a Java programmer must-see books, if you have not seen, then quickly go to Amazon to buy it) is introduced in the equals method: first, the equals method must satisfy reflexive ( x.equals (x) must return true), return true, y.equals (x) must also return symmetry (x.equals (y) true), transitivity (x.equals (y) and y.equals ( z) when the return true, x.equals (z) must also return true) and consistency (when x and y reference target information is not modified, multiple calls x.equals (y) returns the same value should be obtained ), but also for any non-null reference value x, x.equals (null) must return false. High quality tips equals method comprising: using a check operator == "parameter is a reference to this object"; 2 Use instanceof operator to check "parameter is the correct type"; 3 for the class. the key attribute, check whether the parameters of the incoming object attributes to match; 4. after writing the equals method, ask yourself whether it meets the symmetry, transitivity, consistency;. 5:00 override equals always overrides hashCode ;. 6 do not Object object equals method parameter replaced with another type, do not forget when rewriting @Override annotations.

 

17, can inherit String class?

A: String class is final class can not be inherited.

Added: Inheritance String itself is a wrong behavior on the String type is the best way to reuse association (HAS-A) instead of inheriting (IS-A).

 

18, when an object is passed as a parameter to a method, this method can change the properties of this object, and returns the result of changes in, then in the end is passed by reference or value transfer here?

A: The value is passed. Java programming language only pass parameters value. When a method is passed to the object instance as a parameter, the value of the parameter is a reference to the object. Properties of the object can be changed in the called procedure, but the object reference will never change. C ++ and C # may be transmitted or by-reference parameters to change the parameter values ​​passed.

Supplementary: Java is not passed by reference it is very inconvenient, which is referenced Java 8 is still not improved, the code is written in Java so it is to appear in a large number of Wrapper class (the need to amend the method call Wrapper placed in a class, and then pass the object Wrapper method), such an approach will only make the code becomes bloated, especially to make Java programmers and developers transition from C to C ++ can not be tolerated.

 http://hovertree.com/menu/java/

19, String and StringBuilder, StringBuffer the difference?

A: Java platform provides two types of strings: String and StringBuffer / StringBuilder, which can store and manipulate strings. String string which is read-only, which means the contents of a string String references can not be changed. StringBuffer StringBuilder string object and the class represents may be modified directly. StringBuilder JDK 1.5 is introduced, and it is exactly StringBuffer method, except that it is used in a single-threaded environment, because it is not synchronized all respects modified, so its efficiency is slightly higher than StringBuffer.

Supplement 1: There is a face questions Q: Is there any case with the + string concatenation to do better than calling StringBuffer / StringBuilder object append method performance? If the connection string obtained in static memory is already present, it is connected with a string do better than + append method StringBuffer / StringBuilder is.

Supplement 2: The following is an output face questions, ask the program to see if he can not say the correct answer.

 

com.lovo package;  
  // asked hovertree.com
public class StringEqualTest {  
  
    public static void main(String[] args) {  
        String a = "Programming";  
        String b = new String("Programming");  
        String c = "Program" + "ming";  
          
        System.out.println(a == b);  
        System.out.println(a == c);  
        System.out.println(a.equals(b));  
        System.out.println(a.equals(c));  
        System.out.println(a.intern() == b.intern());  
    }  
}

 

20, the difference between overloading (Overload) and rewritable (Override) is. Overloaded method can be differentiated according to the type of return?

A: rewriting method overloading and are multi-state manner, except that it is implemented compile-time polymorphism, which polymorphism is achieved runtime. Overload occurs in a class, the method of the same name if there are different list of parameters (parameters of different types and different number of parameters or both) is considered overloaded; rewrite occurs between the child and parent class, subclasses override the requirement is overridden method with the parent class is overridden method with the same return type, method is overridden better access than the parent, can not be overridden method declares more exceptions (Richter Deby parent change principle). Overloaded no special requirements for the return type.

Added: Huawei face questions are asked such a question: Why can not distinguish overloaded according to the type of return, say your answer now!

 

 

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/93715620