Java Boolean toString()方法及示例

来源:这里教程网 时间:2026-02-17 20:39:55 作者:

Java Boolean toString()方法及示例

布尔类的 toString() 方法是一个内置的方法,用于以字符串格式返回布尔值。

在Java的布尔类中有两个toString()方法的重载。

public static String toString(boolean value)

语法

Boolean.toString(boolean value)

参数 :它需要一个布尔值作为输入,该值将被转换为字符串。

返回类型 :返回值是布尔值的字符串表示。

下面是说明toString()方法的程序。

程序1 :

// java code to demonstrate// Boolean toString() method  class GFG {    public static void main(String[] args)    {          // boolean type value        boolean value = true;          // static toString() method of Boolean class        String output = Boolean.toString(value);          // printing the value        System.out.println(output);    }}

输出:

true

程序2

// java code to demonstrate// Boolean toString() method  class GFG {    public static void main(String[] args)    {          // boolean type value        boolean value = false;          // static toString() method of Boolean class        String output = Boolean.toString(value);          // printing the value        System.out.println(output);    }}

输出:

false

public String toString()

语法

BooleanObject.toString()

返回类型: 返回值是调用此方法的布尔实例的一个字符串表示。

下面的程序说明了上述定义的方法。

程序1 :

// java code to demonstrate// Boolean toString() method  class GFG {    public static void main(String[] args)    {          // creating a Boolean object        Boolean b = new Boolean(true);          // toString method of Boolean class        String output = b.toString();          // printing the output        System.out.println(output);    }}

输出:

true

程序2

// Java code to demonstrate// Boolean toString() method  class GFG {    public static void main(String[] args)    {          // creating a Boolean object        Boolean b = new Boolean(false);          // toString method of Boolean class        String output = b.toString();          // printing the output        System.out.println(output);    }}

输出:

false

相关推荐