Compare integer values
In this chapter you will learn:
Compare two integer values
int compareTo(Integer anotherInteger)
compares two Integer objects numerically.boolean equals(Object obj)
compares this object to the specified object.
The following table lists the return values from compareTo(Integer anotherInteger)
Value | Description |
---|---|
0 | if this Integer is equal to the argument Integer; |
less than 0 | if this Integer is less than the argument Integer; |
greater than 0 | if this Integer is greater than the argument Integer. |
Let's look at an example.
public class Main {
public static void main(String[] args) {
/*ja va 2 s .c om*/
Integer integer1 = new Integer("1");
Integer integer2 = new Integer("2");
System.out.println(integer1.compareTo(integer2));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types