Expressions, Operands and Operators

Expression

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.

 

Operators

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 @ http://javajee.com/short-circuit-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 @ http://javajee.com/the-instanceof-operator-in-java.

 

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 @ http://javajee.com/prefix-and-postfix-operators-in-java.

 

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

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)