Aug 03

The instanceof operator compares an object to a specified type.

You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

Example program demonstrating the use of instanceof operator :

public class Employee { }

public class Manager extends Employee { }

public class Engineer extends Employee { }

public class  xyz

{

public static void main(String args[ ] )

{

Employee  em =new Manager();

doSomething(em);

}

public static void doSomething(Employee  obj)

{

if (obj  instanceof  Manager)

{

// process a Manager

}

else  if (obj  instanceof  Engineer)

{

// process a Engineer

}

else

{

// process any other type of Employee

}

}

}

  • Here ,Manager and Engineer are subclasses of the Employee class.
  • doSomething(Employee obj) method receives object using a reference of type Employee , and perform operation depending upon whether it is an instance of Manager or Engineer class.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Anup

Jul 10

The following table shows the shift oprators present in Java Language.

Operator Use Description
>> A>>B It is Right shift operator.

It is arithmetic or signed operator.

Shifts the binary number A towards right by B times and fills the vacated bit positions by left-most bit present in original binary value of A.

<< A<<B It is Left shift operator.

It is arithmetic or signed operator.

Shifts the binary number A towards left by B times and fills the vacated bit positions by left-most bit present in original binary value of A.

>>> A>>>B It is logical or unsigned right operator.

Shifts the binary number A towards right by B times and fills the vacated bit positions by zeros.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Anup

Jul 10

Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators.

Precedence Order

When two operators share an operand the operator with the higher precedence goes first.

Associativity

When two operators with the same precendence the expression is evaluated according to its associativity.

for example :

consider   :    2 + 6 * 7 /4

since  * and / have same precedence  and have L to R associativity, * is evaluated first then /.

so it is evaluated as :

2 + ((6 *  7) / 4)

Operator Name

Operators

Associative

access array element,
access object member,
invoke a method
[]  .  () L to R
unary ++  – -  +  -   ~  !  (<data_type) R to L
creation or cast new   (type)expression R to L
multiplicative *   /   % L to R
additive +   - L to R
shift << , >> , >>> L to R
relational <  , > , <= , >= , instance of L to R
equality == , != L to R
bitwise AND & L to R
bitwise XOR ^ L to R
bitwise OR | L to R
logical AND && L to R
logical OR || L to R
conditional <boolean_expr> ? <expr1> : <expr2> R to L
assignment =    *=   /=   %=  +=  -=   <<=>>=    >>>=   &=    ^=     | = R to L

Important :

post-increment or post-decrement operators have higher precedence than pre-increment operator or pre-decrement operator.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Anup