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

前端 未结 5 1621
失恋的感觉
失恋的感觉 2020-12-04 17:29

Source:

public class TestVarArgs {
  public void varArgsMethod(Object ... arr) {
     System.out.println(arr.getClass().getName());
     for(Object o : arr)          


        
5条回答
  •  萌比男神i
    2020-12-04 18:18

    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))

提交回复
热议问题