Java Integer equals() method
The equals() method of Integer class compares the given object to the specified object.
Syntax
public boolean equals(Object obj)
Parameters
The parameter ‘obj’ represents the object to be compared with.
Overrides
The equals() method overrides equals in class Object
Return Value
This method returns:
- Boolean value true, if both the objects are identical
- Else it returns false.
Example 1
public class JavaIntegerEqualsExample1 { public static void main(String[] args) { Integer byte1=127; Integer byte2=607; //It compares two Integer objects numerically boolean val=byte1.equals(byte2); if(val){ System.out.println(byte1+" and "+byte2+" values are equal."); } else{ System.out.println(byte2+" and "+byte1+" are not equal."); } }}
Output
607 and 127 are not equal.
Example 2
import java.util.Scanner; public class JavaIntegerEqualsExample2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter your password : "); Integer val1=scanner.nextInt(); System.out.print("Re-Enter your password : "); Integer val2=scanner.nextInt(); Boolean val=val1.equals(val2); if (val){ System.out.println("New Password confirmed! Your password Matched. "); String str =val1.toString(); if (str.length()>7){ System.out.println("Its a Strong password"); } else{ System.out.println("Its a weak password"); } } else { System.out.println("Retry! Your password didn't matched."); } } }
Output
Enter your password : 12345678 Re-Enter your password : 12345678 New Password confirmed! Your password Matched. Its a Strong password