Boolean type

AVAILABLE

"Hi, Amigo. Let me tell you about a new data type. The boolean. Variables of this type can take only two values: true and false."

"How do we use it?"

"This type is implicitly used in many places. Just as any addition operation produces a number, the result of any comparison is a boolean. Here are some examples:"

Code Explanation
1
boolean m;
These two expressions are equivalent. The default value of a boolean variable is false.
2
boolean m = false;
3
if (a > b)
    System.out.println(a);
The result of the comparison (either true or false) will be assigned to the variable m. The condition is satisfied if the expression evaluates to true.
4
boolean m = (a > b);
if (m)
    System.out.println(a);
5
boolean m = (a > b);
if (m == true)
    System.out.println(a);
There is no need to compare a logical (boolean) variable with true or false. The result of the comparison will be a boolean that matches the other variable. For example, true == true evaluates to true; true == false evaluates to false.
6
boolean m = (a > b);
if (m)
    System.out.println(a);

"More examples:"

Code Explanation
1
public boolean isALessThenB (int a, int b)
{
    if (a < b)
        return true;
    else
        return false;
}
This method verifies that number a is less than number b.

Here are four equivalent comparisons. The last one is the most compact and correct. Always try to use compact notation.

2
public boolean isALessThenB (int a, int b)
{
   boolean m = (a < b);
    if (m)
        return true;
    else
        return false;
}
3
public boolean isALessThenB (int a, int b)
{
    boolean m = (a < b);
    return m;
}
4
public boolean isALessThenB (int a, int b)
{
    return a < b;
}
6
Task
Java Syntax,  level 4,  lesson 7
Locked
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and earn a little dark matter. What's more, you combat laziness.

"What if I want to write 0<a<b?"

"Java doesn't have a comparison operator that takes three operands. So, you would need to do it like this: (0<a) AND (a<b)."

"Do I write the word AND?"

"Wait. I'll explain that. Java has three logical operators: AND, OR and NOT. You can use them to construct conditions of varying complexity. You can use these operators only with boolean expressions. So, you can't write (a+1) AND (3), but (a>1)AND (a<3) is OK."

"The NOT operator is unary: it affects only the expression to the right. It's more like a minus sign before a negative number rather than a multiplication sign between two numbers."

"You can perform various operations on boolean (logical) variables."

"Like what?"

"Let's take a look:"

Logical operator Java notation Expression Result
AND && true && true true
true && false false
false && true false
false && false false
OR || true || true true
true || false true
false || true true
false || false false
NOT ! ! true false
! false true
Common combinations and expressions m && !m false
m || !m true
! (a && b) !a || !b
! (a || b) !a && !b

"Could you give me more examples?"

"Sure:"

Java notation Logical notation
(a<3) && (a>0) (a < 3) AND (a>0)
(a>10) || (a<100) (a>10) OR (a<100)
(a<b) && (!(c<=d)) (a<b) AND (NOT (c<=d))

"Now, do some tasks."

3
Task
Java Syntax,  level 4,  lesson 7
Locked
Labels and numbers
Not all labels are equally useful, but sometimes a lack of labels results in chaos. Let's do a task that dishes out labels: use the keyboard to enter an integer, and make the program indicate whether the number is negative or positive (or zero), and even or odd. Why do we need this? Just because, for practice. After all, we're here to learn!
3
Task
Java Syntax,  level 4,  lesson 7
Locked
Describing numbers
Suppose we need to separate numbers according to some criteria. For example, based on whether they are even or odd, or based on the number of digits. Have you got a picture of that in your mind? And now for the work: we'll write a program that describes the number entered by the user (it must be in the range from 1 to 999). The description will indicate whether the number is even or odd, and how many digits it has.
3
Task
Java Syntax,  level 4,  lesson 7
Locked
Positive number
At the "Optimist" school, the scale used to assess knowledge has neither an upper nor lower bound. This pleases young gifted people (wouldn't you be happy to receive a score of a bazillion for a presentation?). All scores above zero are considered good. Scores below zero are bad. Students must pass final exams in three subjects. We need to enter three scores from the keyboard, and display the number of positive scores.
3
Task
Java Syntax,  level 4,  lesson 7
Locked
Positive and negative numbers
Data analysis starts with a simple concept: we need to understand what data matters and should be used, and what data should be discarded. Let's solve a simple data analysis problem. We enter three integers from the keyboard, then we'll analyze them and display the number of positive and negative numbers in the source set.
Comments (25)
  • Popular
  • New
  • Old
You must be signed in
JayLevel 7 , Washington
6 July, 23:27
What's meant by the requirement: Note that 0 is not a positive or negative number?
8 July, 15:06
use if(number > 0) { .... }
Hoang KhaiLevel 7 , Arlington
1 July, 15:17
There are several assignments in this section you can solve using array. I applied array to tasks that can possibly work well with this basic data structure. I aced tasks easily.
8 July, 15:05
me too. but I didn't know how to use the array, but Google helped me =)
Alan LyneLevel 5 , Maynooth
11 June, 14:23
Anyone else notice the massive increase in the number of challenges xD
IevaLevel 5 , Paris
27 June, 15:09
For Sure!!!!!
TangerinLevel 18 , Chongqing
8 June, 16:04
Is there anyway to quickly type BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); in IDEA just like sout and psvm? I'm tired to type these codes:(
AlexLevel 41
10 June, 09:08
Yes, there's a way to add your own shortcuts in IntellijIDEA. Go to File -> Settings -> Live Templates. To the right of this menu window you will see the + and - buttons. Go ahead and press the + button, then choose Live Template, which will create a new temp shortcut template under the "user" section. Add the abbreviation and a short description you wish to use for this code, then in the "Template text" window type/paste your code. Then next to the "No applicable context" text click on the Define link and choose Java. Click Apply and you're done. You should now be able to use your shortcut in any IntellijIDEA code window.
CarlosLevel 11 , Leon
24 May, 21:57
well, I've tested using both Java 12 and Java 8 and I still have to send some tasks for verification more than once
Jyoti MauryaLevel 8 , Maihar
21 May, 08:56
okay but where to use boolean? i am so comfortable with if else that I solved all the problems with them only. 0_o
IevaLevel 5 , Paris
27 June, 15:09
Me Too!!!!!
Bert DoeLevel 18 , Flanders
16 April, 15:18
sweet, moving on
ZibaniLevel 9 , Here
17 February, 08:55
Heyyyy, they're finally teaching something they expected people to know several lessons ago.
HariKrishna KVLevel 6 , Bangalore
12 February, 04:34
false && false is true right? how it is false? please help me for this.
Ștefan MarinescuLevel 24 , Iasi
13 February, 21:57
False AND False is False. If you want to get true with && operator both values must be true. if you want to get true with the || operator only 1 value has to be true.
Don BoodyLevel 8 , Victor
15 February, 15:04
int a = 1;
int b = 2;
int c = 3;

if(a > b && a > c) {
System.out.println("Hello world!");
}
It is true that both of the above conditions are false. However, since they are both false, this code will never execute. The condition must be true in order for the code to execute. Since the condition is false (1 is not greater than 2, and 1 is also not greater than 3) this code will never execute. I hope that helps to clear it up.
Kelvin Level 5 , Lagos
11 January, 13:38
Right from when we learnt how to get keyboard inputs, when ever I write my code to get keyboard inputs, I always get errors but the same code works on my netbeans IDE. Can anyone help me out
Đinh Văn MinhLevel 14 , Hanoi
11 January, 15:33
I met the same trouble with you
Kelvin Level 5 , Lagos
13 January, 22:06
what did you do to solve the problem bro.
Love PatelLevel 5 , San Leandro
24 January, 20:08
what code do you use?
Clark PengLevel 7 , Oakland
26 January, 18:44
go to the run tab and there will be a place to put your inputs in "Input data (System.in)" underneath the system.out place
durmusgulbaharLevel 14 , Birmingham
5 June, 21:21
If you used "Scanner" class , you should import "java.util.Scanner" , the compiler doesn't have this so you must write it. If you would use "BufferedReader" you must import "java.io.*.