File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Are Generics worth the trouble? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Are Generics worth the trouble?" Watch "Are Generics worth the trouble?" New topic
Author

Are Generics worth the trouble?

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I just upgraded my WDSC and it now uses java 5.0. This is cool, except now I'm getting all these warnings pertaining to generics. Originally I didn't realize that this update was going to upgrade the compiler (guess I should have read the docs first). Since I was working in the earlier version of java I hadn't read up on the new version.

So after looking around the boards for a bit and seeing the issues that people are having with this I come to the question...

Is it really worth worrying about? My code is going to run the same as it did before if I don't go through and make a bunch of these generics in my eleventy thousand classes. Is it worth fixing up? What are the pros/cons of just suppressing the warnings and forgetting about it for now and just try to do it on new code?

Thanks guys.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30909
    
158

Bryce,
Congratulations on being on Java 5. There are some cool features and productivity savings available to you now . You'll get used to them as you go.

It's definitely not worth changing all the legacy code to use generics! This is the kind of thing that you want to use for new code (or if you are significantly updating a class.) I can think of a few exceptions to this of course. If you have a common library, it could be worth changing its API so new Java 5 callers can use it more safely.

And sometimes there are times where Java 5 can make an API more powerful. We have on API that currently is implemented as:
public void myMethod(String arg1) {}
public void myMethod(String arg1, String arg2) {}
...
public void myMethod(String arg1, String arg2, ... String arg8) {}
public void myMethod(String[] args) {}
All of the earlier methods delegate to the final one and we have a TODO to remove the interim methods when we migrate to Java 5. This is a case where the API was designed with Java 5 in mind even though we aren't there yet.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
there is likely a compiler switch to stop warning you about the generics mismatches, if you're worried about it,

i'd definitely stay with Java5 !


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39781
    
  28
Originally posted by Bill Shirley:
there is likely a compiler switch to stop warning you about the generics mismatches, if you're worried about it,

i'd definitely stay with Java5 !
Yes, there is, adding @SuppressWarnings("Unchecked") as an annotation (or something similar). But it is probably easier to ignore the warnings.

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In my experience, it's worth fixing the warnings while working on the code. I wouldn't go and fix them for code that I'm not going to touch right now otherwise.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Generics worth the trouble?