Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Unary operators

The unary operators requires only one operand to perform different kind of
operations such as increasing/decreasing a value, negating an expression,
or inverting a boolean value.

The unary operators requires only one operand to perform different kind
of operations such as increasing/decreasing a value, negating an
expression, or inverting a boolean value. These operators can not be
used with final variables. There are different types of unary operators
mentioned in the table given below:
Symbo Name of the
Operation Example
l Operator
 Unary plus  indicates positive value (however, int number
 +  
operator numbers are positive without this) = +1;
 Unary minus number = -
 -   negates an expression
operator number;
 Increment number = ++
 ++    increments a value by 1
operator number;
 Decrement number = --
 --    decrements a value by 1
operator number;
 Logical
 !   compliment  inverts a boolean value  
operator

I. Unary Plus (+) Operator


Unary plus operator (+) indicates positive value. This (+) operator is used
to perform a type conversion operation on an operand. The type of the
operand must be an arithmetic data type i.e. if a value of the integer
operand is negative then that value can be produced as a positively
applying unary plus (+) operator
.
II. Unary minus (-) Operator
Unary minus operator (-) indicates negative value and differ from the
unary plus operator. This (-) operator is also used to perform a type
conversion operation on an operand. If a value of the integer operand is
positive then that value can be produced as a negatively applying unary
minus (-) operator. 

III. Increment (++) and Decrement Operators


The increment/decrement operators can be a prefix or a postfix .In a
prefix expression (++ x or -- x), an operator is applied before an operand
while in a postfix expression (x ++ or x --) an operator is applied after an
operand. In both conditions 1 is added to the value of the variable and
the result is stored back to the variable. However both operators have the
same effect as "x = x + 1;"

Although there is a major difference between a prefix and


a postfix expressions. In a prefix expression, a value
is incremented first then this new value is restored back to the variable.
On the other hand, In postfix expression the current value is assigned to
a variable then it is incremented by 1and restored back to the original
variable.

 IV. Logical Compliment (!) Operator


The logical compliment (!) operator is also known as Boolean Negation
Operator. It is used to invertthe value of a boolean type operand i.e. the
type of the operand  must be boolean while using this operator,. If the
value of the boolean operand is false, the ! operator returns true. But,
if the value of the operand is true, the ! operator returns false. 
Lets have one more example using unary operators in different flavors

Binary operator
Operator Name

, Comma

!= Inequality

% Modulus

%= Modulus/assignment

& Bitwise AND

&& Logical AND

&= Bitwise AND/assignment

* Multiplication

*= Multiplication/assignment

+ Addition

+= Addition/assignment

– Subtraction

–= Subtraction/assignment

–> Member selection

–>* Pointer-to-member selection

/ Division

/= Division/assignment

< Less than

<< Left shift

<<= Left shift/assignment

<= Less than or equal to

= Assignment

== Equality
> Greater than

>= Greater than or equal to

>> Right shift

>>= Right shift/assignment

^ Exclusive OR

^= Exclusive OR/assignment

| Bitwise inclusive OR

|= Bitwise inclusive OR/assignment

|| Logical OR

You might also like