📢 Join My WhatsApp Channel JavaWithSumit

Get Daily ICSE Java Notes, Programs & Exam Tips 🚀

👉 Join Now

Operators in Java and Its Types

 

Operators in Java and Its Types

Java is one of the most popular programming languages used in schools and professional software development. While writing Java programs, we often need to perform different operations such as addition, comparison, or logical decision making.

For this purpose, operators are used.

In this article, we will learn about Operators in Java, their types, and examples as per the ICSE syllabus.


What is an Operator in Java?

An operator is a special symbol used in programming to perform a specific operation on one or more values (operands).

For example:

int sum = 10 + 5;

Here:

  • + is an operator

  • 10 and 5 are operands

  • The operation performed is addition

Operators help us perform mathematical calculations, comparisons, and logical decisions in a program.


Types of Operators in Java

Java operators are mainly divided into the following types:

  1. Arithmetic Operators

  2. Relational Operators

  3. Logical Operators

  4. Assignment Operators

  5. Increment and Decrement Operators

  6. Conditional (Ternary) Operator

  7. Bitwise Operators

Let us understand each type in detail.


1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example Program

class ArithmeticExample
{
public static void main()
{
int a = 10;
int b = 3;

System.out.println("Addition = " + (a + b));
System.out.println("Subtraction = " + (a - b));
System.out.println("Multiplication = " + (a * b));
System.out.println("Division = " + (a / b));
System.out.println("Remainder = " + (a % b));
}
}

Output

Addition = 13
Subtraction = 7
Multiplication = 30
Division = 3
Remainder = 1

2. Relational Operators

Relational operators are used to compare two values.
The result of relational operators is always true or false.

Operator                    Meaning
==                    Equal to
!=            Not equal to
>            Greater than
<            Less than
>=            Greater than or equal to
<=            Less than or equal to

Example

class RelationalExample
{
public static void main()
{
int a = 10;
int b = 20;

System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
}
}

3. Logical Operators

Logical operators are used to combine multiple conditions.

Operator            Meaning
&&
||                   
Logical AND
Logical OR
!Logical NOT

Example

class LogicalExample
{
public static void main()
{
int age = 18;
int marks = 75;

if(age >= 18 && marks >= 60)
{
System.out.println("Eligible");
}
}
}

Explanation:

  • && returns true only if both conditions are true

  • || returns false only if both condition are false.

  • returns True as False and False as True


4. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorMeaning
=Assign


5. Shorthand Operators

A shorthand operator combines an arithmetic operation with an assignment operation.

General form:

variable operator= value

Example:

x += 5

This means:

x = x + 5

Thus, shorthand operators reduce the length of the code and improve readability.


Types of Shorthand Operators in Java

The main shorthand operators used in Java are:

OperatorMeaningEquivalent Expression
+=Add and assigna = a + b
-=Subtract and assigna = a - b
*=Multiply and assigna = a * b
/=Divide and assigna = a / b
%=Modulus and assigna = a % b
Example:

class AssignmentExample
{
public static void main()
{
int x = 10;

x += 5; // x = x + 5

System.out.println(x);
}
}

Output:

15

6. Increment and Decrement Operators

These operators are used to increase or decrease the value of a variable by 1.

OperatorMeaning
++Increment
--Decrement

There are two types:

Pre Increment

++a

Value increases before use

Post Increment

a++

Value increases after use

Example

class IncrementExample
{
public static void main()
{
int a = 5;

System.out.println(++a);
System.out.println(a++);
}
}

7. Conditional (Ternary) Operator

The ternary operator is used as a short form of if-else statement.

Syntax:

condition ? value1 : value2;

Example

class TernaryExample
{
public static void main()
{
int a = 10;
int b = 20;

int max = (a > b) ? a : b;

System.out.println("Maximum = " + max);
}
}

8. Bitwise Operators

Bitwise operators work on binary values (bits).

OperatorMeaning
&Bitwise AND
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

These operators are mostly used in advanced programming and system-level operations.

9. Bitwise Operators

A Unary Operator is an operator that works with only one operand (one variable or value).

The word Unary means one.

Example: 

int a = 5;

a++;

Here the operator ++ works on only one variable (a).

Types of Unary Operators in Java

There are mainly four unary operators in Java.

Operator

Name

Example: 

+        Unary Plus

+a      Indicates positive value

-          Unary Minus

-a         Changes sign of number

++        Increment Operator

a++ or ++a

Increases value by 1

--           Decrement Operator

a-- or --a

Decreases value by 1

1. Unary Plus Operator (+)

The Unary Plus operator indicates that the number is positive.

Example

int a = 10;

int b = +a;

System.out.println(b);

Output

10

This operator does not change the value, it simply shows that the number is positive.

2. Unary Minus Operator (-)

The Unary Minus operator changes the sign of a number.

Example: 

int a = 10;

int b = -a;

System.out.println(b);

Output:

-10

The value becomes negative.

3. Increment Operator (++)

The Increment Operator increases the value of a variable by 1.

There are two types:

1. Pre Increment

++a

Value increases before using the variable.

Example

int a = 5;

System.out.println(++a);

Output

6

2. Post Increment

a++

Value increases after using the variable.

Example

int a = 5;

System.out.println(a++);

System.out.println(a);

Output

5

6

4. Decrement Operator (--)

The Decrement Operator decreases the value of a variable by 1.

There are also two types.

Pre Decrement

--a

Value decreases before use.

Example:

int a = 5;

System.out.println(--a);

Output

4

Post Decrement

a--

Value decreases after use.

Example

int a = 5;

System.out.println(a--);

System.out.println(a);

Output

5

4

2. Operator Precedence in Java

Operator Precedence defines which operator will be executed first in an expression.

When multiple operators are used in one statement, Java follows a priority order.

Example

int result = 10 + 5 * 2;

Multiplication (*) has higher precedence than addition (+).

So the calculation will be:

5 * 2 = 10

10 + 10 = 20

Final Output

20


Importance of Operator Precedence

Operator precedence is important because it:

✔ Determines correct order of evaluation

✔ Prevents logical errors in programs

✔ Makes expressions easier to understand

✔ Helps in writing efficient code

Comments

Popular posts from this blog

Introduction to Object Oriented Programming and Java- ICSE Notes

Introduction to OOP's

Object Modelling – Entities and Their Behaviour in Java