What are the unary operators in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What are the unary operators in Java?

9th Mar 2017, 5:15 PM
Pradipta Roy
Pradipta Roy - avatar
3 Answers
+ 3
/** +Unary plus operator; indicates positive value (numbers are positive without this, however) -Unary minus operator; negates an expression ++Increment operator; increments a value by 1 --Decrement operator; decrements a value by 1 !Logical complement operator; inverts the value of a boolean */ class Unary{ public static void main(String[] args){ int result = 1; // result is 1 result = +result; System.out.println(result); // result is now -1 result = -result; System.out.println(result); boolean success = false; // compliment // success is now true success = !success; System.out.println(success); // increment // result is now 0 result = ++result; System.out.println(result); // decrement //result is now -1 result = --result; System.out.println(result); } }
10th Mar 2017, 5:01 AM
Earl Chukwu
Earl Chukwu - avatar
+ 2
Unary operators need a single operand to realize the operation 1. Unary minus (-x): negative x 2. Increment (++x): x = x+1 3. Decrement (--x): x = x-1 4. Logical complement (!x): works on boolean operands (if x = true, !x = false)
10th Mar 2017, 4:51 AM
Patricio López
Patricio López - avatar
+ 1
@Earl's example is more than sharp.
10th Mar 2017, 5:41 AM
dex