• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question re parameterized types

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exercise 21.4 in Deitel JHTP8 is an assignment to alter a sort program to accept generic types. I've got the code to compile and run but had a question about a compiler warning:

Type safety: The constructor SelectionSort(ArrayList) belongs to the raw type SelectionSort. References to generic type SelectionSort<T> should be parameterized.


And here is the code in question:

I'm just getting started understanding generics. What should I do to get rid of these warnings?>
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SelectionSort is defined as accepting a type parameter that is passed
on to Comparable<T> and ArrayList<T>. But when SelectionSort
is initialized, where the errors appear, none is provided. Initialization
should look something like below, where CompClass extends Comparable.

Jim ... ...

 
richard rehl
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:SelectionSort is defined as accepting a type parameter that is passed
on to Comparable<T> and ArrayList<T>. But when SelectionSort
is initialized, where the errors appear, none is provided. Initialization
should look something like below, where CompClass extends Comparable.


OK, not understanding yet... Is it because ArrayList doesn't implement Comparable?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. It's because you don't use generics in those 2 lines of code
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To get rid of the warnings you can use @SuppressWarnings, but that's not really what you want, is it

Basically generics were invented to enforce compile-time type safety to collections where none previously existed. They're used for other things, but that's really the greatest benefit. In your example, you'll see integer/floatArray do not give warnings, as they are "parameterized". Type safety can be enforced - if you try to put any other object in there, it simply won't compile.

In your sortInteger/FloatArray, they're "raw", they aren't parameterized and the the type of objects they hold are not explicity stated at runtime. This means you can basically put anything in there (try it). The compiler can't stop you, it's only if you do something that causes a ClassCastException will anything appear wrong at runtime. However, you're in no worse a situation than before generics were introduced to the language.

Java 1.5 and later will give you this warning because it is now possible to mix the two, ie. raw and parameterized. This is dangerous because arguably you could refer to a collection using a raw type (and add any type of object), and also refer to that collection using a parameterized type (and try to take something out). You are no longer "protected" against class casting issues, even using the parameterized type, because the collection has been accessed using a raw reference and could hold anything.

So you get a warning. If you mix raw and parameterized and still put in the correct type of object, you're fine. But you no longer have the compile-type type-safety guarantee that generics is designed to give you, thus the warning. Only use parameterized types and the compiler won't complain.

iirc there's an example later in Deitel where it's done "right", and this is example is to illustrate the point, but I may be wrong.
 
richard rehl
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, especially Ben. I understood the issues with mixing generics with raw types but didn't get what exactly was the problem with this particular code. Onward!
 
reply
    Bookmark Topic Watch Topic
  • New Topic