+ 1
What are the unary operators in Java?
3 ответов
+ 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);
}
}
+ 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)
+ 1
@Earl's example is more than sharp.