Operators in Java are special symbols that executes specific operations on one, two, or three operand/operands, and then return a result.They moves to left to right or right to left.In the table bellow all the Java operators has been described with examples
Operators has same precedence |
moves to | explanation |
---|---|---|
[ ] . ( ) (method call) | Left to right | |
! ~ ++ -- + (unary) – (unary) ( ) (cast) new |
Right to left | ~ flips each bit of a particular number, expr++ and expr-- are postfix. ++expr, --expr, +expr, -expr, ~, ! are unary |
+ - | Left to right | additive |
* / % | Left to right | Multiplicative, be alert when using % with negative numbers. As for example -p % q == -(p % q), but p % -q == p % q. For example, -6 % 4 == -2, 6 % -4 == 2. |
< <= > >= instanceof | Left to right | relational, null instanceof T is always false |
<< >> >>> | Left to right | >> is an arithmetic shift (p >> 1 == p / 2 for positive and negative numbers), >>> is logical shift (adding 0 to the highest bits). The right hand side is reduced modulo 32 if the left hand side is an int or modulo 64 if the left hand side is a long. For example, 1 << 35 == 1 << 3. |
== != | Left to right | Looks for identity. Always use equals to check for structural equality. |
| | Left to right | bitwise inclusive OR / bitwise OR; no lazy evaluation with boolean arguments |
^ | Left to right | Bitwise XOR or bitwise exclusive OR |
& | Left to right | Bitwise AND; no lazy evaluation with boolean arguments |
|| | Left to right | logical OR |
?: | Right to left | ternary |
= += -= *= /= %= &= |= ^= <<= >>= >>>= |
Right to left | assignment |
&& | Left to right | logical AND |
PROPOST EXAMPLE:-
int p=5; p++; // System.out.println(p++);//6 System.out.println(p);//7 System.out.println(p++); //7 int u=9; ++u;//10 System.out.println(++u);//11
int Mybitmask = 0x000F; int Myval = 0x6666; prints "6" System.out.println(Myval & Mybitmask);
INSTANCEOF EXAMPLE
package Object; public class instanceOfDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Parent P1 = new Parent(); Parent P2 = new Child(); System.out.println(P1 instanceof Parent); System.out.println(P1 instanceof Child); System.out.println(P1 instanceof MyInterface); System.out.println(P2 instanceof Parent); System.out.println(P2 instanceof Child); System.out.println(P2 instanceof MyInterface); } } class Parent { // methods here } class Child extends Parent implements MyInterface { } interface MyInterface { //abstract methods } //OUTPUT WILL BE true false false true true true% || == = EXAMPLE IN SWITCH CASE
import java.io.BufferedReader; import java.io.InputStreamReader; public class switch1 { public static void main(String[] args) { try { System.out.println("please entert a months's number "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String data = br.readLine(); int choice =Integer.parseInt(data); System.out.println("please enter a year "); String data1 = br.readLine(); int year =Integer.parseInt(data1); int numDays = 0; switch(choice) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println(choice +" month "+" of year "+year +"has "+numDays +"days"); } catch (Exception e) { // TODO: handle exception } } } //OUTPUT WILL BE please entert a months's number 2 please enter a year 2000 2 month of year 2000 has 29 days
No comments:
Post a Comment