• Post Reply Bookmark Topic Watch Topic
  • New Topic

explain this syntax  RSS feed

 
jacob deiter
Ranch Hand
Posts: 588
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class<T> {}

Can you explain this syntax and parameter <T>?
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell that syntax is wrong.

Consider the following syntax

class A<T>
{
}

class A is a generic type with "formal type parameter" T


A<String> is a parameterized type with "actual type argument" String.
You can read it as "A of String str".


note generic type in contrast to parameterized type



The most important advantage of a parameterized type is compile time
checking for type safety. i.e. Suppose Class A has a method put(T).

A<String> str = new A<String>();
str.put(new Integer(4));

The above code will cause a compile time error. You will only be allowed
to do..
str.put(new String("some string"));

Check this tutorial by the Generics Specification Lead Gilad Bracha for more on Generics.
This one should help too.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!