Click here for the previous tutorial.
Continuing with the theme of variables, we have booleans. Fortnuately, booleans are the simplest form of variables. As a fun note, in this tutorial you will literally be working in those 1s and 0s that we dreaded earlier on. I promise it is not as hard as it sounds.
A boolean can be equal to a true or false. That is really it.
boolean booleanName;
booleanName = true;
booleanName = false;
And that is how it works. You can pass this into the println method, and it will print either "true" or "false" onto the screen. This is not a typical use for the boolean type, but it is acceptable.
The general use of booleans is performing tests. We will get to if and else statements, as well as loops later on. For now you can use them for the simplest of tests: the inline if statement.
The inline if statement has a fairly simple form, but can seem fairly archaic if not practiced.
test ? if true : if false
If you do not understand, fear not, because we will explain it now. For test, simply have a boolean. Then, for the if true or if false, simply what will happen in either case. It probably still sounds a bit rough, so here are some examples.
int intName = booleanName ? 7 : 19;
In this particular case, it tests booleanName in order to determine the value that should be stored to intName. If booleanName is found to be true, then intName is set to 7, but if booleanName is false, then intName is set equal to 19.
System.out.println(booleanGreeting ? "Hello" : "Goodbye");
Here, the boolean booleanGreeting presumably keeps track as to whether the user is coming or going. If booleanGreeting is true, then the user is determined to be coming, and is greeted accordingly. If false, then the user is leaving and is answered as such.
Because this concept can seem especially strange at times, I would encourage playing with it until you feel comfortable.
Now it may seem like it is a bit useless to just have a bunch of memory slots remembering true and false scores, and that would be very useless. Fortunately, there is more purpose than that for booleans.
For one, you can determine booleans based on logical operators. Most logical operators are made to work with number type variables, or numbers themselves. Here are some examples.
> (Greater than. so "5 > 3" would return true. "4 > 4" would be false)
< (Less than. "6 < 9" would be true. "8 < 8 " would be false)
== (Is Equal To. "9 == 9" would be true. "7 == 3" would be false)
>= (Greater Than Equal To. "9 >= 9" Would be true. "2 >= 4" Is false)
<= (Less Than Equal To. "5 <= 5" Is true. "5 <= 3" Is false.)
!= (Does Not Equal. "9 != 6" is true. "7 != 7" is false)
Finally, you can also make use of logical ands and logical ors in order to accomplish operations with booleans.
&& (Logical And)
|| (Logical Or)
Here are a few examples.
true && false = false
true && true = true
false || false = false
false || true = true
And, by use of parenthesis, you can make things WAY more complicated. Try and avoid this in actual programs, but sometimes it is fun for puzzles.
(true || false) && ((true && true)||((false && true) || false)) = true
((((true && false) || (false || true)) && ((false || true) && false))||((false || true) && (((false && true)||(true && false)) || false) = ???
Can you figure the second one out?
Click here to coninue to the next tutorial.
Sample code explained here:
boolean booleanName;
booleanName = false;
test ? if true : if false;
Always remember to keep code readible!
The in line if statement is particularly guilty of finding itself in hard-to-read text. This can often be remedied by giving appropriate white space, in the forms of spaces, tabs, new lines, or all three.
Playing with code is actually a very good way to learn! Try to enjoy it, and it will come a lot easier.
Even the best programmers run into errors quite often. It is not at all a sign that you are incompetent. Remember that programming is ultimately about problem solving, and where would that practice be without a headache full of problems?
true && true = true
true && false = false
false && false = false
false && true = false
true || true = true
true || false = true
false || false = false
false || true = true