Java will have to answer interview questions (String articles)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/conconbenben/article/details/102649425

Recently, many learning java is white string I asked related issues, to feel that it is necessary to organize, to facilitate beginners to understand and respond to questions in the future string examiner interview.

If you have experience in Java development a few years, according to the directory can selectively read the following, and comments are welcome supplement.

Questions are listed below:

0.String is the most basic type of data it?
1.String Can be inherited?
Difference in 2.Java String a = "abc" and the String a = new String ( "abc ") of?
3. Why strings are immutable?
4. What is the String constant pool?
5. Use string initialization code: String a = new String ( " abc"); how many objects are created?
6.String class action intern () is?
7. Use equals () and '==' for string comparisons difference?
How 8.String StringBuffer StringBuilder three each thread safety?
9.String, the difference between StringBuffer and StringBuilder?
10. How to connect the plurality of strings.
11. How to split a String?
12. How to determine whether two String equal?
13.String class, the length of the string Is there a limit?
14.Java String "+" connector splicing principles string?

? 0.String is the most basic type of data it
Java basic data types include byte, int, char, long, float, double, boolean, short a total of eight; String is defined in the java.lang package of a class. It is not the basic data types.


Whether 1.String can be inherited?
No, because similar final String class.


2.Java the String a = "abc" and the String a = new String ( "abc ") of the difference?
String a = "abc" is initialized using constants, the constant pool after initialization of memory storage String
String a = new String ( "abc ") to create a string object, the JVM creates a String object, but are not stored in the string pool.


3. Why strings are immutable?
Because similar final String class.


4. What is the String constant pool?
String pool is a special area of memory separate from the conventional store heap memory string constant. These objects are known as string variables in the life cycle of the application.
Such as: double quotes string created directly, String a = "abc", will be "abc" stored in this area.


5. Use string initialization code: String a = new String ( " abc"); how many objects are created?
The above line of code will create one or two strings. If you already have a string "abc" in a string constant pool, it will only create a "abc" string. If the string constant pool is not "abc", then the first in the string pool will be created before created in heap memory, this situation will create the two objects.


6.String class action intern () is?
When this method is invoked, if the pool already contains the strings are equal to the String equals (Object) method identified this object, it returns the string pool. Otherwise, this String object is added to the pool, and returns this String object.
It follows for any two string s and t, s.intern () == t.intern ( ) is true if and only if s.equals (t) is true. It means that if s and t are different string object and has the same sequence of characters, then the call in both intern () will result in a single text string pool referenced by two variables.
7. Use equals and '==' string to compare the difference?
String equals an overridden method, whether the comparison string value character array of characters consistent, that comparison is the value of the string, the string value == not only relatively, but also to compare two strings where the memory address is the same.


How 8.String StringBuffer StringBuilder three each thread safety?
String StringBuffer thread-safe, StringBuilder non-thread-safe.


9.String, the difference between StringBuffer and StringBuilder?
String class is immutable values, each operation on a String object will generate a new object; the StringBuffer StringBuilder and allowed to operate on the original object without the object increments; the StringBuffer is thread safe, but more efficient than low, and the highest StringBuilder efficiency, but not thread-safe.


10. How to connect the plurality of strings.
String: By interpreting the Java API, you can know that Java provides special support for the string concatenation. Connection is achieved by the StringBuilder string (or the StringBuffer) class and append process.
StringBuffer: Use append achieve
StringBuilder: Use append to achieve.


11. How to split a String?
You can use strings separated by function: public String [] split (String regex), passed in the string that is divided, note that if the last one is just an incoming character, returns an array of null characters will not last string.


12. How to determine whether two String equal?
There are two ways to judge whether strings are equal, using the "==" or using the equals method. When using the "==" operator, not only the comparison string value, but also comparison referenced memory address. In most cases, we only need to determine the value of equality, this time using the equals method to compare.
There is also a equalsIgnoreCase can be used to ignore case compare string values.


13.String class, the length of the string Is there a limit?
You can see the source string class, use char array to store a string of characters, the character array to define the maximum length, so, in theory, String is no length limit, limit how much of your memory is.


14.Java String "+" connector splicing principles string?
String String StringBuilder go through an intermediate splicing process, achieved by the append method.

When the program has a lot of stitching string, it would have a direct StringBuilder achieve, you do not need a lot of new underlying the temporary String object. Note that the string would become null involved "null" when splicing.

Update Rollup related issues continued, and gradually add the code and a more detailed analytical principles.

                                                                             

 

Guess you like

Origin blog.csdn.net/conconbenben/article/details/102649425