aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Strike again !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "generics Strike again !!!" Watch "generics Strike again !!!" New topic
Author

generics Strike again !!!

Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102





The answer is error at line 2. WHy?
Doesn't the decalaration means that we can add any supertype of String class?

Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

No! The declaration means that the list is of type String or a super type of String. But you don't know what it is. So you can't add elements of super class of String to it. But since the type of the list is a super type of String, so one thing's for sure, we can add elements of type String to it as a String object will always be assignable to super type of String...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ok. Does that mean If its < ? super TYPE > , then only objects of type < TYPE > can be added? Nothing else?



Ankit Garg wrote:No! The declaration means that the list is of type String or a super type of String. But you don't know what it is. So you can't add elements of super class of String to it. But since the type of the list is a super type of String, so one thing's for sure, we can add elements of type String to it as a String object will always be assignable to super type of String...
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Prav sharma wrote:Ok. Does that mean If its < ? super TYPE > , then only objects of type < TYPE > can be added? Nothing else?


Both TYPE and sub-types of TYPE can be added to the collection. And Prav please don't use unnecessary formatting like colors or font-size ...
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Is it that way? Even sub - types of type <TYPE> ???

Ankit Garg wrote:
Prav sharma wrote:Ok. Does that mean If its < ? super TYPE > , then only objects of type < TYPE > can be added? Nothing else?


Both TYPE and sub-types of TYPE can be added to the collection. And Prav please don't use unnecessary formatting like colors or font-size ...

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes, subtypes of type also.


SCJP 6
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ok.
Then if we have <? extends TYPE> , we can't add anything.
Then whats the use of using this ? extends TYPE syntax.

Punit Singh wrote:Yes, subtypes of type also.

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
<? extends TYPE> and <? super TYPE> used on the RHS and used to restrict LHS list parameter type means <TYPE> on LHS side.
so
List<? extends TYPE> restricts LHS to be <subtype of TYPE or TYPE> means:
List<? extends Number> list=new ArrayList<Integer>();//ok
List<? extends Number> list=new ArrayList<Number>();//ok
List<? extends Number> list=new ArrayList<Double>();//ok
List<? extends Number> list=new ArrayList<Float>();//ok

List<? extends Number> list=new ArrayList<Integer>(); valid as Integer extends Number. Same way

so if we do list.add(new Double(10.0)); then think what will happen, List<? extends Number> allows ArrayList<Integer>().
so finally what happened:

new ArrayList<Integer>().add(new Double(10.0));//not valid

so compiler will make you safe by not allowing this type of gotchas.

But if you say
List<? super Number> list=new ArrayList<Number>(); // Number is super of Number
List<? super Number> list=new ArrayList<Object>(); // Object is super of Number
List<? super Number> list=new ArrayList<Integer>(); // Integer subtype of Number so not valid.

Now suppose
you have
List<? super Number> list=new ArrayList<Object>();
list.add(new Double(10.0));
means
new ArrayList<Object>().add(new Double(10.0));// valid no problem

Got ?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Prav sharma wrote:Ok.
Then if we have <? extends TYPE> , we can't add anything.
Then whats the use of using this ? extends TYPE syntax.

Punit Singh wrote:Yes, subtypes of type also.



The use comes where you want your user to read only the contents of different types of list, you do not want your user to add any content in your list.
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ok !!!

Means if i have lists ( each of different type and i want users to read them via a common function without able to add anything.

Great !!! Correct if wrong !!!

Punit Singh wrote:
Prav sharma wrote:Ok.
Then if we have <? extends TYPE> , we can't add anything.
Then whats the use of using this ? extends TYPE syntax.

Punit Singh wrote:Yes, subtypes of type also.



The use comes where you want your user to read only the contents of different types of list, you do not want your user to add any content in your list.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


This method will allow you to pass List<Number>, List<Integer>, List<Float>, List<Double>, List<Byte>, List<Short>, List<Long>, so multipurpose method.

Any doubt now?
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Got it.

Thanks alot !!!

Punit Singh wrote:

This method will allow you to pass List<Number>, List<Integer>, List<Float>, List<Double>, List<Byte>, List<Short>, List<Long>, so multipurpose method.

Any doubt now?
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: generics Strike again !!!
 
Similar Threads
String array to arraylist
Doubt about collection declaration
generics
in what ways can we change this formula..
Map within a List