Java: What's the difference between autoboxing and casting?

rbrayb picture rbrayb · Feb 1, 2009 · Viewed 32.6k times · Source

This question is about "Why does autoboxing make some calls ambiguous in Java?"

But reading through the answers, there are a number of references to casting and I'm not sure I completely understand the difference.

Can someone provide a simple explanation?

Answer

cmsjr picture cmsjr · Feb 1, 2009

Boxing is when you convert a primitive type to a reference type, un-boxing is the reverse. Casting is when you want one type to be treated as another type, between primitive types and reference types this means an implicit or explicit boxing operation. Whether it needs to be explicit is a language feature.