DEV Community

Divya bharathi G
Divya bharathi G

Posted on

Operators in Java

Opeartors:

Operators are special symbols used to perform operations on variables and values.

Types of Operators:
1.Assignment Operators:

  • Used to assign values. Eg:
package project1;
public class assignment_operator 
{
    public static void main(String[] args)
    {
        int a=10 , b=2;
        System.out.println(a=b);
    }

}
Enter fullscreen mode Exit fullscreen mode

Output:
2

2.Arithmetic Opeartors:

  • Used for mathematical calculations.
1. Operator   Meaning               Example
2. +         Addition                a + b
3. -         Subtraction         a - b
4. *         Multiplication          a * b
5. /         Division                a / b
6. %         Modulus (remainder)     a % b
Enter fullscreen mode Exit fullscreen mode

Eg:

package project1;

public class arrithmetic_operator {

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

}
Enter fullscreen mode Exit fullscreen mode

Output:

12
8
20
5
0
Enter fullscreen mode Exit fullscreen mode

3.Unary Operators

  • Operate on single operand.

       1.++   -> Increment operator
       2.--   -> Decrement operator
       3.no++ --> post increment
       4.++no --> Pre increment
       5.no-- --> post Decrement
       6.--no --> Pre Decrement
    

    Eg:

package project1;
public class unary_operator 
{
    public static void main(String[] args)
        {
        int i = 10;
        System.out.println(i++);// post Increment operator 10 + 1
        System.out.println(i);//11

        int j = 10;
        System.out.println(j--); //Post Decrement operator 10 -1
        System.out.println(j);

                int a = 10;
        System.out.println(++a);//Pre increment operator 10+1=11

        int b = 20;
        System.out.println(--b); //Pre Decrement operator 20-1=19
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
10
11
10
9
11
19

4.Equality and Relational Operators :

  • Used to compare two values → result is boolean.
  1. Operator Meaning
  2. == Equal to
  3. != Not equal
  4. > Greater than
  5. < Less than
  6. >= Greater than or equal
  7. <= Less than or equal

Eg:

package project1;
public class relational_operator 
{
    public static void main(String[] args) 
    {
        int tamil_mark = 70;
        int english_mark = 70;
        System.out.println(tamil_mark == english_mark); // boolean
        System.out.println(tamil_mark < english_mark); // Boolean
                System.out.println(tamil_mark > english_mark);
                System.out.println(tamil_mark != english_mark);
    }

}
Enter fullscreen mode Exit fullscreen mode

Output:
true
false
false
false

5.Conditional Operators :

    - Used with boolean values.
          1. && = AND operator
          2.|| = OR operator
Enter fullscreen mode Exit fullscreen mode

Top comments (0)