Saturday, August 16, 2014

Java Operators

Operators
  • Assignment Operators
  • Increment Decrement Operators
  • Arithmetic Operators
  • Bitwise Operators
  • Relational Operators
  • Logical Operators
  • Ternary Operator
  • Comma Operator
  • instance of
In Java, there are six categories of operators.
  • Unary operators
  • Arithmetic operators
  • Relational and conditional operators
  • Shift and logical operators
  • Assignment operators
  • Other operators
Precedence Operator  DescriptionAssociation
1   ++,--Postincrement, PostdecrementR -> L
2  ++,--Preincrement, PredecrementR -> L
3  +,-Unary plus, unary minusR -> L
4~Bitwise complimentR -> L
5!Boolean NOTR -> L
6new Create objectR -> L
7(type)Type castR -> L
8*,/,%Multiplication, division,remainderL -> R
9+,-Addition, subtractionL -> R
10+String concatenationL -> R
11<<, >>, >>>Left shift, right shift, unsigned right
12<, <=, >, >=
13instance ofType comparisonL -> R
14==, !=Value equality and inequalityL -> R
15==, !=Reference equality and inequalityL -> R
16&Boolean ANDL -> R
17&Bitwise ANDL -> R
18!BooleanL -> R
19|Bitwise ORL -> R
20&&Conditional ANDL -> R
21||Conditional ORL -> R
22?  :Conditional Ternary OperatorL -> R
23=,+=,-=,Assignment Operators
24*=,/ =,%=,  &=,^=, |=,  <<=, >> =, >>=R-> L
Operators with a higher precedence are executed before those of a lower precedence. operators on the same line have the same precedence.
 
Operator Precedence GroupAssociativityOperator Precedence
(), [], postfix ++, postfix --leftHighest
unary +, unary -, prefix ++, prefix --, ~, !right
(type), newleft
* , /, % , +, -  <<, >>, >>>,  < ,<= , >, >=, instanceof ==, != &left
&& ||left
?:left
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=rightLowest
Assignment Operators
Several assignment operators
public class MainClass {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;
 
    a += 5;
    b *= 4;
    c += a * b;
    c %= 6;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
  }
}
//output
a = 6
b = 8
c = 3
Increment Decrement Operators
The increment and decrement operators  
  • add an integer variable by one.
  • increment operator: two successive plus signs, ++.
  • decrement operator: --.
 
public class MainClass {
  public static void main(String[] argv) {
    int count = 10;
    ++count;      // Add 1 to count
    --count;      // Subtract 1 from count
  
    System.out.println(count);
  }
}
output - 10
Arithmetic Operators
  • +   Addition
  • -    Subtraction (also unary minus)
  • *     Multiplication
  • /      Division
  • %   Modulus
  • ++  Increment
  • +=  Addition assignment
  • -=   Subtraction assignment
  • *=   Multiplication assignment
  • /=  Division assignment
  • %=  Modulus assignment
  • --  Decrement
 Bitwise Operators
  • ~     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 Operators
  • >  greater than
  • >=   greater than or equal to
  • == equal to
  • != not equal to
  • <= less than or equal to
  • < less than
Logical Operators
  • *&      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  
The ? Operator (Ternary)
public class MainClass {
  public static void main(String args[]) {
    int i, k;
 
    i = 10;
    k = i < 0 ? -i : i; // get absolute value of i
    System.out.print("Absolute value of ");
    System.out.println(i + " is " + k);
 
    i = -10;
    k = i < 0 ? -i : i; // get absolute value of i
    System.out.print("Absolute value of ");
    System.out.println(i + " is " + k);
  }
}
 //output
Absolute value of 10 is 10
Absolute value of -10 is 10
 
Comma Operator
public class MainClass {
  public static void main(String[] args) {
    for(int i = 1, j = i + 10; i < 5;
        i++, j = i * 2) {
      System.out.println("i= " + i + " j= " + j);
    }
  }
}
 //output
i= 1 j= 11
i= 2 j= 4
i= 3 j= 6
i= 4 j= 8

 instance of
The instanceof keyword can be used to test if an object is of a specified type.
if (objectReference instanceof type)
The following if statement returns true.
public class MainClass {
  public static void main(String[] a) {
    String s = "Hello";
    if (s ~~#FF0000:instanceof~~ java.lang.String) {
      System.out.println("is a String");
    }
  }
}
Output - is a String

No comments:

Post a Comment