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 trialhakan Gülay
8,489 Pointsboolean was dispensed=false
Hi, ı couldnt understand what ''boolean wasDispensed=false;" does ? and also wasDispensed=true? actually ı couldnt understand this video. can anyone explain to me ? thx.
2 Answers
Brendon Butler
4,254 PointsA boolean stores a value that's equal to true/false or 1/0.
Let's look at the information you provided. "boolean wasDispensed = false;"
The first word in the sequence is boolean, this is the variable type. Sort of like String or int. You need this to tell the compiler what type of variable you're about to assign.
"wasDispensed" is the variable name. This can be almost anything you want. Instead of "wasDispensed" you could type "dispensed" or even something totally irrelevant such as "isTheForceWithYou". This is bad practice, and you should keep your variable names as relevant as possible.
the = sign is an assignment operator. Basically telling java to set the variable to the value that you put after it.
"false" is one of the states that a boolean has. Instead of "false", you could use "true"
So knowing all this, "boolean wasDispensed = false;" would read as "Set the boolean variable wasDispensed to the value of 'false'".
Hopefully that clears things up a bit. If not, think of it this way. Imagine a box, this box is empty and we have a boolean variable "isEmpty". Since the box is empty, you would set the value of that variable to "true." Now let's say you have a cat, and you put that cat inside the box. Now that there's something inside the box it would no longer be empty. So you would want to set the value of "isEmpty" to "false" because there is something in the box.
hakan Gülay
8,489 PointsThanks so much Brendon! I got it now :) thank you so much :)
mfod
1,708 PointsBrendon Butler So in the case that it is not empty the wasDispensed variable will be set true and...then what happens? Will it skip down and return true? I'm confused on how it continues to keep dispensing pez if wasDispensed is initially set to false.
Brendon Butler
4,254 PointsMariam Olupitan Oh sorry, yeah. In the case that the dispenser is not empty, it would run the code inside of the if statement (anything between the curly braces) after all that code has been run, it will escape the code block and run any code that's under the if statement.
But if the dispenser is empty, it will completely skip any code within the code block (curly braces) and continue to run any code under it. I should've cleared that up. Sorry! Happy coding!
hakan Gülay
8,489 Pointshakan Gülay
8,489 PointsThanks for answering again :) I understood what you all said but I couldn't understand this code.Can you explaing step by step please this :)
Brendon Butler
4,254 PointsBrendon Butler
4,254 PointsI added some comments to the code, that should hopefully clear some things up.
mfod
1,708 Pointsmfod
1,708 PointsQuick question... public boolean dispense() { boolean wasDispensed = false; if (!isEmpty()) { mPezCount--; wasDispensed = true; } return wasDispensed; } Does dispense() return true or false? I would like to think it returns true because it is the final statement of the if ,but I'm not sure if its out of scope.
Brendon Butler
4,254 PointsBrendon Butler
4,254 PointsMariam Olupitan
It would return false unless the if statement returns true. If the if statement returns false, then it will skip whatever is inside of the if statement's code blocks (curly braces).
For example, let's say that the dispenser is empty.