Java Data Types - Boolean

| Page Views: 42

Java like most programming languages uses the boolean data type to create variables iwth either true or false.

With this type we can use relational operators/comparison operators to compare twi two values. Say you want to compare whether a value is greater than another value.

A variable holding a boolean value is called a Boolean variable.

A boolean variable can hold only either of these two values:

  1. true
  2. false
boolean engineStared = true;
boolean planeLiftedOff = false;

The true and false are literals, just like say number 20. Furthermore they are reserved for use by Java so you cannot use them as identifiers.

Let's look at simple quiz program. This program generates two random numbers using System.currentTimeMillis(). You then provide the answer. Then we compare your answer with what the true result. This returns true or false.

package info.tutorialsloop;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int firstNum = (int) (System.currentTimeMillis()%10);
        int secondNum = (int) (System.currentTimeMillis()/6%10);

        int total=firstNum+secondNum;

        Scanner input=new Scanner(;
        System.out.print("Calculate "+firstNum+" + "+secondNum+" = ");
        int answer=input.nextInt();

        System.out.println(firstNum+" + "+secondNum + " = "+ answer+" is "+(total == answer));


You type the answer then press enter to see result. Here are some interesting answers:

Calculate 3 + 2 = 5
3 + 2 = 5 is true

Calculate 5 + 9 = 12
5 + 9 = 12 is false

Calculate 6 + 9 = 15
6 + 9 = 15 is true

Calculate 1 + 1 = 0
1 + 1 = 0 is false

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post