×

Java Integer compare() method

The compare() method of Integer class compares the two specified int values.

Syntax

public static int compare(int x, int y)

Parameters

The parameters ‘x’ and ‘y’ represent the first and second int values to be compared.

Return Value

This method returns an integer value.

  • It returns zero, if both the parameters are equal(x==y).
  • It returns positive one, if x is greater than y(x>y).
  • It returns negative one, if x if smaller than y(x<y).

Example 1

public class JavaIntegerCompareExample1 {
    public static void main(String[] args) {
        Integer val1 = 500 ;
        Integer val2 = 127654 ;
        // It compares the two specified int values
        int val = Integer.compare(val1,val2);
        if (val==0){
            System.out.println(val1+" and "+val2+" are equal.");
        }
        else if (val>0) {
            System.out.println(val1 + " is greater than " + val2);
        }
        else{
            System.out.println(val2 + " is greater than " + val1);
        }
    }
}

Output

127654 is greater than 500

Example 2

import java.util.Scanner;
public class JavaIntegerCompareExample2 {
    public static void main(String[] args) {
        Integer val1 = 90 ;
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter your 12 percentage : ");
        Integer val2 = scanner.nextInt();
        // It compares the two specified int values
        int val = Integer.compare(val1,val2);
        if (val<0){
            System.out.println("Congratulations! Your percentage is with the first cut off list.");
        }
        else{
            System.out.println("Sorry! Your percentage is not in the range of first cut off list. wait for the second cut off.");
        }
    }
}

Output

Enter your 12 percentage : 90
Sorry! Your percentage is not in the range of first cut off list. wait for the second cut off.

Example 3

import java.util.Scanner;
public class JavaIntegerCompareExample3 {
    public static void main(String[] args) {
        Integer val1 = 18 ;
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter your age : ");
        Integer val2 = scanner.nextInt();
        // It compares the two specified int values
        int val = Integer.compare(val1,val2);
        if (val<=0){
            System.out.println("Congratulations! You are an adult. You can vote..");
        }
        else{
            int year=18-val2;
            System.out.println("Sorry! You are not an adult. You can vote after "+year+" years.");
        }
    }
}

Output

Enter your age : 16
Sorry! You are not an adult. You can vote after 2 years.

Related Topics

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.