Scanner class Methods in Java

Java-Unary Operators

 Unary Operators

The operators that act upon a single operand to produce a new are called Unary Operators. They perform the operation only on one operand.

Now, we will discuss the unary operators that are commonly used by programmers.

Unary Minus(-)

The unary minus operator is used to represent the negative sign of an operand. It is used to negate value from positive to negative. It can be used with operand of any datatype like float,int,long,double etc.It is usually used with negative numbers to represent their sign.

Syntax:      - operandName; 

Example:   

  1. a= - 5.90, a float negative number -5.90 is assigned to a.
  2. a= -10; here -10 is assigned to a
  3. a= - (-10) , here the math rule is applied which says -& - equals to +, so a=10

 The Unary Minus (-) is different from Arithmetic Subtraction (-) Operator as the subtraction operator subtracts a value from other , but unary minus only displays a negative sign .Unary minus is a unary operator whereas arithmetic subtraction operator is a binary operator.

 for example:  a= 5--2,can be interpreted as a=5-(-2), which means we want to subtract -2 from 5.

Unary Increment(++)/Decrement (--) Operators

These operators are used to increment or decrement the value of operand by 1. They are used with a single operand. It does not matter what datatype we are using for operands. It increases/decreases the value of operand by 1.We can say it adds/subtracts 1 from the value of operand. 

Syntax:   operand ++; //for increment , operand --; //for decrement

 For example:  If we want to increase value of 1 by one we don't need to write a+=1 or a=1+1; instead  a++

If a contains 5, after a++; a gets increased by 1 and result will be a=6 . 

Types of increment operator: There are two types of increment operators as below,

1. Prefix Unary Increment (++) Operator: This operator is used when we want to increase the  value of operand before using it. In other words, the operand gets incremented before getting used in other expression.

Syntax: ++ operandName;

 Example: We have a variable a which has a integer value 4,a=4; and we want to print the next value of a , that is 5. For this we can use prefix increment operator.This operator will increase the value of a before printing it on screen.

Prefix Increment Operator in Java
Here, the value of a is incremented before printing by using ++ a in print statement.
2. Postfix Unary Increment (++) Operator: This operator is used when we want to increase the value of operand after using it. In other words, the operand gets incremented after performing an action on it..

Syntax: operandName ++;

 Example: We have a variable a which has a integer value 4,a=4; and we want to print the next value of a , that is 5. For this we can use postfix increment operator.It will increase the value of a, but to print the next value we have to use a++; before the print statement otherwise it will not print next value. 

Postfix Increment Operator in Java

Here, in first print statement the a++ will not print 5 because postfix means to increase the value after the current action. In the first statement the value of a is still 4, after the first print the value of a get increased due to a++ expression and therefore in the second print statement ,5 is printed as the next value of a.

 Types of decrement operator: There are two types of decrement operators as below, 

1. Prefix Unary Decrement (--) Operator: This operator is used when we want to decrease the value of operand before using it. In other words, the operand get decremented or 1 is subtracted from the operand,before getting used in another expression.It is similar to Prefix Increment Operator.

Syntax: -- operandName;

 Example: We have a variable a which has a integer value 4,a=4; and we want to print the previous value of a , which is 3. For this we can use prefix decrement operator.This operator will decrease the value of a before printing it on screen.

Prefix Decrement Operator in Java

Here, the value of a is decremented before printing by using -- a in print statement.
2. Postfix Unary Increment (++) Operator: This operator is used when we want to increase the value of operand after using it. In other words, the operand gets incremented after performing an action on it.It is similar to Postfix Increment Operator.

Syntax: operandName --;

 Example: We have a variable a which has a integer value 4,a=4; and we want to print the previous value of a , which is 3. For this we can use postfix decrement operator.It will decrease the value of a, but to print the previous value we have to use a++; before the print statement otherwise it will not print next value. 

Postfix Increment Operator  in Java

Here, in first print statement the a-- will not print 3 because postfix means to decrease the value after the current action. In the first statement the value of a is still 4, after the first print the value of a get decreased due to a-- expression and therefore in the second print statement 3 is printed as the previous value of a.

Experiment: Write the above programs in your IDE and try to print more values by incrementing and decrementing. Analyse the outputs.

Challenge Yourself! Happy Coding! 

 

 

Comments