Java boolean type

In this chapter you will learn:

  1. What is Java boolean type
  2. Values for Java boolean type
  3. Literals for Java boolean type
  4. Java Boolean class
  5. Example - Java boolean type
  6. Example - How to create boolean Literals

Description type

Java has a boolean type for logical values. This is the type returned by all relational operators.

Value

It can have only one of two possible values, true or false.

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.

Boolean class

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.

Example

Here is a program that demonstrates the boolean type:


public class Main {
  public static void main(String args[]) {
    boolean boolVariable;
    boolVariable = false;//from w  w  w. j a  v a2 s  .  c o m
    
    System.out.println("b is " + boolVariable);
    
    boolVariable = true;
    
    System.out.println("b is " + boolVariable);

  }
}

Output:

Example 2

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;
/*w  w  w .ja  v  a2 s.c  o  m*/
    int i = b;

  }
}

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

Next chapter...

What you will learn in the next chapter:

  1. What is Java char type
  2. Size of Java char type
  3. Value range for Java char type
  4. Java char type literals
  5. Example - Java char type
  6. How to create char Literals
  7. Example - Store unicode into a char
Home »
  Java Tutorial »
    Java Langauge »
      Java Data Types
Java Primitive Data Types
Java boolean type
Java char type
Java char value escape
Java byte type
Java short type
Java int type
Java long type
Java float type
Java double type
Java String type
Java String Escape
Java String Concatenation