Home > Java > javaTutorial > body text

Java Boolean to String

WBOY
Release: 2024-08-30 15:17:59
Original
566 people have browsed it

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.

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";
}
Copy after login

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";
}
Copy after login
Note: There is no much performance difference between valueOf() and toString() method.

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);
}
}
Copy after login

Output:

Java Boolean to String

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();
}
}
Copy after login

Output:

Java Boolean to String

If you enter wrong boolean value then:

Java Boolean to String

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);
}
}
Copy after login

Output:

Java Boolean to String

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();
}
}
Copy after login

Output:

Java Boolean to String

If you enter incorrect boolean value then:

Java Boolean to String

The above is the detailed content of Java Boolean to String. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template