Java Boolean 類的 hashCode() 方法返回 Boolean 的哈希碼。
覆蓋:
Boolean 類的 hashCode() 方法覆蓋了 Object 類的 hashCode() 方法。
用法:
public int hashCode()
public static int hashCode(Boolean value)
參數:
NA
value- 是提供給散列的布爾值。
返回值:
此方法返回此布爾對象的哈希碼值。
- 如果對象表示為真,則返回的結果是整數 1231,
- 如果對象表示為假,則返回的結果是整數 1237。
例子1
public class BooleanHashCodeExample1 {
public static void main(String[] args) {
//create two boolean objects b1 and b2
Boolean b1 = true;
Boolean b2 = false;
// assign hash code of b1 and b2
int val1 = b1.hashCode();
int val2 = b2.hashCode();
//print the hash code
System.out.println("1. Hash code of true = "+val1);
System.out.println("2. Hash code of false = "+val2);
}
}
輸出:
1. Hash code of true = 1231 2. Hash code of false = 1237
例子2
import java.util.Scanner;
public class BooleanHashCodeExample2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age");
int a = scanner.nextInt();
if(a>17) {
System.setProperty("flag1", "true");
}
boolean b2 = Boolean.getBoolean("flag1");
// calling static hash code method
int val1 = Boolean.hashCode(b2);
if (val1 == 1231) {
System.out.println("Hash code match i.e. "+val1);
System.out.println("You are an adult.\nYou are eligible to vote.");
}
else{
int val2 =18-a;
System.out.println("You are not an adult.\nYou will be eligible to vote after "+val2+" years.");
}
}
}
輸出:
Enter your age 18 Hash code match i.e. 1231 You are an adult. You are eligible to vote.
例子3
import java.util.Scanner;
public class BooleanHashCodeExample3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Q How many days are there in a leap year?");
System.out.print("Ans:"); int a = scanner.nextInt();
Boolean b1 = false;
if(a==366){
b1 = true;
}
int val1 = Boolean.hashCode(b1);
if(val1==1231){
System.out.println("Answer is right.");
}
else{
System.out.println("Answer is wrong.");
}
}
輸出:
Q How many days are there in a leap year? Ans:366 Answer is right.
相關用法
- Java Boolean hashCode()用法及代碼示例
- Java Boolean booleanValue()用法及代碼示例
- Java Boolean logicalAnd()用法及代碼示例
- Java Boolean toString()用法及代碼示例
- Java Boolean compare()用法及代碼示例
- Java Boolean getBoolean()用法及代碼示例
- Java Boolean valueOf()用法及代碼示例
- Java Boolean logicalXor()用法及代碼示例
- Java Boolean parseBoolean()用法及代碼示例
- Java Boolean compareTo()用法及代碼示例
- Java Boolean logicalOr()用法及代碼示例
- Java Boolean equals()用法及代碼示例
- Java Guava Booleans.concat()用法及代碼示例
- Java Guava Booleans.countTrue()用法及代碼示例
- Java Booleans.indexOf(boolean[] array, boolean[] target)用法及代碼示例
- Java Guava Booleans.lastIndexOf()用法及代碼示例
- Java Guava Booleans.hashCode()用法及代碼示例
- Java Guava Booleans.asList()用法及代碼示例
- Java Guava Booleans.toArray()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Boolean hashcode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。