What is boxing and unboxing and what are the trade

2019-01-01 10:04发布

I'm looking for a clear, concise and accurate answer.

Ideally as the actual answer, although links to good explanations welcome.

8条回答
余生无你
2楼-- · 2019-01-01 10:33

from C# 3.0 In a Nutshell:

Boxing is the act of casting a value type into a reference type:

int x = 9; 
object o = x; // boxing the int

unboxing is... the reverse:

// unboxing o
object o = 9; 
int x = (int)o; 
查看更多
后来的你喜欢了谁
3楼-- · 2019-01-01 10:34

Like anything else, autoboxing can be problematic if not used carefully. The classic is to end up with a NullPointerException and not be able to track it down. Even with a debugger. Try this:

public class TestAutoboxNPE
{
    public static void main(String[] args)
    {
        Integer i = null;

        // .. do some other stuff and forget to initialise i

        i = addOne(i);           // Whoa! NPE!
    }

    public static int addOne(int i)
    {
        return i + 1;
    }
}
查看更多
美炸的是我
4楼-- · 2019-01-01 10:35

The .NET FCL generic collections:

List<T>
Dictionary<TKey, UValue>
SortedDictionary<TKey, UValue>
Stack<T>
Queue<T>
LinkedList<T>

were all designed to overcome the performance issues of boxing and unboxing in previous collection implementations.

For more, see chapter 16, CLR via C# (2nd Edition).

查看更多
梦寄多情
5楼-- · 2019-01-01 10:40

Boxing & unboxing is the process of converting a primitive value into an object oriented wrapper class (boxing), or converting a value from an object oriented wrapper class back to the primitive value (unboxing).

For example, in java, you may need to convert an int value into an Integer (boxing) if you want to store it in a Collection because primitives can't be stored in a Collection, only objects. But when you want to get it back out of the Collection you may want to get the value as an int and not an Integer so you would unbox it.

Boxing and unboxing is not inherently bad, but it is a tradeoff. Depending on the language implementation, it can be slower and more memory intensive than just using primitives. However, it may also allow you to use higher level data structures and achieve greater flexibility in your code.

These days, it is most commonly discussed in the context of Java's (and other language's) "autoboxing/autounboxing" feature. Here is a java centric explanation of autoboxing.

查看更多
流年柔荑漫光年
6楼-- · 2019-01-01 10:41

Boxing and unboxing facilitates value types to be treated as objects. Boxing means converting a value to an instance of the object reference type. For example, Int is a class and int is a data type. Converting int to Int is an exemplification of boxing, whereas converting Int to int is unboxing. The concept helps in garbage collection, Unboxing, on the other hand, converts object type to value type.

int i=123;
object o=(object)i; //Boxing

o=123;
i=(int)o; //Unboxing.
查看更多
浮光初槿花落
7楼-- · 2019-01-01 10:45

In .Net:

Often you can't rely on what the type of variable a function will consume, so you need to use an object variable which extends from the lowest common denominator - in .Net this is object.

However object is a class and stores its contents as a reference.

List<int> notBoxed = new List<int> { 1, 2, 3 };
int i = notBoxed[1]; // this is the actual value

List<object> boxed = new List<object> { 1, 2, 3 };
int j = (int) boxed[1]; // this is an object that can be 'unboxed' to an int

While both these hold the same information the second list is larger and slower. Each value in the second list is actually a reference to an object that holds the int.

This is called boxed because the int is wrapped by the object. When its cast back the int is unboxed - converted back to it's value.

For value types (i.e. all structs) this is slow, and potentially uses a lot more space.

For reference types (i.e. all classes) this is far less of a problem, as they are stored as a reference anyway.

A further problem with a boxed value type is that it's not obvious that you're dealing with the box, rather than the value. When you compare two structs then you're comparing values, but when you compare two classes then (by default) you're comparing the reference - i.e. are these the same instance?

This can be confusing when dealing with boxed value types:

int a = 7;
int b = 7;

if(a == b) // Evaluates to true, because a and b have the same value

object c = (object) 7;
object d = (object) 7;

if(c == d) // Evaluates to false, because c and d are different instances

It's easy to work around:

if(c.Equals(d)) // Evaluates to true because it calls the underlying int's equals

if(((int) c) == ((int) d)) // Evaluates to true once the values are cast

However it is another thing to be careful of when dealing with boxed values.

查看更多
登录 后发表回答