Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - equals() method



The method determines whether the Number object that invokes the method is equal to the object that is passed as argument.

Syntax

public boolean equals(Object o)

Parameters

o - Any object.

Return Value

The methods returns True if the argument is not null and is an object of the same type and with the same numeric value.

Example - Equality Checks on Integers

Following is an example of the usage of equals method on Integers−

Example.groovy

class Example { 
   static void main(String[] args) { 
      Integer x = 5; 
      Integer y = 10; 
      Integer z = 5; 
		
      // Comparison against an Integer of different value 
      println(x.equals(y));
		
      // Comparison against an Integer of same value 
      println(x.equals(z));  
   } 
}

Output

When we run the above program, we will get the following result −

false 
true

Example - Equality Checks on Doubles

Following is an example of the usage of equals method on Doubles−

Example.groovy

class Example { 
   static void main(String[] args) { 
      Double x = 5.0; 
      Double y = 10.0; 
      Double z = 5.0; 
		
      // Comparison against a Double of different value 
      println(x.equals(y));
		
      // Comparison against a Double of same value 
      println(x.equals(z));  
   } 
}

Output

When we run the above program, we will get the following result −

false 
true

Example - Equality Checks on Longs

Following is an example of the usage of equals method on Longs−

Example.groovy

class Example { 
   static void main(String[] args) { 
      Long x = 5L; 
      Long y = 10L; 
      Long z = 5L; 
		
      // Comparison against a Long of different value 
      println(x.equals(y));
		
      // Comparison against a Long of same value 
      println(x.equals(z));  
   } 
}

Output

When we run the above program, we will get the following result −

false 
true
groovy_numbers.htm
Advertisements