0

What is wildcard argument in generics

11th Aug 2019, 10:06 AM
sushmitha R
2 Respuestas
+ 10
wildcard (?) WildCard means any type for instance if you dont no what type of object user will enter from console and you dont know the return type of method than you can use wildcard here us little example import java.util.*; public class Program { public static void main(String[] args) { ArrayList<String> a =new ArrayList<>(); fun(); } static ArrayList<?> fun() { return new ArrayList<String>(); } }
11th Aug 2019, 2:09 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 2
//and you can specify general group of types import java.util.*; public class Program { public static void main(String[] args) { ArrayList<Integer> a =new ArrayList<Integer> (List.of(1,2,3,4) ); ArrayList<Double> b =new ArrayList<Double> (List.of(1.0,2.0,3.0,4.0,5.0) ); print(a); print(b); } static void print( ArrayList<? extends Number> arr) { // here int i=0; for(var n: arr) System.out.println("idx "+ i++ +": "+n); System.out.println(); } }
12th Aug 2019, 11:59 PM
zemiak