Java Why Do Only Certain Integers Share Addresses in Java?

  • Thread starter Thread starter tmt1
  • Start date Start date
AI Thread Summary
In Java, Integer objects with values that fit within the range of a signed byte (not an unsigned byte, as Java does not support unsigned types) can share the same memory address due to the immutability of the Integer class. However, the discussion clarifies that two Integer objects with the same value do not automatically share the same address unless they are final. The JVM's current design is seen as efficient, as it minimizes memory allocation by allowing the storage of values like 10 in a single byte instead of four bytes if all integers shared addresses based on value. The suggestion to optimize further by having all Integers share addresses based on value is viewed as potentially detrimental to storage efficiency. Overall, the design choices made by Java engineers are considered effective, despite some inherent limitations.
tmt1
Messages
230
Reaction score
0
In java, 2 Integer objects with the same value share the same address if their values fit into the range of an unsigned byte.

My question is, why just Integers that fit into this range?

Wouldn't the jvm be optimized if all Integers shared the same address if they had the same value?

Thanks
 
Technology news on Phys.org
tmt said:
In java, 2 Integer objects with the same value share the same address if their values fit into the range of an unsigned byte.

My question is, why just Integers that fit into this range?

Wouldn't the jvm be optimized if all Integers shared the same address if they had the same value?

Thanks

Hi tmt,

That doesn't sound right.
I don't think two integer objects that happen to have the same value will share the same address, unless they are final.
On the other hand, if we have 2 integer objects, we can only assign one to the other if they have a matching type. Afterwards, they will share their address, and will obviously have the same value.
 
I wasn't aware that the jvm did this. I'll take your word for it; it seems very reasonable since the integer wrapper classes are final and immutable. My only quibble is that Java doesn't have an unsigned byte; all integer types are signed. So I assume you mean 8 bits.

It seems to me that your suggestion is much worse from a storage standpoint. For example:

Byte byteOb=new Byte((byte)10);
Integer intObj=new Integer(10);

If the jvm followed your suggestion, a total of 4 bytes would be allocated; whereas the actual jvm allocates just one byte.

Since all Java methods are "virtual" (C++ terminology), I can't see much problem with your suggestion with regard to methods, but I may be overlooking something.

Final comment. I am very confident that the Java engineers designed a very good language; of course there are bugs, but the basic design is excellent.
 
Last edited:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top