Java boolean keyword is used to define one of the eight primitive data types supported by Java. It provides a means to create boolean-type variables which can accept a boolean value as true or false.
Characteristics of BooleanFollowing are the characteristics of a boolean data type in Java:
boolean data type represents one bit of information
There are only two possible values: true and false
This data type is used for simple flags that track true/false conditions
Default value is false
Example: boolean one = true
A boolean variables represents a reserved memory locations to store boolean values. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store either true or false in boolean variables.
SyntaxThe following is the syntax to declare a variable using boolean keyword:
boolean variable_name = true;Examples of boolean Keyword
Practice the following examples to understand the use of boolean keyword in Java:
Example 1Following Java program shows the usage of boolean primitive data type we've discussed above. We've created a boolean variable as booleanValue and assigned it a true value. Then this variable is printed.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { boolean booleanValue = true; System.out.println("Boolean: " + booleanValue); } }Output
Boolean: trueExample 2
Following Java example shows the usage of boolean primitive data type in an if statement. We've created a boolean variable as booleanValue and assigned it a true value. Then we're using this variable in an if statement to print the value based on given condition.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { boolean booleanValue = true; if(booleanValue){ System.out.println("Boolean: " + booleanValue); } } }Output
Boolean: trueExample 3
Following Java example shows the usage of boolean variable. We've created a boolean variable as booleanValue and assigned it a true value. Then printing this variable. In next statement, we're updating the value of boolean variable and it is printed again.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { boolean booleanValue = true; System.out.println("Boolean: " + booleanValue); booleanValue = false; System.out.println("Boolean: " + booleanValue); } }Output
Boolean: true Boolean: false
java_basic_syntax.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4