Expressions, Operands and Operators


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.

  • Operands are normally variable names (e.g. a) or literals (e.g. 3).


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). 


Logical expression

logical expression (e.g. a>b) is an expression that evaluate to a true or a false value.

boolean isComplete = (a>b);

  • Boolean variable can hold a true or false, or an expression that will evaluate to true or false.



Equality Operator

  1. The equality operator consists of two equals signs (e.g. a==b) and when evaluated will return either a true or a false value. 

  2. It should not be confused with assignment operator, '=' which is used for assigning the value in left to the variable in right.

  3. The equality operator can be used in the logical expressions. 

  4. Similarly we can use other relational operators such as less than (<), greater than (>), greater than or equal to (>=) etc. in logical expressions


Logical operators

  1. We can use logical operators such as AND (&&), OR (||) and NOT(!) in the logical expressions.

  2. The AND operator returns true if both conditions are true while OR returns true if only one of the conditions is true.

  3. The NOT (!) changes a true to a false and a false to a true. 

  4. 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 @


Instanceof operator

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 @


Prefix and postfix operators

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 @


Operator precedence 

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
    • = += -= *= /= %= &= ^= |= <<= >>= >>>=


Evaluation order

All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.


Shorthand forms

  1. 'a+=b' expands to 

    1. 'a=a+b'. 

  2. '+=' is not same as '=+'.  

    1. 'a =+3' simply the assignment operator followed by the unary plus operator and is same as 'a = (+3)'.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!