Unary Operators >
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
Operator
|
Description
|
+
|
Unary plus operator;
indicates positive value
(it’s optional, if not used numbers are positive)
|
-
|
Unary minus operator;
indicates negative value
|
++
|
Increment operator;
increments value by 1
|
--
|
Decrement operator;
decrements value by 1
|
!
|
Logical complement operator;
inverts boolean value
|
Program 2 to demonstrate Unary operators
/** JavaMadeSoEasy.com */
public class UnaryOperatorTest {
public static void main(String[] args) {
int result=+1;
System.out.println("+ "+ result); //1
result=-1;
System.out.println("- "+ result); //-1
result=3;
System.out.println("--------Now, result="+result);
result++;
System.out.println("++ "+ result); //4
result--;
System.out.println("-- "+ result); //3
boolean boo=true;
System.out.println("! "+ !boo); //false
}
}
/*OUTPUT
+ 1
- -1
Now, result=3
++ 4
-- 3
! false
*/
|
Labels:
Core Java
core java Basics