Flutter for Beginners
上QQ阅读APP看书,第一时间看更新

Arithmetic operators

Dart comes with many typical operators that work like many languages; this includes the following:

  • +: This is for the addition of numbers.
  • -: This is for subtraction.
  • *: This is for multiplication.
  • /: This is for division.
  • ~/: This is for integer division. In Dart, any simple division with / results in a double value. To get only the integer part, you would need to make some kind of transformation (that is, type cast) in other programming languages; however, here, the integer division operator does this task.
  • %: This is for modulo operations (the remainder of integer division).
  • -expression: This is for negation (which reverses the sign of expression).

Some operators have different behavior depending on the left operand type; for example, the + operator can be used to sum variables of the num type, but also to concatenate strings. This is because they were implemented differently in the corresponding classes as pointed out before.

Dart also provides shortcut operators to combine an assignment to a variable after another operation. The arithmetic or assignment shortcut operators are  +=, -=, *=, /=, and ~/=.