Java Operators


Java Operators

Java provides a rich operator environment. Most of its operators can be divided into the following four groups: arithmetic, bitwise, relational, and logical. These operators are discussed below-


Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:

Opeartor Result
+ Addition (also unary plus)
- Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
Program
public class Main
{
    public static void main(String[] args) {
     int a = 20;
     int b = 40;
	  
     int Addition=a+b;
     int Subtraction=b-a;
     int Multiplication=a*b;
     int Division=b/a;
	  
     System.out.println(Addition);
     System.out.println(Subtraction);
     System.out.println(Multiplication);
     System.out.println(Division);
  }
}
Run

The Modulus Operator

The modulus operator '%' returns the remainder of a division operation.

Program
public class Main
{
	public static void main(String[] args) 
	{
	  int a = 23;
	  float b = 45.87F;
	  
	  System.out.println("a mod 10 = "+a%10);
	  System.out.println("b mod 10 = "+b%10);
	}
}
Run

Increment and Decrement

The ++ and the -- are Java's increment and decrement operators. The increment operator increases its operand by one. The decrement operator decreases its operand by one.

Program
public class Main
{
	public static void main(String[] args) 
	{
	  int a =10;
	  int Increment=++a;
	  int Decremennt=--a;
	  System.out.println("Increment value = "+Increment);
	  System.out.println("Decremennt value = "+Decremennt);
	}
}
Run

The Bitwise Operators

Java defines several bitwise operators that can be applied to the integer types: long, int, short, char, and byte.

Operator Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
Program
public class Main
{
	public static void main(String[] args) 
	{
	  int a=3;
	  int b=6;
	  int c=a|b;
	  int d=a&b;
	  System.out.println(c);
	  System.out.println(d);
	}
}
Run

The Relational Operators

The relational operators determine the relationship that one operand has to the other.

Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
&= Bitwise AND assignment
Program
public class Main
{
	public static void main(String[] args) 
	{
	  int a=3;
	  int b=3;
	  if(a==b)
	  {
	      System.out.println("Two numbers are same");
	  }
	  else
	  {
	      System.out.println("Two numbers are different");
	  }
	}
}
Run

Boolean Logical Operators

The Boolean logical operators shown here operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value.

Operator Result
& Logical AND
| Logical OR
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
== Equal to
!= Not equal to
?: Ternary if-the-else
Program
public class Main
{
	public static void main(String[] args) 
	{
	  boolean a=true;
	  boolean b=false;
	  String msg= (a&&b)?"this statement return true value":"this statement return false value";
	  System.out.println(msg);
	}
}
Run