Boxing

Today we are going to talking about a term known as Boxing. The first thing which would strike your mind once you hear this word is obviously the boxing game. But, what we are going to discuss today has got nothing to do with that.

So, Boxing in programming terminology means, converting a value type to a reference type. I had  explained what Value Type and Reference Type is in my previous blogs. The Value Type is stored in the stack portion of the memory. And once it is converted to a reference type, it is stored in the Heap Portion of the memory.

Let’s see how we can perform boxing on a Value Type. And let’s say we have a value type which is an Integer.

int i=25;

Now, in order to convert this Integer to a reference type, we would need an object.

object 0=i;

And now, the integer is wrapped in an object which can be similar to placing it in the Box and hence the name! This box is now placed in the heap portion of the memory.

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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