java valueof
Syntax:
句法:
public static Boolean valueOf (boolean value);
public static Boolean valueOf (String value);
布尔类valueOf()方法 (Boolean class valueOf() method)
valueOf() method is available in java.lang package.
valueOf()方法在java.lang包中可用。
valueOf(boolean value) method is used to represent Boolean object denoted by the given argument (value) is of boolean type.
valueOf(boolean value)方法用于表示由给定参数(值)表示的布尔对象是布尔类型。
valueOf(String value) method is used to represent Boolean object holding the boolean value denoted by the given argument (value) is of String type.
valueOf(String value)方法用于表示布尔对象,该布尔对象持有由给定参数(值)表示的布尔值是String类型。
valueOf(boolean value) and valueOf(String value) methods do not throw an exception at the time of returning a Boolean instance.
返回布尔型实例时, valueOf(boolean value)和valueOf(String value)方法不会引发异常。
These are static methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.
这些是静态方法,它们也可以使用类名进行访问,并且,如果我们尝试使用类对象访问这些方法,那么也不会出错。
Parameter(s):
参数:
In the first case, boolean value – represents the value of boolean type.
在第一种情况下, 布尔值 –表示布尔类型的值。
In the second case, String value –represents the value of String type.
在第二种情况下, 字符串值 –表示字符串类型的值。
Return value:
返回值:
In the first case, the return type of this method is Boolean - it returns the Boolean representation of this boolean argument.
在第一种情况下,此方法的返回类型为Boolean-返回此布尔参数的布尔表示形式。
Note:
注意:
If the given argument value is true, it returns Boolean.TRUE.
如果给定的参数值为true,则返回Boolean.TRUE 。
If the given argument value is false, it returns Boolean.FALSE.
如果给定的参数值为false,则返回Boolean.FALSE 。
In the second case, the return type of this method is Boolean - it returns the Boolean representation of this String argument.
在第二种情况下,此方法的返回类型为Boolean-返回此String参数的布尔表示形式。
Note:
注意:
If the given argument value is either not null or true (true can be sensitive or insensitive), it returns true.
如果给定的参数值不为null或true(true可以是敏感或不敏感的),则返回true。
Example:
例:
// Java program to demonstrate the example
// of valueOf() method of Boolean class
public class ValueOfBooleanClass {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
// Display b1,b2 values
System.out.println("b1:" + b1);
System.out.println("b2:" + b2);
// It returns Boolean object holding the value
// denoted by the given boolean argument
Boolean value1 = Boolean.valueOf(b1);
Boolean value2 = Boolean.valueOf(b2);
// String object initialization for valueOf(String s)
String s = "80";
// It returns Boolean object holding the value
// denoted by the given String argument
Boolean value3 = Boolean.valueOf(s);
// Display result values
System.out.println("Boolean.valueOf(b1): " + value1);
System.out.println("Boolean.valueOf(b2): " + value2);
System.out.println("Boolean.valueOf(s): " + value3);
}
}
Output
输出量
b1:true
b2:false
Boolean.valueOf(b1): true
Boolean.valueOf(b2): false
Boolean.valueOf(s): false
翻译自: https://www.includehelp.com/java/boolean-class-valueof-method-with-example.aspx
java valueof