OPERATORS IN JAVA

 

Operators are used to perform operations on variables and values. 

In the example below, we use the + operator to add together two values: 

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable: 

Java divides the operators into the following groups: 

  • Arithmetic Operators 
  • Assignment Operators
  • Relational Operators 
  • Logical Operators
  • Bitwise Operators 
  • Unary operators

ARITHMATIC OPERATORS

Arithmetic operators are used to perform common mathematical operations. 



1. ADDITION 


Adds together two value. 


Eg: X+Y.


EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a+b);

        }
}

OUTPUT

30

2. SUBSTRACTION 

Subtract one value from other. 

Eg: X-Y.

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 100;
                int b = 20;

                System.out.printIn(a-b);

        }
}

OUTPUT

80

3. MULTIPLICATION 

Multiples two values.

Eg: X*Y.

EXAMPLE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                int c = a*b;

                System.out.printIn(c);

        }
}

OUTPUT

200

4. DIVISION 

Divides one value by another. 

Eg: X/Y

EXAMPLE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 100;

                int c = a/b;

                System.out.printIn(c);

        }
}

OUTPUT

10

5. MODULE

Returns the division reminder. 

Eg: X%Y.

EXAMPLE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 1000;

                int c = a%b;

                System.out.printIn(c);

        }
}

OUTPUT

1



EXPRESSION ATRITHMATIC OPERATIOR 

 package mypackage;


public class operators {

        public static void main (String[]args] {


                System.out.printIn(10+10-5*2/15);

        }
}

OUTPUT:

20

USING SCANNER:

package mypackage;

import java.util.Scanner;

public class scanner{

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("input the first number: ");
int a = input.nextInt();

System.out.println("input the secound number: ");
int b = input.nextInt();

System.out.println(a+b);
}

}

ASSIGNMENT OPERATORS 

Assignment operators are used to assign values to variables. 

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: A list of all assignment operators:  


OPOERATOR            EXAMPLE 


    =                                    X=5

    +=                                  X+=5

    -=                                  X-=3

    *=                                  X*=3

    %=                                  X%=3

    &=                                 X&=3

    |=                                    X|=3

    ^=                                   X^=3 

    >>=                                X>>=3

    <<=                                X<<=3


package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;


                a=10;

                System.out.printIn(a);

                a+=3;

                System.out.printIn(a);

                a-=4;

                System.out.printIn(a);

                a*=2;

                System.out.printIn(a);

                a/=2;

                System.out.printIn(a);

        }
}

OUTPUT:

10
13
9
18
9

ASSIGNMENT OPERATOR ADDING SHORT; 

package mypackage;

public class operators {

        public static void main (String[]args] {

                shora = 10;
                short b = 20;

                c=a+b;

                System.out.printIn(a);

        }
}

OUTPUT

Error

AFTER ADDING CASTE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                short a = 10;
                short b = 20;

                 c =(short ) (a*b);

                System.out.printIn(c);

        }
}

OUTPUT

200

RELATIONAL OPERATORS 

Relational operators returns Boolean value 

Comparison operators are used to compare two values:  


1. Equal to                         

Eg: X==Y 

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a==b);

        }
}

OUTPUT

False

2.. Not Equal to                         

Eg: X!=Y 

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a!=b);

        }
}

OUTPUT

True

3. Greater then                         

Eg: X>Y 

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a>b);

        }
}

OUTPUT

False

4. Less then                       

Eg: X<Y 

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a<b);

        }
}

OUTPUT

True

5. Less then or Equal to                         

Eg: X<=Y 

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a<=b);

        }
}

OUTPUT

True

6. Greater then or Equal to                         

Eg: X>=Y 

EXAMPLE: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a>=b);

        }
}

OUTPUT

False


LOGICAL OPETATORS 

Relational operators returns boolean value
  • Logical operators are used to determine the logic between variables or values:
  • logical && operators  doesn't check second condition if first condition is fail.it checks second condition  only if first one is true.
  • Logical || operator doesn't check second condition if first condition is true. It checks if second condition only if first one is fail.
&&        Logical AND 
||             Logical OR
!              Ternary 
EXAMPLE 1: 
package mypackage;

public class operators {

        public static void main (String[]args] {

                boolean a = true;
                boolean b = false;

                System.out.printIn(a && b);
                System.out.printIn(a || b);

        }
}

OUTPUT

False
True

EXAMPLE 2: 

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = 20;

                System.out.printIn(a>b && a<b);
                System.out.printIn(a>b || a<b);

        }
}

OUTPUT

False
True

BIT WISE OPERATORS 

~       Unary bitwise complement
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR

SHIFT OPERATORS 

SIGNED LEFT SHIFT (<<)
EXAMPLE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;

                System.out.printIn(++a);
                System.out.printIn(++a);

        }

>>      Signed right shift
>>>     Unsigned right shift


UNARY OPERATORS

INCREMENT

Increases the value of a variable BY 1. 

Eg; ++X.

EXAMPLE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;

                System.out.printIn(++a);
                System.out.printIn(++a);

        }
}

OUTPUT

11
12

DECREMENT 

Decreases the value of a variable by 1.

Eg: --X.

EXAMPLE:

package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;

                System.out.printIn(--a);
                System.out.printIn(--a);

        }
}

OUTPUT

9
8


UNARY OPERATORS ~ AND !


package mypackage;

public class operators {

        public static void main (String[]args] {

                int a = 10;
                int b = -10;

                boolean c = true;
                boolaead= false;

                System.out.printIn(~a);
                System.out.printIn(~b);
                System.out.printIn(!c);
                System.out.printIn(!d);

        }
}

OUTPUT 
-11
9
false
true