Java Wildcards:
The question mark (?) is known as the wildcard in generic programming . It represents an unknown type. The wildcard can be used in a variety of situations such as the type of a parameter, field, or local variable; sometimes as a return type. Unlike arrays, different instantiations of a generic type are not compatible with each other, not even explicitly.
Code:
The question mark (?) is known as the wildcard in generic programming . It represents an unknown type. The wildcard can be used in a variety of situations such as the type of a parameter, field, or local variable; sometimes as a return type. Unlike arrays, different instantiations of a generic type are not compatible with each other, not even explicitly.
Code:
- import java.util.List;
- import java.util.ArrayList;
- public class WildCardDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- List<Integer>integerList=new ArrayList<Integer>();
- integerList.add(6);
- integerList.add(3);
- print(integerList);
- List<String>sList=new ArrayList<String>();
- sList.add("A");
- sList.add("C");
- print(sList);
- }
- public static void print(List<?> list) {
- for(Object input:list){
- System.out.println("Input "+input);
- }
- }
- }
0 Comments:
Post a Comment