I am not able to clearly understand what is typed array in kotlin. I have seen the fucntion toTypedArray in kotlin. but did not see any proper definition of it like what exactly it does. Can anyone please explain with an example.
Thanks
Arrays are generic data structures because they can contain different types of elements. You can have Array<Int> or Array<String> for instance.
There is no separate concept of "typed" array. The reason for the name of toTypedArray is (I guess) to distiguish it from toArray() which returns an Array<Any?> (without useful type information about its elements, because everything is a Any? in Kotlin).
The reason why those 2 exist is because arrays on the JVM cannot be created without knowing the element type. This means that, in general, you cannot create an arbitrary array generically because generics are erased at runtime so you wouldn't actually know the correct element type at that time. This is why the simple toArray method either returns Array<Any?> or takes an extra array argument. The extra argument allows to either avoid creating the destination array, or at least provides sufficient type information at runtime to create an array of the same type.
In Kotlin, we can go one step further and actually use reified types to use information that we have at compile time to generate more specific code, such as code that create an array of a specific type (not generically, but directly with the correct element type based on the call site information). This is what toTypedArray does by reifying its type parameter.
At the end of the day primitive data types is what matter to the JVM. Kotlin and Java are two languages that create code to use on the JVM.
Java can work directly with these primitives and sometimes wrap them up in a class to make them more manageable. In java the int is the primitive and the Integer is the wrapper class if you choose to use it. Integer comes with a ton of functions and fields to make developers lives easier. checkThemOut() the int primitive doesn't come with that it is just a value.
Kotlin wraps everything in a object to have that capablitity however it interoperability with Java and the JVM means that certain features requires primitive pieces of data such as arrays. That is when IntArrays, LongArrays, and other primitive array structures come in.
Kotlin has 2 types of arrays: Typed Arrays and Primitive Arrays Typed Arrays take your Jvm primtive pieces of data and boxes them in Objects, which causes extra overhead
Primitive Arrays are arrays that isn't boxed and is just store as its JVM primitive.
Why have two different types? It helps keep Kotlin and Java interoperable. Java has JVM primitive data types that is use in its codebase. Kotlin wraps everything in a Object
In Kotlin, everything is an object in the sense that you can call member functions and properties on any variable. Some types can have a special internal representation – for example, numbers, characters and booleans can be represented as primitive values at runtime – but to the user they look like ordinary classes. - kotlinlang
The kotlin documentation recommends that avoid using any type of array and just use Collections if you can.