I want to do something like this:
fun process(minutes: Int) = 0 fun test() { process(System.currentTimeMillis() / 1000 / 60) // error: Int expected }
and if I try process((System.currentTimeMillis() / 1000 / 60) as Int)
I get a ClassCastException
at runtime.
So how can I convert Long to Int?
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.
Use Long.toInt()
:
process((System.currentTimeMillis() / 1000 / 60).toInt())
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