. Advertisement .
..3..
. Advertisement .
..4..
In addition to other data types, you can also return a boolean method in Java. Scroll down to learn more about this technique.
Return A Boolean Method In Java
One Boolean Value
You can use the return keyword to make a method return a boolean value, just as with other return values, unless your code throws an exception before reaching this keyword. Even when you don’t plan to return a true or false value, it is a requirement. The only exception is void methods, which don’t need any return statement.
Make sure that you also specify the type of this return type (boolean) in the method declaration first, such as:
public static boolean yourMethod() {}
You will also need a return statement at the end of the method’s code block, like this:
return return_value;
The return_value variable here should be a boolean object that has been defined in the block. You can’t use a value of another data type, such as string or integer, because your return value should always match the return type you have declared with the method.
Here is an example of returning a boolean method in Java:
import java.util.Scanner;
class checkprime {
public static boolean isPrime(int num) {
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
return true;
else
return false;
}
public static void main(String[] args) {
Scanner userObj = new Scanner(System.in);
System.out.println("Enter a number:");
int userInt = userObj.nextInt();
if(isPrime(userInt))
System.out.print(userInt + " is a prime number.\n");
else
System.out.print(userInt + " is not a prime number.\n");
}
}
Output:
Enter a number:
3
3 is a prime number.
The program above asks for an integer from the user and checks if it is a prime number. We verify this by putting it through a typical primality test to find whether it has another factor besides 1 and itself.
The code for this task is wrapped in a method called isPrime() with this declaration:
public static boolean isPrime(int num) {...}
This declaration means the method needs only one (integer) argument and returns a boolean value. An if statement is a common way to check for certain conditions and return a true or false value depending on those conditions.
In a similar way, our main() method has another if statement to check the boolean value returned by the isPrime() method and print a message.
Multiple Boolean Values
You can put multiple boolean values into a Java array and return it if you need to perform several checks at once, for instance. In this case, you will need to put a boolean array in the method declaration as well as create an array of boolean values within the method’s code block.
The following example illustrates how you can take advantage of this capability in Java:
import java.util.Scanner;
class returnboolean2 {
public static boolean[] check(int num) {
boolean[] results = new boolean[]{true, false};
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
results[0] = false;
break;
}
}
if (num > 100) {
results[1] = true;
}
return results;
}
public static void main(String[] args) {
Scanner userObj = new Scanner(System.in);
System.out.println("Enter a number:");
int userInt = userObj.nextInt();
boolean results[] = check(userInt);
if(results[0]) {
System.out.print(userInt + " is a prime number.\n");
} else {
System.out.print(userInt + " is not a prime number.\n");
}
if(results[1]) {
System.out.print(userInt + " is bigger than 100.\n");
} else {
System.out.print(userInt + " is not bigger than 100.\n");
}
}
}
Output:
Enter a number:
101
101 is a prime number.
101 is bigger than 100.
As you can see, the check() method now returns two separate boolean elements with just a single return statement.
Summary
You can return a boolean method in Java with the return keyword and proper method declaration. This is true for a boolean array as well, which allows a method to return multiple boolean values.Remember that Java can return other data types as well. For instance, follow this guide if you want to return a string.
Leave a comment