Home > Software design >  What field is the value of the int wrapped by an Integer Java object stored in?
What field is the value of the int wrapped by an Integer Java object stored in?

Time:12-22

I am trying to figure out where the value is stored in an Integer (ie what field is it in, or is there some other way it knows its value)? Unless its pertinent, I don't need to know 'how its stored in memory', I need to know how the Object knows what value it represents.

When I looked at the javadoc, I expected to see a field of Type int which stored the value as a primitive, but I don't see that.

The Javadoc says that

An object of type Integer contains a single field whose type is int.

But when I look at the fields, I see 5 static fields, and I think all of them store characteristics of the value, but not the value (BYTES, MAX_VALUE, MIN_VALUE, SIZE, TYPE).

First - this statement seems contradictory to me...there is more than a single field (there is 5)

Second - none of the fields 'store' the value...so how does the object know what value it is representing?

I am not a regular Java user, so be kind with your answers.

CodePudding user response:

The value is stored in a private final int value in OpenJDK; the actual naming and other considerations are implementation-defined and may vary.

Because it's private, it's not listed in javadocs unless the Javadoc tool was called with the -private parameter -- this was not done when the official API docs were generated from sources.

The public fields are static, so their values pertain to the class itself, not to instances. There's nothing unexpected here - MAX_VALUE is a reasonable immutable (final) thing to pertain to Integers.

CodePudding user response:

In the Integer class code there is a field

private final int value;

that holds the value as a primitive int. The code to retrieve the stored value is intValue() whose implementation is

public int intValue() {
    return value;
}

Properties MIN_VALUE, MAX_VALUE ... are static properties so there is no duplication between instances of the Integer class. There are also other interesting static properties in the Integer class. In particular there is a group of cached values between -128 and 127 that are stored in the static property private static Integer[] intCache.

That is why is better to use the method valueOf instead of creating explicitly a new Integer. The implementation of valueOf try to use the cached values when available with a code similar to the following:

public static Integer valueOf(int val) {
    if (val < MIN_CACHE || val > MAX_CACHE)
        return new Integer(val);
    synchronized (intCache) {
       if (intCache[val - MIN_CACHE] == null)
          intCache[val - MIN_CACHE] = new Integer(val);
       return intCache[val - MIN_CACHE];
    }
 }

Travelling around the source code of base classes of java gives you ideas on how to perform better your code, but you need to be sure to know details of java language so that code like the synchronized block before doesn't afraid you to read it.

  • Related