aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Generics Generics !!! 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 "Generics Generics Generics !!!" Watch "Generics Generics Generics !!!" New topic
Author

Generics Generics Generics !!!

Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102


Which is valid and which is Invalid and why?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Both are valid. But both will generate a compiler warning. The second one will generate a compilation warning if you try to modify the collection...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankit Garg wrote:Both are valid. But both will generate a compiler warning...


I think second one will not generate compiler warning.


SCJP 6
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Could you please confirm whether 2nd one will generate warning or not?
First one generates.

P.S: I don't have JDK access.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Punit Singh wrote:
Ankit Garg wrote:Both are valid. But both will generate a compiler warning...


I think second one will not generate compiler warning.


I thought I would correct that before anyone sees it ...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Good one Ankit
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Prav just see this code

Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ok

Means 2nd line will generate warning only if we try to add something to it.

If we don't have add for second list then we won't get any warning???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Can you think Prav why there is compiler warning in second line add() and why not in first line add() method?
And also

List<String> list1 = new ArrayList(); //warning

why this compiles with warning, while this below one not?

List list2 = new ArrayList<String>();
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Is it because that the first declaration tells clearly that Strings can be added Whereas second list is not specifying anything ?

If its like that then what does the RHS of second declaration conveys? Doesn't it serve any purpose?

Punit Singh wrote:Can you think Prav why there is compiler warning in second line add() and why not in first line add() method?
And also

List<String> list1 = new ArrayList(); //warning

why this compiles with warning, while this below one not?

List list2 = new ArrayList<String>();
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Prav wrote:If its like that then what does the RHS of second declaration conveys? Doesn't it serve any purpose?


It is serving its purpose na, but in this case it is not deciding what can be added.

Reference type always restricts what you can assign to this reference type, and what can you add if reference type is collection, or what method can be called. This is the power of reference type, RHS is a small child against the power of LHS, LHS says what is possible and what is not always.

List<String> list1 = new ArrayList(); //warning

Here also its reference type is scolding RHS why it is assigning such an ArrayList(), that has no parameter restriction.
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Got it

Thanks Guys !!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Generics Generics Generics !!!