Skip to main content

Section 2.23 Worked Example: Assigning Booleans

Subgoals for evaluating an assignment statement.

  1. Determine resultant data type of expression
  2. Update variable for pre operators based on side effect
  3. Solve arithmetic equation, operator precedence
  4. Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
  5. Update variable for post operators based on side effect

Subsection 2.23.1

You can watch this video or read through the content below it.
Given the following code snippet, evaluate the final statement (the last line). If invalid, give the reason. If valid, what value is assigned to the variable? Note any possible side effects.
int alpha = 42;
int beta = 1;
int gamma = 5;
boolean result;
            
result = beta <= alpha;

Subsection 2.23.2 SG1 : Determine resultant data type of expression

The expression is the right-hand-side (RHS) of the statement: beta <= alpha. The operator is one of the boolean relational operators, so the result will be boolean.
The statement above would be an acceptable, complete response for this subgoal. However, for the sake of thoroughness, here is some more information about working with different data types and boolean operators.
The relational operators are:
  • <= Less than or equal (i.e. “at most”)
  • >= Greater than or equal (i.e. “at least”)
  • < Strictly less than
  • > Strictly greater than
  • == Equal
  • != Not equal
There are a few important details to note about using these operators.
  • They all work as you might expect for int values. The bulk of your first lessons in boolean operators will involve only integer values.
  • Be careful with equality for float or double values. Because of how these values are stored in memory, computed values may be slighty different than expected, and it is a good practice to use a range of acceptable values for comparing equality. This behavior of floating-point values, as well as the methods to program against it, is a bit beyond the scope of this lesson. If you run into this problem try searching for “comparing doubles in java” on the web.
  • Using equality operators == or != on objects (such as Strings) only compares the memory reference; that is, two objects are only “equal” according to this operator if they are in fact the same object. If you wish to compare the values of object properties, the class definition must implement the .equals() method.
  • Often, the notion of an object being “less than” or “greater than” another is nonsensical. However, sometimes we do wish to sort or rank objects by one of their properties (e.g. Car objects by their price property). In such cases, Java provides the Comparable and Comparator interfaces, but those are quite beyond the scope of this lesson.

Subsection 2.23.3 SG2: Update variables for any pre-increment or pre-decrement operators (side effects)

NOT USED IN THIS EXAMPLE

Subsection 2.23.4 SG3: Evaluate arithmetic expression according to operator precedence

The RHS may be easiest to conceptualize as an algebraic inequality, by replacing the variables right away with their current values : 1 <= 42. Logically, this is a true statement, so the returned value is boolean true.

Subsection 2.23.5 SG4: Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.

The LHS is a variable of type boolean, and the RHS is type boolean. This is valid.
If you have experience in other programming languages, you may have seen boolean true and false values interchanged with 1 and 0 integer values, or even compared with non-numeric values in a logical expression. This is NOT the case in Java. In Java, true and false are the only values that may be stored in a variable declared as a boolean.
Be careful not to confuse the meanings of false, null, 0, empty, and undefined. All these have specific meanings in programming and in Java.

Subsection 2.23.6 SG5: Update variable for post-increment or post-decrement operators (side effect)

NOT USED IN THIS EXAMPLE
Answer.
result is true.

Practice Pages.

You have attempted of activities on this page.