<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents
</aside>
<aside> 💡
<aside> 💡
<aside> 💡
Java Operators
Java operators are special symbols that perform operations on variables or values. They can be classified into several categories based on their functionality. These operators play a crucial role in performing arithmetic, logical, relational, and bitwise operations etc.
Example: Here, we are using + and – operators.
// Java program to show the use of + and - operators
public class SampleProgram
{
public static void main(String[] args)
{
// Declare and initialize variables
int num1 = 500;
int num2 = 100;
// Using the + (addition) operator
int sum = num1 + num2;
System.out.println("The Sum is: "+sum);
// Using the - (subtraction) operator
int diff = num1 - num2;
System.out.println("The Difference is: "+diff);
}
}
Output
The Sum is: 600
The Difference is: 400
Types of Operators in Java
Arithmetic Operators
Arithmetic Operators are used to perform simple arithmetic operations on primitive and non-primitive data types.
Example:
// Java Program to show the use of
// Arithmetic Operators
import java.io.*;
class SimpleProgram
{
public static void main (String[] args)
{
// Arithmetic operators on integers
int a = 10;
int b = 3;
// Arithmetic operators on Strings
String n1 = "15";
String n2 = "25";
// Convert Strings to integers
int a1 = Integer.parseInt(n1);
int b1 = Integer.parseInt(n2);
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
System.out.println("a1 + b1 = " + (a1 + b1));
}
}
Output
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
a1 + b1 = 40
Unary Operators
Unary Operators need only one operand. They are used to increment, decrement, or negate a value.
-
, Negates the value.
+
, Indicates a positive value (automatically converts byte
, char
, or short
to int
).
++
, Increments by 1.
-
, Decrements by 1.
!
, Inverts a boolean value.
Example:
// Java Program to show the use of
// Unary Operators
import java.io.*;
// Driver Class
class SampleProgram {
// main function
public static void main(String[] args)
{
// Interger declared
int a = 10;
int b = 10;
// Using unary operators
System.out.println("Postincrement : " + (a++));
System.out.println("Preincrement : " + (++a));
System.out.println("Postdecrement : " + (b--));
System.out.println("Predecrement : " + (--b));
}
}
Output
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8
Assignment Operator
‘=’
Assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e. value given on the right-hand side of the operator is assigned to the variable on the left, and therefore right-hand side value must be declared before using it or should be a constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with others to create shorthand compound statements. For example, a += 5
replaces a = a + 5
. Common compound operators include:
+= ,
Add and assign.= ,
Subtract and assign.= ,
Multiply and assign./= ,
Divide and assign.%= ,
Modulo and assign.Example:
// Java Program to show the use of
// Assignment Operators
import java.io.*;
// Driver Class
class SampleProgram {
// Main Function
public static void main(String[] args)
{
// Assignment operators
int f = 7;
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println("f *= 4: " + (f *= 4));
System.out.println("f /= 3: " + (f /= 3));
System.out.println("f %= 2: " + (f %= 2));
System.out.println("f &= 0b1010: " + (f &= 0b1010));
System.out.println("f |= 0b1100: " + (f |= 0b1100));
System.out.println("f ^= 0b1010: " + (f ^= 0b1010));
System.out.println("f <<= 2: " + (f <<= 2));
System.out.println("f >>= 1: " + (f >>= 1));
System.out.println("f >>>= 1: " + (f >>>= 1));
}
}
Output
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
f &= 0b1010: 0
f |= 0b1100: 12
f ^= 0b1010: 6
f <<= 2: 24
f >>= 1: 12
f >>>= 1: 6
Relational Operators