In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the connection and difference between = = and equals in javaSE. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Write at the front:
= and equals are the questions we often encounter in the interview. So what are the connections and differences between them? Let's talk about it today.
Typical problems
Here are some typical written examination questions:
Int x = 10
Int y = 10
String str1 = new String ("abc")
String str2 = new String ("abc")
String str3 = "abc"
String str4 = "abc"
System.out.println (x = = y); / / output?
System.out.println (str1 = = str2); / / output?
System.out.println (str1.equals (str2)); / / output?
System.out.println (str3 = = str4); / / output?
System.out.println (str1 = = str3); / / output?
System.out.println (str1.equals (str3)); / / output?
Integer F1 = 100,f2 = 100,f3 = 150,f4 = 150
System.out.println (F1 = = f2); / / output?
System.out.println (f3 = = f4); / / output?
Text
We usually say that "=" is used to determine whether the values between two variables are equal. Variables are divided into basic data types and reference types. If the variable of the basic data type compares the value directly, while the reference type compares the first address of the memory of the corresponding reference. The equals method is commonly used to compare whether two objects look the same. Judge whether some characteristics (content) of two objects are the same. It is actually calling the object's equals method to make a comparison. So let's take a look at the equals method!
The equals method is actually a method of the Object class. Because the Object class is the direct or indirect parent of all classes, that is, the equals () method in all classes inherits from the Object class, and through the source code, we find that the underlying implementation of the equals () method in the Object class is actually the "=" sign.
Public boolean equals (Object obj) {
Return (this = = obj)
}
So, in all the classes that do not override the equals () method, calling the equals () method is actually the same as using the "= =" sign, which is also the object address value for comparison. However, most of the classes provided by Java override the equals () method, and the overridden equals () method generally compares the values of the two objects, such as the String class, the Date class, and the wrapper class for basic data types. You can look at the source code of the String class:
Public boolean equals (Object var1) {
If (this = = var1) {
Return true
} else {
If (var1 instanceof String) {
String var2 = (String) var1
Int var3 = this.value.length
If (var3 = = var2.value.length) {
Char [] var4 = this.value
Char [] var5 = var2.value
For (int var6 = 0; var3--! = 0; + + var6) {
If (var4 [var6]! = var5 [var6]) {
Return false
}
}
Return true
}
}
Return false
}
}
Solve
After reading the above description, I believe you can get it right or answer most of these questions. But there are also two points to pay attention to:
String str3 = "abc"
String str4 = "abc"
System.out.println (str3 = = str4); / / output true
Why is this true? Str3 and str4 are supposed to be two objects. "=" compares the address, which should be false. If that's what you think, you don't know much about the String class. String class, we all know that it is an immutable character sequence, stored in the constant pool, so when you declare a str3= "abc", it will open up a memory space in the constant pool to store "abc", the next time it is declared, it will be found in the constant pool, yes, directly assign the current address to the variable, no, re-create. Therefore, the str3 and str4 here point to the same memory address.
Integer F1 = 100,f2 = 100,f3 = 150,f4 = 150
System.out.println (F1 = = f2); / / output true
System.out.println (f3 = = f4); / / output false
I was very surprised to see the answer above. In fact, there is a knowledge point hidden here. It is the cache problem of the wrapper class. The following is a brief description: the wrapper classes corresponding to integers and char types are cached for values between-128 and 127 when they are automatically boxed. Of course, the purpose is to improve efficiency. The principle of caching is that if the data is in the range of-128 to 127, then objects have been created for each value in that interval when the class is loaded, and the 256 objects are stored in an array called cache. Whenever the automatic boxing process occurs (or when valueOf () is called manually), it will first determine whether the data is in that interval, if so, directly get a reference to the corresponding wrapper class object in the array, and if not, call the wrapper class constructor through new to create the object. Here, take the Integer class as an example. Source code reference: public static Integer valueOf (int I) {if (I > = IntegerCache.low & & I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.