×

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

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.