Comparing boxed value types

If you need different behaviour when you’re dealing with a value-type then you’re obviously going to need to perform some kind of test. You don’t need an explicit check for boxed value-types, since all value-types will be boxed** due to the parameter being typed as object. This code should meet your stated criteria: If value … Read more

How large is the Integer cache?

Internal Java implementation and could not be configured, the range is from -128 to 127. You can check Javadocs or simply take a look at sources: public static Integer valueOf(int i) { final int offset = 128; if (i >= -128 && i <= 127) { // must cache return IntegerCache.cache[i + offset]; } return … Read more

Integer wrapper class and == operator – where is behavior specified? [duplicate]

Because of this code in Integer.valueOf(int): public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); } Explanation: Integer integer1 = 127 is a shortcut for Integer integer1 = Integer.valueOf(127), and for values between -128 and 127 (inclusive), the Integers are put in a … Read more

Why do we need boxing and unboxing in C#?

Why To have a unified type system and allow value types to have a completely different representation of their underlying data from the way that reference types represent their underlying data (e.g., an int is just a bucket of thirty-two bits which is completely different than a reference type). Think of it like this. You … Read more