×

How to Convert String to boolean in Java

How to Convert String to boolean in Java

There are two methods to convert String to boolean:

  • Using parseBoolean(string) method
  • Using valueOf(string) method

If the string contains "True," "true," or "TRUE," it returns Boolean true. It returns Boolean false if the string doesn’t contain "True," “true," or "TRUE." It is case insensitive.  Both the above methods are null safe; it means they will return false if you pass null. For example:

boolean b1=Boolean.parseBoolean(null);

Using parseBoolean(string)

It converts String into Boolean primitive. It is the static method of the Boolean wrapper class. It doesn’t throw an exception if you pass any string value other than true and false. For example, if you pass "Yes," it will return false. The signature of the method is given below:

public static int parseBoolean(String str)

Where str is the string to be converted into Boolean primitive.

Example

In the following example, we have taken two variables str1 and str2 of String type that contains "TRUE" and "true3" respectively. b1 and b2 are two variable that stores the primitive Boolean value of the string str1 and str2, respectively. Boolean is the wrapper class, and it invokes parseBoolean(string) method of the class. The method parses a string argument and converts it to Boolean primitive. The first println statement prints the Boolean value of the string str1, i.e., true and the second println statement prints the Boolean value of the string str2, i.e., false.

public class stringTobooleanExample
{ 
public static void main(String args[])
{ 
String str1="TRUE"; 
String str2="true3";  
boolean b1=Boolean.parseBoolean(str1); 
boolean b2=Boolean.parseBoolean(str2); 
System.out.println("The boolean value of str1: "+b1); 
System.out.println("The boolean value of str2: "+b2); 
}
}

Output

The boolean value of str1: true
The boolean value of str2: false

Using valueOf(string)

It also works similar to parseBoolean(string) method. The valueOf(string) method converts a string into the Boolean object. It returns the instance of the Boolean class instead of primitive Boolean value. It returns true for a non-null string and returns false for everything else. Boolean instances are immutable, so you should always use valueOf(string) method. The signature of the method is given below.

public static Boolean valueOf(String str)

Example

In the following example, we have taken two variables str1 and str2 of String type that contains "TRUE" and "" (null) respectively. b1 and b2 are two variable that stores the primitive Boolean value of the string str1 and str2 respectively. Boolean is the wrapper class and it invokes valueOf(string) method of the class. The method parses a string argument and converts it to Boolean primitive. The first println statement prints the Boolean value of the string str1 i.e. true and the second println statement prints the Boolean value of the string str2, i.e., false.

public class stringTobooleanExample1
{ 
public static void main(String args[])
{ 
String str1="TRUE"; 
String str2="";  //represents null
boolean b1=Boolean.valueOf(str1); 
boolean b2=Boolean. valueOf (str2); 
System.out.println("Boolean value of str1: "+b1); 
System.out.println("Boolean value of str2: "+b2); 
}
}

Output

Boolean value of str1: true
Boolean value of str2: false

Related Topics

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.