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 compareUnsigned() Method

By Chaitanya Singh | Filed Under: java

The compareUnsigned() method compares two primitive int values without considering their sign. It is same as compare() method except that it doesn’t take sign into consideration while comparison.

Syntax of compareUnsigned() method

public static int compareUnsigned(int x, int y)

compareUnsigned() Parameters

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

compareUnsigned() Return Value

It returns:

  • 0, if x == y
  • a value less than 0, if x < y (unsigned)
  • a value greater than 0, if x > y (unsigned)

Supported java versions: Java 1.8 and onwards

Example 1

public class JavaExample{
  public static void main(String[] args){
    int x = 32, y = -41; //negative 41
    int x2 = 32, y2 = 41; // positive 41

    //both the results are same because signs are not considered
    System.out.println(Integer.compareUnsigned(x, y));
    System.out.println(Integer.compareUnsigned(x2, y2));
  }
}

Output:

Java Integer compareUnsigned Output1

Example 2

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();

    //compare user input values
    int result  = Integer.compareUnsigned(x, y);
    if(result ==0) {
      System.out.println("Input numbers are equal");
    }else if(result < 0){
      System.out.println("First number is less than second");
    }else{
      System.out.println("First number is greater than second");
    }
  }
}

Output:

Integer compareUnsigned in Java Output2

Example 3

public class JavaExample{
  public static void main(String[] args){
    Integer x = new Integer("100");
    Integer y = new Integer("-200");
    System.out.println(Integer.compareUnsigned(x, y));
  }
}

Output:

Integer compareUnsigned Output3

Recommended Posts

  • Java Integer compareTo()
  • Java Integer compare()
  • 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