String is one of the most commonly used Java classes, sorting out some important string knowledge to share with everyone.
As a novice Java programmer, it is necessary to have a deeper understanding of string. If you have a few years of Java development experience, you can read the following content selectively based on the catalog.
1. What is a string and what data type is it?
A string is a class that is defined under the Java.lang package. It is not a basic data type.
String is immutable, and the JVM uses a string pool to store all the string objects.
2. What are the different ways to create a string object?
In this way, the JVM goes to the string pool to find there is no value equal to the string, and if so, returns the string reference found. Otherwise, a new string object is created and stored in the string pool.
String ("abc"); ABC ";
3, write a method to determine whether a string is a palindrome (both read and read the same word)?
Palindrome is positive and negative are the same word, if you need to determine whether it is a palindrome, only need to compare the pros and cons are equal. The string class does not provide a reversal method for us to use, but StringBuffer and StringBuilder have reverse methods.
Static Boolean ispalindrome (str) { if (str = = null) false; StringBuilder Strbuilder = new StringBuilder (return strbuilder.tostring (). Equals (str);
Assuming the interviewer lets you do this without using any other class, we only need to know if it's a palindrome for one by one comparisons.
PrivateStatic Boolean ispalindromestring (String str) { if (str = = null) return false; int length = str.length (); System.out.println (Length/ 2); For (int i = 0; i < length/ 2; i++) { if (str.charat (i)! = Str.charat (length-i- c16>1)) return false;} return true;}
4, how to make a string into lowercase or uppercase form?
Use the toUpperCase and toLowerCase methods to turn a string into uppercase or lowercase.
5. How to compare two strings?
The comparable interface is implemented internally in String, with two comparison methods: CompareTo (String anotherstring) and Comparetoignorecase (String str).
- CompareTo (String anotherstring)
Compares the passed-in anotherstring string, if it is less than the passed-in string, and returns a negative number if it is greater than the certificate. Returns 0 when two string values are equal. The Eqauls method returns true at this time.
- Equalsignorecase (String str)
This method is similar to the CompareTo method, the difference is only the internal use of the Character.touppercase and other methods for the comparison of case conversion.
6. How do I convert a string to char, and vice versa?
This is a misleading question, string is a series of characters, all of us cannot convert to a single char, but we can call the ToCharArray () method to turn the string into a character array.
"Java Interview"; //string to char array Str.tochararray (); System.out.println (chars.length);
7. How to convert a string to a byte array, and vice versa?
Use the string's GetBytes () method to turn a string into a byte array, using the construction method of string new string (byte[] arr) to convert the byte data to string.
Stringtobytearray { Main (//print the byte[] elements System. Out.println ("String to byte array:" + arrays.tostring (Bytearr));}}
PublicClassbytearraytostring {Publicstatic void main (string[] args) {byte[] ByteArray = { ' P ', ' A ', ' N ', ' K ', ' A ', ' J '}; byte[] byteArray1 = {80, 65, 78, 75, 65, 74} ; String str = new string (ByteArray); String str1 = new string (byteArray1); System. out.println (str); System. out.println (STR1);}}
<div id= "Question8" ></div>
8, talk about the difference between string, Stringbuffer,stringbuilder?
String is an immutable class, and whenever we manipulate a string, a new string is always created. The operation string is resource-intensive, so Java provides two tool classes to manipulate String-stringbuffer and StringBuilder.
StringBuffer and StringBuilder are mutable classes, StringBuffer are thread-safe, and StringBuilder are not thread-safe. So when multithreaded to the same string operation, we should choose to use StringBuffer. Because there is no need to deal with multithreading, StringBuilder is more efficient than stringbuffer.
9. What are the benefits of a string that is immutable?
String is an immutable class with the following advantages
- Because string is immutable, it is safe to use in multi-threading, and we do not need to do any other synchronization operations.
- The string is immutable and its value cannot be changed, so it is safe to store the data password.
- Because Java strings are immutable, you can save a lot of Java heap space while the Java runtime is running. Because different string variables can refer to the same string in the pool. If the string is to become a word, the value of any one variable will be reflected to other variables, and the string pool will have no meaning.
10, how to split a string?
"Abcaada"; System.out. println (Arrays. ToString (S.Split ("a")); The above code output is [, BC,, d].
data = S.split (2); System. out.println ("Name =" +data[//ysystem. Out.println ("Address =" +data[//kunming,yunnan
The first method actually calls the second method, but does not limit the length of the returned array.
Split (String regex) { 0);}
11, how to determine whether two strings are equal?
There are two ways to determine whether strings are equal, use "= =" or use the Equals method. When the "= =" operator is used, not only the value of the string is compared, but also the referenced memory address is compared. In most cases, we only need to determine if the values are equal, and then compare them with the Equals method.
There is also a equalsignorecase that can be used to ignore case comparisons.
"ABC"; "abc"; String ("abc"); System.out.println (//true System.out.println (//false System.out.println (//true
12. What is a string pool?
As the name implies, a string constant pool is used to store strings. It exists in the Java heap memory.
Explains how a string pool exists in Java heap space and when we use different ways to create strings.
Here is a programming example
Class Stringpool { void Main (String ("Cat"); System.out.println ("S1 = = S2:" + (S1==S2)); System.out.println ("S1 = = S3:" + (S1==S3));}}
Run the above code and the output is as follows:
S1 = = S2:trueS1 = = S3:false
Some Java questions may ask a few strings in the code to be created, for example:
String ("Cat");
The previous line of code will create 1 or 2 strings. If there is already a string "cat" in the string constant pool, create a "cat" string with wisdom. If there is no "Cat" in the string constant pool, it is first created in the string pool and then created in heap memory, which creates 2 of objects.
13. The Intern () method of String
When the Intern () method is called, the string in the pool is returned if the string pool contains a string that is eqauls equal to the string that is currently calling the method. If not in the pool, the current string is added to the pool first, and then the reference is returned.
14. Is string thread-safe?
String is an immutable class, and once a string object is created, we cannot change its value. Therefore, it is thread-safe and can be safely used in multithreaded environments.
15. Why do we always use string to do key when using HashMap?
Because the string is immutable, when the string is created, its hashcode is cached and does not need to be evaluated again. Because the HASHMAP internal implementation uses key hashcode to determine where value is stored, it is faster than other objects. This is why we usually use string as the HashMap object.
16, string programming question 1, the following code input what
String ("abc"); String ("abc"); System.out.println (S1 = = s2);
Enter false
2. What does the following code enter?
"ABC"; StringBuffer (S1); System.out.println (s1.equals (S2));
Enter false because S2 is not of type string, and the Equals method of string is type-judged.
3. What does the following code enter?
"ABC"; String ("abc"); S2.intern (); System.out.println (S1 ==s2);
The output False,intern () method returns a reference to the string object from the string pool, but since we have not assigned to S2,S2 no change, if the third line of code is S2 =
S2.intern (), enter true.
4. The following code will create several string objects.
String ("Hello"); String ("Hello");
The answer is 3 objects.
First, line 1 the "Hello" object in the string pool.
Second, line 1, a new string with the value "Hello" in heap memory.
Third, line 2, a new string with "Hello" in heap memory. The strings in the "Hello" string pool are reused here.
Java Learning Group 669823128
Java String Questions and Answers