Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This question is from javabeat
could anyone explain??
 
Andriy Pererva
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The warning is because raw List reference is referenced to parametrized LinkedList.
LinkedList parametrization means that you have an intent to explcitly restrict collection items to String type, but the raw reference spoils all the generic effort and allows to add any object (like Integer on line 7), despite of LinkedList parametrization
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the rule is:

Non Generic ref. type + Generic instant --> compiles w/o warning
Generic ref.type + Non Generic instant --> compiles with warning
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This line of code does not generate any compiler warning. How did you compile your code?? Did you use any IDE??
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
This line of code does not generate any compiler warning. How did you compile your code?? Did you use any IDE??


No i am trying in notepad and command prompt.
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandana..

i think you need to use Netbeans or other Java IDE..
it could help us to know the warning..
 
Dragos Nica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandana Garlapati wrote:
Ankit Garg wrote:
This line of code does not generate any compiler warning. How did you compile your code?? Did you use any IDE??


No i am trying in notepad and command prompt.


The warning will occur only if you are trying to add something.
Every time when you try to add something to a non parameterized collection , the warning will occur. As such a list can accept any type of object, this is the compiler's way to tell you that you must pay attention to what you are adding to the list.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic