Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Generics Generics !!!

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Which is valid and which is Invalid and why?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Both are valid. But both will generate a compiler warning...


I think second one will not generate compiler warning.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good one Ankit
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prav just see this code

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it

Thanks Guys !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic