当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。