OCA Java SE 8 Operators/Statements - Java Relational Logical Operators








Compound Assignment Operators

Besides the simple assignment operator, =, there are compound assignment operators.

+= and -= are two examples.

For example, the following two statements after the declaration of x and z are equivalent:

int x = 1, z = 2; 
x = x * z;  // Simple assignment operator 
x *= z;     // Compound assignment operator 

Compound operators can explicitly cast a value.

For example, consider the following example, in which the last line will not compile due to the result being promoted to a long and assigned to an int variable:

long x = 10; 
int y = 5; 
y = y * x;  // DOES NOT COMPILE 

This last line could be fixed with an explicit cast to (int), but using the compound assignment operator can make it compile:

long x = 10; 
int y = 5; 
y *= x; 

The compound operator will first cast x to a long, apply the multiplication of two long values, and then cast the result to an int.

The compiler will automatically cast the resulting value to the data type of the value on the left-hand side of the compound operator.

The result of the assignment is an expression in and of itself, equal to the value of the assignment.

For example, the following code is valid:

public class Main{
   public static void main(String[] argv){
        long x = 5; 
        long y = (x=3); 
        System.out.println(x); // Outputs 3 
        System.out.println(y); // Also, outputs 3 
   }
}

(x=3) does two things.

  • it sets the value of the variable x to be 3.
  • it returns a value of the assignment, which is 3.

The code above generates the following result.





Relational Operators

Relational operators compare two expressions and return a boolean value.

OperatorMeaning
<Strictly less than
<=Less than or equal to
>Strictly greater than
>=Greater than or equal to

Let's look at examples of these operators in action:

public class Main{
   public static void main(String[] argv){
//from  w ww .  j  a  va2s.com
        int x = 10, y = 20, z = 10; 
        System.out.println(x < y);  // Outputs true 
        System.out.println(x <= y);  // Outputs true 
        System.out.println(x >= z);  // Outputs true 
        System.out.println(x > z);  // Outputs false 
   }
}

instanceof operator is applied to object references and classes or interfaces.

OperatorMeaning
a instanceof bTrue if the reference that a points to is an instance of a class, subclass, or class that implements a particular interface, as named in b

The code above generates the following result.





Logical Operators

The logical operators, &, |, and ^, may be applied to both numeric and boolean data types.

When they're applied to boolean data types, they're referred to as logical operators.

When they're applied to numeric data types, they're referred to as bitwise operators.

You should familiarize with the truth tables in Figure 2.1, where x and y are assumed to be boolean data types.

The logical true tables for &, |, and ^

         x & y          x | y           x ^ y 
         (AND)          (INCLUSIVE OR)  (EXCLUSIVE OR) 
         y=true y=false y=true y=false  y=true  y=false 
x=true   true   false   true   true     false     true                              
x=false  false  false   true   false    true      false 

AND is only true if both operands are true.

Inclusive OR is only false if both operands are false.

Exclusive OR is only true if the operands are different.

The conditional operators, && and || are referred to as short-circuit operators.

The short-circuit operators are nearly identical to the logical operators, & and |, respectively, except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression.

For example, consider the following statement:

boolean x = true || (y < 4); 

The value x can only be false if both sides of the expression are false.

Since we know the left-hand side is true, there's no need to evaluate the right-hand side.

A more common example of where short-circuit operators are used is checking for null objects before performing an operation, such as this:

if(x != null && x.getValue() < 5) { 

} 

In this example, if x was null, then the short-circuit prevents a NullPointerException from ever being thrown, since the evaluation of x.getValue() < 5 is never reached.

However, if we used a logical &, then both sides would always be evaluated and when x was null this would throw an exception:

if(x != null & x.getValue() < 5) { // Throws an exception if x is null 

} 

What is the output of the following code?

int x = 6; 
boolean y = (x >= 6) || (++x <= 7); 
System.out.println(x); 

Because x >= 6 is true, the increment operator on the right-hand side of the expression is never evaluated, so the output is 6.

Equality Operators

The equals operator == and not equals operator != compare two operands and return a boolean value about whether the expressions or values are equal, or not equal, respectively.

The comparisons for equality are limited to the following three cases, so you cannot mix and match types:

  • When comparing two numeric primitive types. If the numeric values are of different data types, the values are automatically promoted.
  • They can compare two boolean values.
  • They can compare two objects, including null and String values.

For example, each of the following would result in a compiler error:

boolean x = true == 3;  // DOES NOT COMPILE 
boolean y = false != "Giraffe";  // DOES NOT COMPILE 
boolean z = 3 == "Kangaroo";  // DOES NOT COMPILE 

What is the output of the following code.

boolean y = false; 
boolean x = (y = true); 
System.out.println(x);  // Outputs true 

In the code above, though, the expression is assigning the value of true to y, the assignment itself has the value of the assignment. Therefore, the output would be true.

What is the output of the following code.

boolean y = false; 
boolean x = (y == true); 
System.out.println(x);  // Outputs false

For object comparison, the equality operator is applied to the references to the objects, not the objects they point to.

Two references are equal if and only if they point to the same object, or both point to null.

Let's take a look at some examples:

File x = new File("myFile.txt"); 
File y = new File("myFile.txt"); 
File z = x; 
System.out.println(x == y);  // Outputs false 
System.out.println(x == z);  // Outputs true 

Even though all of the variables point to the same file information, only two, x and z, are equal in terms of ==.