Does autoboxing call valueOf()?

I first tought your question was a dupe of What code does the compiler generate for autoboxing? However, after your comment on @ElliottFrisch I realized it was different : I know the compiler behaves that way. I’m trying to figure out whether that behavior is guaranteed. For other readers, assume that “behaves that way” means … Read more

What is the difference between boxing/unboxing and type casting?

Boxing refers to a conversion of a non-nullable-value type into a reference type or the conversion of a value type to some interface that it implements (say int to IComparable<int>). Further, the conversion of an underlying value type to a nullable type is also a boxing conversion. (Caveat: Most discussions of this subject will ignore … Read more

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

Why comparing Integer with int can throw NullPointerException in Java?

The Short Answer The key point is this: == between two reference types is always reference comparison More often than not, e.g. with Integer and String, you’d want to use equals instead == between a reference type and a numeric primitive type is always numeric comparison The reference type will be subjected to unboxing conversion … Read more