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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top