Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java generics doubt from scjp kathy bert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java generics doubt from scjp kathy bert" Watch "java generics doubt from scjp kathy bert" New topic
Author

java generics doubt from scjp kathy bert

shaji kumar
Greenhorn

Joined: Aug 14, 2012
Posts: 6
Which of them are legal declarations ?

1) List<?> list=new ArrayList<Dog>();
2) List<? extends Animal> list=new ArrayList<Dog>();
3) List<?> foo=new ArrayList<? extends Animal>();
4) List<? extends Dog>clist= new ArrayList<Integer>();
5) List<? super Dog>blist=new ArrayList<Animal>();
6) List<? super Animal>dlist=new ArrayList<Dog>();

correct answers are 1 , 2 and 5 , can someone tell what is the use of the correct answers , like where do v apply it .Thanks in advance
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

shaji kumar wrote:Which of them are legal declarations ?

1) List<?> list=new ArrayList<Dog>();
2) List<? extends Animal> list=new ArrayList<Dog>();
3) List<?> foo=new ArrayList<? extends Animal>();
4) List<? extends Dog>clist= new ArrayList<Integer>();
5) List<? super Dog>blist=new ArrayList<Animal>();
6) List<? super Animal>dlist=new ArrayList<Dog>();

correct answers are 1 , 2 and 5 , can someone tell what is the use of the correct answers , like where do v apply it .Thanks in advance



By using List<?> we are actually telling the compiler that on the RHS, we can use any subtype of List as type and in generic argument we can use anything.....

But in this declaration, you can actually not modify the list... Compiler error will be generated....



By using List<? extends Animal> we are telling the compiler that on the RHS, we can use any subtype of List as type and in generic argument we can use anything that is either Animal or extends Animal

In this case also you cannot modify the list....



By using List<? super Dog> we are telling the compiler that on the RHS, we can use any subtype of List as type and in generic argument we can use anything that is either a [b]Dog[b] is a super class of Dog....
In this case, however, you can modify the list....

All the three declarations are syntactically correct....


Remember, we cannot use <?> on the right hand side of the declarations... Because, at runtime, we must have the exact type that can be added to the list...
One more thing, Generic Type info is not available at runtime... Compiler removes it and adds neccessary typecasting needed for JVM to run the code....
shaji kumar
Greenhorn

Joined: Aug 14, 2012
Posts: 6
Thanks a lot rohit jain for your valuable time for clearing my query , i will be really happy if you can provide some small examples for each . Thanking you once again!!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

R. Jain wrote:

By using List<?> we are actually telling the compiler that on the RHS, we can use any subtype of List as type and in generic argument we can use anything.....

But in this declaration, you can actually not modify the list... Compiler error will be generated....


While the rules specified are technically correct, it doesn't really explain why? In fact, based on the explanation, it seems almost arbitrary....


A better way to state it is... you are declaring a variable named list, and the type of the variable is of a List of some generic type, but the compiler does not know what the generic type is. It doesn't mean that the list can hold anything. It means that whatever that generic type is, the compiler doesn't know what it is.

The reason that you can't add an object is because the compiler doesn't know what generic type is legal to add. However, you can add null as that will satisfy any type. And you can remove any object already in the list.... so you can modify the list.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
shaji kumar
Greenhorn

Joined: Aug 14, 2012
Posts: 6
Thanks a lot henry , can you please explain the below scenario . In the first scenario if i can declare anything which extends animal , there is no compile error but in second scenario if i specifically say Animal i am getting
compile error , i am really not able to understand the concept , in one place it says keep the generic type reference and generic type of the object identical (page no 608 scjp kathy ) but in the first scenario as it is varying still no compile error !!! .

1)List<? extends Animal> list=new ArrayList<Dog>(); //no compiler error


2) List<Animal> list=new ArrayList<Dog>(); //compile error
shaileshkumar mistry
Greenhorn

Joined: Dec 19, 2011
Posts: 19
Hi Shaji

List<Animal> list = new ArrayList<Dog> will give compile error . The sun developers white implementing generics faced so many problems.

if it compiles thought it will not if it compiles the List<Animal> holds the list<Dog> so at runtime you might try to add some other objects who extends animal. so, the problem is you have List<Dog> and you are inserting
some other objects extends Animal is not correct. if it is List<Animal> list= new ArrayList<Animal> the compilation will be fine because at compile time you can know that the object that you will be inserting in to the
List<Animal> will be either Animal object or the object of class which extends Animal

List<? extends Animal> list = new ArrayList<Dog> it is one kind of solution of above problem but though it is just to access the elements from the list not to modify to the list . if you have declare the list like this you can have the
reference of animallist who extends Animal class but compile time you try to insert the element to list compiler will give you error.

Thanks and Regards

OCPJP 100%
Varun Selva
Ranch Hand

Joined: Jun 17, 2012
Posts: 44

shaji kumar wrote:Which of them are legal declarations ?

1) List<?> list=new ArrayList<Dog>();
2) List<? extends Animal> list=new ArrayList<Dog>();
3) List<?> foo=new ArrayList<? extends Animal>();
4) List<? extends Dog>clist= new ArrayList<Integer>();
5) List<? super Dog>blist=new ArrayList<Animal>();
6) List<? super Animal>dlist=new ArrayList<Dog>();

correct answers are 1 , 2 and 5 , can someone tell what is the use of the correct answers , like where do v apply it .Thanks in advance


We can use this in run time requirement!


Varun Selva
shaji kumar
Greenhorn

Joined: Aug 14, 2012
Posts: 6
Thank you all ,

Is there any practical implementation of these declarations or they are just as a valid declaration for the compiler, can we use these declarations in any scenarios .As per my understanding
we cant insert anything except null so in that case we can retrieve only null , so what is the use , if there is any use above my understanding please help me ,Thanks in advance !!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

shaji kumar wrote: if i specifically say Animal i am getting compile error , i am really not able to understand the concept , in one place it says keep the generic type reference and generic type of the object identical (page no 608 scjp kathy ) but in the first scenario as it is varying still no compile error !!! .

2) List<Animal> list=new ArrayList<Dog>(); //compile error


Well, first of all, you need to understand why you can *not* assign a list of dog to a list of animal. Don't even worry about wildcards yet. You need to understand the simplier assignments first.

As for why, this example isn't a good one.... it is better to expand your example...



If the compiler allowed a list of dog instance to be assigned to a list of animal reference, as in line 3, then notice, that in line 4, dlist is broken. In line 4, you are able to add an instance into the list, referred to by the dlist reference, that is *not* a dog. In other words, the list and dlist references point to the same list object, and the list reference uses it in a way that breaks dlist.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

shaji kumar wrote:
Is there any practical implementation of these declarations or they are just as a valid declaration for the compiler, can we use these declarations in any scenarios .As per my understanding
we cant insert anything except null so in that case we can retrieve only null , so what is the use , if there is any use above my understanding please help me ,Thanks in advance !!!


Well, as you already figured out, this are just silly examples. They are done this way so that you can understand assignments of them.



After all, why would you declare a list of a specific type, and within the same line, tell your compiler to forget its type?

The more likely use case is an assignment via a method call parameter, like this....




which you can use like this....



without wildcard capability, you would probably have to write a version of the method for each list type..... or don't use generics for the method, or cast the list -- both of which will break the type safety of the list.

Henry
shaji kumar
Greenhorn

Joined: Aug 14, 2012
Posts: 6
Thanks a lot to all of you for your valuable time and effort to make me understand the concept very well .
 
GeeCON Prague 2014
 
subject: java generics doubt from scjp kathy bert