Java Program to Convert String to boolean Last Updated : 21 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, to convert a string to a Boolean, we can use Boolean.parseBoolean(string) to convert a string to a primitive Boolean, or Boolean.valueOf(string) to convert it to a Boolean object. The Boolean data type only holds two possible values which are true and false. If the string equals "true" (ignoring case), it converts to true, otherwise, it converts to false. In this article, we will learn how to convert a String to a Boolean in Java with examples.Note: In Java, only true and false are returned as boolean not 0 and 1.Example: Below is a simple example of converting a String to a primitive boolean by using the Boolean.parseBoolean() method. Java // Java Program to Convert String to Boolean // using parseBoolean() Method public class StringToBoolean { public static void main(String[] args) { // Define a custom string String s = "true"; System.out.println("String: " + s); // Convert String to boolean boolean b = Boolean.parseBoolean(s); System.out.println("Boolean: " + b); } } OutputString: true Boolean: true Syntax of Boolean.parseBoolean() Methodboolean boolValue = Boolean.parseBoolean(String s) Parameter:s: String that contains true (case-insensitive) or false.Return Type:It returns true, if the string (ignoring case) is "true".It returns false, for any other input.Other Way to Convert String to booleanIn addition to using Boolean.parseBoolean() method, there is another way to convert a string to a boolean in Java. We will discuss below this method with example: Using Boolean.valueOf() MethodIt is similar to the above method as discussed just a little difference lies as it returns a boolean object instead of a primitive boolean value.Syntax of Boolean.valueOf() methodboolean boolValue = Boolean.valueOf(String str) Example: Java // Java Program to Convert a String to Boolean Object // using valueOf() method of Boolean Class class GFG { // Method 1 // To convert a string to its boolean object public static boolean stringToBoolean(String s) { // Converting a given string // to its boolean object // using valueOf() method boolean b1 = Boolean.valueOf(s); // Returning boolean object return b1; } // Method 2 // Main driver method public static void main(String args[]) { // Given input string 1 String s = "yes"; // Printing the desired boolean System.out.println(stringToBoolean(s)); // Given input string 2 s = "true"; // Printing the desired boolean System.out.println(stringToBoolean(s)); // Given input string 3 s = "false"; // Printing the desired boolean System.out.println(stringToBoolean(s)); } } Outputfalse true false Points to Remember:The Boolean.parseBoolean() method returns a primitive boolean.The Boolean.valueOf() method returns a Boolean object.Any string other than "true" (case-insensitive) will result in false. Comment More infoAdvertise with us Next Article Compare two Strings in Java P prashant_srivastava Follow Improve Article Tags : DSA Java-Boolean Java-String-Programs Similar Reads Program to check if the String is Empty in Java Given a string str, the task is to check if this string is empty or not, in Java. Examples: Input: str = "" Output: True Input: str = "GFG" Output: False Approach 1: Get the String to be checked in strWe can simply check if the string is empty or not using the isEmpty() method of String class Syntax 2 min read Program to check if a String in Java contains only whitespaces Given a string str, the task is to check if this string contains only whitespaces or some text, in Java. Examples: Input: str = " " Output: True Input: str = "GFG" Output: False Approach: Get the String to be checked in str We can use the trim() method of String class to remove the leading whitespac 2 min read Compare two Strings in Java String in Java are immutable sequences of characters. Comparing strings is the most common task in different scenarios such as input validation or searching algorithms. In this article, we will learn multiple ways to compare two strings in Java with simple examples.Example:To compare two strings in 4 min read Output of Java Programs | Set 53 (String Comparison) Prerequisite : String Comparison in Java 1. What should be the output of this program? Java class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) tru 3 min read Java Guava | Booleans.concat() method with Examples The concat() method of Booleans Class in the Guava library is used to concatenate the values of many arrays into a single array. These boolean arrays to be concatenated are specified as parameters to this method. For example: concat(new boolean[] {a, b}, new boolean[] {}, new boolean[] {c} returns t 2 min read Java Guava | Booleans.contains() method with Examples The contains() method of Booleans Class in Guava library is used to check if a specified value is present in the specified array of boolean values. The boolean value to be searched and the boolean array in which it is to be searched, are both taken as a parameter. Syntax : public static boolean cont 2 min read Like