The latest JAVA basic advanced interview questions in 2020 (quality edition)

1. Is String a basic data type?

String is not a basic data type.

 

2. Is String variable?

String is final and immutable.

 

3. How to compare two strings with the same value, how to compare whether two strings are the same object? Use equals to compare whether the string values ​​are the same, and use == to compare whether the string objects are the same.

 

4. Can String be used in switch?

The switch in jdk7+ can use the String type.

 

5. String str = new String("abc"); Several objects were created, why?

Two are created, "abc" itself is created in the constant pool, and created in the heap by new.

 

6. What is the difference between String, StringBuffer, StringBuilder?

The biggest difference between String, StringBuffer, and StringBuilder is that String is immutable, while the latter is variable. StringBuffer is thread-safe, and StringBuilder is not thread-safe faster.

 

7. Which characters are removed by String.trim() method? trim removes the blank characters at the beginning and end of the string.

 

8. Can String be inherited by subclasses?

Since String is final, it cannot be inherited.

 

9. Can the java.lang.String class be customized and used?

You can customize the java.lang.String class and compile successfully, but it cannot be loaded and used. For details, please learn about the class loading mechanism.

 

10. How to convert between String and byte[]?

String> byte[] uses the getBytes method of the String class; byte[]> String uses the new String(byte[]) constructor.

 

  1. What lists do you know?

 ArrayList、LinkedList、Vector 等。

  1. What is the difference between List and Vector?

Vector is a thread-safe collection under the List interface. Is the list ordered?

List is ordered.

  1. What is the difference between ArrayList and LinkedList? What scenarios are they used in?

The data structure of ArrayList and LinkedList are different. The former is used in situations where there are many queries, while the latter is suitable in situations where there are many insertions.

 

5. What is the underlying data structure of ArrayList and LinkedList?

 

ArrayList uses an array structure, and LinkedList uses a linked list structure.

 

6. What is the default size of ArrayList and how is it expanded?

 

The default size of ArrayList before Jdk1.7 is 10, and after JDK1.7 it is 0. For JDK differences, the capacity is expanded by approximately 1.5 times each time.

 

7. Is List thread safe? What if you want thread safety?

 

The Vector in the List is thread-safe, and other tools must be thread-safe.

Collections.synchronizedList(new ArrayList())方法。

8. How to sort List?

Use the sort method of List itself, or use the Collections.sort(list) method;

9. Can the List after the Arrays.asList method be expanded?

Arrays.asList uses final arrays, and does not support the add method or expansion.

 

10. How to convert between List and Array?

 

List>Array uses toArray method, Array>List uses Arrays.asList(array) method, because it is fixed, you can use new ArrayList(Arrays.asList(array)) if it is not fixed.

 

  • What is the main method for?

 

  • The main method is the entry method of the Java program. The JVM will first look for the main method when it is running.

 

 

  • How to run a class without the main method?

 

  • No, we cannot run Java classes without the main method.
  • Before Java 7, you could run Java classes by using static initialization. However, since Java 7 it will not work.

 

 

  • How does the main method pass parameters? What type of parameters are passed? Can the parameter type be changed?

 

  • String array, cannot be changed.

 

 

  • Why is the main method static? Can it be changed to non-static?

 

  • The main() method must be static. If main() is non-static, the JVM must instantiate its class when calling the main method.

 

  • It cannot be changed to non-static. The main() method must be declared as static so that the JVM can call the main() method without instantiating its class.
  • If the "static" statement is removed from the main() method, although the compilation can still succeed, it will cause the program to fail at runtime.
  • When instantiating, you have to call the constructor of the class. If the constructor of this class has parameters, then there will be ambiguity.

 

 

  • Can the main method be overloaded?

 

  • Yes, we can override the main() method. A Java class can have any number of main() methods.

 

 

  • Can the main method be overridden?

 

  • In Java, static methods are compiled together at compile time. The main method is a static method, so you cannot override static methods in Java.

 

 

  • What is the return type of the main method? Can it be changed?

 

  • void, cannot be changed.

 

 

  • What is the scope of the main method? Can it be changed?

 

  • Public, cannot be changed.

 

 

  • Can the main method be synchronized?

 

  • The main method can be synchronized in Java, and the synchronized modifier is allowed to be used in the declaration of the main method, so that the main method can be synchronized in Java.

 

 

  • Can the main method terminate?

 

  • The main method can be terminated in Java.

Guess you like

Origin blog.csdn.net/yunchan2675/article/details/108239333