13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.2 Fundamental Language Elements81The details for all the different format fields can be found in the Javadocfor the java.util.Formatter class, a class that is used by printf() to doits formatting, but <strong>on</strong>e that you can also use by itself (C programmers: think“sprintf”).In order to implement printf() for Java, the language also had to be extendedto allow for method calls with a varying number of arguments. So as ofJava 5.0, a method’s argument list can be declared like this:methodName(Type ... arglist)This results in a method declarati<strong>on</strong> which takes as its argument an arraynamed arglist of values of type Type. That is, it is much the same as if youdeclared methodName(Type [] arglist) except that now the compiler willlet you call the method with a varying number of arguments and it will load upthe arguments into the array before calling the method. One other implicati<strong>on</strong>of this is that if you have a declarati<strong>on</strong> like this:varOut(String ... slist)then you can’t, in the same class, also have <strong>on</strong>e like this:varOut(String [] alist)because the former is just a compiler alias for the latter.TIPWe recommend that you avoid methods with variable argument list length. Youlose the compile-time checking <strong>on</strong> the number of arguments that you supply(since it can vary). Often the type of the arguments in the list will be Object,the most general type, to allow anything to be passed in. This, too, circumventstype checking of arguments, and can lead to runtime class-cast excepti<strong>on</strong>s andother problems. Methods with variable argument list length are often a lazyapproach, but were necessary to make printf() work, and for that weare grateful.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!