In some cases we need a variable to store only two possible values, either false or true. For this, boolean data type is used in java. The variable declared as boolean can take either true or false as value.
Boolean Variable in java
Boolean variable is declared using boolean
keyword and can take true
or false
value.
public class JavaBooleanExample {
public static void main(String[] args) {
int age1=30;
int age2=40;
boolean elder=true;
boolean younger=false;
if(age1<age2)
{
System.out.println(elder);
}
else
{
System.out.println(younger);
}
}
}
Java Boolean Expressions
An expression involves multiple variables and some arithmetic and logical operators and function calls. Final result of a boolean expression will be true
or false
Following is an example of a boolean expression:
int a = 20;
int b = 30;
int c = 300;
if( (a+b)<c)
{
System.print.ln("Sum of a and b is less than value in c");
}