Operators & Operand

In this tutorial, we will discuss on different types of operators and relationship between Operators and Operand in java.

Objective:

  • What is Operator
  • Types of Operator
  • Use of Operators in Java

What is Operator?

Operators are very crucial and integral part of java programming language. Without operator world of java programmer is nothing. We can use operator to do some mathematical calculation, increment & decrement the value of the variable, to check entry & exit condition. A lot of operators are available in Java so that Java programmer can achieve the goal during coding.

How Operator is used ? Has Operators any existence alone?

Obviously No.

Operator alone has no value. This should be applicable with some values, variables etc. On which operators are applied is known as Operand. So Operators can manipulate the Operand and provide the result in output to achieve certain goal.

For example: a = b+c

Here a, b, c are the Operands and +, = are called Operators.

As you know, we have lot of Operators available in Java program, let’s discuss some of them.

  • Plus operator = +
  • Minus Operator = –
  • Bitwise Inversion = ~
  • Postfix and Prefix increment = ++
  • Postfix and Prefix decrement = – –
  • Casting Operator = ( )
  • Boolean Complement operator = !
Operators & Operand

The above are the list of important operator available in Java programming language. Let’s discuss each operator in detail.

Plus and Minus Operator :

Plus and minus operator is also known as Unary operator. If you want to re[resent the value as positive or negative then you can use this unary operator before the value.

Example :

class Minusoperator {
	public static void main(String args[])
	{
		int a = 4;
		int b = -2;
		System.out.println(b);
	}
}

Output:

-2
class Plusoperator {
	public static void main(String args[])
	{
		int a = 4;
		int b = 2;
		
		System.out.println(a);
	}
}

Output:

4

From the above example, you can notice that of you want to specify the value as negative then you need to add – sign with the value.

You also can use 2 unary operator like 2 plus operator or 2 minus operator in java. see below example for more clarity.

class Minusoperator {
	public static void main(String args[])
	{
		int a = 4;
		int b = -(-2);
		
		System.out.println(b);
	}
}

Output:

2

Postfix and Prefix Operator:

Postfix and Prefix operator is combination of 2 similar unary operator. i.e. ++ & –. ++ is known as postfix / prefix increment and — is known as postfix / prefix decrement.

Now you can ask what is postfix and what is prefix?

Postfix is the operator which can be used after the operand and Prefix is the operator which can be used before the operand.

Syntax :

a++, b–etc.

When you will use the postfix operator it will increase the value by 1 and it will be applicable in next line. But if you will print the postfix operator directly then the current value will be displayed and then it increased by 1 from the next line.

Let’s understand this feature with a simple example.

Example:

class Postfix {
	public static void main(String args[])
	{
		int a = 4;
		int b = a++;
		
		System.out.println(b);
		System.out.println(a);

	}
}

Output:

5

From the example you can notice that a++ value is assigned to b but the current value of a is stored in b. But the value of a is increased by 1 as you have used the postfix with a. Value of a then the incremented value is reflected from next line. So when you tried to print the value of a in last line then it displays the incremented value.

Let’s discuss another scenario on Postfix.

class Postfix {
	public static void main(String args[])
	{
		int a = 4;
		
		System.out.println(a++);
		System.out.println(a++);
	}
}

Output:

4
5

This is slightly differing from previous example. In this scenario, you are using the postfix expression in the print(). So, the current value i.e. 4 is applicable to the print(). But the incremental value i.e. in the second print () output will display 5 as you can see in the example.

Prefix in java is same as Postfix but it decrement the current value of the operand.

When you will use the prefix operator it will decrese the value by 1 and replace to the current value of the operand and it will be applicable in next line.

Let’s understand this feature with a simple example.

class Prefix {
	public static void main(String args[])
	{
		int a = 4;
		int b = ++a;
		
		System.out.println(b);
		System.out.println(a);
	}
}

Output:

5
5

From the example you can notice that ++a increase the value of a by 1 and store in a then the same value is showing for b in print().

Leave a Reply

Your email address will not be published. Required fields are marked *