Boolean Literal in java With Examples using NetBeans
Description:
A boolean literal has only two values: true or false. This section explains how to write boolean literals in Java programs.
In this article, you will learn the following topic
- How to Represent Boolean Literals in java
How to Represent Boolean Literals in java
Write true or false to represent a boolean literal in Java.
true
false
Note that if you enclose it in double quotations, it becomes a string literal.
“true”
“false”
Boolean literals are rarely written directly in a program and are used to obtain true or false as a result of comparing the magnitude of two values. For example, the following sample returns true if 20 is greater than 10 and false if 20 and 10 are equal.
System.out.println(20 > 10); # true
System.out.println(20 == 10); # false
In some other programming languages, true and 1, and false and 0 are treated as the same value, but in Java true and false are completely different data types from 1 and 0.
How to use Boolean literal in java programming:
Let’s create a simple sample program and try it. After writing the following code in a text editor or any ide, save it as BooleanLiteral.java.
Open the command prompt and type
javac BooleanLiteral.java
Then press the enter, after pressing the enter button simply type java BooleanLiteral, and press the enter button. But in my case I am using the NetBeans ide
package com.mycompany.javabasics;
/**
*
* @author Fawadkhan
*/
public class BooleanLiteral {
public static void main(String[] args) {
System.out.println(20 > 10);
System.out.println(20 == 10);
}
}
Output:
It compares two values to see if they are greater or equal, and as a result, obtains a boolean literal of true or false and outputs it to the screen. (The operators used in this example I will explain in detail in my upcoming article.)
I explained how to use a boolean literal in Java program.