How to Convert String Type Variables into Boolean In Java Program

Java Program to convert string type variables into boolean

Java Strings

  • In Java, a string is a sequence of characters. It is an object of the class java.lang.String, which is defined in the Java standard library.
  • Text is represented by strings, which are also frequently used to represent messages, names, and other types of human-readable data.
  • Strings in Java are immutable, which means that once you create a string object, its value cannot be changed.
  • You must be familiar with following topics to understand the correspond example Such as: Java Strings, Java Data Types. 

Steps to Convert String Type Variable into Boolean 

  • Here are the steps to Convert String Type Variable into Boolean  in Java:, You can follow these steps:
    • Declare a string variable: To start with, you need to declare a string type variable to store the string value that you want to convert.

    • Call the Boolean.valueOf() method: The Boolean.valueOf() method takes a string as an argument and returns its boolean equivalent.

    • Store the result in a boolean variable: The boolean value returned by the Boolean.valueOf() method can be stored in a boolean variable for further use.

    • Use the boolean variable: Finally, you can use the boolean variable to perform various operations or as an argument in method calls.

There are three ways to convert a string type variable into a boolean in Java:

Let’s look at a Java Program to Convert String Type Variable into Boolean in Java to perform certain operations.

Example 1: Java Program to Convert String Type Variable into Boolean.

Run

public class Main {
  public static void main(String[] args) {
      //String
    String str = "true";
    //String to boolean
    boolean result = Boolean.parseBoolean(str);
    //Return boolean value
    System.out.println(result);
  }
}

Output

true

Example 2 : Java Program to Convert string to boolean using valueOf()

Run

public class Main {
  public static void main(String[] args) {
      //String
    String str1 = "yes";
    String str2 = "false";
    //String to boolean 
    boolean bool1 = Boolean.valueOf(str1);
    boolean bool2 = Boolean.valueOf(str2);
    //Return boolean value
    System.out.println("String 'yes' converted to boolean: " + bool1);
    System.out.println("String 'false' converted to boolean: " + bool2);
  }
}

Output

String 'yes' converted to boolean: false
String 'false' converted to boolean: false

Example 3: Java Program to Convert string to boolean using valueOf() 

Run
public class Main {
  public static void main(String[] args) {
    String str = "false";
    boolean bool = Boolean.valueOf(str);
    System.out.println("String 'false' converted to boolean: " + bool);
  }
}

Output

String 'false' converted to boolean: false

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription