Thursday, September 4, 2008

How create methods to receives custom arguments?

There are many characteristics interesting about the JDK 5.0 than had not been used. An example of this are Variable Arity methods, sometimes referred such as varargs methods. Prior to JDK 5.0, if you wanted to pass an arbitrary set of arguments to a method need to pass an array of objects, but in JDK5.0 thanks to varargs can pass a sequence of arguments. For example the PrintStream class uses this characteristics in one its printf methods such as you can see below:

public PrintStream printf(String format, Object... args)

The second argument denoted by three points represents the argument may be passed as an array or as a sequence of arguments, exists one restriction about the position of arguments which say that the arguments variable must be the last parameter. Bellow can see two examples using one and two parameters respectively.

No comments: