×

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String.
  • Using valueOf(boolean) method
  • Using toString(boolean) method
For both the above methods, if the passes Boolean value is true, then returned string will be “true,” similarly for false, the return value will be “false.” In the above methods, Boolean value true is not the same as True or TRUE. Using String.valueOf(boolean) method The valueOf(boolean) is a static method of the String class. It accepts Boolean value as an argument and returns the string representation of Boolean value. The signature of the method is given below. public static String valueOf(boolean b) Where b represent the Boolean variable which we want to convert. Example In the following example, there are two variables str1 and str2 of type String that stores the converted value. The String is a class. The valueOf()  method parses the boolean  value true and false respectively and returns the string representation of boolean value. The first println statement prints the string value of the variable str1 and the second println statement prints the string value of the variable str2.
public class booleanToStringExample
{
public static void main(String args[])
{
String str1=String.valueOf(true);
String str2=String.valueOf(false);       
System.out.println("The string value of str1 is: "+str1);
System.out.println("The string value of str2 is: "+str2);
}
}
Output
The string value of str1 is: true
The string value of str2 is: false

Using Boolean.toString(boolean) method

The toString(boolean) is a static method of the Boolean class. It converts the specified Boolean value to String. It returns the string representation of Boolean value. The signature of the method is given below. public static toString (boolean b) Where b represent the Boolean variable which we want to convert. Example In the following example, there are two variables str1 and str2 of type String that stores the converted value. The String is a class. The valueOf() method parses the boolean value false and true respectively and returns the string representation of boolean value. The first println statement prints the string value of the variable str1 and the second println statement prints the string value of the variable str2.
public class booleanToStringExample1
{
public static void main(String args[])
{
String str1=Boolean.toString(false);
String str2=Boolean.toString(true);       
System.out.println("The string value of str1 is: "+str1);
System.out.println("The string value of str2 is: "+str2);
}
}
Output
The string value of str1 is: false
The string value of str2 is: true

Related Topics

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long. Syntax: public static int incrementExact (int a)public static...

1 minute read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.