DEV Community

Cover image for Understanding Operators in Java
scindia bethuraj
scindia bethuraj

Posted on

Understanding Operators in Java

Trainer: Mr.Nantha
Day 6

Overview of Java Operators

What are Operators in Java?

Operators are special symbols used to perform operations on variables and values.

In simple words, operators make it possible to carry out calculations, comparisons, and logical operations in a program.

After understanding how to declare and initialize variables, it is essential to explore their practical applications. Gaining proficiency in the operators of the Java programming language serves as an important next step. Operators are specialized symbols that execute defined operations on one, two, or three operands, subsequently yielding a result.

Example
int a = 10;
int b = 5;
int sum = a + b;

Here,

  • is an operator It adds a and b.

Types of Operators in Java

Arithmetic Operators

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result.

Operator Meaning
+
-
*
/
%

Example:

int result = 10 % 3;

The Unary Operators

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a Boolean.

|Operator| |Description|

|+| |Unary plus operator; indicates positive value (numbers are
positive without this, however)|
|-| |Unary minus operator; negates an expression|
|++| |Increment operator; increments a value by 1|
|--| |Decrement operator; decrements a value by 1|
|!| |Logical complement operator; inverts the value of a boolean|

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

What is Increment Operator?

++ is called the increment operator.
It increases the value of a variable by 1.

Example:

int a = 5;
a++; // now a becomes 6

re-Increment (++a)

👉 Value is increased first, then used.

Example:
int a = 5;
int b = ++a;

Step-by-step:

a becomes 6

b gets 6

Final Values:

a = 6
b = 6
Post-Increment (a++)

👉 Value is used first, then increased.

Example:
int a = 5;
int b = a++;

Step-by-step:

b gets 5

then a becomes 6

Final Values:

a = 6
b = 5

Easy Trick to Remember

Pre = Change first
Post = Print first

Equality, Relational, and Conditional Operators
The Equality and Relational Operators
The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use "==", not "=", when testing if two primitive values are equal.

|==| |equal to|
|!=| |not equal to|
|> | |greater than|
|>= | |greater than or equal to|
|< | |less than|
|<= | |less than or equal to|

The Conditional Operators
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

&& Conditional-AND
|| Conditional-OR

TASK :

int a = 5;
int b = 7;
int c = 10;

int result = a++ + a-- - --a + --b - ++c -  --c;
Enter fullscreen mode Exit fullscreen mode

What is the result?
a++
Post-increment → use a first, then increment

Value used = 5

Then a becomes 6

So first part = 5

a = 6

a--

Post-decrement → use a first, then decrement

Value used = 6

Then a becomes 5

Second part = 6

a = 5

--a

Pre-decrement → decrement first, then use

a = 4

Value used = 4

Third part = 4

a = 4
--b

Pre-decrement → decrement first, then use
**
b = 6**

Value used = 6

Fourth part = 6

b = 6

++c

Pre-increment → increment first, then use

c = 11

Value used = 11

Fifth part = 11

c = 11
--c

Pre-decrement → decrement first, then use

c = 10

Value used = 10

Sixth part = 10

c = 10

Combine Everything

The expression:

result = a++ + a-- - --a + --b - ++c - --c

Substitute values used in order:

result = 5 + 6 - 4 + 6 - 11 - 10

5 + 6 = 11

11 - 4 = 7

7 + 6 = 13

13 - 11 = 2

2 - 10 = -8

Final Values of Variables

After the expression:

a = 4
b = 6
c = 10

Key Takeaways – Operators & Increment/Decrement

Operators are symbols used to perform operations on variables and values.

Arithmetic, Relational, Logical, Assignment, Unary are main types of operators in Java.

Increment (++) and Decrement (--) can be prefix or postfix:

Prefix (++a / --a) → value changes first, then used.

Postfix (a++ / a--) → value used first, then changes.

Complex expressions with multiple operators should be solved step by step, tracking variable values after each operation.

Understanding operators is crucial for automation logic, loops, conditions, and interview questions.

Top comments (0)