5

I'm new to Java but not to programming (I normally code in Ruby). One thing I've seen in Java code examples is the use of <> instead of () to pass params to an object. Below is a code example (taken from a Google Web Toolkit tutorial):

public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();

    // depending on the value of the token, do whatever you need
    ...
}

Does it have to do with casting or is it something else? Can someone explain to me what this signifies or is used for? Thanks!

Nicholas C
  • 1,103
  • 1
  • 7
  • 14

7 Answers7

4

That's a type parameter for a generic class. Basically, a ValueChangeEvent<String> represents an event that notified listeners that a value has changed, and in this case the value is of type String.

It's easier to understand with the standard example in the collections framework:

A List<String> is a List that contains String objects. The advantage over just using the "raw type" List is that the compiler will refuse code that puts anything except String objects into a List<String>, and allow objects retrieved from it to be used as String without casting.

Basically, generics allow cleaner code and improve the overall type safety of Java code by making it unnecessary in many cases to work with the Object type and cast values from it.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
3

It is called generics, or generic parameters. They allow a method or object to operate on multiple types of data while maintaining type safety. One common use is to specify what the type of the objects stored in a collection is.

See the wikipedia article on the subject for more information.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
  • ... but as wiki will tell you as well, generics in Java aren't quite as type safe because of the goddamn type erasure. –  Nov 27 '10 at 21:24
  • They offer much better type safety than pre-generics java where you had to return an `Object` and trust yourself to cast it properly, offering no compile time checking. Yes, type erasure sucks balls some times (especially with arrays), but it is *much* better than nothing. – Reese Moore Nov 27 '10 at 21:27
1

Reese has the right answer. Here are some links for you:

http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf (PDF)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
mchang
  • 681
  • 4
  • 4
1

The use of angle brackets usually signify generic programming methods, which is a style of programming where types are not declared initially, making code much more re-usable.

For example:

// Declare the generic class
public class GenericList<T>
{
    void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}
Greg
  • 21,235
  • 17
  • 84
  • 107
0

<> signify generics. Have a look at Generics. For instance, List<String> is a list of strings.

kraftan
  • 6,272
  • 2
  • 22
  • 24
0

This is known as generics in java. They are similar to templates in C++.

They basically let you implement a generic class for any type and then when you initialise a variable of this class you specify what type (e.g. String, int, double, etc.) it applies its stuff to. A good example is in an ArrayList, what type should the container store? The implementation is the same, only the type differs.

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Saying that they are similar to templates in C++ would be wrong IMO since templates fundamentally work at a very different level when compared to generics. Moar reading: http://stackoverflow.com/questions/36347/what-are-the-differences-between-generic-types-in-c-and-java/498329#498329 – Sanjay T. Sharma Nov 28 '10 at 14:05
0

It's usually used so you can trust that an element of a collection/list is a certain type.

For example,

ArrayList<String> a = new ArrayList();
a.add("Test");

myFunctionWhichAcceptsString(a.get(0));

If myFunctionWhichAcceptsString only accepted a String as the parameter, you wouldn't be able to use a normal ArrayList without casting the result as a String, since it stores an Object and Object ≠ String. By doing it this way you're saying that the list has to be only Strings. You can do this for any type of object.

Tom Marthenal
  • 3,066
  • 3
  • 32
  • 47