Compare two long values
In this chapter you will learn:
Compare two long values
We can use the following two methods to compare two long type value.
int compareTo(Long anotherLong)
compares two Long objects numerically.boolean equals(Object obj)
compares this object to the specified object.
compareTo(Long anotherLong)
returns
Value | Meaning |
---|---|
0 | if this Long is equal to the argument Long; |
less than 0 | if this Long is numerically less than the argument Long; |
greater than 0 | if this Long is numerically greater than the argument Long. |
The following code uses the compareTo(Long anotherLong)
method to do the comparison.
public class Main {
public static void main(String[] args) {
Long long1 = new Long(12345L);
Long long2 = new Long("12346");
/* j a va2 s . c om*/
System.out.println(long1.compareTo(long2));
}
}
The output:
If you just want to check the equality,
public class Main {
public static void main(String[] args) {
Long long1 = new Long(12345L);
Long long2 = new Long("12346");
/*from j a va 2 s . c om*/
System.out.println(long1.equals(long2));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types