equals
is used for non-primitives basically for Objects.==
is used for primitives.
So, you can use it
public static boolean checkStatus (int a) {
if (a == 10)
return true;
return false;
}
Example 1: For object, if equals method are overridden, then "equals" method will return true.
public class Employee {
int id;
@Override
public boolean equals(Object obj) {
Employee e = (Employee) obj;
return id == e.id;
}
Employee(int id) {
this.id = id;
}
public static void main(String[] args) {
Employee e1 = new Employee(5);
Employee e2 = new Employee(5);
System.out.println("e1.equals(e2) is: " + e1.equals(e2));
System.out.println("(e1 == e2) is: " + (e1 == e2));
}
}
Output:
e1.equals(e2) is: true(e1 == e2) is: false
Example 2: For object, if equals method are not overridden, then "equals" method works as "=="
public class Employee {
int id;
Employee(int id) {
this.id = id;
}
public static void main(String[] args) {
Employee e1 = new Employee(5);
Employee e2 = new Employee(5);
System.out.println("e1.equals(e2) is: " + e1.equals(e2));
System.out.println("(e1 == e2) is: " + (e1 == e2));
}
}
Output:
e1.equals(e2) is: false(e1 == e2) is: false
Here "equals" method works as "==". So, don't forget to override the equals method for object.
Original Link: Whats wrong with this boolean method?
No comments:
Post a Comment