Return a Boolean Method in Java

  1. Structure of a boolean method with a return statement in Java
  2. Return a Boolean Method - Example 1
  3. Return a Boolean Method - Example 2

This article will introduce methods to return a boolean method in Java.

Structure of a boolean method with a return statement in Java

Consider the code snippet below.

public boolean CheckPassword(String pass){ 
}
  1. public: this is a modifier that shows that the class, field, method, and constructor can be accessed by all codes regardless of the location.
  2. boolean: This identifies the type of value expected to return after the method performs the specified tasks.
  3. checkPassword(): This the name of the method.
  4. String: This declares the parameter types that the method will accept.

From the method structure above, a method’s return type is declared in the method declaration. A method that declares the return type void will not contain a return statement. Any method that is not declared void must contain the return statement used to exit the method.

Return a Boolean Method - Example 1

The sample code below checks whether a number is even or odd. The boolean method returns true if it is even and false when it is odd.

public class booleanMethod {
  
    public static boolean isEven(int mynumber)
    {
        return (mynumber % 2 == 0);
    }

    public static void main(String[] args)
    {
        int mynumber = 130;
        if(isEven(mynumber) == true)
            System.out.print("Even Number");
        else
            System.out.print("Odd Number");
    }
}

Output:

Even Number

In the code above, the first step is to declare the boolean method and the return value expected. The boolean method returns a value that guides how the code login is implemented in the next method. The public declaration allows the code to be accessed by other methods to check whether the numbers passed are even or odd.

In the second method, each time an int is passed to check whether it is even or odd, the boolean method assigns a boolean value to the outcome. This boolean value is then used to determine the output of the method.

Return a Boolean Method - Example 2

The code sample below checks whether a student’s score is above or below 72.

public class booleanMethod {
  
    public static boolean isAbove (int thescore)
    {
        return (thescore > 72 );
    }

    public static void main(String[] args)
    {
        int thescore = 56;
        if(isAbove(thescore) == true)
            System.out.print("Above average");
        else
            System.out.print("Below average");
    }
}

Output:

Below average

The first method declares the return value as boolean and the acceptable parameter as int. This method’s return statement is of boolean type based on whether the parameter is above or below 72. If the int value passed is below 72, the method returns false, and if the value passed is above 72, the method returns true.

The method below uses the value returned from the boolean method to determine whether the given score is above or below the average score. This simplifies the code and ensures that a developer doesn’t have to repeat each value’s comparison process.

Return a boolean method for a list in Java

The sample code below checks how many students scored above 75 marks in a class using the boolean method’s return statement.

import java.util.*;

import java.util.*;
public class booleanMethod {
  
    public static boolean isAbove (int thescore)
    {
        return (thescore > 75 );
    }

    public static void main(String[] args)
    {
        int aboveAverage=0;
        int belowAverage=0;
        List <Integer> classscores= new ArrayList<>(); 
        classscores.add(90);
        classscores.add(63);
        classscores.add(72);
        classscores.add(75);
        classscores.add(81);
        classscores.add(52);
        classscores.add(69);
        classscores.add(78);
        
        for (int i=0; i<classscores.size(); i++){
            if(isAbove(classscores.get(i))==true){
            aboveAverage++;
            }else{
            belowAverage++;
            }
          
        }
        System.out.print( aboveAverage +
		" scored above 75 and "+ belowAverage 
		+  " scored below 75"); 
    }
}

Output:

3 scored above 75 and 5 scored below 75
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Function

  • Pass a Function as a Parameter in Java