×

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

&amp;&amp; Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 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 HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Polygonal Number in Java

What does a Java Polygonal Number Mean? In mathematics, a polygonal number refers to a number that is expressed through dots or pebbles arranged in a regular polygonal pattern. Alphas are...

4 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.