OCA Java SE 8 Mock Exam Review - Java Operators Review








Valid identifiers

A valid identifier starts with a letter (a-z, upper- or lowercase), a currency sign, or an underscore. There is no limit to its length.

A valid identifier can contain digits, but not in the starting place.

A valid identifier can use the underscore and currency sign at any position of the identifier.

A valid identifier can't have the same spelling as a Java keyword, such as switch.

A valid identifier can't use any special characters, including !, @, #, %, ^, &, *, (, ), ', :, ;, [, /, \, or }





Assignment operators

Assignment operators can be used to assign or reassign values to variables.

A variable can't be assigned to an incompatible value.

+= and -= are short forms of addition/subtraction and assignment.

+= can be read as "first add and then assign" and -= can be read as "first subtract and then assign."

Arithmetic operators

Arithmetic operators can't be used with the boolean data type. Attempting to do so will make the code fail to compile.

++ and -- are unary increment and decrement operators. These operators work with single operands.

Unary operators can be used in prefix or postfix notation.

When ++ and -- are used in prefix notation, the value of the variable increments/decrements just before the variable is used in an expression.

When ++ and -- are used in postfix notation, the value of the variable increments/decrements just after the variable is used in an expression.





Relational operators

Relational operators compare values for equality == and non-equality !=.

Relational operators determine whether two numeric values are greater than (>, >=) or less than (<, <=) each other.

You can't compare incomparable values.

The operators equal to == and not equal to != can be used to compare all types of primitives: char, byte, short, int, long, float, double, and boolean.

The operator == returns true if the primitive values being compared are equal.

The operator != returns true if the primitive values being compared are not equal.

The result of the relational operator is always a boolean value.

Logical operators

The logical operators determine whether a set of conditions is true or false.

Logical AND/&& evaluates to true if all operands are true, and false otherwise.

Logical OR/|| evaluates to true if any or all of the operands is true.

Logical negation/! negates the boolean value. It evaluates to true for false, and vice versa.

The result of a logical operation is always a boolean value.

The logical operators && and || are called short-circuit operators. If these operators can determine the output of the expression with the evaluation of the first operand, they don't evaluate the second operand.

The && operator returns true only if both of the operands are true. If the first operand to this operator evaluates to false, the result can never be true. Therefore, && does not evaluate the second operand.

The || operator returns true if any of the operands is true. If the first operand to this operator evaluates to true, the result can never be false. And || does not evaluate the second operator.