Java boolean type

In this chapter you will learn:

  1. What is Java boolean type
  2. How to create boolean Literals
  3. How to create Boolean value object with constructors
  4. How to Get boolean value from constants in Boolean class
  5. How to Compare two boolean values

boolean type

Java has a boolean type for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators. Here is a program that demonstrates the boolean type:

public class Main {
  public static void main(String args[]) {
    boolean boolVariable;
    boolVariable = false;/*  java 2  s . c o m*/
    System.out.println("b is " + boolVariable);
    boolVariable = true;
    System.out.println("b is " + boolVariable);

    if (boolVariable) {
      System.out.println("This is executed.");

    boolVariable = false;
    if (boolVariable) {
      System.out.println("This is not executed.");
    System.out.println("10 > 9 is " + (10 > 9));


Boolean Literals

Boolean literals are only two logical values: true and false. The values of true and false do not convert into any numerical representation.

The true literal in Java does not equal 1, nor does the false literal equal 0. In Java, they can only be assigned to variables declared as boolean.

public class Main {
  public static void main(String[] argv) {
    boolean b = true;
// j  av a 2  s. c o m
    int i = b;


If you try to compile the program, the following error message will be generated by compiler.

Title:Boolean class ID:BooleanClass Content:

The Boolean class wraps a primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

Boolean class has the methods for converting a boolean to a String and a String to a boolean.

Create Boolean value object with constructors

  • Boolean(boolean value) creates a Boolean object for value.
  • Boolean(String s) creates a Boolean object true value for string "true" igoring case.
public class Main {
    public static void main(String[] args) {
       Boolean boolean1 = new Boolean("true");
       System.out.println(boolean1);//from  j  a  v  a 2 s  .c  o  m
       Boolean boolean2 = new Boolean(true);

The output:

Get boolean value from constants in Boolean class

Boolean class defines two constants to store the two boolean value: true and false.

  • static Boolean FALSE stores the Boolean object value false.
  • static Boolean TRUE stores the Boolean object value true.
public class Main {
    public static void main(String[] args) {
    }/*from  java 2  s . co  m*/

The output:

Compare two boolean values

The following methods do the comparison for boolean values.

  • int compareTo(Boolean b) compares this Boolean instance with another.
  • boolean equals(Object obj) returns true if the argument is not null and is a Boolean object that represents the same boolean value as this object.

compareTo(Boolean b) returns:

zeroif this object represents the same boolean value as the argument;
a positive valueif this object represents true and the argument represents false;
a negative valueif this object represents false and the argument represents true
public class Main {
    public static void main(String[] args) {
       Boolean boolean1 = new Boolean("true");
       System.out.println(boolean1);/*  j  a va 2  s .  c o  m*/
       Boolean boolean2 = new Boolean(true);

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to Convert Boolean object to primitive boolean value
Home » Java Tutorial » Primitive Data Types


    Java Primitive Data Types


    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string


    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes


    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short


    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes


    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations


    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String


    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value


    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing