What is the comparison between equals and ==?
1. boolean tem = a == b;
First of all, ==The comparison is definitely the address. From the perspective of the stack, that is, ==The comparison is the content on the stack. Because the stack is used to store the literal values of automatic variables in the eight basic types in Java (automatic variables are variables defined in the form of int a = 1;). If it is an automatic variable comparison value, it is definitely used to compare it with ==, because equals() is a method, so it must be called by an object to be used for comparison. Automatic variables are neither instances of classes nor references of classes, so the equals() method cannot be used.
2.boolean tem = a.equals("b");
The equals() method is generally used to compare the content of an object, but in some cases it also compares the addresses of two objects.
Next
Write a test program
package com;import java.util.Date;public class test { public static void main(String[] args) { Integer integer1 = new Integer(1); Integer integer2 = new Integer(1); String str1 = new String("123"); String str2 = new String("123"); Date date1 = new Date(); Date date2 = new Date(); Double double1 = new Double("1.0"); Double double2 = new Double("1.0"); Boolean tem1 = new Boolean(true); Boolean tem2 = new Boolean(true); Object object1 = new Object(); Object object2 = new Object(); System.out.println("----Object-------"); System.out.println(object1.equals(object2)); System.out.println(object1 == object2); System.out.println(object1.equals(object1)); System.out.println(object1 == object1); System.out.println("-----------"); System.out.println(tem1.equals(tem2)); System.out.println(tem1 == tem2); System.out.println("---Double--------"); System.out.println(double1.equals(double2)); System.out.println(double1 == double2); System.out.println("----Integer-------"); System.out.println(integer1.equals(integer2)); System.out.println(integer1 == integer2); System.out.println("-----------"); System.out.println(str1.equals(str2)); System.out.println(str1 == str2); System.out.println("-----------"); System.out.println("-----------"); System.out.println(str1 == str2); System.out.println("-----------"); System.out.println(date1.equals(date2)); System.out.println(date1 == date2); }}result:
---Object-----
false
false
true
true
---Boolean-----
true
false
---Double-----
true
false
---Integer-----
true
false
---String-----
true
false
---Date-----
true
false
First, when the two objects compared are the same, the results of == and equals() are true, and when the two objects are different, they are returned false. So when == and equals are used to compare objects, they compare the address of the object, but in fact the essence is the same. Here is the code of the equals() method in the Object class:
public boolean equals(Object obj) { return (this == obj); }For Boolean, Double (Float), Interger (Shot, Long), String, and Date, I also found their source codes. Below I posted the source codes of the equals() method in Boolean, Double, Interger, String, and Date. At this time, the equals() method was rewritten because these classes are inherited from the Object class.
Boolean:
public boolean equals(Object obj) { if (obj instanceof Boolean) { return value == ((Boolean)obj).booleanValue(); } return false; }Double:
public boolean equals(Object obj) { return (obj instanceof Double) && (doubleToLongBits(((Double)obj).value) == doubleToLongBits(value)); }Interger:
public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; } String:
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }Date:
public boolean equals(Object obj) { return obj instanceof Date && getTime() == ((Date) obj).getTime(); }In other words, at these times, the equals() method of the Object class is rewritten to compare the actual content of two objects and no longer the address. Of course, it is definitely not just these. Here are just a few common cases where Java native classes rewrite the equals() method of the Object class.
Thank you for reading, I hope it can help you. Thank you for your support for this site!