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-() → int

This operator-unary-minus operator of int return the negative value of this integer.



✐ Examples

1 Negative value

In this example,

  1. We create an integer variable num1 with the value 10.
  2. We use the unary - operator to get the negative value of num1.
  3. 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,

  1. We create an integer variable num2 with the value -5.
  2. We use the unary - operator to get the negative value of num2.
  3. 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,

  1. We create an integer variable num3 with the value 0.
  2. We use the unary - operator to get the negative value of num3.
  3. 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.