boolean type

Available

1. Boolean type

As we have already seen, Java has the super useful if-else statement. It executes one block of statements if the condition in parentheses is true, and a second block of statements if the condition is false.

For convenience when working with expressions that can be either true or false, Java's creator added the special boolean type. Its main feature is that variables of this type can take only two values: true and false.

It is impossible to assign any other values to boolean variables. The compiler won't allow it.

And why do we need such a primitive type?

Well, the good thing is that you can use it to store the values of logical expressions. Example:

Code Explanation
boolean isOK = true;
The boolean isOK variable contains the value true
boolean hasError = false;
The boolean hasError variable contains the value false
int age = 70;
boolean isSenior = (age > 65);
The boolean isSenior variable contains the value true
int record = 612;
int value = 615;
boolean hasNewRecord = (value > record);
The boolean hasNewRecord variable contains the value true
int min = 0;
int max = 100;
int temperature = -20;
boolean isIce = (temperature < min);
boolean isSteam = (temperature > max);

The boolean isIce variable contains the value true

The boolean isSteam variable contains the value false


2. Using boolean variables

Boolean variables would be of little use if they could only store the results of expressions. The point here is that you can also use them. Where? Wherever you can write a logical expression.

For example, you can use a boolean variable in condition of an if statement:

Code Equivalent
int age = 70;
boolean isSenior = (age > 65);
if (isSenior)
   System.out.println("Time to retire");
int age = 70;
if (age > 65)
   System.out.println("Time to retire");

In this example, there is little benefit gained from making this replacement, but when programs grow larger, their conditions become more complex. You will be convinced of this in the near future.


3
Task
New Java Syntax,  level 3lesson 5
Locked
What's the cat's name?
Help the cat get a name using the setName method.

3. Comparison operators

In Java, as in other programming languages, it is often necessary to compare variables with one another. And Java has just the operators you need to make comparisons:

Operator Explanation Example
< Less than a < 10
> Greater than b > a
<= Less than or equal a <= 10
>= Greater than or equal speed >= max
== Equals age == 18
!= Not equals time != 0

The above operators are used to produce logical expressions. The results can be stored in boolean variables or used as the condition of an if statement.

Important Point No. 1:

The operators that consist of two characters cannot be split apart.

In other words, code like this won't compile:

a < = 10
speed > = max
age = = 18
time ! = 0
Important Point No. 2:

Note that there are no => or =< operators. Only the <= and >= operators. If you write a=< 3, your code simply won't compile.

Important Point No. 3:

In Java, you cannot write an expression like 18 < age < 65. After all, the expression 18 < age will be evaluated to true or false. And you cannot perform the comparison true < 65 (the types are different). At least in Java.

What can be done? You will find the answer to this question in the next lesson.


3
Task
New Java Syntax,  level 3lesson 5
Locked
Cat register
Write code in the addNewCat method to increase the number of cats by 1 each time it is called. The variable catCount corresponds to the number of cats.
3
Task
New Java Syntax,  level 3lesson 5
Locked
Setting the number of cats
Write the setCatCount method. The method must set the number of cats (catCount).
3
Task
New Java Syntax,  level 3lesson 5
Locked
Name register
Finish writing the code of the setName method so that it sets the value of private String fullName to the value of the local String variable fullName.
3
Task
New Java Syntax,  level 3lesson 5
Locked
Count the number of cats
Write a code that correctly counts the number of created cats (count) and correctly displays the count on the screen.
Comments (13)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
haalk3n
Level 6 , Romania
29 January 2024, 13:21
For anyone struggling on the first problem: the "boolean isHigh" and "boolean isLow" is already declared! So when trying to assign them the bodyTemperature condition, remember to not write "boolean" in front of them again, just use "isHigh = bodyTemperature > 37" IT WORKS
Strex
Level 6 , Canada
12 December 2023, 17:48
Learned the importance of ordering my if statements properly today 😅
Abhishek Tripathi
Level 72 , Rewa, India
Expert
22 December 2023, 11:22
Just remember the sequence by how to it is read :- 1. <= (less or equal to) & 2. >= (greater or equal to). So as long as you will put the sign as they are read you are good to go. It is not equal or less than(=<).
Jennifer Olland
Level 4 , United States of America, United States
23 October 2023, 17:17
This one was really confusing as my output matched the output required, but something in the analysis of the program didn't recognize that and I initially had an if statement with several else statements for the input comparisons, however, I had to add an else if with nested if's after all. The ifs/else-ifs/ are confusing and I need to figure out a good method of placing the brackets so I can trace the blocks they belong to.
Johnny Silverhand
Level 3 , United States of America, United States
24 March 2023, 16:18
This one has many many different ways of clearing it, what I tried was first making a boolean "allEqual", which checked if all 3 numbers were the same... and then comparing 2 numbers ([1 & 2], [1 & 3], [2 & 3]) && making sure they're not allEqual, after a series of these "if else if" statements, I finally got to if (allEqual) { System.out.print( num1, num2, num3 );
Chrissi
Level 4 , Vienna, Austria
19 March 2023, 17:17
quite tricky...
Anonymous #11234130
Level 15 , Finland
29 December 2022, 08:29
What I did was create following booleans and then according to those print out the necessary with if statements. boolean areFirstSecondPairs; boolean areFirstThirdPairs; boolean areSecondThirdPairs;
Seras88 Frontend Developer
21 December 2022, 17:35
I didn't even realize the isHigh and isLow variables were already declared near the top of the code. 🙃
Valeria .Rifici
Level 21 , Italy
15 August 2022, 14:06
Pay attention to the first exercise. Remember when assigning the expression, don't use boolean isHigh or boolean isLow it won't clear the exercise.
Anonymous#195
Level 5 , Canada
21 November 2022, 19:03
thank you for the helpful advice i appreciate it
Shubham Patil
Level 5 , Kolhapur , India
23 November 2022, 11:32
then what we need to use
Anonymous#183
Level 3 , Canada
28 November 2022, 18:17
cap
Anonymous #11313983 Software Developer
22 March 2023, 13:53
Exactly! You just have to assign the isHigh and isLow variables. Then use them in the statements!