Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about generic type

 
Runrioter Wung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I enter a fragment of code: Vector<String> names = new Vector<>();I think it is right! But Eclipse told me that I should add the cast: Vector<String> names = new Vector<String>();Anybody tell me what's wrong?Thanks!
 
Stephan van Hulst
Bartender
Pie
Posts: 6106
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's perfectly fine. It sounds like Eclipse is just being grumpy. Is it giving you a warning, or isn't it letting you compile at all?
 
Runrioter Wung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:It's perfectly fine. It sounds like Eclipse is just being grumpy. Is it giving you a warning, or isn't it letting you compile at all?

yeah,I can't compile it,so I have to add that cast.
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of Java are you working with? I believe being able to omit the String on the right side is new in Java 7. Maybe there's a problem with the language level settings in your IDE?
 
Stephan van Hulst
Bartender
Pie
Posts: 6106
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it's not a cast. The correct term is generic type argument.

It sounds like your IDE uses an older source code version setting than 1.7. I don't use Eclipse, but look around in the project properties to see if you can edit the source code version.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic