Joost Verbraeken
Joost Verbraeken

Reputation: 901

Is a Java array consisting of bytes/shorts instead of integers more memory efficient?

On many posts on Stackoverflow people said that using bytes or shorts instead of integers did not reduce the memory usage or CPU utilization. However, if I have an array of bytes or shorts, would that array use less memory and/or be faster to iterate over than a similar array of integers.

To be specific: I'm asking about the primitive type.

Upvotes: 1

Views: 269

Answers (1)

Arthas
Arthas

Reputation: 145

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

As mentioned in Java documentation byte/short can be used for saving memory in large arrays.

In case of byte, the variable declared as byte can also serve as a form of a documentation since its value is limited by the range. ex of byte (-128 to 127).

Also take a look at the excellent answers in the following post,

In java, is it more efficient to use byte or short instead of int and float instead of double?

Upvotes: 1

Related Questions