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() method
import 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() method
import 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() method
import 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
相关用法
- Java Guava Doubles.min()用法及代码示例
- Java Guava Doubles.contains()用法及代码示例
- Java Guava Floats.contains()用法及代码示例
- Java Guava Bytes.contains()用法及代码示例
- Java Guava Longs.contains()用法及代码示例
- Java Guava Shorts.min()用法及代码示例
- Java Guava Longs.min()用法及代码示例
- Java Guava Chars.min()用法及代码示例
- Java Guava Floats.min()用法及代码示例
- Java Guava Longs.max()用法及代码示例
- Java Guava Doubles.max()用法及代码示例
- Java Guava Chars.max()用法及代码示例
- Java Guava Floats.max()用法及代码示例
- Java Guava Booleans.contains()用法及代码示例
- Java Guava Shorts.contains()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | Chars.compare() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。