In this article, we will discuss various ways to convert Boolean to String in Java
Q) What is the need of converting primitive boolean or Boolean wrapper-type to String ?
- Generally, whenever we receive or send data from web application then it is passed in the form of String only
- After logical expression evaluation, results are stored in Boolean flags which needs to be converted into String for displaying purpose
- This article explains about primitive boolean or Boolean wrapper-type to String conversion only, but we can do conversion for other types like int, double, float, long, etc. to String object
- Note: Likewise, sometime String to Boolean conversion is also required
1. Various ways to convert Boolean to String:
- Using Boolean.toString(boolValue);
- Using String.valueOf(boolValue);
- Using String.format(type, boolValue);
- Create Boolean object and then convert to String using toString() method {new Boolean(boolValue).toString();}
- Adding double quotes (“”) to boolean value {i.e.; “” + boolValue;}
- Using append() method of StringBuffer & StringBuilder
Read String class in detail with example
Let us move forward and discuss all possible ways to convert Boolean to String in Java
1.1 Using Boolean.toString(boolValue); method
- This method can be used to convert primitive boolean data-type or Boolean wrapper-type to String object
Method signature:
public static String toString(boolean b);
ConvertBooleanIntoStringUsingToStringMethod.java
package in.bench.resources.bool.to.string.conversion;
public class ConvertBooleanIntoStringUsingToStringMethod {
public static void main(String[] args) {
// primitive boolean data-type
boolean boolValue1 = true;
// 1. converting boolean to String
// by using toString() method
String str1 = Boolean.toString(boolValue1);
System.out.println("1. Converted"
+ " primitive boolean TRUE to String value is : "
+ str1);
// primitive boolean data-type (negative value)
boolean boolValue2 = false;
// 2. converting boolean to String
// by using toString() method
String str2 = Boolean.toString(boolValue2);
System.out.println("\n2. Converted"
+ " primitive boolean FALSE to String value is : "
+ str2);
// Boolean object
Boolean boolValue3 = new Boolean(true);
// 3. converting Boolean object to String
// by using toString() method
String str3 = Boolean.toString(boolValue3);
System.out.println("\n3. Converted"
+ " Boolean object to String value is : "
+ str3);
}
}
Output:
1. Converted primitive boolean TRUE to String value is : true
2. Converted primitive boolean FALSE to String value is : false
3. Converted Boolean object to String value is : true
1.2 Using String.valueOf(boolValue); method
- This method can be used to convert primitive boolean data-type or Boolean wrapper-type to String object
Method signature:
public static Boolean valueOf(boolean b);
ConvertBooleanIntoStringUsingValueOfMethod.java
package in.bench.resources.bool.to.string.conversion;
public class ConvertBooleanIntoStringUsingValueOfMethod {
public static void main(String[] args) {
// primitive boolean data-type
boolean boolValue1 = true;
// 1. converting long to String
// by using valueOf() method
String str1 = String.valueOf(boolValue1);
System.out.println("1. Converted"
+ " primitive boolean TRUE to String value is : "
+ str1);
// primitive boolean data-type (negative value)
boolean boolValue2 = false;
// 2. converting boolean to String
// by using valueOf() method
String str2 = String.valueOf(boolValue2);
System.out.println("\n2. Converted"
+ " primitive boolean FALSE to String value is : "
+ str2);
// Boolean object
Boolean boolValue3 = new Boolean(false);
// 3. converting Long object to String
// by using valueOf() method
String str3 = String.valueOf(boolValue3);
System.out.println("\n3. Converted"
+ " Boolean object to String value is : "
+ str3);
}
}
Output:
1. Converted primitive boolean TRUE to String value is : true
2. Converted primitive boolean FALSE to String value is : false
3. Converted Boolean object to String value is : false
1.3 Using String.format(type, number); method
- This method can be used to convert primitive boolean data-type or Boolean wrapper-type to String object
Method signature:
public static String format(String format, Object... args);
ConvertBooleanIntoStringUsingFormatMethod.java
package in.bench.resources.bool.to.string.conversion;
public class ConvertBooleanIntoStringUsingFormatMethod {
public static void main(String[] args) {
// primitive boolean data-type
boolean boolValue1 = true;
// 1. converting boolean to String
// by using format() method
String str1 = String.format("%b", boolValue1);
System.out.println("1. Converted"
+ " primitive boolean TRUE to String value is : "
+ str1);
// primitive long data-type (negative value)
boolean boolValue2 = false;
// 2. converting negative long to String
// by using format() method
String str2 = String.format("%b", boolValue2);
System.out.println("\n2. Converted"
+ " primitive boolean FALSE to String value is : "
+ str2);
// Boolean object
Boolean boolValue3 = new Boolean(true);
// 3. converting Boolean object to String
// by using format() method
String str3 = String.format("%b", boolValue3);
System.out.println("\n3. Converted"
+ " Boolean object to String value is : "
+ str3);
}
}
Output:
1. Converted primitive boolean TRUE to String value is : true
2. Converted primitive boolean FALSE to String value is : false
3. Converted Boolean object to String value is : true
1.4 Create Boolean object and then convert to String using toString() method
- Here, we will create new Boolean object with primitive bolean value as constructor-argument
- After creating new Boolean object by passing primitive boolean value, then invoke toString() method for converting primitive boolean data-type to String object
Method signature:
public Boolean(boolean value);
public String toString();
ConvertBooleanIntoStringUsingObjectCreation.java
package in.bench.resources.bool.to.string.conversion;
public class ConvertBooleanIntoStringUsingObjectCreation {
public static void main(String[] args) {
// primitive boolean data-type
boolean boolValue1 = true;
// 1. converting boolean to String
// by creating Long object
Boolean boolObj1 = new Boolean(boolValue1);
// and then invoke toString(); method
String str1 = boolObj1.toString();
System.out.println("1. Converted"
+ " primitive boolean TRUE to String value is : "
+ str1);
// primitive long data-type (negative value)
boolean boolValue2 = false;
// 2. converting negative long to String
// by creating Long object
Boolean boolObj2 = new Boolean(boolValue2);
// and then invoke toString(); method
String str2 = boolObj2.toString();
System.out.println("\n2. Converted"
+ " primitive boolean FALSE to String value is : "
+ str2);
// Boolean object
Boolean boolValue3 = new Boolean(false);
// 3. converting Long object to String
// by creating Long object
String str3 = boolValue3.toString();
System.out.println("\n3. Converted"
+ " Boolean object to String value is : "
+ str3);
}
}
Output:
1. Converted primitive boolean TRUE to String value is : true
2. Converted primitive boolean FALSE to String value is : false
3. Converted Boolean object to String value is : false
1.5 Adding double quotes (“”) to Boolean flag-value
- Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation
Syntax:
String temp = “” + boolValue;
ConvertBooleanIntoStringByAddingDoubleQuotes.java
package in.bench.resources.bool.to.string.conversion;
public class ConvertBooleanIntoStringByAddingDoubleQuotes {
public static void main(String[] args) {
// primitive boolean data-type
boolean boolValue1 = true;;
// 1. converting boolean to String by adding ""
String str1 = "" + boolValue1;
System.out.println("1. Converted"
+ " primitive boolean TRUE to String value is :"
+ str1);
// primitive boolean data-type (negative value)
boolean boolValue2 = false;
// 2. converting negative boolean to String by adding ""
String str2 = "" + boolValue2;
System.out.println("\n2. Converted"
+ " primitive boolean FALSE to String value is : "
+ str2);
// Boolean object
Boolean boolValue3 = new Boolean(true);
// 3. converting Boolean object to String by adding ""
String str3 = "" + boolValue3;
System.out.println("\n3. Converted"
+ " Boolean object to String value is : "
+ str3);
}
}
Output:
1. Converted primitive boolean TRUE to String value is : true
2. Converted primitive boolean FALSE to String value is : false
3. Converted Boolean object to String value is : true
1.6 Using append() method of StringBuffer & StringBuilder
- This method can be used to convert primitive boolean data-type to String object by appending to StringBuffer or StringBuilder object
- And then invoking toString() method
Syntax:
StringBuffer sb = sb.append(boolValue);
String temp = sb.toString();
ConvertBooleanIntoStringByAppending.java
package in.bench.resources.bool.to.string.conversion;
public class ConvertBooleanIntoStringByAppending {
public static void main(String[] args) {
// primitive boolean data-type
boolean boolValue1 = true;
// 1. converting boolean to String
// by using append() method
StringBuffer sb1 = new StringBuffer();
String str1 = sb1.append(boolValue1).toString();
System.out.println("1. Converted"
+ " primitive boolean TRUE to String value is : "
+ str1);
// primitive boolean data-type (negative value)
boolean boolValue2 = false;
// 2. converting boolean to String
// by using append() method
StringBuffer sb2 = new StringBuffer();
String str2 = sb2.append(boolValue2).toString();
System.out.println("\n2. Converted"
+ " primitive boolean FALSE to String value is : "
+ str2);
// Boolean object
Boolean boolValue3 = new Boolean(false);
// 3. converting Boolean object to String
// by using append() method
StringBuffer sb3 = new StringBuffer();
String str3 = sb3.append(boolValue3).toString();
System.out.println("\n3. Converted"
+ " Boolean object to String value is : "
+ str3);
}
}
Output:
1. Converted primitive boolean TRUE to String value is : true
2. Converted primitive boolean FALSE to String value is : false
3. Converted Boolean object to String value is : false
2. Auto-boxing and un-boxing feature from Java 1.5 version:
- parseBoolean() method returns primitive boolean data-type, but it can be easily used as Boolean wrapper-type
- as auto-boxing feature helps to convert primitive data-type to respective wrapper-types
- similarly, Boolean.valueOf() method returns Boolean wrapper-type, but it can also be used as primitive boolean
- let’s see one example on this auto-boxing feature
Q) What if we want to convert to Boolean wrapper-type ?
- Auto-boxing feature available from Java 1.5 version
- So, converting primitive data-type to respective wrapper-type can easily be done, by directly assigning
- Let’s see one example based on this auto-boxing feature
AutoBoxingFeatureForBooleanConversion.java
package in.bench.resources.string.to.bool.conversion;
public class AutoBoxingFeatureForBooleanConversion {
public static void main(String[] args) {
// String (either true or false)
String str1 = "true";
// converting String to boolean
boolean boolValue1 = Boolean.parseBoolean(str1);
// 1. Auto-Boxing - converting boolean to Boolean
Boolean boolAutoBoxing = boolValue1;
System.out.println("1. Auto-Boxing : "
+ boolAutoBoxing);
// String (either true or false)
String str2 = "False";
// converting String to Boolean
Boolean boolValue2 = Boolean.valueOf(str2);
// 2. Un-Boxing - converting Boolean to boolean
boolean boolUnBoxing = boolValue2;
System.out.println("\n2. Un-Boxing : "
+ boolUnBoxing);
}
}
Output:
1. Auto-Boxing : true
2. Un-Boxing : false
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- String to int conversion – 3 ways
- Integer to String conversion – 6 ways
- String to float conversion – 3 ways
- Float to String conversion – 6 ways
- String to double conversion – 3 ways
- Double to String conversion – 6 ways
- String to long conversion – 3 ways
- Long to String conversion – 6 ways
- String to boolean conversion – 3 ways
- Boolean to String conversion – 6 ways
- String to char conversion
- Character to String conversion – 6 ways
- String to char[] array conversion – 4 ways
- Character[] array to String conversion – 5 ways
- String to byte conversion – 3 ways
- Byte to String conversion – 5 ways
- String to byte[] array conversion
- Byte[] array to String conversion
- String to short conversion – 3 ways
- Short to String conversion – 5 ways
- StringBuffer to String conversion and vice-versa
- StringBuilder to String conversion and vice-versa
- String to Date conversion
- Date to String conversion
References:
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
- https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- https://docs.oracle.com/javase/tutorial/java/data/converting.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/Boolean.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html
Happy Coding !!
Happy Learning !!