Views on java and C # generics

Source: Internet
Author: User
Tags addall
In the past, for a long time, such a point of view exists until now, that is, C # is more beautiful than Java implementation. Bruce Eckel, author of Think in java, once publicly questioned the generics provided by Java 5. But to be honest, I have never liked Bruce Eckel's book. I feel that he is not an experienced and in-depth technician.

I have come to agree with this idea for a long time, because the cloud is like the cloud!

C #2.0 supports generics and supports them at the virtual machine level. At the beginning, I felt very shocked. I felt that generics had moved into mainstream application development. Compared with C ++, it does not have the functions as powerful as the C ++ template, and does not have the effects of Generative programming or compilation computation. However, it is simple and practical.

Java 5 also began to support generics, and the final release was earlier than that of C #2.0. I have used the generics of Beta version C # before, I am also familiar with the C ++ template syntax, which may be arrogant or lazy. At first, I just started to use the generics provided by Java 5 based on the traditional experience.

It is always confusing to understand and understand things. When you read and analyze the JDK source code, you will always encounter some additional generic usage provided by Java 5, which is negligible at first, but you will always pay attention to it when you read more.

For example, the interfaces of the sort method and binarySearch method in the java. util. Collections class: Public static <T> void sort (List <T> list, Comparator <? Super T> c );

Public static <T> int binarySearch (List <? Extends Comparable <? Super T> list, T key );

The two keywords extends and super are none in the C # And C ++ generics. Why do we need such a function?

For example: Class {}

Class B extends {}

Void addAll (List <A> items ){}

The following code: List <A> aList =;

List <B> bList =;

AddAll (aList); // Yes

AddAll (bList); // compilation fails

AddAll (bList) cannot be compiled. This is true in Java, C #, and C ++. What should I do? Modify the addAll interface in java as follows: Void addAll (List <? Extends A> items ){}

In this way, both addAll (aList) and addAll (bList) can be compiled.

In addition, the key to super is better in algorithms, as shown in the Collections. sort method described above. If you want to implement the same method as java. util. Collections. sort in C #, you will find that it cannot be done!

Why does C # And C ++ fail to provide such functions? Because C # And C ++ are generic support during runtime, The bList and aList types are different. The actual types of List <A> and List <B> are different, currently, the support for generics during runtime cannot be as covariant as the handling of array parameters. The implementation of Java is the feature of the compiler. The disadvantage of this method is that the performance has not been improved, but it can provide better syntax sugar.

I think of ajoo's previous point of view, that is, in application development, the key to providing generic services is type security, and performance is the second. I agree with this. by reexamining java's generics, we will find that its design is innovative and backward compatible!

Summarize my points of view:
Java generics, with innovative syntaxes, better use, backward compatibility, and more convenient compilation of generic algorithms, without performance improvements.
C # Generic, innovative implementation, support at the virtual machine level, runtime support for generic, performance improvement, but it is difficult to write generic algorithms, not backward compatible.

Related Article

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.