×

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

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.