Detailed explanation of the difference between Equals and == in Java

1. Preface - JVM memory

Before explaining the difference between equals and == , let's briefly introduce the problem of memory allocation in the JVM .

 

The memory in the JVM is divided into stack memory and heap memory. What is the difference between the two:

<!--[if !supportLists]--> 1)  <!--[endif]--> Stack memory

Basic data types are stored in stack memory. Basic types include: byte, short, char, int, long, float, double, boolean .

 

<!--[if !supportLists]-->2)  <!--[endif]-->堆内存

Object data is stored into heap memory. When we create an object ( new Object ), its constructor is called to open up space, store the object data in heap memory, and at the same time generate corresponding references in stack memory, when we call in subsequent code All references to stack memory are used.

 

Second, equals and == comparison

Comparisons between primitive types, using the double equals sign ( == ) , compare their values. 

When using == to determine whether two variables are equal, if the two variables are basic type variables and both are numeric types, then return true as long as the values ​​of the two variables are equal .

 

When comparing object types with ( == ), they compare their storage addresses in memory. Therefore, unless they are the same new object, the result of their comparison is true , otherwise the result of the comparison is false . All classes in JAVA inherit from the base class Object, and an equals method is defined in the base class in Object . The initial behavior of this method is to compare the memory address , but in some class libraries this method It has been overwritten. For example, String, Integer, and Date have their own implementation of equals in these classes , instead of comparing the storage address of the class in the heap memory.

       For the equals comparison between composite data types, without overriding the equals method, the comparison between them is still based on the address value of their storage location in memory, because the equals method of Object also uses a double equals sign ( == ), so the result of the comparison is the same as the double equals sign ( == ).

 

public class TestString {
   public static void main(String[] args) {
       String s1 = "Monday";
       String s2 = "Monday";
      if (s1 == s2) {
           System.out.println("s1 == s2");}
      else{
          System.out.println("s1 != s2");}
      }
}

 

Compile and run the program, output: s1 == s2 Explanation: s1 and s2 refer to the same String object -- "Monday"!

2. Change the program a little bit, and there will be even more strange findings:

public class TestString {
	public static void main(String[] args) {
		String s1 = "Monday";
		String s2 = new String("Monday");
		if (s1 == s2) {
			System.out.println("s1 == s2");
		} else {
			System.out.println("s1 != s2");
		}
		if (s1.equals(s2)) {
			System.out.println("s1 equals s2");
		} else {
			System.out.println("s1 not equals s2");
		}
	}
}

 

We will create s2 with the new operator

Program output:

s1! = s2

s1 equals s2

Description: s1 and s2 refer to two "Monday" String objects respectively

 

3. String buffer pool

It turns out that the program will create a string buffer pool when it is running. When using the expression s2 = "Monday" to create a string, the program will first look for an object of the same value in the String buffer pool. In the program, s1 was put into the pool first, so when s2 was created, the program found s1 with the same value

Reference s2 to the object "Monday" referenced by s1

In the second program, the new operator is used, which clearly tells the program: " I want a new one! Not the old one! " So a new "Monday" Sting object is created in memory. They have the same value, but different locations, one swimming in the pool and one resting on the shore. Oops, what a waste of resources, obviously they are the same, why do they have to be separated?

 

4. Change the program again:

 

public class TestString {
	public static void main(String[] args) {
		String s1 = "Monday";
		String s2 = new String("Monday");
		s2 = s2.intern();
		if (s1 == s2) {
			System.out.println("s1 == s2");
		} else {
			System.out.println("s1 != s2");
		}
		if (s1.equals(s2)) {
			System.out.println("s1 equals s2");
		} else {
			System.out.println("s1 not equals s2");
		}
	}
}

 

 

This time add: s2 = s2.intern();

Program output:

s1 == s2

s1 equals s2

It turns out that ( the return value of the intern() method "abc".intern() method of java.lang.String is still the string "abc" , on the surface it seems that this method is useless. But in fact, it does A little action: check whether there is a string "abc" in the string pool , if it exists, return the string in the pool; if it does not exist, the method will add "abc" to the string pool, and then return its citations.

 

Third, summarize the difference between  == and Equals

  1. == is an operator.

  2.Equals is a method of the string object, which can be . (dot) out.

  

  Our comparison is nothing more than these two 1 , basic data type comparison 2 , reference object comparison

  1. Comparison of basic data types

  Both == and Equals compare two values ​​for equality. equal to true otherwise false ;

  

  2. Comparison of reference objects

  == and Equals both compare the addresses in stack memory for equality . equal to true otherwise false ;

  

  A few things to note:

  1. String is a special reference type. For the comparison of two strings, both == and Equals compare whether the strings are the same;

  2. When you create two string objects, the addresses in the memory are not the same, you can assign the same value.

  So the content of the string is the same. The reference address is not necessarily the same, (the object address of the same content is not necessarily the same), but the reverse is true;

 

  3. Basic data type comparison ( except string ) == and Equals are both comparison values;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221582&siteId=291194637