Java Boolean equals() method
The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false.
Syntax
public boolean equals(Object obj)
Parameters
The parameter ‘obj’ represents the Boolean object to compare with.
Return Value
This method returns a Boolean value which are as follows:
- It returns Boolean value true, this object represents the same value
- It returns Boolean value false, if this Boolean object represents different value.
Example 1
public class JavaBooleanEqualsMethodExample1 { public static void main(String[] args) { Boolean a = new Boolean(true); Boolean b = new Boolean(false); // method will check whether a and b are equal or not boolean b3=a.equals(b); System.out.println("equals() method returns : "+b3); } }
Output:
equals() method returns : false
Example 2
public class JavaBooleanEqualsMethodExample2 { static int fact=1; public static void main(String[] args) { Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(true); Scanner scanner=new Scanner(System.in); System.out.print("Enter a no. whose factorial you wanna calculate: "); int a=scanner.nextInt(); if(a<=0 || a>15){ b2=false; System.out.println("Oops! Number out of Reach. \nPlz try a number between 1-15."); } if (b1.equals(b2)){ for (int i=a;i>0;i--){ fact=fact*i; } System.out.println("Fctorial : "+fact); } } }
Output:
Enter a no. whose factorial you wanna calculate: 15 Fctorial : 2004310016
Example 3
public class JavaBooleanEqualsMethodExample3 { public static void main(String[] args) { //for null values it will pass true Boolean b1 = new Boolean(null); Boolean b2 = new Boolean(null); Boolean b3=b1.equals(b2); System.out.println(b3); } }
Output:
true s