Learn what is a Boolean in Java, how to declare & return a Java Boolean, and what are boolean operators along with practical code examples:
In this tutorial, we are going to explore boolean in Java which is a primitive data type. This data type has two values i.e. “true” or “false”.
This tutorial will include an explanation of boolean data type along with its syntax and examples that will help you understand this primitive data type in detail.
=> Visit Here To Learn Java From Scratch.
We are also providing examples that are in collaboration with conditional checks. Based on the boolean condition, the statements will be executed. These kinds of examples will help you in finding more usage of boolean in your programs.
Apart from these, this tutorial also includes frequently-asked questions related to the topic.
Table of Contents:
Java Boolean
Java has eight primitive data types and boolean is one of them. Such data type has only two possible values i.e. a Java boolean variable can be either “true” or “false”. This is the same value that is returned by all the rational operators (a<b or b>c…. etc).
A boolean data type is also used in conditional checks using if statements or loops. Given below is the syntax of boolean Java.
Syntax:
boolean variable_name = true/false;
Boolean In Java With If Statement
In the below example, we have initialized two variables (‘a’ and ‘b’) with a different value. Then, we initialized two boolean variables (‘a1’ and ‘b1’) with the value “true” and “false”.
Thereafter, we used the if statement in which we put a conditional check to print the statement inside the block.
public class example { public static void main(String[] args) { // initialized 'a' and 'b' int a = 20; int b = 30; // initialized boolean variables 'a1' and 'b1' boolean a1 = true; boolean b1 = false; /* * if condition starts here. If this condition matches * then 'a1' will be printed. */ if (b > a) { System.out.println(a1); } /* * if this condition matches then 'b1' will be printed */ else { System.out.println(b1); } } }
Output
Java Boolean Operators
Java boolean operators are denoted by |, ||, &, &&, <, >, <=, >=, ^, !=, ==. These logical boolean operators help in specifying the condition that will have the two return values – “true” or “false”.
In the below example, we will use Java boolean operator to return the boolean values. Here, we have initialized an integer with some value and then we used different kinds of operators to return the values for different conditional checks.
public class example { public static void main(String[] args) { int a = 20; System.out.println(a == 20); System.out.println(a < 35); System.out.println(a == 50); System.out.println(a > 40); } }
Output
Comparing The Value Of Boolean Variables
In this section, we will compare the value of two boolean variables. We have initialized two variables with “false” and “true” values and again we have initialized two boolean variables with values that are based on the outcome of their comparison.
Then, we have printed each of these variables.
public class example { public static void main(String[] args) { boolean b1 = false; boolean b2 = true; boolean b3 = (b1==b2); boolean b4 = (b1!=b2); // b1 is false System.out.println(b1); // b2 is true System.out.println(b2); // b3 is false System.out.println(b3); // b4 is true System.out.println(b4); } }
Output
Finding Prime Number
This is one of the most important/common examples where the boolean variable is used. Here, we have declared two integers count and number. Then we have used a Scanner class with nextInt().
One boolean variable “boo” is set to true. Thereafter, we have used for loop starting from 2, less than half of the number entered and incremented by 1 for each iteration. The count variable will have a remainder for every iteration. If the remainder is 0, then boo will be set to False.
Based on the “boo” value, we are coming to the conclusion that whether our number is prime or not with the help of an if-statement.
import java.util.Scanner; public class example { public static void main(String[] args) { int count, number; boolean boo = true; System.out.println("Enter the number"); Scanner in = new Scanner(System.in); number = in.nextInt(); for (int i = 2; i<= number/2; i++) { count = number%i; if (count == 0) { boo = false; break; } } if(boo) System.out.println(number + " is a prime number"); else System.out.println(number + " is not a prime number"); } }
Output
Frequently Asked Questions
Q #1) How to declare a boolean in Java?
Answer: Boolean in Java is declared using a keyword called “boolean”.
Below is the syntax and based on this syntax, we declare a Java boolean.
boolean variable_name = true/false;
Such as boolean b = true;
Q #2) What is a boolean example?
Answer: Boolean is a primitive data type that takes either “true” or “false” values. So anything that returns the value “true’ or “false” can be considered as a boolean example.
Checking some conditions such as “a==b” or “a<b” or “a>b” can be considered as boolean examples.
Q #3) Is boolean a keyword in Java?
Answer: Java boolean is a primitive data type. All boolean Java variables are declared by a keyword called “boolean”. So, boolean is a keyword in Java.
Q #4) How to print the boolean value in Java?
Answer: Below is an example of printing boolean values.
public class example { public static void main(String[] args) { boolean b = true; System.out.println(b); } }
Output
Q #5) How to compare two Boolean values in Java?
Answer:
Below is an example of comparing boolean values.
Output
Q #6) What is a boolean in Java?
Answer: Boolean is a primitive data type in Java that has two return values. A boolean variable can return either “true” or “false”.
#7) How to return a boolean in Java?
Answer: A boolean value can be returned in Java with the help of the equals() method. Let us see the below example, where, we have initialized b1 and b2 with the same value and implemented one condition with the help of the equals method.
Since this method’s return value is either “true” or “false”, it will return one of them. If the return value is true, then the first print statement will be printed, otherwise, else condition will execute.
Output
public class example { public static void main(String[] args) { /* * b1 and b2 are initialized with the same value. */ Boolean b1 = new Boolean(false); Boolean b2 = new Boolean(false); // if condition with equals method. if(b1.equals(b2)){ System.out.println("b1 is equal to b2"); } else { System.out.println("b1 is not equal to b2"); } } }
Q #8) How to call a boolean method in Java?
Answer: Below is an example of how to call a boolean method in Java. This is probably the simplest way of calling a boolean method as you just have to specify the method name inside the main method.
You must have to add a return statement for your specified boolean method.
[ java]public class example { public static void main(String[] args) { System.out.println(boo()); } public static boolean boo() { boolean b = true; return b; } } [/java]
Output
Conclusion
In this tutorial, we explained Java boolean along with a description, syntax, and some of the very important boolean examples that also include finding a prime number.
Moreover, we saw how to print boolean variables, how to use these variables with the if condition, how these variables are returned using operators, and so on.
This tutorial also provided some important frequently-asked questions that are important and trending.
=> Check ALL Java Tutorials Here.