Let the Programming Begin: Generics with Java, Javadoc’s @version Tag.

This past week marked the start of my re-entry into the world of Java programming. My first assignment in CP213: Introduction to Object-Oriented Programming was due today. I learned a few neat things while doing this assignment so I’m going to try to outline some of them in this post.

  • Java has a very useful feature called Generics, and Eclipse will yell at you if you don’t use it.
    Generics allows you to define classes without a specified type. Instead, you can specify the type of an object by parameterizing it when it is initialized. This ensures that only objects of the specified type are inserted or removed from the initialized object. For this example I’ll use a stack:

    Stack<String> = new Stack<String>();

    As I’ve mentioned before, Eclipse is a very useful IDE, and thus is very strongly suggests parameterizing your stacks.

    Type Safety Warning

    Using code such as the above will yield the following:

    “Stack is a raw type. References to generic type Stack<E> should be parameterized”

    This can be easily fixed by parameterizing the code, as mentioned above.

    Parameterized

    Not only will Eclipse not yell at you for using the latter code versus the former, but the latter will help ensure that only the appropriate data is inserted and removed from the stack.

  • When using Javadoc, don’t use the @version tag.
    After struggling with Javadoc for a while trying to figure out why it wouldn’t properly display the version tag, just as a displays the author tag, I decided to do a little bit of googling on the subject. Eclipsepedia gave me a simple answer.

    @version Tag

    Using the @version tag sort of works, but it doesn’t look as nice as the @author, or any of the other tags.

    Javadoc with @version

    Does anyone know of any way to get this to work properly? It does make sense to have a version tag for classes, so I’m not sure why it doesn’t work properly.

That about wraps it up for the tips I learned from this assignment. As I hack around with Java more, I’m going to try to post various tips that I come across.

If anyone has any tips to add to the above, or information pertaining to the @version tag, please let me know.

5 comments ↓

#1 Phil Downey on 10.15.07 at 5:44 pm

Damn boy you change themes every week, even if you don’t update your site while you’re at it.

Tyler Jon and I also found this quirk but I don’t know if we ever really did anything to deal with it - I think we just stopped our bitching and focused on the code ;).

#2 Tyler Burton on 10.16.07 at 8:53 pm

Actually generics go well beyond this and actually become even more powerful.

For example you can use generics as part of a parameter:

static void doStuff(Stack stackVar) { [code] }

You can also use this to return a generic type only (might be useful in the case of an interface).

Another thing you can do is pre-define generics within an entire class:

public class Sample
{
private String data;
public Sample(String forData)
{
data = forData;
}
}

However the absolute best example of how powerful this can be is when you use it to specify a parameter in terms of what it extends like the following code:

public void fireWorks(LinkedList bigRocket) { [code] }

What this allows you to do is ensure that all elements in the LinkedList object extend that of paintableObjects (in this case). This is very useful if you do not know what type of object you may be receiving in the method but require them to have a particular method (say a print or paint). You may also use or just meaning any.

You can find a lot more of this on http://java.sun.com/developer/technicalArticles/J2SE/generics/

#3 Tyler Burton on 10.16.07 at 8:54 pm

yeah so it mangled my angled brackets into rounded ones…

#4 Tyler Burton on 10.16.07 at 8:54 pm

rather it just outright deleted them

#5 Dave on 10.16.07 at 9:46 pm

Yeah, I haven’t done a lot of work with Generics yet, but I hope to do some more work with it in the future. It seems like a very powerful, very useful feature.

This FAQ regarding Generics is also very useful:
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

Leave a Comment