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

Android Threads and Services Bound Services Making First Contact

I have a question about the mBound variable on the onclicklistener

Ok I am a little confused with the if statement. Were trying to see if the activity is bound to the service. Shouldn't we write the if statement like this? if(mBound == true), then that means that we are bound to the service. Example

if(mBound == true) { //then mPlayerService.isplaying(); }

mPlayButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

            //Check and Make sure we are bound to the service
            //Now we need to know whether we should be playing or pausing
            if(mBound) {

                  //then
                  mPlayerService.isplaying();
            }

        }
    });
}

1 Answer

andren
andren
28,558 Points

Comparing a Boolean variable to a Boolean is redundant. The reason for that requires an explanation of how comparisons and if statements work behind the scenes.

When you compare two values, be that an equals comparison or a greater than comparison or anything else, Java takes the comparison and turns it into a Boolean. So 1 > 3 becomes false, 5 == 5 becomes true and so on. So whenever a comparison is evaluated it gets turned into a Boolean, that is the value that actually gets passed to anything that accepts a comparison.

So if statements and pretty much any other statement that accepts conditions (like loops and the like) is designed to work specifically with Booleans. If the Boolean it is given is true it will run, if the Boolean is false it won't run. That is actually all the if statement cares about, whether it is passed the Boolean true or false.

Given that information what do you think happens when you pass mBound == true into an if statement? Well the first thing that happens is that Java tries to evaluate the comparison. If mBound currently equals true. then the comparison becomes true == true which obviously results in true. If mBound equals false then the comparison turns into false == true which obviously results in false.

The issue with that is that in both cases the result of the comparison is the same Boolean value that was already stored in mBound, meaning that the comparison ended up having no effect at all compared to just passing in mBound directly since that is already a Boolean.

So long story short, mBound == true would have worked fine, but it will not result in anything different from what is already stored in mBound, so passing that directly into the if statement is more efficient and has the exact same effect.