Q:

Java Convert boolean to String

belongs to collection: Java Conversion Programs

0

We can convert boolean to String in java using String.valueOf(boolean) method.

Alternatively, we can use Boolean.toString(boolean) method which also converts boolean into String.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

1) String.valueOf()

The String.valueOf() method converts boolean to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:

 

public static String valueOf(boolean b)  

Java boolean to String Example using String.valueOf()

Let's see the simple example of converting boolean to String in java.

public class BooleanToStringExample1{  
public static void main(String args[]){  
boolean b1=true;  
boolean b2=false;  
String s1=String.valueOf(b1);  
String s2=String.valueOf(b2);  
System.out.println(s1);  
System.out.println(s2);  
}}  

 

Output:

true
false

 

2) Boolean.toString()

The Boolean.toString() method converts boolean to String. The toString() is the static method of Boolean class. The signature of toString() method is given below:

 

public static String toString(boolean b)  

Java boolean to String Example using Boolean.toString()

Let's see the simple code to convert boolean to String in java using Boolean.toString() method.

public class BooleanToStringExample2{  
public static void main(String args[]){  
boolean b1=true;  
boolean b2=false;  
String s1=Boolean.toString(b1);  
String s2=Boolean.toString(b2);  
System.out.println(s1);  
System.out.println(s2);  
}}  

 

Output:

true
false

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Convert Date to Timestamp... >>
<< Java Convert String to boolean...