首页 \ 问答 \ Java短常量池与短基元(Java Short constant pool versus short primitives)

Java短常量池与短基元(Java Short constant pool versus short primitives)

我的解决方案需要许多常量变量,因此在进一步开发中,我可以简单地创建新原语或引用现有数据,而不是创建新数据,从而排除在未来开发过程中可能出现的错误。

我已经读过java池常量变量,当创建新数据时它会与池进行比较,如果存在这样的对象,它会返回对现有对象的引用,而不是创建新的对象。

虽然池化可能听起来是最好的方法,但就我而言,我需要很多变量,每个变量分配2个字节(对于基元)。 但是如果我创建Short我会松开2个字节,因为引用需要4个字节。

即使考虑到Short使用池化,仍然使用原语是否有意义。 另外,从短到短的拆箱也需要一些资源(几乎接近于零,但仍然如此)。 注意short必须不时地转换为原始的3字节数组,所以另一个+用于原语。

public static final short USER = 10;

代替

public static final Short USER = 10;

My solution requires many constant variables, so in further development i could simply create new primitives or reference existing data, instead of creating new one, that excludes possible mistakes made in future development process.

I've read that java pools constant variables, when new data is created it compares with the pool, if such object exists it returns reference to existing one instead of creating new one.

While pooling might sound best approach, in my case, i need many short variables, which allocates 2 bytes (for a primitive) each. But if I'd create Short i would loose 2 byte, because reference would take 4 bytes.

Would it make sense to still use primitives even considering that Short uses pooling. Plus, unboxing from Short to short also takes some resources (which are almost close to zero, but still). Note that short will have to converted to primitive 3 byte array time to time, so another + for primitive.

public static final short USER = 10;

instead of

public static final Short USER = 10;

原文:https://stackoverflow.com/questions/31921860
更新时间:2023-06-04 16:06

最满意答案

position() 。 例如:

<countNo><xsl:value-of select="position()" /></countNo>

position(). E.G.:

<countNo><xsl:value-of select="position()" /></countNo>

相关问答

更多