This week's book giveaway is in the Features new in Java 9 forum.
We're giving away four copies of Java 9 Revealed and have Kishori Sharan on-line!
See this thread for details.
Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Generics  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is from the below quote.
1. What is the point of having a Class of type X when the parameter passed to the constructor is of type T?
2. What is the point of having a class declared with type X when it is not being used?
3. Also from the bold part in the quote: Generics was introduced in version 5. What was the difference between compiler in Java SE 5 and Java SE 7 when both use the diamond symbol for inferring? Can someone give an example of the difference between the versions?

Quoting from the java documentation:

Type Inference and Generic Constructors of Generic and Non-Generic Classes

Note that constructors can be generic (in other words, declare their own formal type parameters) in both generic and non-generic classes. Consider the following example:

class MyClass<X> {
  <T> MyClass(T t) {
    // ...
  }
}


Consider the following instantiation of the class MyClass:

new MyClass<Integer>("")


This statement creates an instance of the parameterized type MyClass<Integer>; the statement explicitly specifies the type Integer for the formal type parameter, X, of the generic class MyClass<X>. Note that the constructor for this generic class contains a formal type parameter, T. The compiler infers the type String for the formal type parameter, T, of the constructor of this generic class (because the actual parameter of this constructor is a String object).

Compilers from releases prior to Java SE 7 are able to infer the actual type parameters of generic constructors, similar to generic methods. However, compilers in Java SE 7 and later can infer the actual type parameters of the generic class being instantiated if you use the diamond (<>). Consider the following example:







In this example, the compiler infers the type Integer for the formal type parameter, X, of the generic class MyClass<X>. It infers the type String for the formal type parameter, T, of the constructor of this generic class.


Note: It is important to note that the inference algorithm uses only invocation arguments, target types, and possibly an obvious expected return type to infer types. The inference algorithm does not use results from later in the program.
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:1. What is the point of having a Class of type X when the parameter passed to the constructor is of type T?

X defines the behavior that you need. T is a type that X uses, without X knowing about the exact concrete type. This is useful if the class that uses X also knows the real type of T.

An example is the List interface. List has a generic type parameter T that holds the type of the elements of the list. While the List type itself doesn't know what the real type held by T will be, the class that uses the list will. Before generics:

Since generics were introduced:

2. What is the point of having a class declared with type X when it is not being used?

Can you give an example?

3. Also from the bold part in the quote: Generics was introduced in version 5. What was the difference between compiler in Java SE 5 and Java SE 7 when both use the diamond symbol for inferring? Can someone give an example of the difference between the versions?

The piece of documentation you quoted also contains an example. In Java 5 when you used the new operator on a generic type, you had to specify the generic type argument between the angle brackets. In Java 7, if the generic type argument can be inferred from the context (such as when you assign the new object to a variable, or return it from a method), you don't have to specify the generic type argument between the angle brackets.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15890
82
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
1. What is the point of having a Class of type X when the parameter passed to the constructor is of type T?
2. What is the point of having a class declared with type X when it is not being used?

The quote you presented only explains that constructors can have their own type parameters, which are separate from the type parameters that the class might have. In the example, the constructor has a type parameter T and the class has a type parameter X. The quote only explains the mechanism - the example given in the quote is not intended to be a useful, practical example of this by itself.

Question 2: Of course, if you are not going to use a type parameter, there is no reason to have it in the code at all. But that's not the point of the text you quoted - that only explains the mechanism, not why it might be useful.

raja singh kumar wrote:
3. Also from the bold part in the quote: Generics was introduced in version 5. What was the difference between compiler in Java SE 5 and Java SE 7 when both use the diamond symbol for inferring? Can someone give an example of the difference between the versions?

The diamond symbol was introduced in Java 7. It does not exist in Java 5; if you would try to use it with the Java 5 or Java 6 compiler you would get an error.
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still not getting. Constructor of a class takes a type argument T, so it makes sense for constructor to have a type. What is the meaning of a class having a type X?
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, please give us a concrete example of what you mean.
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean, why do we do:
Instead of just:
Then the answer is twofold.

First, the type of the variable might not be the same as the type argument passed to the constructor:

Secondly, a generic type that isn't parameterized is already considered to be a "raw type". We have to parameterize the variable because it would have a raw generic type otherwise, and we have to pass a generic type argument to the constructor because otherwise the new keyword would return a raw generic type.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:I am still not getting. Constructor of a class takes a type argument T, so it makes sense for constructor to have a type. What is the meaning of a class having a type X?


Well, considering that the bulk of the code is behind a "//..." comment, meaning that it is not shown,  I don't think that we can answer the question of the meaning of X. Having said that, this looks to be a very thin example showing the mixing of class generics and generic methods, I don't think you will be able to extract any "meaning" or "why implemented" answers here.

