Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 712
rbrayb Avatar asked Feb 01 '09 21:02

rbrayb


People also ask

What is the difference between Autoboxing and unboxing in Java?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

Is Autoboxing and boxing same in Java?

Boxing is the mechanism (ie, from int to Integer ); autoboxing is the feature of the compiler by which it generates boxing code for you. For instance, if you write in code: // list is a List<Integer> list.

What is the major advantage of Autoboxing?

Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique lets us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.

What is casting in Java?

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.


2 Answers

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.

like image 165
cmsjr Avatar answered Sep 22 '22 01:09

cmsjr


Both casting and boxing/unboxing have to do with types and apparent (or real) conversion, but boxing/unboxing is specific to the relationship between primitive types and their corresponding wrapper types, while casting is the term for explicit or implicit change of type in the more general sense.

Casting is a general term with two related-but-different meanings:

  1. Treating a value of one type as if it were a value of another type. Two examples of this first usages are:

    1.1. Given that class B extends class A, you can ask for myB an instance of B to be treated as an instance of A by writing ((A) myB) wherever a reference to an instance of A could appear. This doesn't actually produce a new instance of A.

    1.2. Pre-Java5 collections stored all content as Object; this usually required you to use a cast after retrieving an object from a collection. For example, if you had stored a String in a Map and needed to get its length, you'd write something like ((String) myMap.get(someKey)).length() where the cast would be required in order to call the length method of String. Again, this doesn't cause a new String to be created.

  2. Explicitly converting one type to another (i.e. explicitly changing the representation). An example of this second usage is in the expression ((int) (float_var + 0.5F)) which rounds a floating-point variable by adding 0.5 (which produces a floating-point value) and then explicitly converting that value to an integer. The resulting integer value (after the (int) cast) is produced from the other value by internal computation.

Casting can be done when there's a superclass/subclass or interface/implementor relationship (meaning 1 above) or when the two types are primitive numeric types (meaning 2). You might look up "widening" and "narrowing" for more detail.

Boxing refers to wrapping primitive types in container objects, usually only done when you must have an object (e.g. storing a value in a collection). The primitive and wrapper types come in pairs:

int      Integer long     Long boolean  Boolean ...      ... 

Unboxing simply means retrieving the primitive value from within its object wrapper.

As of Java5, when you write an expression that uses a primitive value where the corresponding wrapper type would be required (such as putting an integer into a collection), the compiler automagically slips in the code that actually wraps that primitive value. Likewise it will provide the unwrapping code for you.

So instead of writing (in pre-Java5) something like:

Map myMap = new HashMap(); ... myMap.put(someKey,Integer.valueOf(3)); ... int nextValue = (myMap.get(someKey)).intValue() + 1; 

you can write:

Map<KeyType,Integer> myMap = new HashMap<KeyType,Integer>(); ... myMap.put(someKey,3); ... int nextValue = myMap.get(someKey) + 1; 

and the boxing/unboxing code is inserted by the compiler.

like image 35
joel.neely Avatar answered Sep 23 '22 01:09

joel.neely