×

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 interface Comparable< Integer >

Return Value

This method returns an integer value.

  • It returns zero, if this Integer is equal to the parameter.
  • It returns positive one, if this Integer is numerically greater than the argument Integer.
  • It returns negative one, if this Integer is numerically smaller than the argument Integer.

Example 1

public class JavaIntegerCompareToExample1 {
    public static void main(String[] args) {
        Integer val1 = 500 ;
        Integer val2 = 127654 ;
        // It compares two Integer objects numerically
        int val = val1.compareTo(val2);
        if(val==0){
            System.out.println("Both 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 JavaIntegerCompareToExample2 {
    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 two Integer objects numerically.
        int val = val1.compareTo(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 : 70
Congratulations! You are an adult. You can vote..

Example 3

import java.util.Scanner;
public class JavaIntegerCompareToExample3 {
    public static void main(String[] args) {
        Integer val1 = 90 ;
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter you 12 percentage : ");
        Integer val2 = scanner.nextInt();
        // It  compares two Integer objects numerically.
        int val = val1.compareTo(val2);
        if (val<0){
            System.out.println("Congratulations! You 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 you 12 percentage : 98
Congratulations! You percentage is with the first cut off list.

Related Topics

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 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.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.