Monday 6 March 2017

Relational Operators, Boolean Logical Operators & The Assignment Operator - Java Tutorials

Relational Operators

The relational operators determine the relationship that one operand has to the other. Specifically, they determine equality and ordering. The relational operators are shown here:


Operator          Result

==                     Equal to

!=                      Not equal to


>                       Greater than

<                       Less than

>=                    Greater than or equal to

<=                    Less than or equal to

The outcome of these operations is a boolean value. The relational operators are most frequently used in the expressions that control the if statement and the various loop statements.

Any type in Java, including integers, floating-point numbers, characters, and Booleans can be compared using the equality test, ==, and the inequality test, !=. Notice that in Java (as in C/C++/C#) equality is denoted with two equal signs, not one. (Remember: a single equal sign is the assignment operator.) Only numeric types can be compared using the ordering operators. That is, only integer, floating-point, and character operands may be compared to see which is greater or less than the other.

As stated, the result produced by a relational operator is a boolean value. For example, the following code fragment is perfectly valid:

  int a = 4;
  int b = 1;
  boolean c = a < b;

In this case, the result of a<b (which is false) is stored in c.

If you are coming from a C/C++ background, please note the following. In C/C++, these types of statements are very common:

  int done;
  // ...
  if(!done) ... // Valid in C/C++
  if(done) ... // but not in Java.

In Java, these statements must be written like this:

  if(done == 0)) ... // This is Java-style.
  if(done != 0) ...

The reason is that Java does not define true and false in the same way as C/C++. In C/C++, true is any nonzero value and false is zero. In Java, true and false are nonnumeric values which do not relate to zero or nonzero. Therefore, to test for zero or nonzero, you must explicitly employ one or more of the relational operators.



Boolean Logical Operators

The Boolean logical operators shown here operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value.


Operator               Result

&                            Logical AND

|                              Logical OR

^                             Logical XOR (exclusive OR)

||                             Short-circuit OR

&&                        Short-circuit AND

!                             Logical unary NOT

&=                         AND assignment

|=                           OR assignment

^=                          XOR assignment

==                          Equal to

!=                           Not equal to

?:                           Ternary if-then-else


The logical Boolean operators, &, |, and ^, operate on boolean values in the same way that they operate on the bits of an integer. The logical ! operator inverts the Boolean state: !true == false and !false == true. The following table shows the effect of each logical operation:

A                 B                  A | B            A & B             A ^ B           !A

False           False            False            False               False             True
True            False            True             False               True              False
False           True             True             False               True              True
True            True             True             True                False             False

Here is a program that is almost the same as the BitLogic example shown earlier, but it operates on boolean logical values instead of binary bits:

  // Demonstrate the boolean logical operators.
  class BoolLogic {
    public static void main(String args[]) {
      boolean a = true;
      boolean b = false;
      boolean c = a | b;
      boolean d = a & b;
      boolean e = a ^ b;
      boolean f = (!a & b) | (a & !b);
      boolean g = !a;
      System.out.println(" a = " + a);
      System.out.println(" b = " + b);
      System.out.println(" a|b = " + c);
      System.out.println(" a&b = " + d);
      System.out.println(" a^b = " + e);
      System.out.println("!a&b|a&!b = " + f);
      System.out.println(" !a = " + g);
    }
  }

After running this program, you will see that the same logical rules apply to boolean values as they did to bits. As you can see from the following output, the string representation of a Java boolean value is one of the literal values true or false:

          a = true
          b = false
        a|b = true
        a&b = false
        a^b = true
   a&b|a&!b = true
         !a = false


Short-Circuit Logical Operators

Java provides two interesting Boolean operators not found in many other computer languages. These are secondary versions of the Boolean AND and OR operators, and are known as short-circuit logical operators. As you can see from the preceding table, the OR operator results in true when A is true, no matter what B is. Similarly, the AND operator results in false when A is false, no matter what B is. If you use the || and && forms, rather than the | and & forms of these operators, Java will not bother to evaluate the right-hand operand when the outcome of the expression can be determined by the left operand alone. This is very useful when the right-hand operand depends on the left one being true or false in order to function properly. For example, the following code fragment shows how you can take advantage of short-circuit logical evaluation to be sure that a division operation will be valid before evaluating it:

  if (denom != 0 && num / denom > 10)

Since the short-circuit form of AND (&&) is used, there is no risk of causing a run-time exception when denom is zero. If this line of code were written using the single & version of AND, both sides would have to be evaluated, causing a run-time exception when denom is zero.

It is standard practice to use the short-circuit forms of AND and OR in cases involving Boolean logic, leaving the single-character versions exclusively for bitwise operations. However, there are exceptions to this rule. For example, consider the following statement:

  if(c==1 & e++ < 100) d = 100;

Here, using a single & ensures that the increment operation will be applied to e whether c is equal to 1 or not.



The Assignment Operator

You have been using the assignment operator since Chapter 2. Now it is time to take a formal look at it. The assignment operator is the single equal sign, =. The assignment operator works in Java much as it does in any other computer language. It has this general form:

  var = expression;

Here, the type of var must be compatible with the type of expression.

The assignment operator does have one interesting attribute that you may not be familiar with: it allows you to create a chain of assignments. For example, consider this fragment:

  int x, y, z;

  x = y = z = 100; // set x, y, and z to 100

This fragment sets the variables x, y, and z to 100 using a single statement. This works because the = is an operator that yields the value of the right-hand expression. Thus, the value of z = 100 is 100, which is then assigned to y, which in turn is assigned to x. Using a “chain of assignment” is an easy way to set a group of variables to a common value.

No comments:

Post a Comment