Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Convert Boolean to String in Java

Tags: boolean string

In this tutorial, we will learn how to convert a primitive data type Boolean to a non primitive data type string in Java easily.

Sometimes, we need to convert values from one data type to another, under certain situation.

Therefore, we will know two different ways to convert boolean to string in Java. Two ways for converting boolean to string are as follows:

  • Convert using String.valueOf(boolean) method of String class
  • Convert using Boolean.toString(boolean) method of Boolean class

Let’s understand both ways one by one with example programs.

Convert boolean to String in Java using String.valueOf(boolean)


To convert primitive data type boolean to string in Java, we use valueOf() method of the String class. The method String.valueOf() converts boolean to string. It is a static utility method of the String class.

Therefore, we do not need to create an object to calling this method. We can simply call by using its class name. The general signature of valueOf() method is as below:

public static String valueOf(boolean b)

This method accepts a boolean value that needs to be converted and returns the string representation of the boolean value.

Let’s create a simple program for converting a primitive boolean type to string in Java.

Program code 1:

// Java Program to demonstrate the conversion of boolean into string using String.valueOf().
package stringPrograms;
public class BooleanToString {
public static void main(String[] args) 
{		
  boolean b1 = true;  
  boolean b2 = false;
// Converting boolean to string using valueOf() method of String class.  
   String str1 = String.valueOf(b1);  
   String str2 = String.valueOf(b2);  
   System.out.println("String representation of boolean value: " +str1);  
   System.out.println("String representation of boolean value: " +str2);  
 }
}
Output:
      String representation of boolean value: true
      String representation of boolean value: false

Boolean to String Conversion using Boolean.toString() method


Boolean wrapper class in Java wraps a value of the primitive data type boolean in an object. An object of Boolean class contains a single field whose type is boolean.

The Boolean wrapper class also provides a toString() method that converts boolean value to String. The toString() method of Boolean class is a static utility method.

The general signature of this method is as below:

public static String toString(boolean b)

This method accepts a parameter b that is to be converted. It returns a string object representing the specified boolean value.

Let’s create a simple Java program for converting primitive type boolean to non primitive data type String using Boolean.toString() method.

Program code 2:

// Java Program to demonstrate the conversion of boolean into string using Boolean.toString().
package stringPrograms;
public class BooleanToString {
public static void main(String[] args) 
{		
  boolean b1 = true;  
  boolean b2 = false;
// Calling toString() method of Boolean class for converting boolean to string.  
   String str1 = Boolean.toString(b1);  
   String str2 = Boolean.toString(b2);  
   System.out.println("String representation of boolean value: " +str1);  
   System.out.println("String representation of boolean value: " +str2);
 }
}
Output:
      String representation of boolean value: true
      String representation of boolean value: false

In this tutorial, you learned how to convert one data type boolean to another type String in Java. Hope that you will have understood the basic concept for converting boolean into string and practiced all programs.
Thanks for reading!!!

The post How to Convert Boolean to String in Java appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

How to Convert Boolean to String in Java

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×