Java Why Do Only Certain Integers Share Addresses in Java?

  • Thread starter Thread starter tmt1
  • Start date Start date
Click For 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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
21
Views
13K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
3
Views
3K
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
31K