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

Java genericity - Inference and instanciation

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i have a question regarding genericity. I don't understand why the compiler can use inference to give a type. But it doesn't work for instanciation.

With inference, i'm not obligated to complet the '<>' from the instanciation of 'a'. From the context, it will find out that <T> parameter from the class is a String so 'a' is therefor a String.
But why can't it do the code below if he know that 'T' is of String type ?


Thanks
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what you mean when you say "doesn't work" there. Perhaps you could explain a bit?

Anyway if you have a suitably recent version of Java you can usewhich works perfectly well, although it isn't related to generics.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fabien Dupuit wrote:From the context, it will find out that <T> parameter from the class is a String so 'a' is therefor a String.


That context is outside the class, in your example.  How far away should the compiler look for context like this?  Like, in the same file, in the same package, in the same project, on the same computer, in the whole organization you're sharing your computer with?  What if there's code somewhere that uses T as a String, and somewhere else there's code that uses T as an Integer?  Or a StringBuffer?  How can the compiler know, within the class A, what other code is going to use it for?

In general, we want to be able to compile a class based on declarations in that class, not by having to scan code outside the class.  Of course when we use another specific class within our class, that other class needs to be compiled before our class.  But with generics like T, there's no other class to go and compile first.  Instead, your class A needs to have all the info it needs to be compiled, within the definition of T.  Code lying around elsewhere is not relevant for compiling class A. Within the class A, the generic parameter T could be anything, since it's just declared there as <T>, with no other restrictions (no super or extends).

Furthermore, the compiler is never going to let you create an instance using "new" without specifying exactly what the class is.  That's how the rules work.  You can't just say "create some sort of T" and let the computer surprise you - T could mean many things.  And with generics, they are generally used in places you need to be flexible, where more than one type is possible.  So the rule is, you can't create a new something with a generic type.  That is, you can create something like new ArrayList<T> (which is fundamentally an ArrayList), but not a new T.
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To continue from what Mike said, what did the compiler error say? You need to give the name of a class, which T isn't.
To look at it another way, whichever way you write the argument to the compiler, can the compiler ensure that the constructor will have those parameters? You cannot know that you want a String there. How would you compile it if you later wrote this?Generics allows for all possibilities.
Besides, it is probably not good programming to use the copy constructor for a String, nor that no‑arguments constructor.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if I did the following?
What would a.f() do, considering that LocalDate doesn't have a no-arg constructor?

But even if you could find a way to declare that a constructor exists (and I actually have devised syntax for this), it's not possible because of type erasure. Generics in Java only exist at compile time. In the compiled byte code it's replaced by the best matching type:
  • With <T> the best matching type is Object
  • With <T extends X> the best matching type is X (for example, <T extends Serializable> leads to Serializable)
  • With <T extends X & Y> the best matching type is X as well. This is why Collections.max uses <T extends Object & Comparable<? super T>> and not <T extends Comparable<? super T>>; the former results in Object which was needed for binary compatibility, the latter results in Comparable
  • With <T super X> and <T super X & Y> the best matching type is Object


  • So your example would compile to this:

    If I would try to access a.a, the compiler turns that into (String) a.a. Because it wouldn't be a String you'd get a ClassCastException.
     
    Rob Spoor
    Sheriff
    Posts: 22815
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Note that functional programming allows us to store the constructor as a field and provide it during construction of instances:
     
    When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic