Compare two byte values
In this chapter you will learn:
Compare two byte values
We can use compareTo(Byte anotherByte)
to compare two Byte objects numerically.
The following table lists the return value from compareTo(Byte anotherByte)
.
Value | Meanings |
---|---|
0 | if this Byte is equal to the argument Byte; |
less than 0 | if this Byte is numerically less than the argument Byte; |
greater than 0 | if this Byte is numerically greater than the argument Byte (signed comparison). |
Let's give it a try.
public class Main {
public static void main(String[] args) {
//from java 2s . c om
Byte byte1 = new Byte("1");
Byte byte2 = new Byte("2");
System.out.println(byte1.compareTo(byte2));
}
}
The output:
equals(Object obj)
compares this object to the specified object.
The result is true
if and only if the argument is not null and is a
Byte
object that contains the same byte
value as this object.
public class Main {
public static void main(String[] args) {
/*from j a va 2s . c om*/
Byte byte1 = new Byte("1");
Byte byte2 = new Byte("2");
System.out.println(byte1.equals(byte2));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types