Saturday, 15 August 2015

java - What is the need for WildCard when Object type is available? -



java - What is the need for WildCard when Object type is available? -

what need wildcard when object can used parameter in function definition can take objects when compared unbounded type..

package wildcards; import java.util.arraylist; import java.util.list; public class needforwildcard { public void processinput(list<string> values){ //insted of list<string> have used list<object> , need wildcard in here? for(string valueextractor:values){ system.out.println("values="+valueextractor); } } public static void main(string[] args) { arraylist<integer> valuesinteger=new arraylist<>(); valuesinteger.add(100); valuesinteger.add(200); valuesinteger.add(300); needforwildcard example=new needforwildcard(); example.processinput(valuesinteger);//valuesinteger arguments since used in method call. } }

let's have method next (with wildcard argument object list)

public void getlist(list<object> list){ ... }

and in different method following

list<string> strlist = new arraylist<string>(); getlist(strlist);

the sec line through compilation error

the method getlist(list<object>) in type testclass not applicable arguments (list<string>)

you can't reference object different object.

java generics wildcard

No comments:

Post a Comment