加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 字节数据类型如何用于在大型数组中保存内存

发布时间:2020-12-15 04:10:23 所属栏目:Java 来源:网络整理
导读:The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays,where the memory savings actually ma

The byte data type is an 8-bit signed two’s complement integer. It has
a minimum value of -128 and a maximum value of 127 (inclusive). The
byte data type can be useful for saving memory in large arrays,where
the memory savings actually matters. They can also be used in place of
int where their limits help to clarify your code; the fact that a
variable’s range is limited can serve as a form of documentation.

我在java文档中看到了这一点.我的问题是字节数??据类型如何将内存保存在大型数组中?.我对此感到困惑

提前致谢….

解决方法

它的含义非常简单.

假设我有40个“数字”来存储.如果我将它们存储在以下内容中:

byte[] numbers = new byte[40];

如果我将它们存储在下面,它将占用更少的空间:

int[] numbers = new int[40];

为什么?因为在一个数组中,40个字节的实例占用40个字节的内存,但40个int实例占用40 x 4 = 160个字节的内存.

注意事项:

>显然,这仅在数字足够小以表示为字节时才有效…没有溢出;即它们必须在-128到127的范围内>这不适用于简单变量.在Java中,字节变量和int变量通常每个占用4个字节. (这是一个低级JVM的东西,需要很多解释……)>我正在掩盖堆内存可能以比4字节更粗糙的粒度分配的事实.分配粒度通常为8个字节.但是,对于大型阵列,分配粒度的贡献可以忽略不计.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读