Operators and expressions

  • Operator = a symbol that represents an operations

  • The operators in Java:

      Assignment  Arithmetic   Relational   Logical  Special
     ==========================================================
         =                         ==          &&      ++
        +=            + (add)      !=	   ||      --
        -=		  -	       <	   !
        *=		  *	       <=    You should know the
        /=		  /	       >     operation representd
        %=		  %	       >=    by each operator    

  • Operand = the value used in an operation

      • An operator performs an operation on its operands and produce some result value

  • Expression = a combination of one or more operators and operands that performs a computation.

Expressions

  • An expression can be built up from other expressions

  • Example:

      score = score − 10*lateDays ; 

    consists of the arithmetic expression   10 * lateDays

Expressions

  • An expression can be built up from other expressions

  • Example:

      score = score − 10*lateDays ; 

    which itself is an operand of the arithmetic expression   score −  

Expressions

  • An expression can be built up from other expressions

  • Example:

      score = score − 10*lateDays ; 

    which itself is an operand of the assignment expression   score =  

Special attention in the pre/post increment/decrement operators

  • Pre-operators performs the operation first (= pre) and will return the new value of the variable

  • Post-operators performs the operation later (= postpone) and returns the old value of the variable

    Examples:

       x = 4;
       y = ++x;  <===>  x = x + 1; y = x;  (PRE increment)      
    
       Result:   x = 5    y = 5
    
    
    x = 4; y = x++; <===> y = x; x = x + 1; (POST increment) Result: x = 5 y = 4