Home Java Java-tips Summaries Summary - Expressions

Ask Questions?

View Latest Questions

Advertisement


 
 

Summary - Expressions
Posted on: July 27, 2006 at 12:00 AM
Know about the some Expressions

Java: Summary - Expressions

Parentheses () have three uses:
  1. Grouping to control order of evaluation, or for clarity.
    Eg, (a + b) * (c - d)
  2. After a method name to enclose parameters. Eg, x = sum(a, b);
  3. Around a type name to form a cast. Eg, i = (int)x;
Order of evaluation
  1. Higher precedence are done before lower precedence.
  2. Left to right among equal precedence except: unary, assignment, conditional operators.

Abbreviations
i, j - integer (int, long, short, byte, char) values.
m, n - numeric values (integers, double, or float).
b, c - boolean; x, y - any primitive or object type.
s, t - String; a - array; o - object; co - class or object
Operator Precedence
. [] (args) post ++ --
! ~ unary + - pre ++ --
(type) new
* / %
+ -
<< >> >>>
< <= > >= instanceof
== !=
&
^
|
&&
||
?:
= += -= etc
Remember only
  1. unary operators
  2. * / %
  3. + -
  4. comparisons
  5. && ||
  6. = assignments
Use () for all others

Operators are special characters within the Java language that perform operations on arguments to return a specific result. Some of the types of basic operators are given below :

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Boolean Logical Operators

Arithmetic Operators

In java, arithmetic operators have the same use as in algebra.  Given below table consist of list of arithmetic operators :

Operator  Result
  + Addition
  -  Subtraction (also unary minus)
 * Multiplication
  / Division
   % Modulus
  ++ Increment
  += Addition assignment
  -= Subtraction assignment
   *= Multiplication assignment
  /= Division assignment
   %= Modulus assignment
  -- Decrement

Bitwise Operators

Java defines several bitwise operators which can be applied to the integer types, long, int, short, char, and byte. These operators act upon the individual bits of their operands. They are summarized in the following  :

Operator Result
   ~ Bitwise unary NOT
  & Bitwise AND
   | Bitwise OR
  ^ Bitwise exclusive OR
   >> Shift Right
  >>> Shift Right  zero fill
  << Shift left
  &= Bitwise AND assignment
  \=  Bitwise OR assignment
  ^=  Bitwise exclusive OR assignment
   >>=  Shift Right assignment
   >>>=  Shift Right  zero fill assignment
  <<= Shift left assignment

Relational operator

The relational operator determine the relationship that one operand has to other. Specifically, they determine equality and ordering . The relational operators are shown here :

Operator Result
   = = Equal to
   != Not equal to
  > greater than 
  < less than
   >= greater than or equal to
   <= less than or equal to

Boolean Logical Operators

The Boolean logical operators shown here operates only on Boolean operands. All of the binary logical operators combine  two Boolean values to form a resultant Boolean value.

Operator Results
  & Logical AND
  \ Logical OR
  ^ Logical XOR(exclusive OR)
   || Short-circuit OR
  && Short-circuit  AND
   ! Logical Unary not
  &= AND assignment
  \= OR assignment
  ^= XOR assignment
  = = Equal to
  != Not equal to
  ? : Ternary if-then-else

Precedence of Java Operators :

Given below table shows the order of precedence for java operators, from highest to lowest :

Highest      
( ) [] .  
++    --   ~     !
*   /    %    
+   -     
>> >>>    <<  
>   >=   <  <= 
= =  !=     
&      
^      
|      
&&      
||      
? :      
=      
Lowest      

Note :The elements in the same row have the same precedence.


Copyright 2005 Fred Swartz
Advertisement


DMCA.com