當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Boolean valueOf()用法及代碼示例


Java Boolean 類的 valueof() 方法返回一個與定義的布爾值或定義的字符串相對應的布爾值實例。

如果定義的布爾值或字符串值為真,則此方法返回布爾值 'true',如果定義的布爾值或字符串值為假,則返回布爾值 'false'。

用法:

public static Boolean valueOf(Boolean b)
public static Boolean valueOf(String s)

參數:

b- 這是一個要傳遞的布爾參數

s- 這是要傳遞的字符串參數。

返回值:

valueOf() 方法返回對應於 'b' 或字符串 's' 的布爾實例。

  • 如果定義的布爾值 (b) 或字符串值為真,則返回真。
  • 如果布爾值 b 為假,則返回假
  • 如果字符串包含除 true 以外的任何值,則返回 false。

例子1

public class BooleanValueOfExample1 {
    public static void main(String[] args) {
        Boolean b1 = true;
        // will return a boolean instance corresponding to Boolean b1
        Boolean b2 = Boolean.valueOf(b1);
        System.out.println("valueOf() method will return = "+b2);
        Boolean b3 = Boolean.valueOf(false);
        System.out.println("valueOf() method will return = "+b3);
       }
}

輸出:

valueOf() method will return = true
valueOf() method will return = false

例子2

import java.util.Scanner;

public class BooleanValueOfExample2 {
    public static void main(String[] args) {
        Boolean b1 = true;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number whose table you want to print:");
        int n =scanner.nextInt();
        if(n<0||n>20){
            b1 = false;
            System.out.println("Please enter a valid number between 0-20.");
        }
      // will return the boolean value of b1
        if(Boolean.valueOf(b1)==true) {
            System.out.println("Table of "+n+":");
            for (int i = 1; i < 11; i++) {
                System.out.println(n +" * "+  i + " = " + n * i);
            }
        }

    }
}

輸出:

Enter the number whose table you want to print:19
Table of 19:
19 * 1 = 19
19 * 2 = 38
19 * 3 = 57
19 * 4 = 76
19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190

例子3

import java.util.Scanner;

public class BooleanValueOfExample3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter you age");
        int a = scanner.nextInt();
        if(a>17) {
            System.setProperty("flag1", "true");
        }
        boolean b2 = Boolean.getBoolean("flag1");
        if (Boolean.valueOf(b2) == true) {
            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 you age
78
You are an adult.
You are eligible to vote.

示例 4

import java.util.Scanner;

public class BooleanValueOfExample4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("True/False:");
        System.out.println("Q. TalMahal is located in Delhi.");
        System.out.print("Ans "); String str1 = scanner.next();
       // will return boolean value corresponding to the String str
        Boolean b1 = Boolean.valueOf(str1);
          if (b1 ==true) {
            System.out.println("Correct answer.\nAs TajMahal is located in Agra nor in Delhi.");
        } else  {
            System.out.println("Incorrect. Because TajMahal is located in Agra.");
        }
    }
}

輸出:

True/False:
Q. TalMahal is located in Delhi.
Ans false
Incorrect. Because TajMahal is located in Agra.

例 5

public class BooleanValueOfExample5 {
    public static void main(String[] args) {
     //for "true" (ignoring the case) string value this method will return true.
        String str = "True";
        Boolean b1 = Boolean.valueOf(str);
        System.out.println("1. Boolean value for "+str+" is "+b1);
     //for "false" String value this method will return false
        String str1= "false";
        Boolean b2 = Boolean.valueOf(str1);
        System.out.println("2. Boolean value for "+str1+" is "+b2);
    //for any String value other than "true" this method will return false.
        String str2 = "hwhfjh";
        Boolean b3 = Boolean.valueOf(str2);
        System.out.println("3. Boolean value for "+str2+" is "+b3);

    }
}

輸出:

1. Boolean value for True is true
2. Boolean value for false is false
3. Boolean value for hwhfjh is false





相關用法


注:本文由純淨天空篩選整理自 Java Boolean valueOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。