Monday, December 21, 2009

Value Type vs. Reference Type + Boxing vs. Unboxing


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

I was initially thinking to discuss the compound types today, but have decided to briefly explain two important concepts that will be helpful to understand the building blocks and inner workings of compound types. Let's start with the first concept - value type vs. reference type.

Value Type: Variable of value type directly contains value. If you assign a variable of a value type to another, the data will be copied to the target variable. If you pass a variable by value to a method, changes made to the argument will not be changed since a copy of it will be passed to the function. All basic types except string (but it's also copied) discussed earlier are value types.

int a = 10;
int
 b = 20;

b = a; // value of a copied to b
b++; //increment b by 1

printf(" a = %d,\n b = %d", a,b);

Output:

a = 10;
b = 11;

Reference Type: Objects of this type hold a marker to the real data values. So assigning a variable of this type, points to the same data value. This type is more common in object oriented programming.

class A
{

private
:
    int
 m_value = 10;

public
:
   /* Return the value */

   int
 GetValue()
   {

      return
 m_value;
   }

   /* Set a new value */

   void
 SetValue(int value)
   {

      m_value = value;
   }
}


A* a = new A();
A* b = a; // b points to a

b->SetValue(11);

printf(" a = %d\n b = %d", a->GetValue(), b->GetValue);

delete
 a; // delete object, only once

Output:

a = 11;
b = 11;


Value types are stored on the stack and Reference types are stored on the managed heap.

Another important concept very important in managed environment like Java and C# is Boxing and Unboxing. Each value type in Java and C# has a corresponding reference type. Boxing/Unboxing deals with the conversion of a value type to its corresponding reference type and vice versa.

Boxing:
Boxing is the process of converting a value type (e.g. primitive type) to a reference type. Java has reference types for its primitive value types. Conversion from value type of the corresponding reference type is automatic. C# primitive types are converted to the corresponding .Net types as listed in Basic/Primitive Type post.

Unboxing:Unboxing is, as expected, opposite to boxing. It converts a reference type to the corresponding value type. In both C# and Java, the conversion is implicit, therefore, doesn't need explicit casting.

Java
/* Boxing Example */

int
 i = 10;

Object obj = (Object) i; //explicit boxing

Integer intObj = (Integer) i; // explicit boxing

Object obj2 = i; //implicit boxing

Integer intObj2 = i; //implicit boxing


/* Unboxing Example */


int
 i = 10;
Object obj = i; //boxing

/* Following will not compile. 
No explicit conversion from object to int 
*/
/*int j = (int) obj;*/


Integer intObj2 = i; //boxing
j = intObj2.intValue(); //unboxing
j = intObj2; //auto unboxing
C#
/* Boxing Example */

int
 i = 10;

object
 obj = (object) i; //explicit boxing

Int32 intObj = (Int32) i; // explicit boxing

object
 obj2 = i; //implicit boxing

Int32 intObj2 = i; //implicit boxing



/* Unboxing Example */


int
 i = 10;
object
 obj = i; //boxing
int j = (int) obj; //unboxing

Int32 intObj2 = i; //boxing
j = (int) intObj2; //unboxing


References: The Frequently Asked Questions (faq) series on various programming languages are great resources to deepen knowledge in the respective languages.

Programming Language FAQC++ FAQs (2nd Edition)The Java Faq (Java Series)

No comments:

Post a Comment