String to Boolean
This example shows parsing a string literal to a boolean primitive using built-in parse function.
package com.bethecoder.tutorials.core.bool;
public class ParseBoolean {
public static void main ( String [] args ) {
/**
* 'parseBoolean' method treats any string
* other than TRUE (ignore case) as FALSE
*/
boolean bool = Boolean.parseBoolean ( null ) ;
System.out.println ( bool ) ; //false
bool = Boolean.parseBoolean ( "true" ) ;
System.out.println ( bool ) ; //true
bool = Boolean.parseBoolean ( "True" ) ;
System.out.println ( bool ) ; //true
bool = Boolean.parseBoolean ( "TRue" ) ;
System.out.println ( bool ) ; //true
bool = Boolean.parseBoolean ( "false" ) ;
System.out.println ( bool ) ; //false
bool = Boolean.parseBoolean ( "FaLSE" ) ;
System.out.println ( bool ) ; //false
bool = Boolean.parseBoolean ( "ABC" ) ;
System.out.println ( bool ) ; //false
}
}
It gives the following output,
false
true
true
true
false
false
false