Updated April 13, 2023
Introduction to Java Boolean to String
Java Boolean to String conversion is done in 2 ways. First way is by using valueOf() method and second way is by using toString() method. Boolean in Java represented with true or false values. Human naked eye may be treated it as String but it may or may not be. If it is String type there is no issue but if it is boolean value we have issue while performing any operation with the Strings. So before proceed with boolean values we must convert into String.
Real-Time Scenario: Most of the browser reads the String data directly so if we have any boolean values it is better to convert them into Strings.
How to Convert Boolean to String in Java?
Conversion can be done in 2 ways in java:
- Using valueOf() method
- Using toString() method
1. Using valueOf() method
This method is directly used on String like String.valueOf() method to convert boolean value to String value. It is static method so we have used with class name of String. Method signature is given below.
Syntax:
public static String valueOf(boolean bool)
Internal implementation of valueOf() method:
public static String valueOf(boolean bool) {
returnbool ? "true" : "false";
}
2. Using toString() method
This method is directly used on Boolean like Boolean.toString() method to convert boolean value to String value. It is static method so we have used with class name of Boolean. Method signature is given below.
Syntax:
public static String toString(boolean bool)
Internal implementation of toString() method:
public static String toString(boolean bool) {
returnbool ? "true" : "false";
}
Examples
Given below are the examples mentioned:
Example #1
Converting static boolean values to String by using valueOf() method.
Code:
StaticBooleanStringValueOf.java
//importing packages
//package com.bool.string;
//creating a class
public class StaticBooleanStringValueOf {
// main method to run the java application
public static void main(String[] args) {
// declaring true boolean value
boolean trueValue = true;
// declaring false boolean value
boolean falseValue = false;
// converting Bolean to String by using valueOf() method
String trueString = String.valueOf(trueValue);
String falseString = String.valueOf(falseValue);
// displaying boolean to string output
System.out.println("Converting true Boolean value to String is: " + trueString);
System.out.println("Converting false Boolean value to String is: " + falseString);
}
}
Output:
Example #2
Converting dynamic boolean values to String by using valueOf() method.
Code:
DynamicBooleanStringValueOf.java
//importing packages
//package com.bool.string;
import java.util.Scanner;
//creating a class
public class DynamicBooleanStringValueOf {
// main method to run the java application
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean trueValue = false, falseValue = false;
try {
System.out.println("Enter true as input:");
// Asking user to enter true boolean value
trueValue = scanner.nextBoolean();
System.out.println("Enter false as input:");
// Asking user to enter false boolean value
falseValue = scanner.nextBoolean();
} catch (Exception e) {
System.out.println("You have entered incorrect boolean value");
System.exit(0);
}
// converting Boolean to String by using valueOf() method
String trueString = String.valueOf(trueValue);
String falseString = String.valueOf(falseValue);
// displaying boolean to string output
System.out.println("Converting true Boolean value to String is: " + trueString);
System.out.println("Converting false Boolean value to String is: " + falseString);
scanner.close();
}
}
Output:
If you enter wrong boolean value then:
Example #3
Converting static boolean values to String by using toString() method.
Code:
StaticBooleanStringtoString.java
//importing packages
//package com.bool.string;
//creating a class
public class StaticBooleanStringtoString {
// main method to run the java application
public static void main(String[] args) {
// declaring true boolean value
boolean trueValue = true;
// declaring false boolean value
boolean falseValue = false;
// converting Boolean to String by using toString() method
String trueString = Boolean.toString(trueValue);
String falseString = Boolean.toString(falseValue);
// displaying boolean to string output
System.out.println("Converting true Boolean value to String is: " + trueString);
System.out.println("Converting false Boolean value to String is: " + falseString);
}
}
Output:
Example #4
Converting dynamic boolean values to String by using toString() method.
Code:
DynamicBooleanStringtoString.java
//importing packages
//package com.bool.string;
import java.util.Scanner;
//creating a class
public class DynamicBooleanStringtoString {
// main method to run the java application
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean trueValue = false, falseValue = false;
try {
System.out.println("Enter true as input:");
// Asking user to enter true boolean value
//enter TRUE or true and false or FALSE both are same in java
trueValue = scanner.nextBoolean();
System.out.println("Enter false as input:");
// Asking user to enter false boolean value
falseValue = scanner.nextBoolean();
} catch (Exception e) {
System.out.println("You have entered incorrect boolean value");
System.exit(0);
}
// converting Boolean to String by using toString() method
String trueString = Boolean.toString(trueValue);
String falseString= Boolean.toString(falseValue);
// displaying boolean to string output
System.out.println("Converting true Boolean value to String is: " + trueString);
System.out.println("Converting false Boolean value to String is: " + falseString);
scanner.close();
}
}
Output:
If you enter incorrect boolean value then:
Recommended Articles
This is a guide to Java Boolean to String. Here we discuss how to convert boolean to string in java with respective programming examples for better understanding. You may also have a look at the following articles to learn more –