Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changes in type inference through versions  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 185
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept of generics was introduced in Java 5. So I am just making a summary of the changes in different versions and want to know if it is correct.

Please suggest corrections

Java 5, Java 6:

In this version we are able to define a method using a type and call the method using a specific value

Eg


Output:
5

In the above example it was inferred automatically that 5 is an integer and as a result 5 was printed to the output. Alternatively a type witness could be used as

Demo.<Integer>m1(5);

Java 7:

The diamond symbol can be used . Eg:



Target types:
When we are doing an assignment List<T> can be assigned to List<String> since the compiler can infer T as a String in this case.

Eg:


Java 8:

Target types:

Passing a method call as an argument which returns the generic type













 
Stephan van Hulst
Saloon Keeper
Posts: 7473
134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure that many of the features you listed were already available from the very beginning, such as passing the result of method calls directly to another method call that accepts a generic type, as well as assigning the result of a method call to a variable without using a type witness.

Furthermore, in your first example the type parameter is useless. System.out.println() would have been able to print the Integer regardless of whether you used a generic type parameter.


Declaring generic type parameters for methods is only useful when you have multiple method parameters that need to work together, or when you want to return a value of the generic type argument.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!