Henry
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone post an example where the class has a certain type, say X and the constructor of that class accepts some other type say T, as an argument? Can a class have a particular type while the constructor of that class has a different type?
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between using a type like this <Integer> compared to using the type Integer like this method1(Integer i)?
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Can someone post an example where the class has a certain type, say X and the constructor of that class accepts some other type say T, as an argument? Can a class have a particular type while the constructor of that class has a different type?

You mean that the type argument list of the variable is different than the type argument list of the constructor. Yes, I already gave an example of this in my last post.

What's the difference between using a type like this <Integer> compared to using the type Integer like this method1(Integer i)?

In the first case, you pass Integer as a type argument to a variable or type declaration, or to a method or constructor call. In the second case you just declare a variable of type Integer.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:What's the difference between using a type like this <Integer> compared to using the type Integer like this method1(Integer i)?


Can you clarify your question a bit? Are you asking what is the difference between ...

... and this ...


[EDIT] And if this is the case, it looks like Stephan already addressed this in the previous post.

Henry
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets take an example



Here, the type parameter which the class takes is an Integer while it is inferred that the type argument that the constructor takes is a String. How can they be different? Also what does a class having a type Integer mean when the constructor accepts a String argument? Shouldn't they be of the same type?
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what is the definition of type witness in generics?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15890
82
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Here, the type parameter which the class takes is an Integer while it is inferred that the type argument that the constructor takes is a String. How can they be different?

Because there are two separate type variables: X and T, which are not related or connected to each other. In the example you give, the type Integer is used for X and String for T (where the latter is not specified explicitly, but determined by type inference - because you pass an empty string "" as the argument of the constructor).

raja singh kumar wrote:Also what does a class having a type Integer mean when the constructor accepts a String argument? Shouldn't they be of the same type?

X and T are separate type variables, that are not in any way connected to each other. So there is no anwer to that question (it doesn't mean anything - they are unrelated type variables), and no reason why they should be the same.

raja singh kumar wrote:Also, what is the definition of type witness in generics?

It's an explicit indication of a type that you want to use for a type parameter, instead of letting the Java compiler figure out the type by itself through type inference. This tutorial page explains it: Type Inference
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok fine. Now, is this the difference between Java 5 and 7:

In Java 5:


In Java 7
 
raja singh kumar
Ranch Hand
Posts: 179
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's an explicit indication of a type that you want to use for a type parameter, instead of letting the Java compiler figure out the type by itself through type inference. This tutorial page explains it: Type Inference


Is this applicable to both, the input parameters and the return type?
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Ok fine. Now, is this the difference between Java 5 and 7:
In Java 5:

In Java 7

This is yet another Java feature being added to this discussion. And yes, as you figured out, this feature didn't exist in Java 5.

It is called a diamond operator. It is actually a minor feature -- with the goal of reducing some of the verbose aspect of Java. This is done by Java inferring the generic type based on how it is used in the rest of the declaration.

Henry
 
Mohammed Sardar.
Ranch Hand
Posts: 94
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for interruption. But trying to understand a question that was asked  me recently. I think that question is quite related to this thread. But please help me to understand. The first question was how to declare an ArrayList I answered as ArrayList al = new ArrayList(); then we add elements to it. Then how to declare Generics with ArrayList and it's feature of Java 8. What topic should I read in Java 8 regarding ArrayList and Generics declaration ?

Thanks
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Sardar. wrote:Then how to declare Generics with ArrayList and it's feature of Java 8. What topic should I read in Java 8 regarding ArrayList and Generics declaration ?


Generics was actually introduced into Java in 2004, with the release of Java 5. There are probably tons of books and tutorials regarding Java generics available... including the Oracle Tutorial on Generics (https://docs.oracle.com/javase/tutorial/extra/generics/).

Also...

Mohammed Sardar. wrote:The first question was how to declare an ArrayList I answered as ArrayList al = new ArrayList(); then we add elements to it.


While Java does allow the use of raw types... it is *not* a good idea to use them, as they are mainly legacy, and used for backward compatibility purposes. Additionally, since it has been more than 10 years since generics was released, unless you are maintaining ridiculously old code, there is no reason to ever use it.

I highly recommend to always use generics with collections. Using a raw type, when asked, especially during a job interview, is not good.

Henry
 
Mohammed Sardar.
Ranch Hand
Posts: 94
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry Wong. It helps and what is that new feature is all about in  Java 8 for Array List and Generics.I could not figure out and I guess the questions was mess.

Thanks
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Sardar. wrote:It helps and what is that new feature is all about in  Java 8 for Array List and Generics.I could not figure out and I guess the questions was mess.


This topic was basically regarding generic classes and generic methods, which were introduced at Java 5. There was also a portion regarding the diamond operator, which were introduced at Java 7.... not exactly sure what "new feature" related to Array List and Generics, that you are referring to.

Could you be referring to Lambdas and Streams?  Regardless, perhaps it would be a good idea to show us an example of this feature that you "could not figure out"?

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!