Skip to main content

Java Fundamentals

Operators

So far, we've been working with literal values and variables, but in real projects, we often need to perform operations with them.

Today, we'll explore operators! These special symbols let us do cool things like adding, subtracting, and more with our values and variables.

You might not have realised it, but you've already used one operator a lot - 

it's the "=" sign.

For example

int count = 10;

Here, the = sign helps set the value on the right (10) to the variable on the left (count). We call this the assignment operator.

Today, we'll focus on more operators that can help us do different operations with our values. Let's dive in!

Unary Operators 

Unary operators are special operators that perform operations on a single operand to make a new value.

Increment operator (++) - It is used to increment the value of a variable by 1.

Syntax -

++var // prefix
var++ // postfix

How to use the ++ operator ?

Both the prefix increment and postfix increment operators are used to increase the value of a variable. However, they work a bit differently depending on where they are placed in relation to the variable.

Prefix Increment - In Prefix Increment, the increment operator is placed before the variable. The value is first incremented based on the operator's precedence, and then other less important operations are carried out.

Syntax-

answer = ++var;

The above expression can be understood as:

First, increase the variable var by 1:

var = var + 1;

Then, set the variable answer equal to the updated value of answer, 

answer = var;

Postfix Increment - In Prefix Increment, the increment operator is placed after the variable. This operation happens after all other calculations are completed. It is also known as postfix increment.

Syntax-

answer = var++;

The above expression can be understood as:

the current value of var is assigned to answer, and then var is increased by 1.

Note: In the postfix operation, the value is increased after it has been assigned to the variable.

Let's see an example.

class Main {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 10;
 
        // PREFIX
        int pre_increment = ++num1;
        System.out.println("Prefix result = " + pre_increment);
 
        // POSTFIX
        int post_increment = num2++;
        System.out.println("Postfix result = " + post_increment);
    }
}

Output-

Prefix result: 11
Postfix result: 10
  • Decrement operator (--) - It is used to decrease the value of a variable by 1.

Syntax -

-- var // prefix
var-- // postfix

Similar to the increment operator, the decrement operator can also be used in two different manners.

Prefix Decrement - The pre-decrement operator reduces the value of the variable immediately upon encountering it. It is used as the prefix of the operand.

Syntax

answer = --var;

The above expression can be understood as:

First, decrease the variable var by 1:

var = var - 1;

Then, set the variable answer equal to the updated value of answer, 

answer = var;

Postfix Decrement - The post-decrement occurs when the decrement operator is placed after the variable(var--). Here, the decrement operation happens after all other operators are evaluated.

Syntax

answer = var--;

The above expression can be understood as:

the current value of var is assigned to answer, and then var is decreased by 1.

Let's see an example.

class Main {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 10;
 
        // PREFIX
        int pre_decrement = --num1;
        System.out.println("Prefix result = " + pre_decrement);
 
        // POSTFIX
        int post_decrement = num2--;
        System.out.println("Postfix result = " + post_decrement);
    }
}

 Output-

Prefix result = 9
Postfix result = 10

Not Operator(!) - The 'NOT' operator in Java is represented as '!', and it's used to flip the value of a condition. For example:

Syntax

!(operand)

For example:

flag = !true; // flag becomes false

This means if the initial value is true, the 'NOT' operator will make it false, and if the initial value is false, the 'NOT' operator will make it true. It's like flipping a switch: if the switch is on, the 'NOT' operator turns it off, and if the switch is off, the 'NOT' operator turns it on.