List to varargs with pre-difined size or without size

java varargs to list
java 8 varargs
varargs java
varargs in java interview questions
overloading varargs method in java
variable-length arguments python
varargs vs list
redundant array creation for calling varargs method

What is the better way to convert List to varargs to call below 'builder( String... s )' method.

builder( stringList.toArray( new String[stringList.size()] ) );//with pre-difined array size


builder( stringList.toArray( new String[0] ) );//without predifined size

for example

void test() {

  List<String> stringList = new ArrayList<>();
  builder( stringList.toArray( new String[stringList.size()] ) );//call builder method with pre-difining array size
  builder( stringList.toArray( new String[0] ) );//call builder method with array size 0


void builder( String... s )


I faced this question in a review and I was suggested that builder( stringList.toArray( new String[0] ) ) is more efficient than using builder( stringList.toArray( new String[stringList.size()] ) ). Is there a significant difference between these two? Thanks

OCP Oracle Certified Professional Java SE 11 Programmer I Study , 32: String[] array = { "hawk", "robin" }; // [hawk, robin] 33: List<String> list = List.of(​array); // returns immutable list 34: System.out.println(list.size()); // 2 35: Line 38 shows that changing a list value in an immutable list is not allowed. Using Varargs to Create a List Using varargs allows you to create a List in a cool way: List<  Variable-length arguments, varargs for short, are arguments that can take an unspecified amount of input. When these are used, the programmer does not need to wrap the data in a list or an alternative sequence. In Python, varargs are defined using the *args syntax. Let's reimplement our my_min() function with *args:

builder(stringList.toArray(new String[0])) is slightly less efficient since you create an empty array that will be discarded and never used after the method returns. toArray will have to create a new array in order to store the elements of the List.

On the other hand, builder(stringList.toArray(new String[stringList.size()])) passes an array of the required length to the toArray method, and therefore that method will use that array instead of creating a new array.

Variable-Length Arguments in Python with *args and **kwargs, In this article, we'll define and use functions with variable length arguments. we want to offer a flexible API to other developers or we don't know the input size. When these are used, the programmer does not need to wrap the data in a list or an alternative sequence. In Python, varargs are defined using the *args syntax. The second macro is va_arg. You call it with a va_list and a type, and it takes value pointed at by the va_list as a value of the given type, then increment the pointer by the size of that pointer. For example, va_arg (argp, int) will return (int) *argp, and increment the pointer, so argp += sizeof int.

There is a difference and it's mainly outlined by the Alexey Shipilev. Long story short:

toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now

A Programmer's Guide to Java SCJP Certification: A Comprehensive , the varargs parameter, a variable arity method is identical to a fixed arity method. Only one varargs parameter is permitted in the formal parameter list, and it is do not correspond to the formal parameters preceding the varargs parameter. The calls above would result in the publish() method printing: n: 1, data size:0  In JDK 5, Java has included a feature that simplifies the creation of methods that need to take a variable number of arguments. This feature is called varargs and it is short-form for variable-length arguments. A method that takes a variable number of arguments is a varargs method. Prior to JDK 5, variable-length arguments could be handled two

I thought that c.toArray(new String[c.size()])) is more efficient, because we define here an array with required size.


IntelliJ IDEA has Collection.toArray() inspection, which is on by default. This is description:

There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).

In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.

This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

So it seems, that after JDK6, we should use c.toArray(new String[0]). My personal opinion, is that it doesn't matter what aporoach to use this time. Only if profiler says that this is a bottle neck, then we should worry about it.

Java: Arrays should not be created for varargs parameters, Declarations should use Java collection interfaces such as "List" rather than specific implementation The default unnamed package should not be used. The reflective instantiation and invocation no longer require explicit array creation, because the getMethod and invoke methods accept a variable argument list. The program also uses the new printf facility, which relies on varargs. The program reads much more naturally than it would without varargs. So when should you use varargs?

Variable Arguments (Varargs) in Java, This feature is called varargs and it is short-form for variable-length fun(); // no parameter. } only one varargs parameter that should be written last in the parameter list of BitSet class methods in Java with Examples | Set 2 · Shadowing of static functions in Java · How does default virtual behavior differ in C++ and Java ? Per the docs on Lists, I can use array notation to create a list of pre-allocated length: List<String> colors = new String[1]; However, replacing the primitive String type with a list doesn't

Java Varargs (Variable Arguments) With Examples, In case of no arguments, the length of nums is 0. Overloading Varargs methods. Similar to typical methods, you can overload vararg methods. Recommended  List to varargs with pre-difined size or without size Tags ajax android angular api button c++ class database date dynamic exception file function html http image input java javascript jquery json laravel list mysql object oop ph php phplaravel phpmysql phpphp post python sed select spring sql string text time url view windows wordpress xml

Variable Length Arguments, Introduction; The Problem; Default varargs support; Argument replacement using % that dynamically creates a list of arguments and which invokes a varargs function are their types and sizes is never going to change during program execution. Needless to say, there is no obvious way to make the C compiler generate  Java Varargs. Beginning with JDK 5, Java has included a feature that simplifies the creation of methods that need to take a variable number of arguments. This feature is called as varargs (short for variable-length arguments). A method that takes a variable number of arguments is called a variable-arity method, or simply a varargs method.

  • It doesn't matter. This is a micro-optimization question that will have 0 noticeable impact on the performance. Use what you find the most readable.
  • It depends. Both methods behave differently. The first will use the given array if it is large enough. If it's not, it is not used. So, if you use it with the correct size, the code will create one object less. Which is one object less to clean up.
  • One of the Oracle performance engineers did tests on this:
  • @JornVernee awesome! thanks.
  • @CarlosHeuberger So you mean that builder method is again creating an array when we called 'new String[0]' ?
  • just the question if list.size() is slower than what other(?) method toArray uses to find out the size (probably the same, so no difference) / well, actually toArray will have to check the size anywawys...
  • toArray internally will call list.size() a second time, to check the size, and possible to create an own array. So one extra call is made. For Java SE List implementations, where the size is a counter field, the overhead is neglectable compared to new String[0]. But some idiotic lazy immutable linked list implementation could have no such counter maintained, needing to count every node. Absolutely unlikely though. I think I should not have mentioned it.
  • @JoopEggen it's the other way around:
  • @Eugene thanks for this very interesting link. It always is nice to be corrected on such fundamental case. Added to my answer.
  • well, unless you measure this is called guessing and some people proved that it's the other way around
  • profilers and professionals have already tested this:
  • @Eugene Thanks. Good link