**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 | Description | Association |

1 | ++,-- | Postincrement, Postdecrement | R -> L |

2 | ++,-- | Preincrement, Predecrement | R -> L |

3 | +,- | Unary plus, unary minus | R -> L |

4 | ~ | Bitwise compliment | R -> L |

5 | ! | Boolean NOT | R -> L |

6 | new | Create object | R -> L |

7 | (type) | Type cast | R -> L |

8 | *,/,% | Multiplication, division,remainder | L -> R |

9 | +,- | Addition, subtraction | L -> R |

10 | + | String concatenation | L -> R |

11 | <<, >>, >>> | Left shift, right shift, unsigned right | |

12 | <, <=, >, >= | ||

13 | instance of | Type comparison | L -> R |

14 | ==, != | Value equality and inequality | L -> R |

15 | ==, != | Reference equality and inequality | L -> R |

16 | & | Boolean AND | L -> R |

17 | & | Bitwise AND | L -> R |

18 | ! | Boolean | L -> R |

19 | | | Bitwise OR | L -> R |

20 | && | Conditional AND | L -> R |

21 | || | Conditional OR | L -> R |

22 | ? : | Conditional Ternary Operator | L -> 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 Group | Associativity | Operator Precedence |

(), [], postfix ++, postfix -- | left | Highest |

unary +, unary -, prefix ++, prefix --, ~, ! | right | |

(type), new | left | |

* , /, % , +, - <<, >>, >>>, < ,<= , >, >=, instanceof ==, != & | left | |

&& || | left | |

?: | left | |

=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^= | right | Lowest |

**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);

}

}

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

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);

}

}

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);

}

}

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

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);

}

}

}

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

The instanceof keyword can be used to test if an object is of a specified type.

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) {

public static void main(String[] a) {

String s = "Hello";

if (s ~~#FF0000:instanceof~~ java.lang.String) {

System.out.println("is a String");

}

}

}

if (s ~~#FF0000:instanceof~~ java.lang.String) {

System.out.println("is a String");

}

}

}

Output - is a String

## No comments:

## Post a Comment