For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2006 > Optional Parameters?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Optional Parameters?
tomb

2006-09-17, 7:02 pm

Excuse my ignorance - I'm coming from the other development environment :(
Is there a way to specify a function parameter as being optional? I
know C++ has them, and PHP parameters are all optional, but I can't find
anything about it in Java. Thanks in advance.

Tom
Patricia Shanahan

2006-09-17, 7:02 pm

tomb wrote:
> Excuse my ignorance - I'm coming from the other development environment :(
> Is there a way to specify a function parameter as being optional? I
> know C++ has them, and PHP parameters are all optional, but I can't find
> anything about it in Java. Thanks in advance.
>
> Tom


You can provide two or more method declarations with the same name but
different parameter lists.

Often, the ones with the shorter lists just call the full version with
default values of the missing parameters, and only the full version has
a non-trivial implementation.

See, for example, the indexOf methods in String. The fromIndex
parameters can be thought of as being optional parameters, although
there are actually separate method declarations with and without them.

Patricia
tomb

2006-09-17, 7:02 pm

Thank you :)

Tom

Patricia Shanahan wrote:

> tomb wrote:
>
>
>
> You can provide two or more method declarations with the same name but
> different parameter lists.
>
> Often, the ones with the shorter lists just call the full version with
> default values of the missing parameters, and only the full version has
> a non-trivial implementation.
>
> See, for example, the indexOf methods in String. The fromIndex
> parameters can be thought of as being optional parameters, although
> there are actually separate method declarations with and without them.
>
> Patricia


Ian Shef

2006-09-18, 10:03 pm

Patricia Shanahan <pats@acm.org> wrote in news:HjdPg.2625$UG4.1603
@newsread2.news.pas.earthlink.net:

> tomb wrote:
>
> You can provide two or more method declarations with the same name but
> different parameter lists.

<snip>
> Patricia


Starting in Java 1.5, of course, there is also the varargs feature. See for
example:

http://java.sun.com/j2se/1.5.0/docs...ge/varargs.html

http://java.sun.com/developer/JDCTe...5/tt0104.html#1

To be used sparingly and appropriately as these articles point out.



--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com