Java.lang.Integer.equals() Method

Advertisements


Description

The java.lang.Integer.equals() method compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

Declaration

Following is the declaration for java.lang.Integer.equals() method

public boolean equals(Object obj)

Parameters

  • obj -- This is the object to compare with.

Return Value

This method true if the objects are the same, else false.

Exception

  • NA

Example

The following example shows the usage of java.lang.Integer.equals() method.

package com.tutorialspoint;

import java.lang.*;

public class IntegerDemo {

   public static void main(String[] args) {

     Integer obj1 = new Integer(75);
     Integer obj2 = new Integer(75);
     System.out.print("Is obj1 and obj2 equal ? ");
     System.out.println(obj1.equals(obj2));
    
     obj1 = new Integer(32);
     obj2 = new Integer(26);
     System.out.print("Is obj1 and obj2 equal ? ");
     System.out.println(obj1.equals(obj2));
   }
} 

Let us compile and run the above program, this will produce the following result:

Is obj1 and obj2 equal ? true
Is obj1 and obj2 equal ? false



Advertisements
Advertisements