In This Article, You Will Learn about Java Booleans.
Java Booleans – Before moving ahead, let’s know a bit about Java Math.
Table of Contents
Java Booleans
When there is a condition of returning one value between two values, that’s where Java Booleans come into effect.
Values –
True/False
Yes/No
To handle this condition in Java, we have a boolean data type that stores true or false values.
Boolean Values
In Java, a boolean data type is defined with the keyword boolean that stores true or false.
Example: Use Boolean data type to return true or false based on condition.
public class Main {
public static void main(String[] args) {
boolean learningJavaIsFun = true;
boolean doYouSkipClass = false;
System.out.println(learningJavaIsFun);
System.out.println(doYouSkipClass);
}
}
Boolean Expression
Boolean expression is a Java expression in Java that returns a boolean value, true or false.
Boolean expression takes place when comparing two values to find output.
Let’s say we can use a comparison operator, e.g., less than (>) operator, to find out if an expression (or a variable) is true.
Example: Use a boolean expression to return true or false based on the condition.
public class Main {
public static void main(String[] args) {
int num = 12;
int number = 13;
System.out.println(num < number);
}
}
Let’s make it easier!
public class Main {
public static void main(String[] args) {
System.out.println(14 < 15);
}
}
Example: Use boolean expression to return true or false based on condition.
public class Main {
public static void main(String[] args) {
int num = 12;
int number = 13;
System.out.println(num > number);
}
}
Let’s make it easier!
public class Main {
public static void main(String[] args) {
System.out.println(12 > 13);
}
}
Let’s move ahead and use one more comparison operator that’s equal (==) operator.
Example: Use boolean expression to return true or false based on condition.
public class Main {
public static void main(String[] args) {
int num = 12;
int number = 13;
System.out.println(num == number);
}
}
Let’s take another view –
Example: Use boolean expression to return true or false based on condition.
public class Main {
public static void main(String[] args) {
int num = 12;
System.out.println(num == 12);
}
}
Example: Use boolean expression to return true or false based on condition.
public class Main {
public static void main(String[] args) {
System.out.println(15 == 12);
}
}
Real Practical example
Let’s assume that bank account opening age eligibility is a minimum of 18 years old, so now check whether you’re eligible to open a bank account or not.
Example: Use a boolean expression to return true or false based on the condition.
public class Main {
public static void main(String[] args) {
int yourAge = 17;
int accountEligibilityAge = 18;
System.out.println(yourAge >= accountEligibilityAge);
}
}
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: