0

So i want to use Byte and Short to decrease memory usage, but some methods asks explicitly for Int.

private val numberOne : Byte = 50
someMethodAskingInt(numberOne.toInt())

If i convert a variable to Int with .toInt() will it get more space in memory? Will it even be worth it to use Byte or Short in those cases?

  • this answer here might help: https://stackoverflow.com/a/2346398/6892294 – DocJ457 Jun 11 '22 at 03:39
  • This may be relevant, but I don't know if Android works the same: https://stackoverflow.com/questions/14531235/in-java-is-it-more-efficient-to-use-byte-or-short-instead-of-int-and-float-inst – Slaw Jun 11 '22 at 03:53
  • Thank you both very much for the answers! – aweirdtrashcan Jun 11 '22 at 04:03
  • Just to clarify, the Q&A I linked to talks about the Java Virtual Machine (possibly the HotSpot implementation specifically). But Android uses the "Android Runtime" (not a JVM implementation), if I'm not mistaken, so that Q&A may not apply. – Slaw Jun 11 '22 at 04:19
  • 3
    In practice this won’t save you any memory. As far as I know, on most VMs bytes and shorts only save memory as part of ByteArrays, ShortArrays, ByteBuffers, and ShortBuffers. – Tenfour04 Jun 11 '22 at 04:29

0 Answers0