• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 231
  • 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: 56819
173
  • 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 bondugula
    Ranch Hand
    Posts: 231
    • 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: 56819
    173
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!