Java Operators

operators in java

Java Operator:

In Java a symbol are used perform operations. Like: +, -, *, / and many other Java Operators.

There are many types of operators in Java which are given below:

1. Unary Operator,
2.Arithmetic Operator,
3. Shift Operator,
4. Relational Operator
5. Bitwise Operator,
6. Logical Operator,
7.Ternary Operator and
8.Assignment Operator.


Java Operator Precedence:

Operator TypeCategoryPrecedence
UnaryPostfix    –  

Prefix     –
expr++ expr—

++expr –expr +expr -expr ~ !
ArithmeticMultiplicative  

additive
* / %

+ –
RelationalComparison  

equality
< > <= >= instanceof

== !=
Bitwisebitwise AND  

b itwise exclusive OR
&  

^
Logicalbitwise inclusive OR  

logical AND  

logical OR
|    

&&  

||
Ternaryternary? :
Assignmentassignment= += -= *= /= %= &= ^= |= <<= >>= >>>=
Shiftshift<< >> >>>

Java Unary Operator:

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e. :

  • incrementing/decrementing a value by one
  • negating an expression
  • inverting the value of a boolean

Unary Operator Example: ++ and –

class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x–);//12 (11)
System.out.println(–x);//10
}}

Unary Operator Example 2: ++ and —

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}}

Unary Operator Example: ~ and !

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//-11 (minus of total positive value which starts from 0)
System.out.println(~b);//9 (positive of total minus, positive starts from 0)
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}}

Arithmetic Operators:

Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.

Example :

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *