• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Static Final

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private static class IntegerCache {
private IntegerCache(){}

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}

In this piece of code, I am not able to

understand

this line
static final Integer cache[] = new Integer[-(-128) + 127 + 1];

can someone please elaborate this for me...what exactly we are trying to do here by creating this object instance of type Static and Final...if it is static why the heck it is declared final...

Any Explaination would be much appreciated
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madan since the array is final, you cannot assign another array object to it. Eg

final int[] arr = new int[10];
arr = new int[10]; //error

but you can change the value of individual elements of the array



In the statement that you gave,

-(-128) + 127 + 1

will evaluate to 256. So the statement is equivalent to

static final Integer cache[] = new Integer[256];

Now I think it will be clear for you...
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's trying to cache the Integer objects for int range of -128 to 127. It's done so that JVM can save bit of memory by caching the objects.

In the above two case, i1 and i2 will refer to same object in JVM as the int value is in between -128 to 127.
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The keyword final ensures that the reference to the Integer[] cannot point to another Object array. Note, that it doesn't stop you from changing the contents of the array.

The keyword static ensures that the reference is shared among all the instances.
 
Madan Mohan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much Ankit, Raj and Prateek...

that helps to the core....hats off to you all ..
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rajsekhar...
Is your quote correct?

Integer i1=122;
Integer i2=122;

if we metioned in the above format then jvm is going to create only one object which is referenced by i1 and i2 because the values are in the range -128 to 127 and it returns true for '==' .

whenever we are using new operator then it going to create different objects in heap...




[ December 10, 2008: Message edited by: Ganeshkumar cheekati ]
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the thing that JVM does in case of Wrapper classes, is to check for a wrapper object with the same primitive value before creating a new object in the heap for the following primitives-

Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah but i think that is applicable only for following format

Integer i1=122;
Integer i2=122;

not for

Integer i1=new Integer(122);
Integer i2=new Integer(122);

Am i right?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya you are right Ganesh...

Integer i=122;

means
you are calling:
Integer i=Integer.valueOf(122);

If you see source code of Integer class, you will find:

public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}



You can see this code taking Integer Object from the cache.

private final int value;

public Integer(int value) {
this.value = value;
}



And if you see constructor then it is setting value of final int;

I think you have got the whole concept.
[ December 10, 2008: Message edited by: Punit Singh ]
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ganeshkumar & Punit for catching that mistake.
 
reply
    Bookmark Topic Watch Topic
  • New Topic