Java Byte compare()方法及示例
Byte类的 compare() 方法是Java中的一个内置方法,用于比较两个字节的值。
语法
Byte.compare(byte a, byte b)
参数: 它需要两个字节值’a’和’b’作为要比较的输入参数。
返回值: 它返回一个 int 值。它返回。
如果’a’等于’b’,返回0。正值’a’大于’b’。负值’b’大于’a’。下面是比较()方法在Java中的实现。
例1 :
// Java code to demonstrate// Byte compare() method class GFG { public static void main(String[] args) { // byte value 'a' byte a = 20; // byte value 'b' byte b = 20; // compare method of Byte class int output = Byte.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); }}
输出:
Comparing 20 and 20 : 0
例2 :
// Java code to demonstrate// Byte compare() method class GFG { public static void main(String[] args) { // byte value 'a' byte a = 20; // byte value 'b' byte b = 10; // compare method of Byte class int output = Byte.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); }}
输出:
Comparing 20 and 10 : 10
例3 :
// Java code to demonstrate// Byte compare() method class GFG { public static void main(String[] args) { // byte value 'a' byte a = 10; // byte value 'b' byte b = 20; // compare method of Byte class int output = Byte.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); }}输出:
Comparing 10 and 20 : -10
