- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Boxing and Unboxing in C# programming.
C# provides two methods to link value type to reference type and vice e Versa. These two methods for linking are named boxing and unboxing where Boxing is used for the conversion of the value type to an object type while Unboxing refers to the conversion of the object type to the value type.
The following are the important differences between Boxing and Unboxing.
Sr. No. | Key | Boxing | Unboxing |
---|---|---|---|
1 | Implementation | Boxing made object type referred to as the value type. | Unboxing basically processes the retrieving value from the boxed object. |
2 | Storage | In the case of boxing, the value stored on the stack is copied to the object stored on heap memory. | On the other hand in case of unboxing the object's value stored on the heap memory is copied to the value type stored on the stack. |
3 | Type of conversion | Boxing in general known as implicit conversion. | Unboxing refers to the explicit conversion. |
Example of Boxing vs Unboxing
JavaTester.java
public class JavaTester { public static void main(String[] args){ int val = 119; // Boxing Object o = val; // Change the value of val val = 120; //unboxing int x = (int)o; System.out.println("Value of x is {0}"+ x); System.out.println("Value type of val is {0}"+val); System.out.println("Object type of val is {0}"+o); } }
Output
Value of x is {0}119 Value type of val is {0}120 Object type of val is {0}119
Advertisements