Varargs can be seen as a simplification of array when we need to pass a multiple value as a method parameter. Varargs itself is an array that automatically created, for these reason you will be enabled to do things you can do with array to varargs.
In the example below you can see the messages
parameter can be assigned to the String array variables, we can call the length method to the messages
parameter as we do with the array. So actually you don’t need to convert varargs to array because varargs is array.
package org.kodejava.lang;
public class VarargsToArray {
public static void main(String[] args) {
printMessage("Hello ", "there", ", ", "how ", "are ", "you", "?");
}
public static void printMessage(String... messages) {
String[] copiedMessage = messages;
for (int i = 0; i < messages.length; i++) {
System.out.print(copiedMessage[i]);
}
}
}
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023