• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Type inference

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting from the java documentation

https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html


Type Inference

Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable. The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned. Finally, the inference algorithm tries to find the most specific type that works with all of the arguments.

To illustrate this last point, in the following example, inference determines that the second argument being passed to the pick method is of type Serializable:




Here in the part in bold, is the document trying to say that new ArrayList<String>() is of type serializable? I understand that the return type is of type serializable. But how can new ArrayList<String>() be of type Serializable?
 
Saloon Keeper
Posts: 12253
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList implements Serializable. String implements Serializable. The return type is Serializable. Serializable is the most specific type that all arguments and returned value have in common.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a more general level, a class IS-A its class type, its super class types, and all the interfaces it (and its super classes) implements. To test this, you can simply use the instanceof operator -- and you will see that it returns true.

Henry
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:ArrayList implements Serializable. String implements Serializable. The return type is Serializable. Serializable is the most specific type that all arguments and returned value have in common.


String doesn't even matter here, it's only the ArrayList part that needs to be Serializable. The following still compiles, even though Object doesn't implement Serializable:
 
Stephan van Hulst
Saloon Keeper
Posts: 12253
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring to "d"  :)
 
Rob Spoor
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah right.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic