What are Operators in Java?
Operators are symbols used to perform operations on variables and values.
How many types of Operators available in Java?
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
What are Arithmetic Operators?
- Arithmetic operators are used to perform arithmetic operations like Addition,Subtraction, Multiplication, Division, and Modulo on variables and data. Following are the list of arithmetic operators.
For Addition + operator is used.
For Subtraction - operator is used.
For Multiplication ***** operator is used.
For Division / operator is used.
For Modulo % Operator (Remainder after division) is used.
What are the priority levels of arithmetic operation in Java?
- There are two priority levels of arithmetic operation in java.
- High priority ⇒ * / %
- Low priority ⇒ + –
Example :
public class Operator {
public static void main(String args[])
{
int a = 13,b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Output :
a + b = 18
a - b = 8
a * b = 65
a / b = 2
a % b = 3
What is Assignment Operator?
- Assignment operators are used to assign values to variables.
Example :
public class Operator {
public static void main(String args[])
{
int a = 5;
int b = 13;
int var;
// assign value using =
var = b;
System.out.println("var using =: " + var);
// assign value using +=
var = b;
var += a; //var = var+a;
System.out.println("var using +=: " + var);
// assign value using -=
var = b;
var -= a; //var = var-a;
System.out.println("var using -=: " + var);
// assign value using *=
var = b;
var *= a; //var = var*a;
System.out.println("var using *=: " + var);
// assign value using /=
var = b;
var /= a; //var = var/a;
System.out.println("var using /=: " + var);
// assign value using %=
var = b;
var %= a; //var = var%a;
System.out.println("var using %=: " + var);
}
}
Output :
var using =: 13
var using +=: 18
var using -=: 8
var using *=: 65
var using /=: 2
var using %=: 3
What are Relational Operators?
- Relational operators are used to compare two values and give result in BOOLEAN data type.
- == equal to(3 == 5 returns false)
- != not equal to(3 != 5 returns true)
- > greater than(3 > 5 returns false)
- < less than(3 < 5 returns true)
- >= greater than or equal to(3 >= 5 returns false)
- <= less than or equal to(3 <= 5 returns true)
Example :
public class Operator {
public static void main(String args[])
{
int a = 7, b = 11;
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.print("is "+a +" equal to "+b+"? ");
System.out.println(a == b); // false
// != operator
System.out.print("is "+a +" not equal to "+b+"? ");
System.out.println(a != b); // true
// > operator
System.out.print("is "+a +" greater than "+b+"? ");
System.out.println(a > b); // false
// < operator
System.out.print("is "+a +" less than "+b+"? ");
System.out.println(a < b); // true
// >= operator
System.out.print("is "+a +" greater than or equal to "+b+"? ");
System.out.println(a >= b); // false
// <= operator
System.out.print("is "+a +" less than or equal to "+b+"? ");
System.out.println(a <= b); // true
}
}
Output :
a is 7 and b is 11
is 7 equal to 11? false
is 7 not equal to 11? true
is 7 greater than 11? false
is 7 less than 11? true
is 7 greater than or equal to 11? false
is 7 less than or equal to 11? true
Unary Operators :
- ++ Increment operator
- -- Decrement operator
- ++a Preincrement operator
- If you use the ++ operator as a prefix like: ++a, the value of a is incremented by 1; then it returns the value.
- --a Predecrement operator
- If you use the -- operator as a prefix like: --a, the value of a is decremented by 1; then it returns the value.
- a++ postincrement operator
- If you use the ++ operator as a postfix like: a++, the original value of a is returned first; then a is incremented by 1.
- a-- postdecrement operator
- If you use the -- operator as a postfix like: a--, the original value of a is returned first; then a is decremented by 1.
Example :
public class Operator {
public static void main(String args[])
{
//post increment
int a = 10;
System.out.println(a++); //output is 10
System.out.println(a); //output is 11
//post decrement
a = 10;
System.out.println(a--); //output is 10
System.out.println(a); //output is 9
//pre increment
a = 10;
System.out.println(++a); //output is 11
//pre decrement
a = 10;
System.out.println(--a); //output is 9
}
}
Excercise Task:
public class Operator {
public static void main(String args[])
{
int a =10;
int b = 20;
int c = 30;
int result = a++ + --a - c++ - --c + a++; // 10 + 10 - 30 - 30 + 10
System.out.println(result);
}
}
Output : -30

Top comments (0)