Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a compilation warning here (var args method call in Java)

Tags:

java

Source:

public class TestVarArgs {   public void varArgsMethod(Object ... arr) {      System.out.println(arr.getClass().getName());      for(Object o : arr) {        System.out.println(o);      }   }    public static void main(String[] args) {     TestVarArgs tva = new TestVarArgs();     tva.varArgsMethod(args);       } } 

Compile:

javac TestVarArgs.java  

Error:

TestVarArgs.java:15: warning: non-varargs call of varargs method with inexact argument type for last parameter; cast to java.lang.Object for a varargs call cast to java.lang.Object[] for a non-varargs call and to suppress this warning     tva.varArgsMethod(args);      ^  1 warning 

I am using javac 1.6.0_20 and the code o/p indicates that a non var arg call was made anyways.

like image 823
Ankur Agarwal Avatar asked Jul 16 '13 03:07

Ankur Agarwal


People also ask

What is var ARG method in Java?

Variable Arguments (Varargs) in Java is a method that takes a variable number of arguments. Variable Arguments in Java simplifies the creation of methods that need to take a variable number of arguments.

Is the Varargs parameter safe to modify?

Put simply, the varargs usage is safe if we use them to transfer a variable number of arguments from the caller to the method and nothing more!

How many maximum numbers of Varargs or variable arguments can be there in a method or a constructor in Java?

13) How many maximum numbers of Varargs or Variable-Arguments can be there in a method or a constructor in Java? Explanation: Yes, only one. Because a VARARG can be present only as the last parameter or argument.

Which of the following symbol is used for a Varargs method parameter?

In Java, an argument of a method can accept arbitrary number of values. This argument that can accept variable number of values is called varargs. In order to define vararg, ... (three dots) is used in the formal parameter of a method.


2 Answers

It is because String[] and Object... do not exactly match up.

You have to cast the String[] to either Object[] (if you want to pass the Strings as separate parameters) or Object (if you want just one argument that is an array) first.

 tva.varArgsMethod((Object[])args);    // you probably want that   tva.varArgsMethod( (Object) args);    // you probably don't want that, but who knows? 

Why is this a warning and not an error? Backwards compatibility. Before the introduction of varargs, you had these methods take a Object[] and code compiled against that should still work the same way after the method has been upgraded to use varargs. The JDK standard library is full of cases like that. For example java.util.Arrays.asList(Object[]) has changed to java.util.Arrays.asList(Object...) in Java5 and all the old code that uses it should still compile and work without modifications.

like image 179
Thilo Avatar answered Sep 19 '22 07:09

Thilo


The argument of type String[] should explicitly be cast to Object[] for the invocation of the varargs method varArgsMethod(Object...) from type TestVarArgs. It could alternatively be cast to Object for a varargs invocation
You can fix it by doing either one of the way If you cast the String[] to Object[] (ref:tva.varArgsMethod((Object[])args);)
OR
change the parameter of method to String[]
(ref:public void varArgsMethod(String ... paramArr))

like image 44
Freak Avatar answered Sep 20 '22 07:09

Freak