Do autoboxing and unboxing behave differently in Java and C#
Asked Answered
A

2

10

I am manually converting code from Java (1.6) to C# and finding some difficulty with the behaviour of primitives (int and double). In C# it appears that almost all conversions happen automatically

        List<double> list1 = new List<double>();  // legal, C#
        double d0 = 3.0;
        list1.Add(d0);                            // legal, C#
        Double dd = 2.3f;                         // legal, C#
        list1.Add(dd);                            // legal, C#
        List<Double> list2 = new List<Double>();  // legal, C#
        double d1 = 3.0;
        list2.Add(d1);                            // legal, C#
        list2.Add(2.0);                           // legal, C#
        double d2 = list2.get(0);                 // legal, C#

but in Java only some are allowed

        List<double> list1 = new ArrayList<double>();  // illegal, Java
        List<Double> list2 = new ArrayList<Double>();  // legal, Java
        double d1 = 3.0;
        list2.add(d1);                                 // legal, Java
        list2.add(2.0);                                // legal, Java
        double d2 = list2.get(0);                      // legal, Java

I'd be grateful for a systematic analysis of the differences and any underlying rationale.

Attainable answered 20/10, 2009 at 20:38 Comment(0)
A
10

In your C# example there is no boxing or unboxing (and autoboxing) happening. double is just an alias for the struct Double.

In Java, the boxing is necessary. Because of type erasure, you can't create a List<double>, only List<Double>. At compile time, List<?> becomes List<Object> and boxing/unboxing will need to take place so you can add a native type variable to a List<Object> or assign a native variable to an element of the List.

Alodee answered 20/10, 2009 at 20:50 Comment(2)
I was under the impression that List<Double> existed long before Type Erasure in Java, or am I misunderstanding what you're saying?Hoshi
@RHSeeger, List<T> is a generic Type. Generics in Java where/are implemented with Type Erasure. So this implies that List<Double> didn't existed before Type Erasure (at least with the Type parameters). Notice that, List<T> will be translated to the raw type List.Alodee
F
20

In C#, double and Double are exactly the same thing (as long as you haven't created your own type called Double, which would be stupid).

double is defined as an alias to global::System.Double. As such, there is no boxing here.

In java, Double is a boxed double, with type-erasure being a key part of the generics implementation.

Filip answered 20/10, 2009 at 20:42 Comment(0)
A
10

In your C# example there is no boxing or unboxing (and autoboxing) happening. double is just an alias for the struct Double.

In Java, the boxing is necessary. Because of type erasure, you can't create a List<double>, only List<Double>. At compile time, List<?> becomes List<Object> and boxing/unboxing will need to take place so you can add a native type variable to a List<Object> or assign a native variable to an element of the List.

Alodee answered 20/10, 2009 at 20:50 Comment(2)
I was under the impression that List<Double> existed long before Type Erasure in Java, or am I misunderstanding what you're saying?Hoshi
@RHSeeger, List<T> is a generic Type. Generics in Java where/are implemented with Type Erasure. So this implies that List<Double> didn't existed before Type Erasure (at least with the Type parameters). Notice that, List<T> will be translated to the raw type List.Alodee

© 2022 - 2024 — McMap. All rights reserved.