41st: Careful use of heavy-duty

Source: Internet
Author: User

An attempt is made to categorize a collection based on its set, Liist, or other collection type:

 Public classCollectionclassifier { Public StaticString Classify (set<?>s) {return"Set"; }     Public StaticString Classify (list<?>l) {return"List"; }     Public StaticString Classify (collection<?>c) {return"Unknown Collection"; }         Public Static voidMain (string[] args) {Collection<?>[] Collections = {                NewHashset<string>(),                NewArraylist<biginteger>(),                NewHashmap<string, string>(). values ()};  for(collection<?>c:collections)        {System.out.println (classify (c)); }    }}

The expected print is Set,list,unknown Collection, but it is actually three Unknown Collection. The classify method is overloaded, and the overloaded method to invoke is determined at compile time, while the parameters of the three collection are collection<?> at compile time, although the runtime type is different, but does not affect the selection of overloaded methods. So the only suitable method is the one with the parameter collection<?>.

Unlike the Overwrite method, the selection of the override method is dynamic, and the selection is based on the type of runtime, regardless of the type at compile time.

Fix the scenario by replacing the overloaded classify method with a single method, that is, not using overloaded mechanisms:

 Public Static String classify (collection<?> c) {    returninstanceof Set? "Set" :            instanceof List? "List": Unknown Collection ";}

The ObjectOutputStream class, for each base type, and several reference types, has a different name for the write method, such as Writeint,writeboolean,writelong. This is a good practice.

After the release of Java 1.5, there are some problems caused by automatic boxing:

 Public classSetlist { Public Static voidMain (string[] args) {Set<Integer> set =NewTreeset<integer>(); List<Integer> list =NewArraylist<integer>();  for(inti =-3; I < 3; i++) {set.add (i);        List.add (i); }         for(inti = 0; I < 3; i++) {set.remove (i);        List.remove (i); } System.out.println (Set+ " " +list); }}

The result of the printing is:

The reason is that the list has the Remove (int) and remove (object) methods, and set has only one remove (object) method, and List.remove (i) calls the Remove (int) method, which results in this result.

Change to List.remove ((Integer) i), and the results will be consistent.

For multiple methods with the same number of parameters, you should try to avoid overloading methods, for the constructor, you can choose a static factory, to avoid the parameters can be passed through the type conversion to different overloaded methods, if not avoided, should be guaranteed to pass the same parameters, all overloaded method behavior consistent, Otherwise, the efficient use of overloaded methods by programmers can be difficult.

41st: Careful use of heavy-duty

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.