Can I create a boolean variable from string?

Bookmark this example!  
Category: java.lang, viewed: 1275 time(s).
 
package org.kodejava.sample.java.lang;
 
public class BooleanParseExample
{
    public static void main(String[] args)
    {
        // Parsing string "true" will result boolean true
        boolean boolA = Boolean.parseBoolean("true");
        System.out.println("boolA = " + boolA);
 
        // Parsing string "TRUE" also resutl boolean true, as the
        // parsing method is case insensitive
        boolean boolB = Boolean.parseBoolean("TRUE");
        System.out.println("boolB = " + boolB);
 
        // The operation below will return false, as Yes is not
        // a valid string value for boolean expression
        boolean boolC = Boolean.parseBoolean("Yes");
        System.out.println("boolC = " + boolC);
 
        // Parsing a number is also not a valid expression so the
        // parsing method return false
        boolean boolD = Boolean.parseBoolean("1");
        System.out.println("boolD = " + boolD);
    }
}
 
 
Can't find what you are looking for? Join our FORUMS and ask some questions!

Most Viewed Examples

Latest Code Examples

100 Top & Latest


Locations of visitors to this page
eXTReMe Tracker
visitor stats
Get Chitika Premium