Operators
Arithmetic Operators
They are the same (+ - * / %
) except for the divide and return integer operator ~/
Equality and Relational Operators
They are the same (== != < <= > >=
)
Reference equality
In Dart in order to check if two variables refers to the same class instance use identical
.
In Kotlin use ===
.
Type Test and Type Cast Operators
The are the same except for is!
in Dart that is !is
in Kotlin.
Kotlin has also the safe cast operator as?
//type cast
val pp:Any = 1
println(pp as Int + 2) //3
//type check
println(pp is Int) //true
println(pp !is Int) //false
//type cast
Object pp = 1;
print((pp as int) + 2); //3
//type check
print(pp is int); //true
print(pp is! int); //false
is!
instead of !is
in Dart is!
is the operator for testing if an object is NOT of a type. In Kotlin it is !is
which is actually the is
operator, prefixed by the logical not !
operator
Assignment Operators
They are the same except for assigment operator based on Dart bitwise operators << >> >>> ~ & |
and the divide and return integer operator ~/
which are not supported in Kotlin
Logical Operator
They are the same (&& || !
)
Bitwise and shift operators
Kotlin is known to use non-standard notations for bitwise and shift operators. So they are all different from Dart
fun toHex(num:Int) = num.toString(16)
//bitwise AND
println(toHex(0xffff and 0xff)) //ff
//bitwise OR
println(toHex(0xfff0 or 0x0fff)) //ffff
//bitwise XOR
println(toHex(0xfff0 xor 0x0fff)) //f00f
//bitwise signed shift left
println(1 shl 2) //4
//bitwise signed shift right
println(4 shr 2) //1
//bitwise unsigned shift right
println(4 ushr 2) //1
//bitwise not
println(toHex(0xff00ff00.toInt().inv()) ) //ff00ff
String toHex(int number) => number.toRadixString(16);
//bitwise AND
print(toHex(0xffff & 0xff)); //ff
//bitwise OR
print(toHex(0xfff0 | 0x0fff)); //ffff
//bitwise XOR
print(toHex(0xfff0 ^ 0x0fff)); //f00f
//bitwise signed shift left
print(1 << 2); //4
//bitwise signed shift right
print(4 >> 1); //1
//bitwise unsigned shift right
print(4 >>> 1); //1
//bitwise not
print(toHex(~0xff00ff00) ); //ff00ff
Conditional expressions
Dart has the conditional operator Kotlin has instead if-else expression
//conditional (ternary) operator
var smaller_than_ten=5
var bigger_than_ten=11
println (if(smaller_than_ten<10) "smaller" else "bigger") //"smaller"
println (if(bigger_than_ten<10) "smaller" else "bigger") //"bigger"
//conditional (ternary) operator
var smaller_than_ten=5;
var bigger_than_ten=11;
print (smaller_than_ten<10 ? "smaller": "bigger"); //"smaller"
print (bigger_than_ten<10 ? "smaller": "bigger"); //"bigger"
Null coalescing operator (elvis operator)
Dart uses the ??
syntax Kotlin uses the ?:
syntax.
Member Access Operators
They are the same (. ?.
)
Cascade notation
In Dart Cascades (.., ?..) allow you to make a sequence of operations on the same object.
in Kotlin there are instead the with
and apply
idioms.
var paint = Paint().apply {
color = Colors.black
strokeCap = StrokeCap.round
strokeWidth = 5.0
}
var paint = Paint()
..color = Colors.black //no semicolon here!!
..strokeCap = StrokeCap.round //no semicolon here!!
..strokeWidth = 5.0;
Not Null assertion operator
In Dart the not null assertion operator is !
. In Kotlin it is !!
Indexed access operator
It is the same ([]
)
In Dart there is also the conditional indexed access operator ?[]
. In Kotlin ?[]
is not supported, but the indexed operator can specify more than one index.
Invoke (function call) operator
It is the same ()
.