0

I've already look at similar questions on this topic and their answers, but I'm still not satisfied.

In this posted answer, it says that IntArray corresponds int[] in Java, and it goes on to say Array<T> corresponds to T[].

My question is, what does int[][] correspond to? Is the answer Array<IntArray> like this other posted answer says? Because that just sounds like it has way more overhead than the primitive and nice looking int[][].

Are they the exact same thing? If not, how are they different? And most importantly, how can I express Java's int[][] in Kotlin?

Interoperability is sadly not an option as it cannot find the Java class during runtime.

In case it matters, here's the piece of code:

5
  • Yes, Array<IntArray> is the same as int[][]. What do you mean by saying it adds overhead? Commented Nov 25, 2022 at 19:44
  • @broot Just looking at the names, Array<IntArray> looks a lot more high-level than int[][] and it looks like there are more abstractions and levels of indirection involved, all of which translate to more overhead. Commented Nov 25, 2022 at 19:53
  • Do you mean runtime performance overhead or "source code overhead"? Kotlin decided to not go the way of Java where there are separate objects and primitives and where arrays are something much different than collections. Conceptually everything is an object in Kotlin, so arrays also look like some regular types. Then internally when targeting JVM, Kotlin uses primitives and arrays to optimize the resulting code. Commented Nov 25, 2022 at 19:57
  • But yes, that may add some confusion, because e.g.: Array<Int> is Integer[], IntArray is int[] and Array<IntArray> is int[][]. Kotlin authors attempted to hide the type complexity of underlying Java, but this is not entirely possible without sacrificing the access to its low-level types. Commented Nov 25, 2022 at 20:02
  • @broot I mean performance overhead. If that's the case, then what translates to Integer[][]? Commented Nov 25, 2022 at 20:05

1 Answer 1

1

At least according to @broot, this is how Kotlin optimizes array types.

Kotlin Java
Array<Int> Integer[]
Array<Array<Int>> Integer[][]
IntArray int[]
Array<IntArray> int[][]

This would mean they're equally efficient. I currently have no official doc or source to back this claim up though.

Sign up to request clarification or add additional context in comments.

3 Comments

You got it right. Unfortunately, I don't have any official documentation that describes this in details, but it is partially explained here: kotlinlang.org/docs/java-interop.html#java-arrays Also, I can clearly verify this by looking into the resulting bytecode. Generally, you can think of Array as "array of objects" and IntArray as "array of primitives". Then you add dimensions again with Array<>.
Frankly, this confusion was not introduced by Kotlin, but long ago by the Java itself. This pattern for arrays in Kotlin isn't something new, it is sometimes used in other libraries/tools related to JVM as well. For example it is is used in... newer versions of Java. See Stream<Integer> and IntStream - looks very similar and it repesents a similar difference to Array<Int> vs IntArray.
In Java, an array is either an array of primitives or an array of objects. Arrays themselves are also objects. Therefore, a 2D array of primitives is literally an array of objects where the object type is an array of primitives. With that understanding, you can see that Kotlin isn’t just reinterpreting (or “optimizing”) the code in the left column of the table as the column on the right, but it is literally the same thing in Kotlin syntax. Nothing different happening under the hood between them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.