The moose likes Beginning Java and the fly likes Boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean" Watch "Boolean" New topic


Linnea Palin

Joined: Oct 02, 2003
Posts: 12
Okay, here's the deal. I searched FAQ and all other links i found but none contains anything about booleans, which my programming teacher told me to look into a bit..
Would anyone please tell me what a boolean is and what it's used for?
Thanks in advance..
Sandeep Achar

Joined: Sep 26, 2003
Posts: 18
Hi Linnea,
boolean is a primitive datatype of Java that can have one of only 2 possible values:
1. true
2. false
Other than these, it is not legal to assign any other value to a boolean variable.
The following are examples of some legal assignments:
boolean x = true;
boolean y = false;
y = x; // valid since x is of type boolean.
The following are some examples of illegal assignments:
boolean x = 1; // Cannot assign anthing other than "true" or "false"
boolean y = 0; // same reason as above
Hope this suffices. Bye.

Regards,<br />Sandeep<br />(SCJP 1.4, SCBCD 1.3)
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 253
The boolean type represents true/false values. Java defines the values true and false using the reserved words true and false. Thus, the value of a variable or expression of type boolean will be one of these two values. The conditional expression in the if statement and the various loops are controlled by boolean expressions.
Here is a program that demonstrates some aspects of the boolean type:

The output generated by this program is shown here:
b is false
b is true
This is executed.
10 > 9 is true
There are three interesting things to notice about this program. First, as you can see, when a boolean value is output by println(), "true" or "false" is displayed. Second, the value of a boolean variable is sufficient, by itself, to control the if statement. There is no need to write an if statement like this (although it is not wrong to do so):
if(b == true) ...
Third, the outcome of a relational operator, such as >, is a boolean value. This is why the expression 10 > 9 displays the value "true." Further, the extra set of parentheses around 10 > 9 is necessary because the + operator has a higher precedence than the >
[ October 07, 2003: Message edited by: Herb Schildt ]

For my latest books on Java, including Introducing JavaFX 8 Programming, see
Linnea Palin

Joined: Oct 02, 2003
Posts: 12
This took some reading, but thanks a lot for your answers and I'll test Herb's code and change and try some stuff with it..
Now I think I've got a good picture of what it's about: time for testing, changing, trying, testing, changing, trying and it all over again
Thanks again.
Linnea Palin

Joined: Oct 02, 2003
Posts: 12
I've posted a thread that bears the topic "Help - boolean" aswell. Please read it through if you haven't.
I'm doing an assignement for my programming lessons, so therefore, please do not give me the answers, but give me help to get there Fair enough?
Okay, this is how far I've come after playing around with codes..

The thing is the errors I get are these:

why won't it work, what's the thing i missed about booleans? or is there something wrong with the int's i've made?
Thank you in advance..
Linnea Palin

Joined: Oct 02, 2003
Posts: 12
Another test I made:

gives only one error. One the line

If you respond, please make sure i understand what version of code you meant
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Linnea -
I'm addressing your last message, which appears incomplete. So, if I misunderstand your question, please elaborate.

In this line, you're trying to use a comparison operator to assign a value. You can fix that by assigning the result to a boolean:

Now you can run tests against the variable b.
Or, you can embed the test into a conditional statement:

If you're trying to assign the value to the int a, then:

Note the single equal sign for assignment.
Hope that helps!
[ October 07, 2003: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Linnea Palin

Joined: Oct 02, 2003
Posts: 12
ok, i changed into a single equalsign.. also changed some other things, so please tell if i messed up something else instead.

and error is

Why is that? I can't put a ; in the middle of the equal signs..
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

I'm not an expert, but I'll tell you what I see.
a "=" means "put the value on the Right Hand Side (RHS) into whatever is on the Left Hand Side (LHS)". the things on the right must all exist, but the thing on the left can be created in this step.
a "==" means "tell me if these two things are the same". any variables must already be created and given a value (i'm pretty sure that is correct) before we do this comparison, but we can do a computation on both sides. it's generally only used in an if, while, for, or do-while loop.

in your last post, you have the line
boolean b == (a<1);
are you trying to assign b the "truthfulness" of (a<1)?? if so, this needs a single "=".
if you are trying to test whether b and (a<1) HAVE the same truthfullness, you need to GIVE b a value first.
boolean b = true;
b == (a<1);
But this is not even valid (at least on my compiler). why? what does that second line do? it compares two things, but throws the result away. so the compiler says "what's the point of this???"
go back through your code, and look at all your == and =. try to figure out when you want to SET some value, and when you wan to COMPARE two things. if you are comparing, what are you doing with the comparison???
hope this helps, or doesn't confuse you too much. i didn't want to just give you the answer. it's the former teacher in me...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 253
This line

is incorrect. This is the same problem that you had before, which was explained by Jeff. When you declare a variable, you can give it an initial value, but to do this, you must use a single equals, like this:

Furthermore, both of the variables a and x are used before they are assigned values. This is an error in Java.
Also, this if statement

is technically correct, but I doubt that it does what you intend. To control the if, there is no need for b. The outcome of a>0 produces a boolean result, which is sufficient in itself. For example,

is better. What your original code did is compare a>0 and then assign this boolean result to b, with the overall value of the expression being the result of a>0.
In my original reply to your question, I was not intending to suggest that you need to use a boolean variable, per se, to control an if statement. Only that all conditional statements in Java are controlled by boolean expressions. In Java, the result of any relational or logical operation, such as a < 10, produces a boolean result.
Linnea: I think that you need to get a good introductory book on Java and read through the first few chapters. Doing so will answer a lot of your questions.
Herb Schildt
Ranch Hand

Joined: Oct 01, 2003
Posts: 253
After thinking about your posts, it occurred to me that your original question may have been referring more generally to the concept of boolean expressions, such those used to control an if statement, rather than to the boolean data type, specifically. Along these lines, here's some basic information on the if statement and the expression that controls it. Perhaps it will clear up some of the trouble that you have been having.
The simplest form of the if is shown here:
if(condition) statement;
Here, condition must be an expression that produces a boolean (i.e., true/false) result. If the condition is true, then the statement is executed. If the condition is false, then the statement is bypassed. Here is an example:

In this case, since 10 is less than 11, the conditional expression is true, and println() will execute. However, consider the following.

In this case, 10 is not less than 9. Thus, the call to println() will not take place.
Of course, you can add an else clause to the if, as you have done in your code, to provide an alternative. You can also make the target of an if or else be a block of statments rather than a single statement.
Java defines a full complement of relational operators that can be used in a conditional expression. They are shown here.
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal to
!= Not equal
Notice that the test for equality is the double equal sign.
You can also use the logical operators:
| OR
&& Short-circuit AND
|| Short-Circuit OR
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Glad to see those operators in the post above. If your instructor suggested a general look at booleans, you should be aware that there is a whole algebra for booleans. Frinstance, DeMorgan's transform:
the opposite of (a and b) is (not a or not b)
These can be hard to follow the first time through. If I say "To have a parade you must have a clear day (A) and temperature above 50 degrees (B)" then what would cancel a parade? A cloudy day (not A) or a cold day (not B).
The short way to write it is
!(A & B) = (!A | !B)
If that kind of thing grabs your imagination, I'm sure that gang here will chime in with much more than I can remember right off. This is handy stuff to understand just writing and reading code, but down at the chip level it's all you can see for miles and miles.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
alrighty! Bring on the boolean algebra and karnaugh maps!!
I agree. Here's the link:
subject: Boolean
It's not a secret anymore!