|
This chapter covers a lot of ground about Java operators, expressions and evaluation of expressions. We learned how different Java operators can be used in a Java expression. We also reviewed the datatypes on which they can be applied and how they work. We learned the simplest operators doing basic arithmetic as well as the shift operators which operate on underlying bit representations. Following is a quick summary of important details of Java operators and their use in Java expressions.
q Operators
Operators are symbols used to indicate some operation on its operands. Depending on the number of operands, operators can be classified as the unary operators, binary operators and ternary operator. Java also has assignment operators that help in storing the value of variable or expression into a variable.
q Unary Operators
Unary operators operate on only one operand. Java unary operators are has + - ++ -- ! ~ and the cast operator ().
q Increment ++ and decrement -- operators
§ There are two types of increment and decrement
o Prefix increment and decrement operators
o Postfix increment and decrement operators.
§ In case of increment, both prefix (++n) and postfix (n++) expressions increment the value of n by 1. The prefix expression (++n) will return incremented value whereas the expression n++ will return original value of n.
q Unary + operator has no effect on value of expression, it only affirms that the expression is positive. On the other hand, the unary – operators is used to negate the expression.
q The ! operator operates only on boolean expression. It inverts the value of boolean expression true to false and false to true.
q The ~ operator operate on only the integral data types. It inverts each and every bit of the bit representation of its operand.
q The cast operator is used to forcibly change the type of expression. Java has extensive compile –time and runtime checks to ensure the proper use of this operator.
q The binary operators operate on two operands. Java binary operators are further classified as...
q Arithmetic operators
These operators are related to arithmetic operations. Java has five arithmetic operators * / % + and - . They are applied to numeric data types.
§ The + operator works on string objects as well. If one of its operands is an expression of type java.lang.String, then the second operand is forced to be a string. The string conversion is performed on this operand to produce a string at run time. The result is a reference to a newly created String object that is the concatenation of the two string operands.
§ The arithmetic operators can cause in inaccurate result due to overflow during evaluation. But this loss of precision does always cause an exception.
§ Only the integral division by 0 and % operation with 0 causes the ArithmeticException at runtime.
§ The floating-point arithmetic does not cause ArithmeticException. It uses naming constants such as Float.NaN to denote the erroneous results.
q Shift Operators
The shift operators include left shift <<, signed right shift >>, and unsigned right shift >>> . Note that there is no such thing as unsigned left shift.
§ They perform bit-by-bit shifting on bit representation of their left operand.
§ They can be applied only to the integral data types. Due to arithmetic promotions, they are really applied to only int and long type of operands.
§ The right hand operand is wrapped with modulo operation. If the left operand is int, it is wrapped to modulo 32 and if it is long, it is wrapped to modulo 64.
§ Shifting creates empty spaces.
o The << operators shift bits to the left. Therefore creates empty spaces on the right. It fills them with 0s.
o The >> operators shift bits to the right. Therefore creates empty spaces on the left. It fills them with the sign bit of original operand.
o The >>> operators shift bits to the right. Therefore creates empty spaces on the left. It fills them with 0s.
q Comparison Operators
Java has three types of comparisons, the relational comparisons (operators are < > <= >= ), equality comparisons (operators (== !=) and type comparisons (The instanceof operator). The comparison operators always result in boolean value.
q The relational comparison operators perform the numeric comparison. You can compare any numeric operand with any other numeric operand.
q The instanceof operator performs the object's type comparison. It operates on object references only (including null) and checks whether this object if of a type ( specified in second operand).
q The Equality Operators are used to compare numeric values, boolean values and also the object references for equality.
§ In case of object references the == operator checks whether the object references are same (identity test) whereas the equals() method checks whether the objects are equal usually by comparing their content.
§ The equals() method is defined in java.lang.Object class. Its signature is public boolean equals(Object anObject).The default behavior of equals() is to do an identity test.
§ In case of numeric operands, the identity test == returns true when both operands represent the same numeric value. In case of object references it return true only when both the objects references are pointing to the same object.
q The bitwise Operators & | and ^
There are three bit wise binary operators & (AND), | (OR) and ^ (XOR). They can be applied to the integral data types. They work on the bit representation of the operands. The operator is applied on bit-by-bit basis to produce the bit representation of the result.
q The logical operators
Java has four logical operators & | &&and ||. Logical operators operate on boolean data types.
§ The operators & | and ^ work on boolean data type in the similar fashion as they work on bit representations.
§ The &&and & operators return true if both the operands are evaluated as true.
§ The || and | operators return true if either of the operands or both are evaluated as true.
§ The && (Short circuit AND) and || (Short circuit OR) operators are introduced for optimization.
§ In case of &&,
(expr1) && (expr2); //expr2 is never evaluated if expr1 is false.
§ In case of ||,
(expr1) || (expr2); //expr2 is never evaluated if expr1 is true.
The & and | always evaluates both the operands to find out
the result.
q Ternary operator
§ Java has one ternary operator called the conditional operator ?: . This operator has three-operand expressions. The ? appears between the first and second expressions, and : appears between the second and third expressions. The first expression is a condition and it must be of type boolean.
§ The condition decides which of two other expressions should be evaluated. If the Conditional Operator ? : is used in expression as:
result = condition ? expression1: expression2;
The above expression is equivalent to :
if(condition)
result = expression1;
else
result = expression2;
Please note that expression1 and expression1 must have the compatible data types to that of result. The condition must be of boolean type.
q Assignment Operators
§ The operator = is simple assignment operator. Operators *= /= %= += -= <<= >>= >>>= &= ^= |= are compound assignment operators.
§ The simple assignment assigns the value of right operand to the variable in the left operand.
§ The compound assignment expression is of the form operand1 op= operand2. It is equivalent to operand1 = (Type) (operand1) op (operand2);
§ The right-hand value is implicitly cast before it is assigned to the left-hand variable. The Type to which it is cast is the type of the left hand variable operand1.
§ An assignment expression returns value. Therefore, it can be used as an operand in larger expressions.
q Order of Evaluation
§ When any Java expression is executed, the operands are evaluated first before applying any operator. They are resolved from left to right. Then the expressions in parentheses (if any) are evaluated. After that, the operators are applied as per their precedence.