Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do autoboxing and unboxing behave differently in Java and C#

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.

like image 869
peter.murray.rust Avatar asked Oct 20 '09 20:10

peter.murray.rust


People also ask

Is Autoboxing and Boxing same in Java?

Boxing is the mechanism (ie, from int to Integer ); autoboxing is the feature of the compiler by which it generates boxing code for you. For instance, if you write in code: // list is a List<Integer> list.

What is the difference between Autoboxing and unboxing in Java?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

What are the advantages of Autoboxing and unboxing in Java?

Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique lets us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.

Who is responsible for performing Autoboxing and unboxing in Java?

Who is responsible for performing Autoboxing and unboxing in java? Java compiler is responsible for making such conversions. Before java 5 we used to write such code for performing Autoboxing.


1 Answers

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.

like image 112
Marc Gravell Avatar answered Oct 13 '22 11:10

Marc Gravell