Boolean type

New Java Syntax
Level 3 , Lesson 10
Available

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"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 isALessThanB (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 isALessThanB (int a, int b)
{
   boolean m = (a < b);
    if (m)
        return true;
    else
        return false;
}
3
public boolean isALessThanB (int a, int b)
{
    boolean m = (a < b);
    return m;
}
4
public boolean isALessThanB (int a, int b)
{
    return a < b;
}
3
Task
New Java Syntax, level 3, lesson 10
Locked
Labels and numbers
Use the keyboard to enter an integer. Display a string description as follows: "Negative even number" - if the number is negative and even, "Negative odd number" - if the number is negative and odd, "Zero" - if the number is 0, "Positive even number" - if the number is positive and even, "Positive o

"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
New Java Syntax, level 3, lesson 10
Locked
Describing numbers
Enter an integer from the keyboard in the range 1 - 999. Display a string description as follows: "even single-digit number" - if the number is even and has one digit, "odd single-digit number" - if the number is odd and has one digit, "even two-digit number" - if the number is even and has two digi
3
Task
New Java Syntax, level 3, lesson 10
Locked
Positive number
Use the keyboard to enter three integers. Display the number of positive numbers in the original set. Here are some examples: a) if you enter the numbers -4 6 6 then we display 2 b) if you enter the numbers -6 -6 -3 then we display 0 c) if you enter the numbers 0 1 2 then we display 2
3
Task
New Java Syntax, level 3, lesson 10
Locked
Positive and negative numbers
Use the keyboard to enter three integers. Display the number of positive and negative numbers in the original set in the following form: "Number of negative numbers: a", "Number of positive numbers: b", where a and b are the relevant values. Examples: a) if you enter the numbers: 2 5 6 then we displ
Comments (94)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
BaDZeR Level 6, Doha, Qatar
27 January 2025
Is it just me or are the problems labeled as medium difficulty are easier than the ones labeled as easy?
rags 3 Level 5, Wichita, United States Expert
28 April 2024
first one got me tried the %2== but positive and negative numbers return positive and negative numbers keep in mind
Justin Level 5, Taiwan, Province of China
21 March 2024
Using boolean makes the code look cleaner I think.
SvenskKung Level 10, United States of America, United States
4 October 2023
I was stuck on the first medium problem for a long time, utilizing an entirely incorrect approach. I believe my approach got the right answer but it wasn't accepted. Consider the various approaches you have available to you. You don't need to utilize anything that hasn't been presented yet in a lesson or video.
L Level 6, Dearborn, United States
18 August 2023
It says unknown error but I tested my code in IntelliJ and all my test cases passed. So I'm not sure why it's failing here. Case 1: all pos nums Case 2: all neg nums Case 3: all 0's Case 4: mix pos and neg (no 0's) Case 5: mix of pos and 0's Case 6: mix of pos, neg, 0's Case 7: mix of neg and 0's
Laura Veerkamp Level 6, Netherlands
7 September 2023
You shouldn't define all possibilities but rather add 1 or not when considering values. Look at the solution.
rags 3 Level 5, Wichita, United States Expert
2 July 2023
I love this sometimes it will give you code thats incredibly long but you can figure out ways to shorten and its challenging im usually wrong with right concept that i have to figure out how to implement right I love the fact that there is not just one way to solve the problems.
rags 3 Level 5, Wichita, United States Expert
1 July 2023
i think some times its buggy lol cause my code is working fine why why why
abhishe_kira Level 18, India Expert
6 June 2023
to solve MEADIUM level problem (problem 2) you need to 1. define a variable :-"count" which will start from zero , 2.study arrays in java first and then make a array of inputs numbers . Array-: int array = {number1, number2, number3}; 3. then make a loop which will increase the count to count ++ , if the value stored in array is positive else it will not change the value of count(variable). 4. print count variable. I HOPE IT WILL DEFINITELY HELP YOU. Please message me if i'm able to help you : )
Anonymous #10782038 Level 24, United States of America
14 December 2023
You do not need an array for that problem. In the forloop you can have the input reader and just loop over that 3 times.
lonk Level 7, Atlanta, United States
15 May 2024
ngl, I did use an array as I was thinking about solving the problem if there were more than just 3 inputs to consider. i thought it might be the smartest approach given that you could have an arbitrary number of integers that you want to take from a user and count the positive ones
lonk Level 7, Atlanta, United States
15 May 2024
Also, I tried this approach for the last problem and it worked well. Thank you for your suggestion.
David Level 6, Spain
3 May 2023
The perfect trainer!!!
Robbie_Robot Level 4, California, United States
10 October 2022
For the last 2 assignments here, think in terms of incrementing counters myCounter = 0, myCounter++; and displaying that as output. I struggled on one assignment for a couple hours and had to download the solution. Once I had that "Doh" moment in hand I was able to quickly complete the second assignment. Probably best to understand the printf statement as well. Luckily I was already familiar with it. "printf("Number of numbers: %d%n", myCounter);" So for those that don't know '%' is a place holder, 'd' digit (integer in this case, you can use 'f' for float, 's' for string, etc...) then another '%' and 'n' for newline. Hope that helps someone else, So long level 3, I am outta here! C/Ya 😜