The difference between "equals" and "=="

1. Different functions

  • "==" is to judge whether two variables or instances point to the same memory space.
  • "equals" is to determine whether the value of the memory space pointed to by two variables or instances is the same

2. Different definitions

  • "equals" is a method in JAVA.
  • "=="In JAVA, it is just an operation conformity.

3. Compare the difference

  • "==" refers to the comparison of memory addresses
  • "equals" compares the contents of the string

Summary:
== is the same as the
guideline and equals refers to the same value

A picture is worth a thousand words, Insert picture description here
for example:

public class EqualsTest {
    
    
    public static void main(String[] args) {
    
    
        // TODO Auto-generated method stub
        Integer aaa=new Integer(5);
        Integer bbb=new Integer(5);
        
        int a=10;
        int b=10;
        String str1=new String("justice");
        String str2=new String("justice");
        String str3;
        str3=str1;
        
        
        System.out.println(aaa==bbb);
        System.out.println(aaa.equals(bbb));
        System.out.println(a==b);
        
        System.out.println(str1==str2);
        System.out.println(str1.equals(str2));
        
        
        System.out.println(str1==str3);
        System.out.println(str1.equals(str3));
    }
  }

answer:
Insert picture description here

Guess you like

Origin blog.csdn.net/MiaoWei_/article/details/109198535