Dark Mode On/Off

# Java Long compareUnsigned() Method

Java` compareUnsigned()` method belongs to the `Long` class of the `java.lang` package and is specified by `Comparable<Long>` interface. This method is used to compare the unsigned value of two long values passed to find which one is greater than the other.

The `compareUnsigned(long x, long y)` method returns the value:

• 0; if the unsigned value of x and y are equal.

• -1; if the unsigned value of x is less than y.

• 1; if the unsigned value of x is more than y.

### Syntax:

``public static int compareUnsigned(long x, long y)``

### Parameters:

The parameters passed consists of two long values `long x` and `long y` that represents the first and second long values to be compared as an unsigned value respectively.

### Returns:

Value 0, 1, -1 is returned depending upon the values of x and y.

## Example 1:

Here, four long values n1, n2, n3, n4 are taken with respective values and are checked for the values are either less, more, or equal than the compared.

``````import java.lang.Long;

public class StudyTonight
{
public static void main(String[] args)
{
long n1 = 100L;
long n2 = -200L;
long n3 = 300L;
long n4 = 100L;

System.out.println(Long.compareUnsigned(n1, n2));  //Output will be less than zero

System.out.println(Long.compareUnsigned(n3, n1)); // Output will be greater than zero

System.out.println(Long.compareUnsigned(n1,n4));  // Output will be equal to zero
}
}``````

-1
1
0

## Example 2:

Here, four long values n1, n2, n3, n4 are taken in an array and are checked for the values are either less, more, or equal when compared with the first value of the array.

``````import java.lang.Long;

public class StudyTonight
{
public static void main(String[] args)
{
long []n = {-100L,-200L,300L,100L};

for(int i=0;i<4;i++)
{
System.out.println(Long.compareUnsigned(n, n[i]));
}
}
}``````

0
1
1
1

## Example 3:

Here is a general example where the user can enter the numbers of his choice and get the desired output. The `try` and `catch` block is used for handling any exception taking place during the execution of the program. (For example, the user enters a String, etc. in place of the long value).

``````import java.util.Scanner;
import java.lang.Long;

public class StudyTonight
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first and second number ");
try
{
long n1 = sc.nextLong();
long n2 = sc.nextLong();
int r =  Long.compareUnsigned(n1, n2);
if(r > 0)
{
System.out.println("first number is greater");
}
else if(r< 0)
{
System.out.println("second number is greater");
}
else
{
System.out.println("both numbers are equal");
}
}
catch(Exception e)
{
System.out.println("Error!!");
}
}
}  ``````

Enter first and second number 45 84
second number is greater
*********************************************
Enter first and second number 90 12
first number is greater
*********************************************
Enter first and second number 0x67 0x68
Error!!

## Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6828527,"math_prob":0.96604955,"size":3056,"snap":"2022-27-2022-33","text_gpt3_token_len":758,"char_repetition_ratio":0.16480996,"word_repetition_ratio":0.15592515,"special_character_ratio":0.29777488,"punctuation_ratio":0.18571429,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97491467,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-19T10:36:13Z\",\"WARC-Record-ID\":\"<urn:uuid:f20ff87d-4cf7-48b9-8018-ec9c498fd8f7>\",\"Content-Length\":\"263203\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c6b0182b-753d-48a8-9748-a9e252f036f5>\",\"WARC-Concurrent-To\":\"<urn:uuid:2532e8a6-2e26-4a7e-ae03-aae3c8bc69c7>\",\"WARC-IP-Address\":\"65.2.112.116\",\"WARC-Target-URI\":\"https://www.studytonight.com/java-wrapper-class/java-long-compareunsigned-method\",\"WARC-Payload-Digest\":\"sha1:3WNKIK5QUENMRVGGF63HLBGOKFUJVCAB\",\"WARC-Block-Digest\":\"sha1:BGBVZLWNWFPA6PZKXD2G5CJT73Q2AV5X\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882573667.83_warc_CC-MAIN-20220819100644-20220819130644-00117.warc.gz\"}"}