Write a Java Program to convert string type variables into boolean

Java Program to convert string type variables into boolean

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert string type variables into boolean
Java Program to convert string type variables into boolean

Write a Java Program to convert string type variables into boolean

PROGRAM: Java Program to convert string type variables into boolean

/*Java Program to convert string type variables into boolean*/

1: Convert string to boolean using parseBoolean()

class Main {
public static void main(String[] args) {

// create string variables
String str1 = “true”;
String str2 = “false”;

// convert string to boolean
// using parseBoolean()
boolean b1 = Boolean.parseBoolean(str1);
boolean b2 = Boolean.parseBoolean(str2);

// print boolean values
System.out.println(b1); // true
System.out.println(b2); // false
}
}

 2: Convert string to boolean using valueOf()

class Main {
public static void main(String[] args) {

// create string variables
String str1 = “true”;
String str2 = “false”;

// convert string to boolean
// using valueOf()
boolean b1 = Boolean.valueOf(str1);
boolean b2 = Boolean.valueOf(str2);

// print boolean values
System.out.println(b1); // true
System.out.println(b2); // false
}
}

Leave a Reply

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