Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Harnessing the Power of Objects Abstraction at Play

João Ignácio Brito
João Ignácio Brito
1,201 Points

Boolean method

Can anyone explain to me the boolean method:

public boolean isEmpty() {
      return pezCount == 0;
}

Is it like an if statement or something like that? I don't know if I'm missing something, but I got very confused now. Will it only return true if pezCount == 0?

2 Answers

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Joao, I'd rather say that an if statement uses a boolean value for control flow and that one can also return a boolean value in a method.

One could also rewrite the method you posted like this:

public boolean isEmpty() {
    if (pezCount == 0) {
        return true;
    } else {
        return false;
    }
}

So I think it's less of an if statement in a method, but rather a boolean value used in an if statement one time and used in a method another time.

"pezCount == 0" being the boolean value here in both cases.

Kind Regards, Florian

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hi, Joao, yes it will only return true if the count is 0. If it is any other value it will return false. You are right that an if statement also checks for a boolean value, being either false or true.

Kind regards, Florian

João Ignácio Brito
João Ignácio Brito
1,201 Points

Thanks for the answer! So we could say that it's the same as an if statement but in a method form, that allows you to only write a line of code, calling this method, and it does the same as if we wrote an if statement?