java.lang.Integer compare(int x, int y)

Description :

This java tutorial shows how to use the compare(int x, int y) method of Integer class under java.lang package. This method simply returns the int equivalent of the result of comparing the two int method argument.

Method Syntax :

public static int compare(int x,int y)

Parameter Input :

DataType Parameter Description
int x The first int to compare
int y The second int to compare

Method Returns :

The compare(int x,int y) method simply returns the result of the comparison of two int in numerical values

Compatibility Version :

Requires Java 1.7 and up

Exception :

N/A

Discussion :

The compare(int x, int y) method is static thus we should invoke it statically for example Integer.compare(int x, int y). This method simply compares the two int method argument. The following values will be returned:

  • 0 if x is equals to y
  • less than 0 if x is less than y
  • greater than 0 if x is greater than y

Java Code Example :

This java example source code demonstrates the use of compare() method of Integer class. Basically we just explore all the possibilities that the compare method can do.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

/*
 * This example source code demonstrates 
 * the use of compare method of Integer class
 */

public class IntegerCompare {

	public static void main(String[] args) {
		out.println(Integer.compare(100, 100));
		out.println(Integer.compare(100, 98));
		out.println(Integer.compare(100, 102));

	}

}

Sample Output :

Running the compare(int x, int y) method example source code of Integer class will give you the following output

java integer compare method example

java integer compare method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :