• 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

Basic Generic Question

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following code:



Why does it fail at System.out.println(node2.getO()), since i am making it clear at line MyNode <String> node2 = new <String> MyNode(10) that it will accept only String, why does it allow me to pass Integer 10 in the constructor.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your generics syntax is backwards. It should be:



Which does give you a compiler error due to the int instead of a String.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:It should be:


Or
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naresh Chaurasia wrote:Why does it fail at System.out.println(node2.getO())...


Actually, a very good question - if it does indeed compile - since I would have expected it to fail at the assignment statement which, as everyone has pointed out, is wrong.

Winston
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently there is some kind of parsing error that is overlooked within that statement as it does compile which can be seen in the snapshot...

It is probably that the parser is matching and verifying the generic notation against the both sides of the assignment operation but doesn't check the specific ordering with the constructor call...

I presume it can be related to generic notation within method definitions: public <T> void method(T param){}
snapshot.png
[Thumbnail for snapshot.png]
Compilation of Seemingly Incorrect Statement
 
Marshal
Posts: 28193
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 didn't even know you could use type arguments like that when constructing a new object, and when I see it I can't even imagine what it might mean.

However my Eclipse compiler says for "List<String> strings = new <Double> ArrayList<String> ();"

Unused type arguments for the non generic constructor ArrayList<String>() of type ArrayList<String>; it should not be parameterized with arguments <Double>



Actually... the warning message is talking about a "non generic constructor" which means that there must be "generic constructors" somewhere. Anybody ever seen such a thing?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently it allows things like this, where the constructor behaves like any generic method:
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its even more interesting than that...
Have a look at the following snippet of code and snapshot:


I'm guessing its related to something around the lines of the following C++ point-of-view:
snapshot.png
[Thumbnail for snapshot.png]
 
pie. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic