Oracle Certified Professional (OCP) Java Programmer, 1.4 - Operators ☕

|

Relational Operators (Objective 7.6)

  • Relational operators always result in a boolean value
  • There are six relational operators: >, >=, <, <=, ==, and !=. The last two (== and !=) are sometimes referred to as equality operators.

  • There are actually 11 or so compound assignment operators, but only the four most commonly used are: +=, -=, *=, and /= e.g.
x -= y; //same as x = x – y;
  • The operator precedence for compound operators is slightly different than you might expect, the right side is always evaluated first.
x *= 2 + 5; //evaluated as x = x * (2 + 5)
  • Think of it as putting the right-side in parenthesis, then remove the equals sign to get the equation!

  • When comparing characters, Java compares the numerical Unicode values.

  • Four types of things can be tested using the equality operators: numbers, characters, booleans, and reference variables.

  • When comparing reference variables, == returns true only if both reference variables refer to the same object.
  • == gives a compiler error if the operands are cast-incompatible (BUT all numeric primitives including char can be compared with ==).

instanceof Operator (Objective 7.6)

  • instanceof can be used on reference variables only. It checks whether the object is of a particular type.
  • The instanceof operator can only be used only to test objects (or null) against class types of which they are in the same class hierarchy.
interface Vessel{}
interface Toy{} //not implemented
class Boat implements Vessel { }
class Speedboat extends Boat{ }
class Dingy extends Speedboat{}

public class Test{
	  public static void main(String[] args) {
			Boat b = new Boat();
			if(b instanceof Boat)
				System.out.println("b is a Boat"); //true
			if(b instanceof Vessel)
				System.out.println("b is a Vessel"); //true
			if(b instanceof Toy)
				System.out.println("b is a Toy"); //false
			if(b instanceof Dingy)
				System.out.println("b is a Dingy"); //false

			Dingy d = new Dingy();
			if( d instanceof Boat)
				System.out.println("d is a Boat");//false
			if( null instanceof Boat)
				System.out.println("null is a Boat"); //false
			if(d instanceof MyClass) //compiler error
	   }	   
}
  • If you test an object reference against a class using instanceof that is NOT in it’s class hierarchy, it will result in a compiler error.
  • If you compare an object reference to an unrelated interface, or an interface reference to a class, there will be NO compiler error.

  • For interfaces, an object passes the instanceof test if any of its superclasses implement the interface on the right side of the instanceof operator.

Arithmetic Operators (Objective 7.6)

  • There are four primary math operators: add +, subtract -, multiply *, and divide /.
  • The modulus operator(%) returns the remainder from a division operation.

  • The *, /, and % operators have higher precedence than + and -.

  • Expressions are evaluated from left to right
  • Evaluation of an expression is changed if you add parentheses, or use operators in the expression that have higher precedence than others. e.g.
int a = 4 + 5 * 6; //a = 34
int a = (4 + 5) * 6 ; //a = 54

String Concatenation Operator (Objective 7.6)

  • If either operand is a String, + concatenates the operands.
  • If both operands are numeric, + adds the operands.
int x = 42;
int y = 44;
System.out.println(x + y + text); //prints: 86text

Increment/Decrement Operators (Objective 7.6)

  • Prefix operators (++ and --) run before the value is used in the expression.
static int players = 0;
System.out.println("value of players: " + ++players);

//Prints: value of players: 1
  • Postfix operators (++ and --) run after the value is used in the expression.
static int players = 0;
System.out.println("value of players: " + players++);

//Prints: value of players: 0

Ternary (Conditional Operator) (Objective 7.6)

  • Condition ? Expression if true: Expression if false;
int numOfPets = 3;
String status = (numOfPets < 4) ? "Pet limit not exceeded": "too many pets";
System.out.println(status); //Prints: Pet limit not exceeded

Bitwise Operators

You should merely be aware that the following are bitwise operators: &, |, ^,~, <<, >>, >>> .

Logical Operators (Objective 7.6)

  • The exam covers six “logical” operators: &, |, ^, !, &&, and ||.

  • Logical operators work with two expressions (except for !) that must resolve to boolean values.

  • The && and & operators return true only if both operands are true.
  • The || and | operators return true if either or both operands are true.

  • The && and || operators are known as short-circuit operators, they do not waste their time evaluating both expressions if it knows the result of the complete expression from the evaluation of the first expression:
    • If the left operand is false, the && operator does NOT evaluate the right operand.
    • If the left operand is true, the || does NOT evaluate the right operand.
int z = 5;
if(++z > 5 || ++z > 6){
	z++;
}  
// z = 7 now
  • ^ (called the “logical XOR” operator) returns true if exactly one operand is true.

  • ! (called the “inversion” operator) returns the opposite value of the boolean operand it precedes.

Related Posts