Win a copy of Spring in Action (5th edition) this week in the Spring 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Ranch Hand
Posts: 252
  • 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: 61710
193
  • 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: 252
    • 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: 61710
    193
    • 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!