Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 208 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are Kotlin data types built off primitive or non-primitive Java data types?

#1
I am new to Kotlin and was playing around with the data types. I took an `Int` type and then tried to cast it as a `Double` by saying `num as Double`, a call that is valid in java (non syntactically but you get the point). However, this failed, saying that Int cannot be cast to Double. I am assuming this is because it is built off the Integer class rather than the raw int data type. Am I correct, and what is the most efficient way to cast values? There is a `.toDouble()` function, but this seems inefficient and unwieldy.
Reply

#2
This is because Kotlin does not work like Java in widening numbers.

There are no implicit widening conversions for [numbers][1] in Kotlin. for example, you can write something in Java as below:

int a = 1;
double b = a;

However, you can't write something in Kotlin. for example:

val a:Int = 1
// v--- can't be widening
val b:Double = a

This is because everything in Kotlin is object, there is no primitive types, so you should convert an `Int` to a `Double` explicitly, for example:

val a:Int = 1
// v--- convert it explicitly by `toDouble()` method
val b:Double = a.toDouble()

[1]:

[To see links please register here]

Reply

#3
> I took an `Int` type and then tried to cast it as a `Double` by saying `num as Double` <...> However, this failed, saying that Int cannot be cast to `Double`. I am assuming this is because it is built off the `Integer` class rather than the raw `int` data type.

No, and there are two important points to note:

* Kotlin positions its numeric types (`Int`, `Long`, `Double` etc.) as not being nested into each other, there is **no** subtyping relationship between these types. That's why the cast `intNum as Double` does not succeed in Kotlin. That's also why there's no implicit conversions between these types. Instead, [the numeric conversion is done with the corresponding functions][1] (e.g. `.toDouble()`)

* The numeric type usages in Kotlin are compiled into JVM primitives where possible. Some usages require boxed types (e.g. a nullable `Int?` requires boxing, and so does a generic type implementation with an `Int` as a type argument), but the compiler decides whether they are necessary for each case.

> <...> What is the most efficient way to cast values? There is a `.toDouble()` function, but this seems inefficient and unwieldy.

The most efficient way is to use the numeric conversion functions like `.toDouble()`. In fact, these functions are *intrinsified*, and there is no function call overhead when you use them. They are compiled closely to what javac would produce for a Java numeric cast or an implicit conversion. You can [inspect the bytecode][2] that the Kotlin compiler produces to find out what it's under the hood and whether a specific conversion introduces any overhead.

See also: an answer to a similar question, [(link)][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through