java 解释警告:最后一个参数的参数类型不准确的可变参数方法的非可变参数调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32275560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:56:27  来源:igfitidea点击:

Explain warning: non-varargs call of varargs method with inexact argument type for last parameter

java

提问by Dexter

This is my sample code where i am getting the warning.

这是我收到警告的示例代码。

String lsSQL = foMetaQuery.getSQL();
String  lsNewSQL = replace(lsSQL,"'' {","''{");
lsNewSQL = replace(lsNewSQL," } ''","}''");
lsNewSQL = replace(lsNewSQL," }","}");
lsNewSQL  = MessageFormat.format(lsNewSQL,foSubstitutionArray);
loVSQueryDef.setSQL(lsNewSQL);

The compiler says

编译器说

cast to java.lang.Object for a varargs call cast to java.lang.Object[] for a non-varargs call and to suppress this warning

lsNewSQL  = MessageFormat.format(lsNewSQL,foSubstitutionArray);

为可变参数调用强制转换为 java.lang.Object 为非可变参数调用转换为 java.lang.Object[] 并取消此警告

lsNewSQL  = MessageFormat.format(lsNewSQL,foSubstitutionArray);

回答by Zastai

You don't show what type foSubstitutionArrayhas, but I'll assume it's an array of a type other than Object.

您没有显示foSubstitutionArray具有什么类型,但我会假设它是 Object 以外的类型的数组。

Now, MessageFormat.Format()is a varargs method, which means that you can pass it any number of arguments (well, at least 1) and Java will internally collect them all in an array of objects. However, here you're passing in an array, so Java gets confused: are you trying to pass a single argument (that just happens to be an array), or are you passing in the variable arguments?

现在,MessageFormat.Format()是一个可变参数方法,这意味着您可以向它传递任意数量的参数(好吧,至少是 1 个),Java 将在内部将它们全部收集在一个对象数组中。但是,这里传入的是一个数组,因此 Java 会感到困惑:您是要传递单个参数(恰好是一个数组),还是要传递变量参数?

If you intend to pass a single argument (unlikely), add a cast to Object:

如果您打算传递单个参数(不太可能),请向 Object 添加强制转换:

MessageFormat.format(lsNewSql, (Object) foSubstitutionArray)

If you intend values to be taken from your array, cast to Object[]:

如果您打算从数组中获取值,请转换为 Object[]:

MessageFormat.format(lsNewSql, (Object[]) foSubstitutionArray)

回答by Raffaele

MessageFormat.format()has a variable-aritysignature that is a convenience for the programmer, that allows one to write

MessageFormat.format()有一个变量签名,这对程序员来说很方便,它允许一个人写

  • format("Hello")
  • format("Hello {name}", name)
  • format("Hello {first} {last}", first, last)
  • format("Hello")
  • format("Hello {name}", name)
  • format("Hello {first} {last}", first, last)

At the bytecode level, the method signature has two parameters of type Stringand Object[], but behind the scenes the compiler creates the array for you, so you don't have to type new Object[] {first, last}. You can still create the array explicitely with format("Hello", new Object[]{}), and the compiler will be happy.

在字节码级别,方法签名有两个类型为String和 的参数Object[],但在幕后编译器会为您创建数组,因此您不必键入new Object[] {first, last}。您仍然可以使用 显式创建数组format("Hello", new Object[]{}),编译器会很高兴。

However when you pass an array of strings as the last parameter, there are two possible interpretations:

但是,当您将字符串数组作为最后一个参数传递时,有两种可能的解释:

  • format("Hello", new String[] {first, last})
  • format("Hello", new Object[]{new String[] {first, last}})
  • format("Hello", new String[] {first, last})
  • format("Hello", new Object[]{new String[] {first, last}})

For backward compatibility, the compiler assumes the former and issues a warning instead of an error, but it still asks you to insert a cast to either Objector Object[]to state in the source code what you really mean.

为了向后兼容,编译器假定前者并发出警告而不是错误,但它仍然要求您插入强制转换ObjectObject[]在源代码中说明您真正的意思。