Java wildcard and java wildcard

Source: Internet
Author: User

Java wildcard and java wildcard
Overview

It was easy to understand the wildcard content in java at the beginning, but when it comes to wildcards, I am dizzy, because I learned the guiding ideology of the java content, yes.. net, but the wildcard in java ,. net does not have such a concept, so I took the time to learn about it. The following example shows how to use wildcards.


Instance code

package com.tgb.mydemo;import java.util.ArrayList;import java.util.List;public class Test {public static void main(String[] args){Integer[] integers = {1,2,3,4,5};ArrayList<Integer> integerList = new ArrayList<Integer>();for (Integer integer : integers) {integerList.add(integer);}System.out.println("Total of the elements in integerList:" + sum(integerList));Double[] doubles = {1.1,2.2,3.3,4.4,5.5};ArrayList<Double> doubleList = new ArrayList<Double>();for (Double element : doubles) {doubleList.add(element);}System.out.println("Total of the elements in integerList:" + sum(doubleList));Number[] numbers = {1.1,2.2,3,4,5.5};ArrayList<Number> numberList = new ArrayList<Number>();for (Number element : numbers) {numberList.add(element);}System.out.println("Total of the elements in integerList:" + sum(numberList));}public static double sum(ArrayList<Number> list){double total = 0;for(Number element : list ){total += element.doubleValue();}return total;}}
Note: Number is the parent class of Double and Integer.

If you put the above Code in java IDE, a syntax error may occur. Why? Because the parameter type of the sum function is ArrayList <Number>, when we use the sum function, we pass it the ArrayList <Integer> or ArrayList <Double> type, although the Number type is a parent class of the Double and Integer types, the compiler does not treat the ArrayList <Number> and ArrayList <Integer> (ArrayList <Double>) as the same type, in this case, an error is reported during code editing. If the parameter type in sum is changed to ArrayList <? Extends Number> solves this problem, and "?" That is, the wildcard, which is why the wildcard appears.


Differences between wildcard and generic Constraints

The generic constraints mentioned here are generic constraints used to define generic classes. From this sentence, we will know that generic constraints are used to define generic classes, it is a constraint on Generics when defining generic classes. Wildcards are a constraint on types when using generic classes.


Wildcard constraint types

There are three types of wildcard constraints.

No constraints: <?>, That is, the type parent class must be of the Object type.

Upper Limit constraint: <? Extends type 1>, that is, the type must be a subclass of type 1 or type 1.

Deprecation constraint: <? Super type 1>, that is, the type must be the parent class of type 1 or type 1.


Summary

Wildcard makes it possible for us to use a generic class to give a specific type. By parameterization the specific type, the end user's range becomes smaller.

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.