The differences between java equals and = == are described in detail

  • 2020-05-10 18:14:37
  • OfStack

The difference between equals and == in Java

The data types in java can be divided into two categories:

1. Basic data type, also known as raw data type. byte short, char int, long, float, double, boolean  

The comparison between them, using the double equal sign (==), compares their values.
2. Composite data type (class)  

When they compare   with (==), they compare their location in memory, so unless it's an object with an new, they end up with true, otherwise they end up with false. JAVA among all the classes are inherited from Object the base class, in the base class defines a Object equals method, the method of the initial behavior is the object of memory address, but this method of 1 some libraries have covered off, such as String Integer, Date among these classes equals has its own implementation, rather than the comparison classes stored in the heap memory address.

  equals comparison between for complex data types, in the case of not overwrite equals method, the comparison between them is based on their location in memory address values, because Object equals method is compared with the double equal sign (= =), so after comparing with the result of the double equal sign (= =) at the same result.

java   equals and =, ==

1. The difference between == and equals

1. == is the operator

2. equals is a method of the String object

There are generally two types of comparison

1. Comparison of basic data types

2. Comparison of reference objects

1. Comparison of basic data types

== and equals both compare whether the values are equal; if they are equal, true; otherwise, false

2. Comparison of reference objects

== and equals are both compared as to whether the addresses in the stack memory are equal. If the addresses are equal, true will be used; otherwise, false will be used

Note:

1. String is a special reference data type, == compares whether the reference address of the string object is 1, and equals compares whether the content in the stack is 1.


 String ss = new String("abc");
 String sss = new String("abc");

 if(ss == sss){
  System.out.println("ss == sss is true");  
 }
 if(ss.equals(sss)){
  System.out.println("ss equals sss is true");
 }

Console output:

ss != sss
ss equals sss

Note: ss and sss have different memory addresses in the stack, but the same contents in the heap.

String ss = new String("abc");

String ssss = ss;


// judge ss and ssss The reference address in the stack 1 sample 

  if(ss == ssss){
  System.out.println("ss == ssss");
 }else{
  System.out.println("ss != ssss");
 }

// judge ss and ssss Whether the contents in the heap are 1 sample 
  if(ss.equals(ssss)){
  System.out.println("ss equals ssss");
 }else{
  System.out.println("ss not equals ssss");
 }

Console output:

ss == ssss
ss equals ssss

This indicates that ss and ssss are the same objects, and that they are one of the things in the heap

2. Comparison of reference objects


  TestBean obj1 = new TestBean();
  TestBean obj2 = new TestBean();
  TestBean obj3 = obj1;
  if(obj1 == obj2){
  System.out.println("obj1 == obj2");
  }else{
  System.out.println("obj1 != obj2");
  }
  
  if(obj1 == obj3){
  System.out.println("obj1 == obj3");
  }else{
  System.out.println("obj1 != obj3");
  }

Console output:

obj1!= obj2
obj1== obj3

Indicates that obj1 and obj2 are not the same object and have different reference addresses in the stack

obj1 and obj3 are the same object, with the same reference address in the stack

2. The difference between = and equals ==

= represents an assignment, which assigns the value on the right of = to the variable on the left. equals and == are operations

  thanks for reading, hope to help you, thank you for your support of this site!


Related articles: