Wednesday 25 June 2014

What is Boxing and Unboxing? and explain in detail?

Boxing:
  • Boxing is the process of converting a variable type to reference type.
  • Supports 2 types
    • Implicit boxing
    • Explicit boxing
  • Boxing 20 times Costlier than normal initialization
Unboxing:
  • Unboxing is the process of converting a reference type to value type.
  • Supports only 1 type
    • Explicit Unboxing
  • Unboxing 4 times costlier than normal initialization
Example: Program for boxing and Unboxing:
Output:

  • Boxing is 20 times costlier than normal initialization, because whenever a boxing is done following tasks will be performed internally.
    • Runtime will search for the respective data with in the stack.
    • A copy of this value is made into heap.
    • Reference to this copy is maintained from the object variable.
  • Unboxing is 4 times costlier than normal initialization, because whenever a boxing is done following tasks will be performed internally.
    • Object referenced value is searched within the Heap.
    • A copy of this is made into stack.

No comments:

Post a Comment