Why Do Only Certain Integers Share Addresses in Java?

  • Java
  • Thread starter tmt1
  • Start date
In summary, in Java, two Integer objects with the same value will share the same address if their values fit into the range of an unsigned byte. This is due to the fact that the integer wrapper classes are final and immutable. However, implementing this for all integers would result in more storage being allocated, as seen in the example provided. The Java engineers have designed the language in a way that is efficient and well-optimized, making it unlikely that all integers would share the same address if they had the same value.
  • #1
tmt1
234
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
  • #2
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.
 
  • #3
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:

Related to Why Do Only Certain Integers Share Addresses in Java?

1. What are addresses of different objects?

Addresses of different objects refer to the specific location in memory where an object is stored. These addresses are used by the computer to access and manipulate the data stored in the object.

2. How are addresses of different objects assigned?

Addresses of different objects are assigned by the computer's memory management system. When an object is created, the system assigns a unique address to the object based on its size and the available space in memory.

3. Why are addresses of different objects important?

Addresses of different objects are important because they allow the computer to keep track of and access data stored in different objects. Without addresses, the computer would not know where to find the data for a specific object.

4. Can addresses of different objects change?

Yes, addresses of different objects can change. When an object is moved or deleted, the memory management system may reassign its address to another object. This is known as memory reclamation and is important for efficient memory usage.

5. How can addresses of different objects be accessed and manipulated?

Addresses of different objects can be accessed and manipulated through the use of pointers. Pointers are variables that store the memory address of an object, allowing the programmer to directly access and manipulate the data stored in that object.

Similar threads

  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
33
Views
8K
  • Programming and Computer Science
Replies
21
Views
12K
  • Precalculus Mathematics Homework Help
Replies
2
Views
381
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
40
Views
3K
  • Programming and Computer Science
7
Replies
235
Views
11K
Replies
3
Views
1K
Back
Top