Adam Milburn
Adam Milburn

Reputation: 11

How to Create a Public Boolean in Java?

This is my first time asking a question here, so I'll ask you to bear with me: I am trying to create a public boolean method, isEven(), that will check if a number is evenly divisible by two and return a value of true or false based on that. However, as this is a public method, I am unsure of how exactly to write it; this is my process thus far:

public boolean isEven()
    {
    if(WHAT_GOES_HERE? % 2 == 0)
        return true;

    else
        return false;
    }

I would appreciate some advice on how exactly to go about writing this method; thanks in advance!

Upvotes: 1

Views: 21444

Answers (1)

sneelhorses
sneelhorses

Reputation: 175

The simplest way would be

public boolean isEven(int value){
    return value % 2 == 0;
}

Using an if/else statement to return or set variables to boolean values is almost always redundant. Since you can return the condition you put in the if/else itself, the if/else is not needed.

Upvotes: 2

Related Questions