- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Boolean toString() Method
Description
The Java Boolean toString() returns a String object representing this Boolean's value. If this object represents the value true, a string equal to "true" is returned. Otherwise, a string equal to "false" is returned.
Declaration
Following is the declaration for java.lang.Boolean.toString() method
public String toString()
Overrides
toString in class Object
Parameters
NA
Return Value
This method returns a string representation of this object.
Exception
NA
Getting String Representation from a Boolean as true Example
The following example shows the usage of Boolean toString() method for a boolean object value as true. In this program, we've a Boolean variable with unerlying value as true and using toString() method, we're getting a String as "true" and then result is printed.
package com.tutorialspoint; public class BooleanDemo { public static void main(String[] args) { // create 1 Boolean objects b1 Boolean b1; // assign value to b1 b1 = Boolean.valueOf(true); // create 1 String s1 String s1; // assign string value of object b1 to s1 s1 = b1.toString(); String str1 = "String value of boolean object " + b1 + " is " + s1; // print s1 value System.out.println( str1 ); } }
Output
Let us compile and run the above program, this will produce the following result −
String value of boolean object true is true
Getting String Representation from a Boolean as false Example
The following example shows the usage of Boolean toString() method for a boolean object value as false. In this program, we've a Boolean variable with unerlying value as false and using toString() method, we're getting a String as "false" and then result is printed.
package com.tutorialspoint; public class BooleanDemo { public static void main(String[] args) { // create 1 Boolean objects b1 Boolean b1; // assign value to b1 b1 = Boolean.valueOf(false); // create 1 String s1 String s1; // assign string value of object b1 to s1 s1 = b1.toString(); String str1 = "String value of boolean object " + b1 + " is " + s1; // print s1 value System.out.println( str1 ); } }
Output
Let us compile and run the above program, this will produce the following result −
String value of boolean object false is false
Getting String Representation from a null Boolean Example
The following example shows the usage of Boolean toString() method for a null boolean object value. In this program, we've a Boolean variable with unerlying value as null and using toString() method, we're getting a String as "false" and then result is printed.
package com.tutorialspoint; public class BooleanDemo { public static void main(String[] args) { // create 1 Boolean objects b1 Boolean b1; // assign value to b1 b1 = Boolean.valueOf(null); // create 1 String s1 String s1; // assign string value of object b1 to s1 s1 = b1.toString(); String str1 = "String value of boolean object " + b1 + " is " + s1; // print s1 value System.out.println( str1 ); } }
Output
Let us compile and run the above program, this will produce the following result −
String value of boolean object false is false
To Continue Learning Please Login
Login with Google