Unary Operators in JavaIn Java, the unary operator is an operator that can be used only with an operand. It is used to represent the positive or negative value, increment/decrement the value by 1, and complement a Boolean value. In this section, we will discuss the unary operator in Java with examples and also understand the differences between i++ and i+=1. There are five unary operators in Java:
The following table describes the short description of the unary operators.
Unary PlusIt is used to represent positive values. Usually, we do not write the operator before the operand. Hence, it is optional. Syntax: Example: x=+99 or x=99; Unary MinusIt is used to convert a positive value into a negative one. Syntax: Increment OperatorIt is used to increment the value of an operand by one. The operator is represented by a pair of plus operators (++). The operator can be applied before or after an operand. Pre-increment Operator: If an increment operator is written before (prefix) the operand is known as pre-increment. In such a case, the value is first incremented by 1 and then taken for the computing purpose. Syntax: Example: Suppose x=9, then the value of ++x will be 10. Post-increment Operator: If an increment operator is written after (postfix) the operand is known as post-increment. In such a case, the value is first processed and then incremented. It means the value that the variable holds first used for the computing purpose and then incremented by 1. Syntax: Example: Suppose x=11, then the value of x++ will be 12. Let's use the pre-increment and post-increment operators in a Java program. PrePostIncrementExample.java Output: 11 12 13 13 14 What is the difference between i++ and i+=1?Both the expressions i++ and i+=1 evaluated the same result. But the only difference is that i++ uses the increment operator (unary operator) and i+=1 uses the assignment operator. Another difference is that the expression i++ directly increments the value of i but the expression i+=1 first converts into i=i+1 by the JVM and then executed. Decrement OperatorIt is used to decrement the value of an operand by 1. The operator is represented by a pair of minus operators (--). The operator can be applied before or after an operand. Pre-decrement Operator: If a decrement operator is written before (prefix) the operand is known as pre-decrement. In such a case, the value is first decremented by 1 and then used for the computing purpose. Syntax: Example: Suppose x=9, then the value of --x will be 8. Post-decrement Operator: If a decrement operator is written after (postfix) the operand is known as post-decrement. In such a case, the value is first used and then decrements by 1. Syntax: Example: Suppose x=11, then the value of x-- will be 10. Let's use the pre-decrement and post-decrement operators in a Java program. PrePostDecrementExample.java Output: 19 18 17 17 16 Remember
Logical Complement OperatorIt is used to reverse the value of an operand. It means if an operand has value true, the complement if the operator will be false and vice-versa. It is represented by the exclamatory symbol (!). Syntax: Example: flag=!true; Example of Unary OperatorIn the following example, we have used all the unary operators that we have discussed above. UnaryOperatorExample.java Output: 76 -76 21 20 false true
Next TopicWhat is Advance Java
|