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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。