Java.lang.Boolean.toString() 方法
描述
java.lang.Boolean.toString(boolean b) 返回一个表示指定布尔值的 String 对象。 如果指定的布尔值为真,则返回字符串 "true" ,否则返回字符串 "false"。
声明
以下是 java.lang.Boolean.toString() 方法的声明。
public static String toString(boolean b)
参数
b − 要转换的布尔值
返回值
此方法返回指定布尔值的字符串表示形式。
异常
NA
示例
下面的例子展示了 lang.Boolean.toString() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class BooleanDemo {
public static void main(String[] args) {
// create 2 boolean primitives bool1, bool2
boolean bool1, bool2;
// assign values to bool1, bool2
bool1 = true;
bool2 = false;
// create 2 String's s1, s2
String s1, s2;
/**
* static method is called using class name
* assign string value of primitives bool1, bool2 to s1, s2
*/
s1 = Boolean.toString(bool1);
s2 = Boolean.toString(bool2);
String str1 = "String value of boolean primitive " +bool1+ " is " +s1;
String str2 = "String value of boolean primitive " +bool2+ " is " +s2;
// print s1, s2 values
System.out.println( str1 );
System.out.println( str2 );
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
String value of boolean primitive true is true String value of boolean primitive false is false

