Java long compareTo(Long anotherLong) method example

HQ ┬╗ Java Tutorial ┬╗ java.lang ┬╗ Long ┬╗ compareTo(Long anotherLong)
Java long compareTo(Long anotherLong) method example 2018-02-16T07:26:15+00:00

java.lang.Long compareTo(Long anotherLong)


The Long.compareTo(Long anotherLong) java method Compares two Long objects numerically.



  • specified by┬ácompareTo in interface Comparable

Method Syntax

public int compareTo(Long anotherLong)

Method Argument

Data Type Parameter Description
Long anotherLong the Long to be compared.

Method Returns

The compareTo(Long anotherLong) method of Longclass returns the value 0 if this Long is equal to the argument Long; a value less than 0 if this Long is numerically less than the argument Long; and a value greater than 0 if this Long is numerically greater than the argument Long (signed comparison).


Requires Java 1.2 and up

Java Long compareTo(Long anotherLong) Example

Below is a simple java example on the usage of compareTo(Long anotherLong) method of Long class.

Sample Output

Below is the sample output when you run the above example.

Java Long compareTo(Long anotherLong) example output

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.