Functions with variable number of input arguments in Java

static public final float myMax(float... input) {
    -- code for finding max of an array --

Java automatically transforms your input into an array which you can traverse like an array.
This means that you only need to define a single function and it will be able to deal with all of these scenarios

myMax(new float[]{1,2,-1,2,20000000,9,3});

You’re not constrained to just the float type though. Any class will do, from Strings, to booleans, to custom classes you’ve created yourself.

Wish I’d known this when I first began using Java.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s