BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Integer compare() Method

By Chaitanya Singh | Filed Under: java

The compare() method compares two primitive int values passed as arguments to this method. It compares both the int values numerically.

Syntax of compare() method

public static int compare(int x, int y)

compare() Parameters

  • x – First int number to compare.
  • y – Second int number to compare.

compare() Return Value

  • It returns the value 0 if first number is equal to the second number, a value less than 0 if first number < second number, or a value greater than 0 if first number > second number.

Supported java versions: Java 1.7 and onwards.

Example 1

public class JavaExample{
  public static void main(String[] args){
    int x = 125, y = 250, z = 250;
    //comparing x and y, x < y so result is -1
    System.out.println(Integer.compare(x, y));
    //comparing x and z, x < z so result is -1
    System.out.println(Integer.compare(x, z));
    //comparing y and z, y == z so result is 0
    System.out.println(Integer.compare(y, z));
  }
}

Output:

Java Integer compare output1

Example 2

Order of arguments matter in compare() method.

public class JavaExample{
  public static void main(String[] args){
    int x = 10, y = 20;
    
    //First argument is < second argument
    // so the result is -1
    System.out.println(Integer.compare(x, y));

    //Arguments in reverse order: Here, first argument is
    // greater than second argument so the result is 1
    System.out.println(Integer.compare(y, x));
  }
}

Output:

Java Integer compare output2

Example 3

Comparing user entered numbers using Scanner class and java.lang.Integer.compare() method.

import java.util.Scanner;
public class JavaExample{
  public static void main(String[] args){
    int x, y;

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter first int number: ");
    x = scan.nextInt();
    System.out.print("Enter second int number: ");
    y = scan.nextInt();
    scan.close();

    if(Integer.compare(x, y)==0){
      System.out.println("Input numbers are equal");
    }else if(Integer.compare(x, y) > 0){
      System.out.println("First number > second number");
    }else{
      System.out.println("First number < second number");
    }
  }
}

Output:

Comparing user entered numbers using compare() method

Recommended Posts

  • Java Integer byteValue()
  • Java Integer bitCount()
❮ Java Integer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap