简体   繁体   中英

Memory comparison of Scala's Array[Int] v/s int[] in Java?

Do they take the same amount of memory? Array is an abstract class so does it incur any object header cost? Is it same for other arrays of Java primitives in Scala?

PS: I read somewhere that Scala stores them as primitive arrays in JVM but now am confused.

Scala's Array[T] is exactly represented as Java's T[], there's no overhead. They generate the same bytecode. You additionally have the operations provided by ArrayOps, but it is an implicit conversion, which does not affect the pure Array[T] representation.

If you are not concerned about potential difference in several bytes (Scala Array vs Java array headers), they are roughly the same in terms of memory use, since Scala Int is represented as Java primitive int :

http://www.scala-lang.org/api/current/index.html#scala.Int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM