空Java字符串的大小 [英] Size of empty Java String

查看:120
本文介绍了空Java字符串的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到一位同事说如果我在Java类中删除一个String成员,即使String为空,我也会支付24个字节。这是准确的吗? Integer,Float,Double是一样的吗? (而不是int,float,double,每个只有4,4和8个字节)。

I heard a colleague say that I would pay "24 bytes" if I dropped a String member in a Java class, even if the String is empty. Is that accurate? Is it the same for Integer, Float, Double? (as opposed to int, float, double, which would be only 4, 4 and 8 bytes each).

推荐答案

你' ll为引用支付4或8个字节。是否为容器对象的每个实例支付额外的对象取决于您获取空字符串的方式。例如,如果您使用文字然后所有,实例将引用同一个对象,因此您只需支付费用引用本身。

You'll pay 4 or 8 bytes for the reference. Whether you'll pay for an extra object per instance of your "container" object depends on how you get your empty string. For example, if you use the literal "" then all the instances will refer to the same object, so you'll only need to pay for the reference itself.

如果你为每个实例创建一个单独的空字符串,那么显然会占用更多内存。

If you're creating a separate empty string for each instance, then obviously that will take more memory.

这篇关于空Java字符串的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