Are String [] and String ... (Var-args) the same when they work internally?

class WrongOverloading{ void something(String [] a){ .. } Integer something(String... aaa){ return 1;} } 

The above code does not compile! The compiler says these are duplicate methods. So using a String array or String var-args means the same thing?

How are they implemented internally?

+7
source share
9 answers

They are actually the same, except that the compiler will not accept varargs unless its last argument and allows you to pass multiple arguments to an array.

 public void methodA(int... ints, int a); // doesn't compile public void methodA(int[] ints, int a); // compiles public void methodB(int... ints); // compiles public void methodC(int[] ints); // compiles methodB(1); // compiles methodB(1,2,3,4); // compiles methodC(1); // doesn't compile methodC(1,2,3,4); // doesn't compile 
+6
source

From this SO discussion

A suitable type of the variational method function(Object... args) is function(Object[] args) . Thus, Sun adds varargs to maintain backward compatibility.

So, as every other answer said, yes, they are the same.

+4
source

String... aaa similar to String[] aaa .

I assume the semicolon after the second function is a typo ...

+1
source

Yes, this is the same.

You can read this article :

It is true that several arguments must be passed in the array, but the varargs function automates and hides the process.

+1
source

The [vararg] attribute indicates that the method accepts a variable number of parameters. To do this, the last parameter must be a safe array of type VARIANT, which contains all the other parameters:

 [vararg [, optional-attributes]] return-type function-name( [optional-param-attributes] param-list, SAFEARRAY(VARIANT) last-param-name); 

The varargs syntax basically allows you to specify that there are possible parameters, right? They may or may not be there. This is the goal of three points. When you call a method, you can call it with or without these parameters. This was done to avoid having to pass arrays to methods.

Check this out :

See When do you use varargs in Java?

 final public class Main { private void show(int []a) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+"\t"); } } private void show(Object...a) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+"\t"); } System.out.println("\nvarargs called"); } public static void main(String... args) { int[]temp=new int[]{1,2,3,4}; Main main=new Main(); main.show(temp); main.show(); //<-- This is possible. } } 

For this reason, varargs is generally not recommended when overloading methods.


System.out.printf(); is an example of varargs and is defined as follows.

 public PrintStream printf(String format, Object ... args) { return format(format, args); } 

format - A format string, as described in the format string syntax

args - Arguments referenced by format specifiers in the format string. If there are more arguments than format specifiers, additional arguments are ignored. The number of arguments is a variable and can be zero. The maximum number of arguments is limited by the maximum size of the Java array, as defined by the Java Virtual Machine specification. The behavior on the null argument depends on the conversion.

+1
source

yes, they are the same because when you call a method with elipsis (String ...), it is converted to an array of String.

+1
source

The compiler behind the scenes actually converts your var args method into an array input method. For this reason, your var args method with an array may be overloaded, because after compilation both of them will be identical.

+1
source

Yes, both are the same ... http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html Just read this site, you will find out

+1
source

when calling a method, he does not care about the type of return, he will consider the name of the method, the number of parameters and the type of parameters and the order of the parameters. Here you specify a method with the same name as the same .bcoz parameters in the case of var arg, if we call a method with two parameters, the same method will be executed, if we call a method with 3 parameters, it will call the same method . here, if we call something (String [] a) and something (String ... aaa), the same method will be called .bcoz, we can replace the array with var-arg, then there will be confusion, which should cause method. then the ambiguity method will be occour. that is why it shows a repeating method.

here, if we pass an array to the var - arg parameter method, it will be executed. Inside, it converts var-args to a one-dimensional array.

0
source

All Articles