Operators in java with example is simple tutorial based on operators in java.
In this tutorial we will see What is Operators, Operators in java with example, Types of operators and program for operators in java.
What is Operators ?
Operators are the symbols which performs various operations on operands such as addition,subtraction, multiplications, divisions or modulus etc.
Operators definition: Operators performs various mathematical operations.
An “Operator” is a symbol that tells the computer to perform certain mathematical or logical manipulations.
Operators are used in program to manipulate data and variables.
What is operand
Operands are which on which operators performs these operations.
Operators perform operations and results stored into the variables (operands) as result value of the expression.
Types of Operators in java
Operators in java
- Arithmetic operators
- Increment and decrements operators
- Logical operators
- Bitwise operators
- Assignment operators
- Relational operators
- Cast operators
1. Arithmetic operators
Arithmetic operators are used to performs various Arithmetic operations on operands.
Arithmetic operators must have two operands.
Arithmetic operators are : +, – ,* , /
Example : a= b + c
2. Increment and decrements operators :
This operator used to add or subtract number by 1 Increment operator used to increment operand value by 1 and decrement operand used for decrements value by 1.
We only require one operand for this.operator can be prefix or postfix that is write operand after and before operators.
Increment ++ , Decrement —
Prefix : In this we write operators before the operands.that is value first increments before the result of expression.
Postfix : In this we write operators after the operands.that is value first increments after the result of expression.
Example : a++,++b;
3. Logical Operators :
Logical operators used for performing logical operations such as Logical AND , Logical OR , Logical NOT.
Logical AND (&&) : This operator returns true if both operands are non zero.
Logical OR(||) : This operators returns true if either one of two operands are non zero.
Logical NOT(!) : This operators used to make condition reverse that is if condition true it returns false.
4. Relational Operators :
This operators used to check relation between the two operands.
Relational operators are as follow:
< Less than
> greater than
<= Less than equal to
>=greater than equal to
== equal to
5. Assignment Operators :
This used for assigning the result to operand after expression solved.
we assign result to operands which is at left end.
Assignment operators are as follow:
= equal to
+= Add and assign result to operand a+=b => a=a+b
-= subtract and assign result to operand a-=b => a=a-b
*= multiply and assign result to operand a*=b => a=a*b
/= divide and assign result to operand a/=b => a=a/b
6. Bitwise Operator :
This performs on bit present in each byte of variable.bit can be 0 or 1.
Bitwise operators are as follow:
~ one’s complements
<< left shift (Binary multiplication by 2)
>> Right shift (Binary divide by 2)
7. Cast Operators :
This used to convert a value of operands to another type that is int to float or any type.
Example : float(10);
Program for operators in java
public class Main
public static void main (String args)
//assigning static variable
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
String x = "Technical", y = "Seek";
// + and - operator
System.out.println("a + b = "+(a + b));
System.out.println("a - b = "+(a - b));
// + operator if used with strings
// concatenates the given strings.
System.out.println("x + y = "+x + y);
// * and / operator
System.out.println("a * b = "+(a * b));
System.out.println("a / b = "+(a / b));
// modulo operator gives remainder
// on dividing first operand with second
System.out.println("a % b = "+(a % b));
Important Operator in java
instanceOf Operator : This operator returns true if object belongs to class name.
Syntax for instanceOf Operator
boolean b=object instanceOf class_name;