
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Boolean Variable from String in Java
Use the parseBoolean() method to create a boolean variable from a string. Here for boolean variable, firstly take a boolean and pass the string to it using Boolean.parseBoolean().
boolean val1 = Boolean.parseBoolean("TRUE");
The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
Do the same for FALSE as well.
boolean val2 = Boolean.parseBoolean("FALSE");
Let us now see the complete example to form a boolean variable from string.
Example
public class Demo { public static void main(String[] args) { boolean val1 = Boolean.parseBoolean("TRUE"); System.out.println(val1); boolean val2 = Boolean.parseBoolean("FALSE"); System.out.println(val2); } }
Output
true false
Advertisements