mb61ab431c11928 2022-01-26 13:45:41 阅读数:660
System.out.println(“a==b”);
if (a.equals(b)) // true
System.out.println(“aEQb”);
if (42 == 42.0) { // true
System.out.println(“true”);
}
}
}
explain :
String Class equals The method is rewritten , therefore String Of equals The method compares the value of the object , and Object Of equals Method is to compare the memory address of the object ;
When creating a String When an object of type , The virtual machine looks in the constant pool for objects that have the same existing value as the value to be created , If so, assign it to the current reference . If not, recreate one in the constant pool String object .
2 Use hashCode And equals Judge
============================================================================================
hashCode Is used to get the hash code , It can also be called hash code , The actual return value is one int Type data , It is often used to determine the position of an object in a hash table .
Because superclass Object There is hashcode Method , This means that all classes have hashCode Method .
Example : By rewriting equal Method , Make the contents of two objects the same , Then the hash codes of the two objects are different .
public class EqualTest {
public static void main(String[] args) {
Person p1 = new Person(10, “ Zhang San ”);
Person p2 = new Person(10, “ Zhang San ”);
System.out.println(
“p1.equals(p2)=” + p1.equals(p2) + “, p1.hashcode=” + p1.hashCode() + “, p2.hashcode=” +p2.hashCode());
}
}
class Person {
int age;
String name;
……
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Person other = (Person) obj;
if (age != other.age) return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name)){
return false;
}else{
return true;
}
}
}
Because there is no rewriting hashCode Method , So the output is :
Last , Attach a brain map prepared before the interview :
Before the interview, you must brush the questions , In order to facilitate everybody to review , I'd like to share a collection of personal interviews
Step3: Brush problem
Since it's an interview , Then it's necessary to brush the questions , In fact, after the Spring Festival home , I can't go anywhere , I did a lot of interview questions myself , So in the interview process to be able to know , Basically, it will be clear what knowledge points will be asked during the interview , What are the high frequency questions , So brushing questions is a very important point in the pre interview preparation process .
The following is my private interview question bank :
Many people sigh “ Learning is useless ”, In fact, the reason why there is the theory of uselessness , It's because what you want doesn't match what you learn , This means that I have not learned enough . Whether it's study or work , There should be initiative , So if you have a big factory dream , Then we should try our best to realize it .
Finally, I wish you all good health , I got what I wanted offer!
This article has been CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included
copyright:author[mb61ab431c11928],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261345389126.html