Operators
Dart defines the operators shown in the following table.You can override many of these operators, as described inOverridable operators.
Description | Operator |
---|---|
unary postfix | expr++ expr— () [] . ?. |
unary prefix | -expr !expr ~expr ++expr —expr await expr |
multiplicative | / % ~/ |
additive | + - |
shift | << >> >>> |
bitwise AND | & |
bitwise XOR | ^ |
bitwise OR | | |
relational and type test | >= > <= < as is is! |
equality | == != |
logical AND | && |
logical OR | || |
if null | ?? |
conditional | expr1 ? expr2 : expr3 |
cascade | .. |
assignment | = = /= += -= &= ^= etc. |
Warning: Operator precedence is an approximation of the behavior of a Dart parser. For definitive answers, consult the grammar in the Dart language specification.
When you use operators, you create expressions. Here are some examplesof operator expressions:
a++
a + b
a = b
a == b
c ? a : b
a is T
In the operator table,each operator has higher precedence than the operators in the rowsthat follow it. For example, the multiplicative operator %
has higherprecedence than (and thus executes before) the equality operator ==
,which has higher precedence than the logical AND operator &&
. Thatprecedence means that the following two lines of code execute the sameway:
// Parentheses improve readability.
if ((n % i == 0) && (d % i == 0)) ...
// Harder to read, but equivalent.
if (n % i == 0 && d % i == 0) ...
Warning: For operators that work on two operands, the leftmost operand determines which version of the operator is used. For example, if you have a Vector object and a Point object, aVector + aPoint
uses the Vector version of +.