Nicholas C
Nicholas C

Reputation: 1113

What does Object<String> signify in Java?

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!

Upvotes: 5

Views: 802

Answers (7)

Tom Marthenal
Tom Marthenal

Reputation: 3146

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.

Upvotes: 0

mchang
mchang

Reputation: 681

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)

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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.

Upvotes: 4

Greg
Greg

Reputation: 21899

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>();
    }
}

Upvotes: 1

moinudin
moinudin

Reputation: 138347

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.

Upvotes: 0

kraftan
kraftan

Reputation: 6312

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

Upvotes: 0

Reese Moore
Reese Moore

Reputation: 11640

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.

Upvotes: 3

Related Questions