The vast sea of millions, thank you for this second you see here. I hope my series of interview questions can help you! ‘!
May you keep your love and go to the mountains and seas in the coming days!
Three interview questions a day, achievement better self
Welcome back, let’s continue yesterday’s topic! Now that we’re talking about the String class, so
1. What else do you know about the String class?
A String contains a char array. There are several important constructors in a String:
// Default no-argument construction
public String(a) {
this.value = "".value;
}
// A constructor that takes String
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
// use char[] as the argument constructor
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
// Constructor that takes StringBuffer
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length()); }}// A constructor that takes StringBuilder
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
Copy the code
On the other hand, in the String class we have methods like equals, which compare whether two strings are equal. It overrides the equals method of the Object class.
- Check whether the references of the two objects are the same. It returns true if they are the same.
- If not, the value to be compared is a String, and if not, false is returned.
- If it is a 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 for the same object reference
if (this == anObject) {
return true;
}
// Check whether the value to be compared is of type String. If not, return false
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
// Convert both strings to char arrays for comparison
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
// Loop over each character of the two strings
while(n-- ! =0) {
// If one of the characters is not equal, true false, otherwise continue to compare
if(v1[i] ! = v2[i])return false;
i++;
}
return true; }}return false;
}
Copy the code
In the String class, there is a method equalsIgnoreCase() similar to equals(), which is used to compare strings regardless of case.
Of course, the String class also has a lot of methods that we often use:
- CompareTo () : Compares two strings
- IndexOf () : The first subscript position of the query string
- LastIndexOf () : The subscript position at the end of the query string
- Contains () : Queries whether a string contains another string
- ToLowerCase () : converts all strings toLowerCase
- ToUpperCase () : converts all strings toUpperCase
- Length () : indicates the length of the query string
- Trim () : Trim the first and last Spaces of the string
- Replace () : Replaces some characters in a string
- Split () : Splits the string and returns an array of strings
- Join () : Converts an array of strings to a string
These are all approaches that we might all use in practical applications.
Cool, just asking if you know anything about String, you’re acting like I haven’t read the String source code. Be afraid, young people these days. In that case, I’m asking you something else
2. The difference between == and equals
-
= = :
- For basic data types, it is used to compare “values” for equality;
- For reference types, it is used to compare whether the reference address is the same.
-
Equals () : Also checks whether two objects are equal. But it is generally used in two ways:
-
Case 1: When a class does not override equals(), its default equals method (inherited from Object) uses the == operator to compare whether two variables refer to the same Object. Using equals and == gives the same result. It compares the reference addresses to see if they are the same.
-
Case 2: The class overrides equals(). In general, we override equals() to make the contents of two objects equal; Return true if their contents are equal (that is, the objects are considered equal).
-
Okay, one last thing for you:
3. String s= new String (“nz”)
There are two common ways to create a String: new String() and direct assignment.
- The direct assignment method first looks for the value in the string constant pool, and if so, points the reference directly to the value. Otherwise, it creates the reference in the constant pool and then points the reference to the value.
- The new String() method always creates a String object on the heap, and then queries the constant pool to see if the String already exists. If not, the String is created in the constant pool, and then references the value to the String.
So two objects are created, one is the string constant pool object “nz”, and a new is created in the heap string object S. The referenced value then points to an object in the string constant pool.
Let’s take a quick test:
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
}
Copy the code
Nice guy! Today is over here, looking forward to your arrival tomorrow, I hope to continue to keep a surprise!
Note: If there are any mistakes and suggestions, please leave a message! If this article is helpful to you, I hope you will pay attention to it for three times. Thank you very much! You can also search the wechat prince Nezha public number private chat me, thank you guys!