Dart int operator-unary-minus
Syntax & Examples
int.operator-unary-minus operator
The `unary-` operator in Dart returns the negative value of this integer.
Syntax of int.operator-unary-minus
The syntax of int.operator-unary-minus operator is:
operator unary-() → intThis operator-unary-minus operator of int return the negative value of this integer.
✐ Examples
1 Negative value
In this example,
- We create an integer variable
num1with the value 10. - We use the unary
-operator to get the negative value ofnum1. - We then print the result to standard output.
Dart Program
void main() {
int num1 = 10;
int result1 = -num1;
print('Negative value of $num1: $result1');
}Output
Negative value of 10: -10
2 Negative value
In this example,
- We create an integer variable
num2with the value -5. - We use the unary
-operator to get the negative value ofnum2. - We then print the result to standard output.
Dart Program
void main() {
int num2 = -5;
int result2 = -num2;
print('Negative value of $num2: $result2');
}Output
Negative value of -5: 5
3 Negative value
In this example,
- We create an integer variable
num3with the value 0. - We use the unary
-operator to get the negative value ofnum3. - We then print the result to standard output.
Dart Program
void main() {
int num3 = 0;
int result3 = -num3;
print('Negative value of $num3: $result3');
}Output
Negative value of 0: 0
Summary
In this Dart tutorial, we learned about operator-unary-minus operator of int: the syntax and few working examples with output and detailed explanation for each example.