×

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

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.