Millions of people, thank you for this second you see here. I hope my series of interview questions can be helpful to you! ‘!

Wish you in the future days, keep love, go to the mountains and seas!

Three interview questions a day to make you a better self

Welcome back. Let’s pick up where we left off yesterday. Since we’re talking about the String class, that

1. What else do you know about the String class?

char char char char

Public String() {this.value = "".value; } public String(String original) {this.value = original. Value; this.hash = original.hash; } public String(char Value []) {this.value = copyOf(Value, Value.length); } public String(StringBuffer) {synchronized(Buffer) {this.value = Arrays.copyOf(buffer.getValue(), buffer.length()); }} / / StringBuilder as the constructor parameter public String (StringBuilder builder) {this. Value = Arrays. The copyOf (builder. The getValue (), builder.length()); }

equals = =#ff1493>equals = =#ff1493>equals It overrides the equals method of class Object. Here’s how it compares:

  • First check whether the reference to both objects is the same. Returns true if they are identical.
  • If not, it will first determine if the value to be compared is of type String, and if not, it will simply return false.
  • In the case of String, it loops over each character in the two strings, returning true if all characters are equal, and false otherwise.

The specific source code is as follows:

Public Boolean equals(Object anObject) {return true if (this == anObject) {return true; } false if (anObject instanceof String) {String anotherString = (String)anObject; int n = value.length; If (n = = anotherString. Value. Length) {/ / contrast the two strings are converted to char array char v1 [] = value; char v2[] = anotherString.value; int i = 0; While (n--!); // while (n--!); If (v1[I]! = 0) {if (v1[I]! = 0) {if (v1[I]! = v2[i]) return false; i++; } return true; } } return false; }

EqualSignoRecase () is a similar method to equals() in the String class. It is used to compare strings regardless of their case.

Of course, there are a number of methods in the String class that we often use:

  • CompareTo () : compareTo compare two strings
  • IndexOf () : The index position at which the query string first appears
  • LastIndexOf () : The index position of the last occurrence of the query string
  • Contains () : Whether the query string contains another string
  • TolowerCase () : Converts all strings toLowerCase
  • ToUpPerCase () : Converts all strings toUpperCase
  • Length () : The length of the query string
  • Trim () : Trims the leading and trailing Spaces of the string
  • Replace () : Replaces some characters in a string
  • Split () : Splits a string and returns an array of strings
  • Join () : Converts an array of strings to a string

These are all methods that we are likely to use in practical applications.

Awesome. Just asking if you know anything about String, you’re making it sound like I haven’t read the String source code. Scared, young people today. In that case, I was asking something else

2. The difference between equals and equals

  • = = :

    • For basic data types, it is used to compare whether “values” are equal.
    • For reference types, it is used to compare if the reference address is the same.
  • Equals () : Equals () is used to determine whether two objects are equal. But it can be used in two ways:

    • Case 1: When a class doesn’t override equals(), its default equals method (inherited from Object) uses the == operator. It compares two variables to see if they are referring to the same Object. Using equals and == gives the same result. It will compare the reference addresses to see if they are the same.
    • Case 2: The class overrides the equals() method. Typically, we override the equals() method to make the contents of two objects equal; Returns true if their contents are equal (that is, the two objects are considered equal).

Ok, let me ask you one last question:

3. String s= new String (“nz”) How many String objects are created?

There are two common ways to create a String: new String() and direct assignment.

  • The direct assignment first looks in the string constant pool to see if the value already exists. If so, the reference address is directly referenced to the value. Otherwise, the reference is created in the constant pool and then the reference is referenced to the value.
  • New String() creates a String object on the heap, and then checks the constant pool to see if the value already exists. If it doesn’t, it creates the String in the constant pool, and then points the referenced value to the String.

So we create two objects, an object “nz” in the string constant pool, and a new object “s” in the heap. The referenced value then points to the object in the string constant pool.

Let’s test it briefly:

public static void main(String[] args) {
    String s1 = "nz";
    String s2 = new String("nz");
    System.out.println(s1 == s2);  // false
    System.out.println(s1.equals(s2)); // true
}

Nice guy! So much for today, looking forward to your arrival tomorrow, I hope I can continue to keep surprise!

Note: if the article has any mistakes and suggestions, please leave a message! If this article is also helpful to you, I hope you can pay close attention to it. Thank you very much! You can also search for Prince Nezha’s official account on WeChat and chat about me in private. Thank you guys!