This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Java 7 Type Inference Wrong example in sun web site  RSS feed

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going through type inference topic of generics in Java 7 oracle(sun) web site. In the web site they provided below example:
http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html

Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. For example, the following example does not compile:


I did not get any compiler or run time error after executing above program. Did they provide  wrong example?

 
Marshal
Posts: 59786
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you read the documentation for the addAll method carefully. Please confirm that:-
  • 1: You wrote code like that in a method in a class and it compiled normally.
  • 2: You were able to run that method and it dodn't throw any exceptions.
  • 3: You were using the same version of Jave┬« as the example on the website.
  • I managed to get that sort of code to run without exception on JDK8u121. Maybe the type inference is more developed in Java8? Does the Java┬« Language Specification differ between Java7 and Java8?
     
    vipul John
    Ranch Hand
    Posts: 245
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Gotcha.! My Bad. I was using Java v8. As you said, type inference is more developed in java 8. I changed JAVA_HOME  to v7 and i am getting compiler error.
    Thanks for your time.
     
    Campbell Ritchie
    Marshal
    Posts: 59786
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    vipul bondugula wrote:Gotcha.! My Bad.

    You haven't done anything wrong.

    . . . type inference is more developed in java 8. I changed JAVA_HOME  to v7 and i am getting compiler error.
    Thanks for your time.

    That's a pleasure Interesting problem.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!