Boolean type

Open

"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,  3 level,  10 lecture
Locked
Labels and numbers
Labels and numbers

"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,  3 level,  10 lecture
Locked
Describing numbers
Describing numbers
3
Task
New Java Syntax,  3 level,  10 lecture
Locked
Positive number
Positive number
3
Task
New Java Syntax,  3 level,  10 lecture
Locked
Positive and negative numbers
Positive and negative numbers
Комментарии (5)
  • популярные
  • новые
  • старые
Для того, чтобы оставить комментарий Вы должны sign in
vakhet
Уровень 26
15 January, 08:04
Nice
Vadym
Уровень 34
27 December 2022, 03:57
I solved this task same way but only with another variables names and I'v got error...strange a little bit
Anonymous #3179019
Уровень 30
6 October 2022, 12:38
Java code convention by Oracle explicitly states that using if statements without braces should be avoided:
if (m)
    return true;
else
    return false;
Avoid such form! Use instead:
if (m) {
    return true;
}
else {
    return false;
}
See Code convention
Andrei Karavai
Уровень 30
24 October 2023, 15:35
I saw tons of ifs without braces on Leetcode. They try to save every line with that. I'd prefer this way to keep track of code blocks and to be able to find opening and closing braces easily:
if (m)
{
    return true;
}
else
{
    return false;
}
Drigo
Уровень 9
4 August 2024, 23:41
There is a reason braces and other parts of code are not written from the same starting point on a line. To make them easier to read especially when they branch out more and more in a more complex code. People created the rules of code writing for a reason. It is the same as writing in any written language. There is an order to how you write code to make it more accessible. Programmers, to be successful, should learn to work as a part of a team as opposed to be a singular solo wiz programmer. Therefore instead of
if (m)
{
    return true;
}
programmers should try to write conventionally as a standard, it will be helpful when working in a team. Therefore, try this instead
if (m) {
    return true;
}
p.s. you probably already figured this out seeing as your level is 30, but I am just leaving this for others to read