Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - boolean Keyword



Java boolean keyword is used to define one of the eight primitive datatype supported by Java. It provides means to create boolean type variables which can accept a boolean value as true or false.

Following 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.

Java boolean Datatype - Example 1

Following 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: true

Java boolean Datatype - Example 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: true

Java boolean Datatype - Example 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
Advertisements