DEV Community

Rakshambika
Rakshambika

Posted on Edited on

Operators in Java

What is Operator?

  • Operators are used to perform a operation on operands.

Types of Operators:

  • Arithmetic Operator
  • Assignment Operator
  • Relational Operator
  • Logical Operator
  • Unary Operator
  • Ternary Operator
  • Shift Operator
  • Bitwise Operator

Arithmetic Operator:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
class Arithmetic
{

public static void main (String[] args)
{

int a=100;
int b=20;

System.out.println("Addition:"+(a+b));
System.out.println("Sub:"+(a-b));
System.out.println("Multiply:"+(a*b));
System.out.println("Divide:"+(a/b));
System.out.println("Modulo:"+(a%b));

}

}
Enter fullscreen mode Exit fullscreen mode
Addition:120
Sub:80
Multiply:2000
Divide:5
Modulo:0
Enter fullscreen mode Exit fullscreen mode

Assignment Operator:

  • Used to assign values to a variable.
Operator Example
= a = 10
+= a += 5
-= a -= 5
*= a *= 5
/= a /= 5
%= a %= 5
class Assignment
{

public static void main (String[] args)
{

int a;
System.out.println("Assigning:"+(a = 10));
System.out.println("Add:"+(a += 20));
System.out.println("Sub:"+(a -= 10));
System.out.println("Multiply:"+(a *= 10));
System.out.println("Divide:"+(a /= 10));
System.out.println("Modulo:"+(a %= 3));

}
}

Enter fullscreen mode Exit fullscreen mode
Assigning:10
Add:30
Sub:20
Multiply:200
Divide:20
Modulo:2
Enter fullscreen mode Exit fullscreen mode

Relational Operator:

  • Used to compare two values.
Operator Meaning
== Equal to
!= Not Equal to
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal
class Relational
{

public static void main (String[] args)
{

int a=10;
int b=30;

System.out.println("Equal to:"+(a == b));
System.out.println("Not Equal to:"+(a != 20));
System.out.println("Greater than:"+(a > b));
System.out.println("Less than:"+(a<b));
System.out.println("Greater than or Equal to:"+(a >= b));
System.out.println("Greater than or Equal to:"+(a <= b));

}

}
Enter fullscreen mode Exit fullscreen mode
Equal to:false
Not Equal to:true
Greater than:false
Less than:true
Greater than or Equal to:false
Greater than or Equal to:true
Enter fullscreen mode Exit fullscreen mode

Logical Operator:


class Logical
{

public static void main (String[] args)
{

int a=10;
int b=30;

System.out.println("AND:"+((a > b) && (a != 20)));
System.out.println("OR:"+((a == 20) || (b>a)));
System.out.println("NOT:"+(a != b));
}

}
Enter fullscreen mode Exit fullscreen mode
AND:false
OR:true
NOT:true
Enter fullscreen mode Exit fullscreen mode

Unary Operator:

Operator Meaning
+ Unary Plus
- Unary Minus
++ Increment
-- Decrement
! Logical NOT
class Unary
{

public static void main (String[] args)
{

int a=10;
int b=30;

System.out.println("Unary Plus:"+(a+b));
System.out.println("Unary Minus:"+(a-b));
System.out.println("Increment:"+(++a));
System.out.println("Decrement:"+(a--));
System.out.println("Logical Not:"+(!true));
}

}

Enter fullscreen mode Exit fullscreen mode
Unary Plus:40
Unary Minus:-20
Increment:11
Decrement:11
Logical Not:false
Enter fullscreen mode Exit fullscreen mode

Ternary Operator:

A shortcut for if-else.

Syntax

condition ? value1 : value2;
Enter fullscreen mode Exit fullscreen mode
class Ternary
{

public static void main (String[] args)
{

int a=10;
int b=30;

String result = (a>b ? "A is Larger" : "B is Larger");

System.out.println("Which number is Larger?"+result);
}

}
Enter fullscreen mode Exit fullscreen mode
Which number is Larger? B is Larger
Enter fullscreen mode Exit fullscreen mode

Top comments (0)