Why does Java not support creating fan arrays?

Source: Internet
Author: User

Http://www.blogjava.net/sean/archive/2005/08/09/9630.html

Recently I saw someone on blogjava discussing why the Java paradigm does not support array http://www.blogjava.net/myqiao/archive/2005/08/08/9580.html. I think the answer to this question is: because doing so will undermine type security. The core problem lies in the fundamental difference between Java and C #: the Java model stays at the compilation level. At runtime, the information of these dimensions is actually erased, while the C # model implements the msil layer. Java does not need to modify the JVM, which reduces the potential for significant changes and the risks that come with it. It may also reflect the inherent limitations of the Java bytecode specification at the beginning of the design. C # is a big deal, the CLR can be modified together to support a more thorough model. In other words, C # is more C ++. In Java, the object [] array can be the parent class of any array, or any array can be transformed up to an array of the parent class of the specified Element type during definition, at this time, if we put a different data type than the original data type but the parent class type is used later, the compilation will not be a problem, but the runtime will check the type of the object to be added to the array, therefore, arraystoreexception is thrown: String [] strarray = NewString [20]; Object [] objarray = strarray; Objarray [0] = NewINTEGER (1 );// Throws arraystoreexception at runtime Because the Java model will erase the type information after compilation, So If Java allows us to use similar Map <integer, string> [] maparray = NewMap <integer, string> [20]; For such a statement Code You can convert it to object [] and then put the map <double, string> instance in it. In this way, not only can the compiler not find a type error, but the Array Storage check in the runtime cannot do anything about it. What it can see is the object we put in the map, which we define <integer, string> it has been erased at this time, so for it, as long as it is map, it is legal. Think about it. We defined an array containing Map <integer, string>, but we can put any map in it. If there is code that tries to set the value according to the original definition, the consequences are self-evident. Therefore, the Java compiler does not allow us to use new arrays.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.