2

Let me explain the scenario.

If i am not wrong, Generics in java are only for compile time type safety. If i write

List<String> list = new LinkedList<String>();  // fine
list.add("test");  // fine

which is fine.But If i write

List list = new LinkedList<String>();  // fine
list.add(9);  // fine

Here i have created LinkedList of String and List variable is pointing to LinkedList. It is allowing to insert integer into list.

Now, Generics are related to types.Here Type is List and i have not written generics for that ie List<String> list So writing generics at right side while creating object and not writing at left side doesn't make List generic. So why compiler doesn't give warning ar exception at run/compile time for following statement

List list = new LinkedList<String>();

Is it useless to write such statement while using Generics in java.

Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89
  • 5
    The compiler does give a warning: _List is a raw type. References to generic type List should be parameterized_ – Keppil Dec 10 '13 at 09:12
  • possible duplicate of [Type safety with generics in Java](http://stackoverflow.com/questions/16689477/type-safety-with-generics-in-java) – Ivaylo Slavov Dec 10 '13 at 09:12

5 Answers5

2

You should not be instantiating the raw type List with the generic type List<T>. The compiler will give you a warning; however, this can be done due to Type Erasure.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
2

Actually, you will get a "rawtypes" warning if u write:

List list = new LinkedList<String>();
Merlin
  • 125
  • 7
  • oh might give warning.I am using netbeans s it was not giving me warning for that statement – Prashant Shilimkar Dec 10 '13 at 09:18
  • 1
    @PrashantShilimkar you need to enable warnings in the compiler settings – Sean Patrick Floyd Dec 10 '13 at 09:24
  • 1
    @PrashantShilimkar oh, sorry I'm not familiar with netbeans, I often use eclispe, but I think they both invoke javac to compile the code. So both of them should be able to display that message. Maybe there are some settings in netbeans to prohibit such warnings :) and I think Sean is right. – Merlin Dec 10 '13 at 09:25
2

The compiler will display these warnings if you turn compiler warnings on using either

-Xlint:rawtypes,unchecked

or

-Xlint:all

See the Non-Standard Options section of the javac docs

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

From JDK 1.5, Java introduced Generics, which are used to define the type of the contents of a Collection. If you initialize an ArrayList like this..

List list  = new ArrayList();

then the list can contain any type of data, It may be String, Integer or an Object. but if you want to define your List with a particular data type, than you can define it in angular brackets, like this..

List<String> list  = new ArrayList<String>();
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
earthmover
  • 4,395
  • 10
  • 43
  • 74
0

Yes, this will compile with raw type warning and you can insert Object of any type. It will also work on run time due to Type Erasure.

But this should be avoided since it doesn't make sense.

Jafar Ali
  • 1,084
  • 3
  • 16
  • 39