java - Why do some languages need Boxing and Unboxing? -


This is not a question of boxing and unblocking, rather it is like why languages ​​like Java and C # is required?

I am very familiar wtih C ++, STL and Boost.

I can write it very easily in C ++,

  std :: vector & lt; Double & gt; Dummy;  

I have some experience with Java, but I was surprised because I had to write something like this,

  ArrayList < Double & gt; Dummy = New Arrestist & lt; Double & gt; ();  

My question, why should it be an object, while talking about generic, it is very difficult to include primitive types?

In the case of Java, the reason for this is that in a generic way, Java is a compile-time move, which will give you the image Code> Aranyst & lt; String & gt; . However, Java's generics are implemented with type extinction: during normal run-time the information is lost, it was for compatibility reasons, because the generics were added for a long time in the life of Java, because of its Meaning, run-time, a ArrayList & lt; String & gt; effectively an ArrayList & lt; Object & gt; (or better: Just ArrayList returns the object in all of its methods) that when you retrieve a value, the string is entered

but since int does not get from object , you can not put it in an arrelist which you hope (At runtime) Object and you can not enter any object to int which means that the primitive int Need to get wrapped up in kind that is the object , such as Integer from.

For example, C # works differently, Generic C # also applies to runtime and list & lt; Int & gt; No boxing is required with . Boxing in C # only occurs when you try to store a value type in a variable type such as object such as int . Since int from c object to c #, written object obj = 2 is fully valid, although int will be boxed, which is done Automatically by the compiler (no integer reference type is user or nothing).


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -