• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type inference

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is java type inference.can you provide some begginer example
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please SearchFirst(⇐click): java type inference (⇐click)
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type inference means that the compiler can determine the exact type you want to use from the context you're using it in.

One example is using methods that have generic type parameters. A generic type parameter is much like a regular method parameter, but instead of assigning a value to it, you assign a type to it. You even have to pass types to a generic method!
This method will return a List<String>. However, the compiler can infer that you want a List<String> if you assign the return value to a variable of that type. Then you don't have to pass the type argument explicitly:

Since Java 7, there's a second situation in which the compiler uses type inference. Before Java 7, when you assigned a new generic object to a variable, or returned it from a method, you explicitly had to state the type argument of the object:You can see that you have to repeat the type argument when constructing the object, even though it should be clear from the context. With Java 7, you can use the diamond operator instead:
Here the compiler will also infer what the type argument needs to be.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic