Why Do Only Certain Integers Share Addresses in Java?

  • Context: Java 
  • Thread starter Thread starter tmt1
  • Start date Start date
Click For Summary
SUMMARY

In Java, Integer objects with values fitting within the range of a signed byte (from -128 to 127) share the same memory address due to the JVM's optimization for memory efficiency. This behavior is attributed to the immutability of Integer objects, which allows for this memory sharing. However, the JVM does not implement this optimization for all Integer values, as it would lead to increased memory allocation and complexity. The discussion highlights the importance of understanding Java's memory management and object handling.

PREREQUISITES
  • Understanding of Java Integer class and its immutability
  • Knowledge of JVM memory management
  • Familiarity with signed and unsigned data types in Java
  • Basic concepts of object references in Java
NEXT STEPS
  • Research Java Integer caching and memory optimization techniques
  • Learn about the implications of immutability in Java objects
  • Explore JVM internals related to object allocation and memory management
  • Investigate the differences between signed and unsigned data types in programming languages
USEFUL FOR

Java developers, software engineers, and computer science students interested in understanding Java's memory management and optimization strategies.

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:

Similar threads

Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · 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
4K
  • · Replies 20 ·
Replies
20
Views
31K