Both "==" and "equals()" are used to check if two things equal to each other.
The "==" is an operator which can be used to compare primitives as well as Objects. It checks if the two items reference to the same memory location. Since it is an operator, it cannot be overridden.
The "equals()" is a method in the Object class. The "equals()" method in the Object class works the same as the "==" operator. However, since it is a method, it can be overridden to compare based on object components or business logic. As in the case of ArrayList, the equals method compares if the two ArrayLists have the same sequence of objects.
Below are some examples.
String strA = "Welcome!";
String strB = "Wel" + "come!";
String strC = new String(strA);
String strD = new String("Welcome!");
System.out.println("strA == strB? " + (strA == strB));
System.out.println("strA.equals(strB)? " + (strA.equals(strB)));
System.out.println("strA == strC? " + (strA == strC));
System.out.println("strA.equals(strC)? " + (strA.equals(strC)));
System.out.println("strC == strD? " + (strC == strD));
System.out.println("strC.equals(strD)? " + (strC.equals(strD)));
The output:
strA == strB? true
strA.equals(strB)? true
strA == strC? false
strA.equals(strC)? true
strC == strD? false
strC.equals(strD)? true
private ArrayList<String> array;
private int numb;
public ObjA() {
array = new ArrayList<>();
numb = 3;
}
public static void main(String[] args) {
ObjA theFirst = new ObjA();
ObjA theSecond = new ObjA();
ObjA theThird = theFirst;
System.out.println("Is theFirst == theSecond? " + (theFirst == theSecond));
System.out.println("Is theFirst.equals(theSecond)? " + (theFirst.equals(theSecond)));
System.out.println("Is theFirst == theThird? " + (theFirst == theThird));
System.out.println("Is theFirst.equals(theThird)? " + (theFirst.equals(theThird)));
System.out.println("Is theSecond == theThird? " + (theSecond == theThird));
System.out.println("Is theSecond.equals(theThird)? " + (theSecond.equals(theThird)));
}
}
The Outpput:
Is theFirst == theSecond? false
Is theFirst.equals(theSecond)? false
Is theFirst == theThird? true
Is theFirst.equals(theThird)? true
Is theSecond == theThird? false
Is theSecond.equals(theThird)? false
private ArrayList<String> array;
private int numb;
public ObjB() {
array = new ArrayList<>();
numb = 3;
}
public ArrayList getArray() {
return array;
}
public int getNumber() {
return numb;
}
@Override
public boolean equals(Object obj) {
if (obj instancdof ObjB){
ObjB ob = (ObjB)obj;
if (getArray().equals(ob.getArray()) && getNumber() == ob.getNumber()){
return true;
}
}
return false;
}
public static void main(String[] args) {
ObjB theFirst = new ObjB();
ObjB theSecond = new ObjB();
ObjB theThird = theFirst;
System.out.println("Is theFirst == theSecond? " + (theFirst == theSecond));
System.out.println("Is theFirst.equals(theSecond)? " + (theFirst.equals(theSecond)));
System.out.println("Is theFirst == theThird? " + (theFirst == theThird));
System.out.println("Is theFirst.equals(theThird)? " + (theFirst.equals(theThird)));
System.out.println("Is theSecond == theThird? " + (theSecond == theThird));
System.out.println("Is theSecond.equals(theThird)? " + (theSecond.equals(theThird)));
}
}
The Outpput:
Is theFirst == theSecond? false
Is theFirst.equals(theSecond)? true
Is theFirst == theThird? true
Is theFirst.equals(theThird)? true
Is theSecond == theThird? false
Is theSecond.equals(theThird)? true
The "==" is an operator which can be used to compare primitives as well as Objects. It checks if the two items reference to the same memory location. Since it is an operator, it cannot be overridden.
The "equals()" is a method in the Object class. The "equals()" method in the Object class works the same as the "==" operator. However, since it is a method, it can be overridden to compare based on object components or business logic. As in the case of ArrayList, the equals method compares if the two ArrayLists have the same sequence of objects.
Below are some examples.
1. The String class
The String class is peculiar for it can be initiated with a string constant or a new String instance. Two String constants with the same sequence of characters are always equal regardless of using "==" or "equals()". However, it is a good practice to use equals when comparing two Strings.String strA = "Welcome!";
String strB = "Wel" + "come!";
String strC = new String(strA);
String strD = new String("Welcome!");
System.out.println("strA == strB? " + (strA == strB));
System.out.println("strA.equals(strB)? " + (strA.equals(strB)));
System.out.println("strA == strC? " + (strA == strC));
System.out.println("strA.equals(strC)? " + (strA.equals(strC)));
System.out.println("strC == strD? " + (strC == strD));
System.out.println("strC.equals(strD)? " + (strC.equals(strD)));
The output:
strA == strB? true
strA.equals(strB)? true
strA == strC? false
strA.equals(strC)? true
strC == strD? false
strC.equals(strD)? true
2. Without overriding the equals() method
public class ObjA {private ArrayList<String> array;
private int numb;
public ObjA() {
array = new ArrayList<>();
numb = 3;
}
public static void main(String[] args) {
ObjA theFirst = new ObjA();
ObjA theSecond = new ObjA();
ObjA theThird = theFirst;
System.out.println("Is theFirst == theSecond? " + (theFirst == theSecond));
System.out.println("Is theFirst.equals(theSecond)? " + (theFirst.equals(theSecond)));
System.out.println("Is theFirst == theThird? " + (theFirst == theThird));
System.out.println("Is theFirst.equals(theThird)? " + (theFirst.equals(theThird)));
System.out.println("Is theSecond == theThird? " + (theSecond == theThird));
System.out.println("Is theSecond.equals(theThird)? " + (theSecond.equals(theThird)));
}
}
The Outpput:
Is theFirst == theSecond? false
Is theFirst.equals(theSecond)? false
Is theFirst == theThird? true
Is theFirst.equals(theThird)? true
Is theSecond == theThird? false
Is theSecond.equals(theThird)? false
3. With overriding the equals() method
public class ObjB {private ArrayList<String> array;
private int numb;
public ObjB() {
array = new ArrayList<>();
numb = 3;
}
public ArrayList getArray() {
return array;
}
public int getNumber() {
return numb;
}
@Override
public boolean equals(Object obj) {
if (obj instancdof ObjB){
ObjB ob = (ObjB)obj;
if (getArray().equals(ob.getArray()) && getNumber() == ob.getNumber()){
return true;
}
}
return false;
}
public static void main(String[] args) {
ObjB theFirst = new ObjB();
ObjB theSecond = new ObjB();
ObjB theThird = theFirst;
System.out.println("Is theFirst == theSecond? " + (theFirst == theSecond));
System.out.println("Is theFirst.equals(theSecond)? " + (theFirst.equals(theSecond)));
System.out.println("Is theFirst == theThird? " + (theFirst == theThird));
System.out.println("Is theFirst.equals(theThird)? " + (theFirst.equals(theThird)));
System.out.println("Is theSecond == theThird? " + (theSecond == theThird));
System.out.println("Is theSecond.equals(theThird)? " + (theSecond.equals(theThird)));
}
}
The Outpput:
Is theFirst == theSecond? false
Is theFirst.equals(theSecond)? true
Is theFirst == theThird? true
Is theFirst.equals(theThird)? true
Is theSecond == theThird? false
Is theSecond.equals(theThird)? true
---------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.