简体   繁体   English

Java编译器错误没有任何意义( <identifier> 预期)

[英]Java compiler error not making any sense (<identifier> expected)

I have done a lot of searching for what this compiler error <identifier> expected means, and none of them seem to apply to my situation. 我已经做了很多搜索这个编译器错误<identifier> expected意味着什么,并且它们似乎都不适用于我的情况。 Really sorry if this is a duplicate or basic info, but I can't find anything anywhere. 如果这是重复或基本信息,真的很抱歉,但我找不到任何地方。

The following code works fine. 以下代码工作正常。 Note that I am positive myObject1 is indeed a HashSet<String> so the cast is ok. 请注意,我是肯定的myObject1确实是一个HashSet<String>所以演员阵容是好的。

@SuppressWarnings("unchecked")
HashSet<String> s1 = (HashSet<String>) myObject1;

The following code does NOT work fine. 以下代码不能正常工作。 It will compile, but with warnings. 它会编译,但有警告。

@SuppressWarnings("unchecked")
HashSet<String> s1;
s1 = (HashSet<String>) myObject1;

So then I try the code below. 那么我尝试下面的代码。

@SuppressWarnings("unchecked")
HashSet<String> s1;
@SuppressWarnings("unchecked")
s1 = (HashSet<String>) myObject1;

Now it refuses to even compile, giving me the <identifier> expected error that is puzzling me so much. 现在它拒绝编译,给我<identifier> expected错误,这让我很困惑。 The ^ symbol in my command line is pointing right before the = in the last line. 我的命令行中的^符号指向最后一行中的=之前。 I'm not sure what on earth I could be expected to put BETWEEN the s1 and the = . 我不确定究竟是什么原因我可以把它放在s1=

Any ideas? 有任何想法吗? Thanks! 谢谢!

You can't apply an annotation to a simple assignment statement. 您不能将注释应用于简单赋值语句。 From section 9.7 of the JLS : JLS第9.7节

Annotations may be used as modifiers in any declaration, whether package (§7.4.1), class (§8.1.1) (including enums (§8.9)), interface (§9.1.1) (including annotation types (§9.6)), field (§8.3.1, §9.3), method (§8.4.3, §9.4), formal parameter (§8.4.1), constructor (§8.8.3), or local variable (§14.4.1). 注释可以在任何声明中用作修饰符,无论是包(第7.4.1节),类(第8.1.1节)(包括枚举(第8.9节)),接口(第9.1.1节)(包括注释类型(第9.6节)) ),field(§8.3.1,§9.3),方法(§8.4.3,§9.4),形式参数(§8.4.1),构造函数(§8.8.3)或局部变量(§14.4.1) 。

I agree that the compiler error message could be rather clearer, admittedly... 我同意编译器错误消息可能相当清楚,诚然......

You could look at the Java Api for Annotations. 您可以查看Java Api for Annotations。

http://docs.oracle.com/javase/6/docs/api/java/lang/SuppressWarnings.html http://docs.oracle.com/javase/6/docs/api/java/lang/SuppressWarnings.html

http://docs.oracle.com/javase/6/docs/api/java/lang/annotation/ElementType.html#LOCAL_VARIABLE http://docs.oracle.com/javase/6/docs/api/java/lang/annotation/ElementType.html#LOCAL_VARIABLE

There you can see, that SupressWarnings has the target LOCAL_VARIABLE. 在那里你可以看到,SupressWarnings的目标是LOCAL_VARIABLE。

And LOCAL_VARIABLE stands for the declaration not the assignment. LOCAL_VARIABLE代表声明而非作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM