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):

publicvoid 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!


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:

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 aList<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.




Frank 發表在 痞客邦 PIXNET 留言(0) 人氣()