var a : Double
a = Math.sin(10) // error: the integer literal does not conform to the expected type Double
a = Math.sin(10.0) //This compiles successfully
println(a)
Why doesn't kotlin perform implicit type conversion and force us to pass the exact type of data?
fun sin(value: Double): Double // at kotlin documentation
Widening Type Casting It is done automatically. It is safe because there is no chance to lose data. It takes place when: Both data types must be compatible with each other.
It happens automatically when converting from a narrower range data type to a wider range data type. It also means converting a lower data type like an int to a higher data type like a double .
In Smart Casting, we generally use is or !is an operator to check the type of variable, and the compiler automatically casts the variable to the target type, but in explicit type casting we use as operator. Explicit type casting can be done using : Unsafe cast operator: as.
Type Conversion example – In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler. 2. Type casting can be applied to compatible data types as well as incompatible data types.
We all know that Kotlin has both non-nullable Int
and nullable Int?
.
When we use Int?
this happens: Kotlin actually 'boxes' JVM primitives when Kotlin needs a nullable reference since it's aimed at eliminating the danger of null references from code.
Now look at this: (assuming this is a compilable code)
val a: Int? = 1
val b: Long? = a
Kotlin doesn't perform implicit type conversion because of this thing happens. If Kotlin did implicit type conversions, b
should be 1
. but since a
is a boxed Int
and b
is a boxed Long
, a == b
yields false
and falls into contradiction, since its ==
operator checks for equals()
and Long
's equals()
checks other part to be Long
as well.
Check the documentation:
Kotlin does not allow implicit conversions of numeric types. There is a misconception that implicit conversions are "no harm, no foul" ... which is wrong.
The process in Java for implicit conversions is more complicated than you think, read the docs to see what all is entailed. And then you can try to analyze all of the cases that can go wrong.
Kotlin, does not want the compiler to guess as to your intention, so it makes everything in the language explicit, including numeric type conversions. As explained in the Kotlin docs for Explicit Conversions it says clearly:
Due to different representations, smaller types are not subtypes of bigger ones. [...] As a consequence, smaller types are NOT implicitly converted to bigger types. [...] We can use explicit conversions to widen numbers.
And the documentation shows one such sample of where things can go wrong, but there are many others.
Nor can you just cast one numeric type to another, as mentioned here in incorrect comments and answers. That will only result in a nice runtime error. Instead look at the numeric conversion functions such as toInt()
and toDouble()
found on the numeric types, such as on the Number class.
Explicitness is part of the Kotlin personality, and it is not planned to change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With