Boolean Keyword in Java
The Boolean Keyword is a primary data type in Java. It is a primitive data type used in many programming contexts, including conditional statements, loops, and methods. It specifies a single piece of information, and it is impossible to describe its size. There are just two possible values that can be stored: true or false. The Boolean keyword is used to declare a variable that can hold a true or false value. To declare a boolean variable in Java, you use the keyword Boolean followed by the variable name and an optional initial value, like this.
Syntax
boolean isReady = true;
In this syntax, we declare an " isReady " boolean variable and assign it the "true.". The value can also be assigned later in the program like this.
boolean isReady;
isReady = false;
Boolean variables can be used in conditional statements to control program flow like this.
Simple Boolean Method in Java
This Example demonstrates the use of a simple if-else statement to compare two numbers and print the smaller of the two. Here's a breakdown of the code:
class demoprogram {
public static void main(String[] args) {
int number1 = 40;
int number2 = 50;
boolean a1 = true;
boolean a2 = false;
if (number1 < number2) {
System.out.println(a1);
} else {
System.out.println(a2);
}
}
}
This program defines a class named demoprogram which contains a main method. The main method initializes two integer variables, number1, and number2, with 40 and 50, respectively. It also initializes two boolean variables, a1 and a2, with the values of true and false, respectively.
The main method then uses an if-else statement to compare number1 and number2. If number1 is less than number2, the program prints the value of a1 (which is true) to the console. Otherwise, the program prints the value of a2 (which is false) to the console.
In this case, the program will print the value of a1 (which is true) to the console since 40 is less than 50. Therefore, the output of the program will be:
Here is the output that would be produced by running this program
Output
Since number1 is less than number2, the program prints the value of the a1 variable, which is set to true.
Comparing the variables in Java
This Java example demonstrates the use of Boolean variables and the equality operator. Here's a breakdown of the code:
class demoprogram {
public static void main(String[] args) {
boolean a1 = true;
boolean b2 = false;
boolean c3 = (a1 == b2);
System.out.println(a1);
System.out.println(b2);
System.out.println(c3);
}
}
This Example begins by declaring two boolean variables, a1 and b2, and initializing them to true and false, respectively. It then displays a third boolean variable, c3, and sets its value to the result of the expression (a1 == b2). This expression uses the equality operator == to compare the values of a1 and b2. Since a1 is true and b2 is false, c3 will be false.
The program then prints the values of a1, b2, and c3 using System.out.println(). The output of this program will be:
Output
This output shows how boolean variables can store true or false values and how the equality operator can be used to compare boolean values.
Boolean type in Java
This is a Java program that demonstrates the use of a boolean function to check whether an integer is greater than 20
. Here's a breakdown of the code
class demoprogram {
public static boolean Data(int number) {
if (number > 20) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
System.out.println(Data(25));
}
}
This statement defines a function named Data that takes an integer parameter named number. The process uses if-else information to check whether the number is greater than 20. If it is, the function returns true. Otherwise, the function returns false. The program also defines a main function that calls the Data function with an argument of 25 and prints the result using System.out.println(). Since 25 is greater than 20, the Data function will return true, and the program will output true.
This program demonstrates how boolean functions can encapsulate logic and return true or false values based on input parameters.
Output
Prime number in Java
A prime number is a number that is greater than one and divided by one or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For Example, 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.
Note 0 and 1 are not prime numbers. 2 is the only even prime number because all the other actual numbers can be divided by 2. Let's see the prime number program in Java. In this Java program, we will take a number variable and check whether the number is Prime.
class DemoProgram {
public static void main(String[] args) {
int num = 9;
boolean Name = false;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
Name = true;
break;
}
}
if (Name) {
System.out.println("Not prime");
} else {
System.out.println("Prime");
}
}
}
This program is that checks if a given number is prime or not. The program takes an integer value, num, and then uses a for loop to check if it is divisible by any integer between 2 and num-1. If num is divisible by any integer in that range, the program sets the flag variable to true and breaks out of the loop.
After the loop, the program checks the value of the name variable. If true, the program prints Not prime, indicating that num is not a prime number. If the name is still false after the loop, the program prints Prime indicating that num is a prime number.
Note that the code assumes that the input value is greater than 1. The program may produce incorrect results if the num is less than or equal to 1.
Output
Conclusion
In this article, you will learn about code that taught us what Boolean Keyword is in Java.