3 Ways To Convert String To Boolean Inwards Java? Examples
Senin, 7 April 2025

Advertisement

Masukkan script iklan 970x90px

3 Ways To Convert String To Boolean Inwards Java? Examples

Jumat, 27 Maret 2020

You tin convert a String object to Boolean object or boolean primitive past times using the Boolean.valueOf() in addition to Boolean.parseBoolean() method. The steps are similar rot converting to String to other information types e.g. String to Integer in addition to String to Long. You tin purpose valueOf() method to convert String to Boolean object in addition to parseBoolean() method to convert given string to a boolean primitive value. Internally, valueOf() also uses parseBoolean() for parsing String but on overstep of that it also provides caching e.g. it tin render Boolean.TRUE in addition to Boolean.FALSE cached value for "true" in addition to "false" string. 


In fact, the Boolean.TRUE is returned exclusively when String is equal to truthful ignoring instance e.g. "True", "true", "TRUE" volition evaluate into boolean true, so Boolean.TRUE volition hold upwardly returned. For string similar "Yes", Boolean.FALSE volition hold upwardly returned. We'll utter over the rules of String to boolean conversion inward side past times side section.



1. Rules of String to Boolean Conversion inward Java

The parsing logic is encapsulated inward parseBoolean() method which is also leveraged or used past times valueOf(). According to this logic, the parseBoolean() method render truthful if given String is non null in addition to equal to truthful ignoring instance in addition to faux otherwise.

For example, "true", "True", in addition to "TRUE" all volition render Boolean.TRUE value but "Yes" volition render Boolean.FALSE. Similarly, "false", "False", or "FALSE" volition also render Boolean.FALSE

Here are around examples:

Boolean.parseBoolean("True") returns true.
Boolean.parseBoolean("TRUE") returns true.
Boolean.parseBoolean("true") returns true.
Boolean.parseBoolean("yes") returns false.
Boolean.parseBoolean("y") returns false.
Boolean.parseBoolean("no") returns false.
Boolean.parseBoolean("false") returns false.
Boolean.parseBoolean("False") returns false.
Boolean.parseBoolean("FALSE") returns false.

If you lot desire to know to a greater extent than virtually how to convert i information type to others inward Java,  The Complete Java Masterclass is a skilful resources to acquire it inward depth.




2. Boolean.parseBoolean() Example

The parseBoolean() method is similar to parseInt() method in addition to it returns a primitive boolean value afterward parsing given String. It returns a boolean value, true or false based upon the rules given above.

It compares String past times ignoring instance in addition to exclusively render truthful if String matches truthful afterward ignoring cases.

Boolean.parseBoolean("True") returns true.
Boolean.parseBoolean("TRUE") returns true.
Boolean.parseBoolean("true") returns true.
Boolean.parseBoolean("yes") returns false.

You should purpose this method if you lot necessitate a primitive boolean value.



3. Boolean.valueOf() Example

This method should hold upwardly used to convert a String object to a Boolean object inward Java. It leverages the parsing logic of parseooleBan() method but it also uses the Flyweight pattern pattern to cache ofttimes used value in addition to returns them.

Since boolean tin either hold upwardly truthful or false, it simply uses 2 Boolean instances, Boolean.TRUE in addition to Boolean.FALSE, for all String to Boolean conversion, which drastically reduces the break of objects in addition to causes less overhead for Garbage collector.

Here are around examples of converting String to Boolean using the valueOf() method:

Boolean.valueOf("True") returns Boolean.TRUE.
Boolean.valueOf("TRUE") returns Boolean.TRUE.
Boolean.valueOf("true") returns Boolean.TRUE.
Boolean.valueOf("yes") returns Boolean.FALSE.
Boolean.valueOf("y") returns Boolean.FALSE.
Boolean.valueOf("no") returns Boolean.FALSE.
Boolean.valueOf("false") returns Boolean.FALSE.
Boolean.valueOf("False") returns Boolean.FALSE.
Boolean.valueOf("FALSE") returns Boolean.FALSE.

You should purpose this method if you lot necessitate a Boolean object from String rather than boolean primitive value.  If you lot desire to know to a greater extent than virtually primitive information types inward Java then Eclipse IDE in addition to run. This programme accepts user input equally String in addition to tries to convert it to a boolean. If successful, it prints that value into the console, otherwise, it throws an error.
package tool;  import java.util.Scanner;  /**  *   * Influenza A virus subtype H5N1 unproblematic Java Program to convert String to Boolean or boolean information type.  */ public class Hello {    public static void main(String[] args) {      System.out.println("Please acquire inward a boolean String e.g. truthful or false");      Scanner sc = new Scanner(System.in);      String input = sc.next();      boolean b = Boolean.valueOf(input);      System.out.println("converted boolean value from String using valueOf: "         + b);      boolean value = Boolean.parseBoolean(input);      System.out         .println("converted boolean value from String using parseBoolean: "             + value);      sc.close();      // you lot tin also purpose constructor but that is non encouraged past times Effective Java      Boolean bool = new Boolean(input);     System.out         .println("converted boolean value from String using constructor: "             + bool);    }  }  Output Please enter a boolean String e.g. true or false true converted boolean value from String using valueOf: true converted boolean value from String using parseBoolean: true converted boolean value from String using constructor: true


Here is the summary of all iii methods to convert String to Boolean inward Java:

 You tin convert a String object to Boolean object or boolean primitive past times using the  3 Ways to convert String to Boolean inward Java? Examples


5. Important points

5.1 Even though you lot tin also purpose the constructor of java.lang.Boolean flat to convert String to a Boolean object, it's non encouraged by Effective Java of Joshua Bloch, which advice prefer static manufacturing works life methods similar valueOf() to convey wages of caching they offer.

5.2. The parsing rules i.e. how the string is genuinely converted into a boolean value is written inward parseBoolean() method.

5.3 Both valueOf() in addition to parseBoolean() method belongs to java.lang.Boolean class.

5.4. The valueOf() method render either the Boolean.TRUE or  Boolean.FALSE object, which is shared past times all boolean values converted.


That's all virtually how to convert String to Boolean or boolean inward Java. As I said, you lot tin purpose either Boolean.valueOf(), a static manufacturing works life method or the parseBoolean() method for this conversion. The dominion of pollex is to prefer valueOf() if you lot necessitate Boolean object in addition to parseBoolean() if you lot necessitate a boolean primitive value.


Further Learning

Thanks for reading this article so far. If you lot similar this tutorial so delight part alongside your friends in addition to colleagues. If you lot direct maintain whatsoever questions or feedback so delight drib a note. 

Loading