• Post Reply Bookmark Topic Watch Topic
  • New Topic

What does public class GenericDelegate<T> {} means  RSS feed

 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Everyone!

I can't understand what does this following meaning.

I am making an ejb project with few of my collegeus in my workplace. I have no much idea about ejb. I wonder if anyone can simply explain me what does that symbol representation meaning in Java.
What I only knew was the traditional way we create normal Java classes as below :



Any explanation for a beginner like me highly appreciated.
Thank you.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming you want to know what the <T> means in the class declaration? You could take a look at this article on generics but it's not really beginner stuff.

The basic idea is that the <T> is a placeholder for a type. This type becomes the type of the Generic Delegate.

Have you ever written List<String> = new ArrayList<>();? <String> is a way to tell what kind of List this will be. <String> can be any object, so List<Integer> is a list of integers.

The ArrayList class declaration starts public class ArrayList<E> (Using E or T is not important.) The <E> is a way to say, the user will supply the type and E will stand for that type.

Does that make any sense?
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute,
Thank you for your response. I do actually know about generic usage in Collections. But what I don't understand is when we declare a class, why do we give that <E> or <T> ? I really don't understand what does that thing do.
The <E> is a way to say, the user will supply the type and E will stand for that type.

Sorry. I don't understand the above statement. (I'm not a native speaker. Can you please describe it in simplest manner?)
What do you mean by 'type' ?

Normally When we use a object out of a particular class we just simply say,

Don't we? (This is my assumption. Correct me if I'm wrong)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The <T> is a type parameter. Did you read the article that Knute posted a link to? It explains it all in detail.

Sometimes you want to write a class that can work with different data types. Collections are the prime example of this - you want to be able to have a List of Strings, or a List of Integers, or a List of Person objects. Ofcourse you could just make a class List that stores Objects, and that would work fine. But you wouldn't know that the List is supposed to contain only String objects, and if you would get something out of the List, you would always have to cast. (This is, in fact, how it worked up to Java 1.4).

By using generics, you can specify that this List only contains String objects, and the compiler will check it for you. If you try to put something else in the list, you get an error from the compiler.

You can make your own interfaces, classes and methods that take type parameters by using the <...> syntax in the declaration.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other way to think about it as about a box. Look at the example below:
So, T refers to a particular type. So, the constructor takes an object of type T (could be any other letter, it is convention to use T, there are more letters in other cases), but it does not mean any object. It has to be a certain type.
So in different words you have to replace T with particular class, so primitives cannot be as a type. In case you want to add integers into the box, you need to use wrapper class Integer, ie:
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:So, T refers to a particular type. So, the constructor takes an object of type T (could be any other letter, it is convention to use T, there are more letters in other cases)

Actually, it does not have to be a single letter. It could be any valid identifier. Although, the convention is to use single letters.
Just mentioning this in case OP sees something like:
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Have you ever written List<String> = new ArrayList<>();? <String> is a way to tell what kind of List this will be. <String> can be any object, so List<Integer> is a list of integers.

@Jude: Just a minor additional point that might be worth mentioning:
That '<>' in Knute's example is a fairly recent addition to Java called the diamond operator; and in most cases it's simply used as "shorthand".

You can also write his example as

  List<String> strList = new ArrayList<String>();

and I have to admit that I still tend to do that.

But then, I am a bit of a dinosaur.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!