Engineering Full Stack Apps with Java and JavaScript
An expression consists of operands and operators.
Example: Valid expressions
In the expression 3+5, '+' is the operator and 3 and 5 are the operands.
In the expression a + 5, '+' is the operator and a and 5 are the operands.
Example: Evaluating and assigning an expression to a variable
int sum = a + b;
The equal-to operator (=) evalates the right side and assign the result to left side variable (e.g. sum).
here sum is a variable of type int.
here, a+b should be an expression that evaluate to an int (e.g. a and b are int).
A logical expression (e.g. a>b) is an expression that evaluate to a true or a false value.
boolean isComplete = (a>b);
A Boolean variable can hold a true or false, or an expression that will evaluate to true or false.
The equality operator consists of two equals signs (e.g. a==b) and when evaluated will return either a true or a false value.
It should not be confused with assignment operator, '=' which is used for assigning the value in left to the variable in right.
The equality operator can be used in the logical expressions.
Similarly we can use other relational operators such as less than (<), greater than (>), greater than or equal to (>=) etc. in logical expressions
We can use logical operators such as AND (&&), OR (||) and NOT(!) in the logical expressions.
The AND operator returns true if both conditions are true while OR returns true if only one of the conditions is true.
The NOT (!) changes a true to a false and a false to a true.
Parentheses can be used to control the order of evaluation of complex logical operators.
Please also read about short cuircuit and bitwise AND and OR operators @ http://javajee.com/short-circuit-and-bitwise-and-and-or-operators.
The instanceof operator compares an object to a given type and checks if the type of object in left side is assignment-compatible to the type in the right side. i.e., if we can say right-side-type = left-side-type object without any casting.
Read more @ http://javajee.com/the-instanceof-operator-in-java.
The unary operator '++expr' is a prefix operator and 'expr++' is a postfix operator.
When used in a assignment or print context (like within a print statement), a prefix operator (e.g. ++a) first increments a and then return the value of a, whereas the postfix operator (e.g. a++) returns the value of a and then increments a.
Read more @ http://javajee.com/prefix-and-postfix-operators-in-java.
Operators with higher precedence are evaluated before operators with relatively lower precedence, when they come together.
Example: operator precedence
multiplication has more precedence over addition.
so '2*3+5*6' will be treated as (2*3) + (5*6).
The below Operator precedence table shows operators in the order if precedence from high to low. Operators in each section have same precedence.
- Postfix
- expr++ expr--
- unary
- ++expr --expr +expr -expr ~ !
- multiplicative
- * / %
- additive
- + -
- shift
- << >> >>>
- relational
- < > <= >= instanceof
- equality
- == !=
- bitwise AND
- &
- bitwise exclusive OR
- ^
- bitwise inclusive OR
- |
- logical AND
- &&
- logical OR
- ||
- ternary
- ? :
- assignment
- = += -= *= /= %= &= ^= |= <<= >>= >>>=
All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
'a+=b' expands to
'a=a+b'.
'+=' is not same as '=+'.
'a =+3' simply the assignment operator followed by the unary plus operator and is same as 'a = (+3)'.