• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What does public class GenericDelegate<T> {} means

 
Ranch Hand
Posts: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 8532
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic