[英]Warning: compile with -Xlint:unchecked pops on this code!
import java.lang.reflect.Method;
public class ClassLoadingTester
{
public static void main(String a[]){
try{
ClassLoader loader=new CustomClassLoader();
Class c=loader.loadClass("AddClass");
loader=null;
Object o=c.newInstance();
Method m=c.getMethod("getTwoNumbers",new Class[]{String.class,String.class});
m.invoke(o,new Object[]{"2","3"});
}catch(Exception e){
e.printStackTrace();
}
}
}
This code snippet generates a warning when i compile in java. 当我在Java中编译时,此代码段生成警告。 I think it pertains to this line,
我认为这与这条线有关
Method m=c.getMethod("getTwoNumbers",new Class[]{String.class,String.class});
When I attempt to put <String>
like this new Class<String>[]{String.class,String.class}
当我尝试将
<String>
放置为new Class<String>[]{String.class,String.class}
It generates an error saying generic array type
I'm doing my code in BlueJ. 它生成一个错误,指出我正在BlueJ中执行代码的
generic array type
。
But when I leave the code as is it generates that irritating warning ^^. 但是当我按原样保留代码时,它会产生令人讨厌的警告^^。
I just want to get rid of that warning what should I do with that line of code? 我只是想摆脱该警告,该行代码应该怎么做?
You can't create arrays of generic types like Class<String>
, hence the error. 您不能创建泛型类型的数组,例如
Class<String>
,因此会出错。 So you don't have much choice other than adding a @SuppressWarnings("unchecked")
annotation before the statement to silence the warning. 因此,除了在语句之前添加
@SuppressWarnings("unchecked")
注释以使警告静音之外,您别无选择。
Note that these warnings almost always have a reason, and silencing them this way regularly may easily result in runtime class cast exceptions! 请注意,这些警告几乎总是有原因的,以这种方式使它们静默可能会轻易导致运行时类强制转换异常! So always think three times before using
@SuppressWarnings("unchecked")
- use it only when you are absolutely sure the cast is safe and there can be no negative consequences. 因此,在使用
@SuppressWarnings("unchecked")
之前,请务必三思@SuppressWarnings("unchecked")
-仅在绝对确定@SuppressWarnings("unchecked")
是安全的并且不会有负面影响的情况下使用它。 In this case it looks safe to me to use it. 在这种情况下,使用它对我来说看起来很安全。
int t=10, a=7, b=52, d=97;
if((t + d) / 2 > b && (d - t) % 2 != 0)
if(a%2==0 || b%2==0)
System.out.println(--t + a);
else
System.out.println(--t - a);
else
System.out.println(t + ++a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.