Summary of String-related Interview Questions

Source: Internet
Author: User

Summary of String-related Interview Questions

1. First, let's look at the oldest. Is String a basic data type?

A: No. Basic data types include byte, char, short, int, long, float, double, and boolean. These eight types are built-in types in Java, also called native types, string is not. It is a class.

2. String s1 = "abc"; String s2 = "abc"; System. out. println (a = B); what is the output result?

A: The result is true. Because the String is a final class, the String is a constant (cannot be modified after being created in memory), and the String is often used in programs, therefore, Java provides a buffer for it.

The string is shared. When a string is defined in double quotation marks, it is stored in the buffer. When "abc" is used, the system first checks whether the string exists in the buffer, create one,

If yes, it will be used directly. The first time "abc" is used, it will be created in the buffer, and the second time it will be referenced directly.

Therefore, if the code in the question creates several objects, it creates one.

Comparison:

public class StringDemo1 {public static void main(String[] args) {test1();test2();}private static void test1() {String s1="abc";String s2="abc";System.out.println(s1==s2);//true}private static void test2() {String s3=new String("bcd");String s4="bcd";System.out.println(s3==s4);//false}} 

Therefore, when you create a String object using the constructor, a new object is created outside the buffer every time, and the comparison with "=" will never be equal.

3. String s1 = "abc"; String s2 = "AB" + "c"; System. out. println (a = B); what is the output result?

A: The result is true. In fact, s2 does not add strings. The two string constants are automatically converted into a string during compilation.

4. How many objects are created in the following statement: String s = "a" + "B" + "c" + "d ";

A: we already know the answer to the previous two questions. It is not that difficult to understand its principles. The answer is either 0 or 1.

Because if the object "a" + "B" + "c" + "d" is automatically compiled into "abcd ", so this question becomes String s = "abcd"; how many objects are created?

If the "abcd" object exists in the buffer before the code is executed, the object will not be re-created, so there will be 0. If there is no "abcd" object in the buffer, so there is one.

5. String s = new String ("xyz"); how many objects are created? What is the difference?

A: You have created one or two objects. First, the new String ("xyz") will certainly create an object in the memory. It is an object created by referring to the constant "xyz, therefore, we will first check whether the buffer is frequently used.

Volume "xyz". If it does not exist, create a constant "xyz" in the buffer; otherwise, create one or two constants.

6. Differences between = and equals

(1) Basic data types, also known as raw data types (byte, char, short, int, long, float, double, boolean) the comparison between them uses the equal sign (=) to compare their values.

(2) Comparison of objects. When objects are compared with =, the storage addresses in their memory are compared. Therefore, the two identical objects produced by new are not equal when compared with =, because they store different addresses in the memory, so the comparison of objects should be compared using equals. It is worth noting that, in fact, the equals method defined in the Object class is based on =. The source code is as follows:

    public boolean equals(Object obj) {        return (this == obj);    }
Equals in the Object class compares memory addresses, but the equals method in the Object class is overwritten in the String, Integer, Date, and other classes. Let's take a look at the equals method in the String class:
Public boolean equals (Object anObject) {if (this = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = value. length; // its own String length if (n = anotherString. value. length) {// if the length of the character string is equal to the length of the passed string, continue to compare char v1 [] = value; // obtain the character char v2 [] = anotherString at each position. value; int I = 0; while (n --! = 0) {if (v1 [I]! = V2 [I]) // compare the characters at each position one by one. return false; I ++;} return true ;}} return false ;}
The equals method overwrites the String to compare whether the content of the two strings is the same, rather than the memory address.

7. String s = "abcdefg"; s. substring (0, 3); System. out. println (s );

Answer: This is simple. The answer is abcdefg. Because the String class is a final class, the value cannot be changed, and the final class cannot be inherited.

8. Differences between the String class and the StringBuffer class

The Java platform provides two classes: String and StringBuffer. The String class provides strings whose values cannot be changed. The StringBuffer class provides strings that can be modified.

Therefore, we can use StringBuffer to construct dynamic data. For example:

We need to combine all the strings from 1 to 100 to form a new String. When we use the String class and the StringBuffer class to execute them separately:

Import java. util. date; public class StringDemo {public static void main (String [] args) {StringTest (); StringBufferTest ();} private static void StringTest () {long d1 = new Date (). getTime (); String s = ""; for (int I = 1; I <= 10000; I ++) {s = s + I ;} long d2 = new Date (). getTime (); System. out. println ("application String class time" + (d2-d1); // 190} private static void StringBufferTest () {long d3 = new Date (). getTime (); StringBuffer s1 = new StringBuffer (); for (int I = 1; I <= 10000; I ++) {s1 = s1.append (I );} long d4 = new Date (). getTime (); System. out. println ("when applying the StringBuffer class" + (d4-d3); // 1 }}

The print shows that the StringBuffer class is much more efficient than the String class. Because 100 objects need to be created cyclically when strings are used, and the value of an object is constantly changing when StringBuffer is used.

In addition, there is an important difference between the two, that is, the String class overwrites the equals method of the Object class, so the new String ("abc "). equals (new String ("abc") returns true, while

The StringBuffer class does not overwrite the Object's equals method. Therefore, the result of new StringBuffer ("abc"). equals (new StringBuffer ("abc") is false.

Note: The StringBuffer object uses append () to modify its object, instead of "+". The StringBuffer object cannot be assigned a value using "=", but can only be used as a constringbuffer ("")) laifu

Value.

8. Differences between StringBuffer and StringBuilder

The difference is that StringBuffer supports multi-threaded operations and is thread-safe, while StringBuilder only supports single-threaded operations, which is thread unsafe. StringBuffer supports multi-thread operation guide.

This is slower than StringBuilder.

I will try to add new ones later. You are also welcome to help me add new ones ......


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.