Today I Learned

Some of the things I've learned every day since Oct 10, 2016

53: Boxing (Java)

In the Java programming language, boxing refers to the conversion of a primitive value to some equivalent object wrapping that value, which allows the object containing the value to be passed by reference. (There are probably other benefits/motivations that I’m unaware of.) The conversion back to a primitive value is called unboxing. When boxing and unboxing are done automatically by the Java compiler, it is referred to as autoboxing.

A common example is the autoboxing between \texttt{int}s (a primitive) and \texttt{Integer}s (a class). For instance, when

\texttt{Integer x = 1}

is executed, there is no error, even though \texttt{1} is an \texttt{int}. The Java compiler autoboxes the \texttt{int}   \texttt{1} into an equivalent  \texttt{Integer} object (\texttt{Integer(9)}) and binds \texttt{x} to that object.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: