当前位置:网站首页>What does the equals method compare? Who told you
What does the equals method compare? Who told you
2022-06-26 16:57:00 【Tianweichang】
Official account “Java Back end technology stack ”
reply “000” Get the necessary e-books for programmers
The story
Some time ago , When I gave an online mock interview to a little friend , A casual question :” say something == and equals Differences in methods ?“, The result gave me an answer equals The method is to compare the contents .
Let's talk about it first ==,== There are two cases of comparison :
1. Compare the basic data types
int a=100;
int b =10;
// Compare the values of two variables
if(a==b){
System.out.println(" Something is wrong. ");
}
2. The comparison is the reference type
Integer a=Integer.valueOf(100);
Integer b =new Integer(100);
// The comparison is a and b Whether the addresses of the objects pointed to are the same
if(a== b){
System.out.println(" Something is wrong. ");
}
then , Let's talk about equals Method .
equals The method is in Object As defined in , As follows :
public boolean equals(Object obj) {
return (this == obj);
}
From this method , We can conclude that : If you do not override equals Method , Then the two object addresses are compared .
as for , Rewrote , What is the comparison ?
It's like eating , In most people's minds , When it comes to eating , You will think of eating noodles or rice . I'm sorry , Many people now eat only vegetables , No rice or noodles .
So let's talk about that , Two common classes :String Classes and Integer class .
String Class equals Method :
public boolean equals(Object anObject) {
// First compare the address of the object
// The object address is the same , That proves to be the same object
if (this == anObject) {
return true;
}
// Comparison of type
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
// Compare the length
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
// Compare content
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
From the source code logic of the whole method , It's easy for us to see ,String Class equals The method does not have to compare the content , First, compare the addresses of two objects , Then compare the length , In comparison with the specific content .
Integer Class equals Method :
public boolean equals(Object obj) {
// Comparison of type
if (obj instanceof Integer) {
// Unpack first Then compare the specific values
// Be careful there value yes int Variable of type
return value == ((Integer)obj).intValue();
}
return false;
}
summary
This interview question , It seems very simple , I can also ask two other friends who have worked for three years , The answer is the same as that of the previous friend . Here is a summary of this :
== If it is used for the comparison of basic data classes , Then the specific value is compared , If you are comparing reference types ( Packaging type ), The comparison is whether the object addresses pointed to by the two objects are equal .
equals Method , The default is to compare whether the addresses of two objects are the same . If you are right equals Method overridden , It depends on how it is rewritten .
Okay , So much for today , Remember to follow me , Tell me something different , It can also take you to see some source code , remember : Many good things can only be seen in the source code .
Recommended reading
边栏推荐
- Set up your own website (16)
- JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
- Développer un opérateur basé sur kubebuilder (démarrer)
- JUnit unit test
- 去中心化NFT交易协议将击败OpenSea
- Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
- Community ownership of NFT trading market is unstoppable
- Niuke Xiaobai monthly race 50
- proxy
- Teach you to learn dapr - 3 Run the first with dapr Net program
猜你喜欢

QT 5.9.8 installation tutorial

Teach you to learn dapr - 3 Run the first with dapr Net program

Summary of all knowledge points of C language

Discussion: the next generation of stable coins

无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity

Don't believe it, 98% of programmers are like this

建立自己的网站(16)

C language --- basic function realization of push box 01
![[force deduction question] two point search: 4 Find the median of two positive arrays](/img/4f/43aa7e14344e7e1a2fb7c1d209d13b.png)
[force deduction question] two point search: 4 Find the median of two positive arrays
随机推荐
QT 5.9.8 installation tutorial
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
proxy
Don't believe it, 98% of programmers are like this
[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
Overall context of concurrent programming
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
Redis overview
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
Knowing these commands allows you to master shell's own tools
Community ownership of NFT trading market is unstoppable
Codeforces Round #802 (Div. 2)
Redis 概述整理
Sandboxed container: container or virtual machine
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
Redis' 43 serial cannons, try how many you can carry
Kubernetes essential tools: 2021
[latex bearer] use tables in \title (error \begin doesn't match its definition.)
Least squares system identification class II: recursive least squares