Guava – Chars.compare()方法与实例
Guava的Chars类的Chars.compare()方法是用来比较两个指定的char值。这些值被作为参数传递,比较的结果是第一个值和第二个值的差。因此,它可以是正数、零或负数。
语法:
public static int compare(char a, char b)
参数: 这个方法接受两个参数。
a: 这是第一个要比较的char对象。b: 这是第二个要比较的char对象。返回类型。 该方法返回一个int值。它的返回值是:
如果’a’等于’b’则为0,正值’a’大于’b’,负值’a’小于’b’异常情况。 该方法没有任何异常。
示例1:
// Java code to show implementation of// Guava's Chars.compare() methodimport com.google.common.primitives.Chars; class GFG { public static void main(String[] args) { char a = 'c'; char b = 'c'; // compare method in Char class int output = Chars.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); }}
输出:
Comparing c and c : 0
示例2:
// Java code to show implementation of// Guava's Chars.compare() methodimport com.google.common.primitives.Chars; class GFG { public static void main(String[] args) { char a = 'd'; char b = 'D'; // compare method in Char class int output = Chars.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); }}
输出:
Comparing d and D : 32
示例3:
// Java code to show implementation of// Guava's Chars.compare() methodimport com.google.common.primitives.Chars; class GFG { public static void main(String[] args) { char a = 'E'; char b = 'c'; // compare method in Char class int output = Chars.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); }}输出:
Comparing E and c : -30
