java.lang.Integer equals(Object obj)

Description :

This java tutorial shows how to use the equals() method of Integer class under java.lang package. This method return boolean which corresponds to the equality of this Integer and method argument Object. This method override the equals method of Object class.

Method Syntax :

public boolean equals(Object obj)

Parameter Input :

 

DataType Parameter Description
Object obj the object that we want to test equality with this Integer.

 

Method Returns :

The equals(Object obj) method simply returns true if the argument is not null and also if the Integer object contains the same int as the method argument Object, otherwise false.

Compatibility Version :

Requires Java 1.0 and up

Exception :

InputMismatchException

Java Code Example :

This java example source code demonstrates the use of equals(Object obj) method of Integer class. Basically we just compare the int value assigned to this Integer and Object.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * equals() method of Integer class to test for equality
 * of Integer and Object
 */

public class IntegerToString {

	public static void main(String[] args) {
		// declare the Integer object
		Integer intValue = new Integer(100);
		// Declare the Object
		Object obj = new Integer(100);
		// test for equality
		if(intValue.equals(obj)){
			out.println("Integer is equals to the object");
		}
		else{
			out.println("Not equal, something is wrong");
		}

	}

}

Sample Output :

Running the equals(Object obj) method example source code of Integer class will give you the following output

Integer is equals to the object

Exception Scenario :

  • N/A

Similar Method :

  • N/A

Suggested Reading List :

References :