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 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)
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.
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:
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad: