String common interview questions

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.

Guess you like

Origin blog.csdn.net/qq_43078445/article/details/108395408