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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Conversion can be done in 2 ways in java:
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"; }
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"; }
Given below are the examples mentioned:
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:
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:
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:
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:
The above is the detailed content of Java Boolean to String. For more information, please follow other related articles on the PHP Chinese website!