beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java String to boolean Conversion with examples

By Chaitanya Singh | Filed Under: Java Conversion

In this guide, we will see how to convert a String to a boolean with the help of examples.

When converting a String to a boolean value, if the string contains the value “true” (case doesn’t matter) then the boolean value after the conversion would be true, if the string contains any other value other than “true” then the converted boolean value would be “false”.

Java String to boolean conversion using Boolean.parseBoolean() method example

Here we have three Strings str1, str2 and str3 and we are converting them into boolean value using the Boolean.parseBoolean() method, this method accepts String as an argument and returns boolean value true or false. If the value of the string is “true” (in any case uppercase, lowercase or mixed) then this method returns true, else it returns false.

public class JavaExample{  
   public static void main(String args[]){  
	String str1 = "true";  
	String str2 = "FALSE";  
	String str3 = "Something";  
	boolean bool1=Boolean.parseBoolean(str1);  
	boolean bool2=Boolean.parseBoolean(str2);  
	boolean bool3=Boolean.parseBoolean(str3);  
	System.out.println(bool1);  
	System.out.println(bool2);  
	System.out.println(bool3);  
   }
}

Output:
Java String to Boolean conversion

Java String to boolean using Boolean.valueOf() example

Here we will see another method which we can use for string to boolean conversion. Similar to Boolean.parseBoolean() method, the Boolean.valueOf() method accepts string as an argument and returns a boolean value true or false.

public class JavaExample{  
   public static void main(String args[]){  
	String str1 = "true";  
	String str2 = "TRue";  
	String str3 = "Something";  
	boolean bool1=Boolean.valueOf(str1);  
	boolean bool2=Boolean.valueOf(str2);  
	boolean bool3=Boolean.valueOf(str3);  
	System.out.println(bool1);  
	System.out.println(bool2);  
	System.out.println(bool3);  
   }
}

Output:
Java String to boolean example

❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap