Home > Software design >  Comparing an int initialization with a boolean operator for an if statement parameter
Comparing an int initialization with a boolean operator for an if statement parameter

Time:01-20

I've declared ints skyB and day, and written this line:

if (skyB == true || (day=1) != true) {
System.out.print("example text");  
}

Am I correct in assuming that the code will properly execute the argument given these parameters? I'm specifically unsure about whether or not the (day="1") would be properly detected as false, given the instance that the objects input was indeed a false.

minimal reproducable example(not including main method or packages):

 int skyB = false;

 int day =2;

 if (skyB == true || (day =1) != true) {
System.out.print("example text");  }

CodePudding user response:

The simple answer: this isn't C

You cannot assign numeric value to boolean fields. In Java, the only two valid values are true and false. Therefore, you cannot assign integer values to a boolean variable.

For your boolean expressions to be valid, your variables skyB and day must be boolean data types

boolean skyB = false
boolean day = true

if (!skyB || day) {
  System.out.print("example text");
}

The boolean fields are evaluated without the need to compare them to true or false. The expression !skyB (read "not skyB") evaluates to false. So this is the equivalent to skyB == false. Since that portion evaluates to true (skyB is equal to false), the second part of the expression is skipped and the System.out.print() is called. If the value of skyB was changed to true, the variable day would then be evaluated and the if-statement would be executed if the value of that variable equals true (which it is).

Alternatively, you could build boolean expressions using numeric values. How? Simple. Any mathematical expression is evaluated the same. For example:

int x = 5;
int y = 3;

if (x > y) {
  System.out.println("x is greater than y");
}

Basically... The logic of what you are trying to do (the way I understood it) is correct, but your implementation was not. You cannnot mix boolean fields and numeric values. It is simply not allowed in Java.

Now, to your question...

boolean skyB = false; // this must be boolean, not int
int day =2;       // this is fine

if (skyB || day == 1) {
  System.out.print("example text");
}

Should produce the desired outcome. In plain English, the above is read:

If skyB is false OR is day one, print "example test"

If either part of the expression fails to evaluate to true (i.e. if skyB is true and day variable is any value other than one), it will not print anything.

UPDATE:

Even if this answer has been accepted and upvoted, I would like to include some information about boolean expressions for the benefit of new developers that might be struggling with this concept.

What is a Boolean Expression?

According to Oracle documentation, "A Boolean expression is a logical statement that is either TRUE or FALSE." These expressions take a few different forms. For example,

  • Boolean literals
if (true) {...}

Although there is no real reason for such if-statement, the above is a valid expression. "If true, do the stuff inside the curly braces." This means that the stuff inside will always execute (making it the same as not having the "if". However, this is the basis for understanding the rest.

  • Boolean variables
boolean isBlue = false;
if (isBlue) {...}

In the above case, the Boolean expression is the variable itself. The expectation is that, if the variable value evaluates to true, the stuff inside the curly braces will be executed. Since the value is false, it won't be executed. The above "if" is the more accepted short form of

boolean isBlue = false;
if (isBlue == true) {...}

So, since the left hand (variable) value of this evaluation is false and the right side is true, the complete expression evaluates to false. Therefore, the stuff inside curly braces won't be executed (false does not equals true). However

boolean isBlue = false;
if(!isBlue == true){...}

Negating a boolean variable toggles it's value. In this case, the isBlue variable holds the value of false. The "not" operator toggles its value to true. Since true == true evaluates to true (true equals true is a true expression), the stuff inside curly braces is executed.

  • Functions that yield a boolean value
public boolean isSkyBlue() {
  return true; // sorry for the over simplification
}
...
if (isSkyBlue()) {...}

This is nothing more than an advance form of what I have shown already. In this case, the method "computes" the boolean value to be returned. Since boolean values are expressions onto themselves, it is valid to insert a method call such as the one illustrated above inside the parentheses. The stuff inside curly braces will execute so long the function returns true.

  • Comparison operations

I included the following example already:

int x = 5;
int y = 3;

if (x > y) {
  System.out.println("x is greater than y");
}

Any type of comparison operation will yield true or false. Therefore, these comparison operations are boolean expressions. This also includes the result of overridden Object#equals(Object) method. For example:

String s = "Hello";
if (s.equals("Hello")) {...}

As a side note, objects in Java should be compared using an overridden equals() method, and not using the double-equals operator (unless you are only interested in comparing the object references (address) to be equal.

  • Related