When do we use the variable argument in Java?

Asked 2 years ago, Updated 2 years ago, 131 views

I learned about the variable factor, but I don't know what it's for. I don't know if you need this. I'm a little worried that people might pass as many parameters as they want by using variable arguments. Can I give you an example of what this is for?

java varargs

2022-09-22 22:17

1 Answers

Variable arguments are very useful. Many methods use variable arguments for handling when the number of parameters is uncertain. One good example is the String.format method. This method accepts multiple parameters.

String.format("This is an integer: %d", myInt);
String.format("This is an integer: %d and a string: %s", myInt, myString);

Like this. Instead, you'll have to set some rules and make them throw errors when they go against the rules.


2022-09-22 22:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.