File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes C++ style default values for method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "C++ style default values for method parameters" Watch "C++ style default values for method parameters" New topic
Author

C++ style default values for method parameters

Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Hi,
I want to implement methods with Default Values for parameters. I know we can do this in C++. Other than overloading the method, is there any simpler way I can do this in Java? Any help will be appreciated. Thanks.
-Bala.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Overloading a method is so ridiculously simple - and you want it even MORE simple? sheesh! :roll:
No - overloading is the proper way to do it.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
For those who aren't sure . . . . .
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If I could be brave:

This is a pretty old argument, and I felt the same way when I first started Java. In fact, I sorta sometimes feel the same way.

Let's say that I have a method that accepts 3 arguments.

That makes 2^3 possible combinations where combinations are the form:

(value, value, value)
(value, value, null)
(value, null, value)

And here we've run across the first example of how 'default' values in C++ are not precisely the same thing as overloading a method in Java.

How in Java do I distinguish between those last two? I need to supply an overriden version of my method that supports the last two cases above.. but I really can't. Because how do I determine which value is missing from the list?Overloading is in both C++ and Java, and I think it's slightly different than default values of parameters (at least in this case).

In C++, you just need one method. And in the argument list, wherever the method sees 'null', it substitutes the default value. But in Java, there isn't a similar facility.

For Java, you'd need to check each of arg1, arg2 and arg3 for null, and subsitute a default value in the *body* of the method. I think it's just a matter of style, and yes, simplicity to enable a default value.

Or have I been missing something for a year or so?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, OK - I will conceed that you have a MINOR point . . .
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

On the other hand, not having default values and making parameters in method invocations match one-for-one to the method's declared parameters promotes clarity of intent in code and reduces the chances of introducing subtle bugs in subclasses or client code. I would rather make the sacrifice of having to pass a bunch of "nulls" in method calls (which takes what, a whole 5 seconds to key in?) than have to spend an hour stepping through and debugging code that somebody else wrote


Junilu - [How to Ask Questions] [How to Answer Questions]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Well, the passing of nulls is there, whether it's Java or C++, my complaint stems from the fact that each and every parameter then needs to be checked for null and given the default value.

I've never thought coding C++ default parameters was unclear. In fact, I'd rather see it that way. One simple line, you see the parameter list, the type, and the default value. Which looks simpler?But I think we've whipped this one to death.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm... I admit it's simpler-looking in C++, and I don't really see a compelling reason why it couldn't have been present in Java. But I'm not sure I fully understand how they work in C++ - my skills in that language are rather rusty. Mike, what exactly do these "null" values look like in C++? Are you talking about passing a null pointer? I thought this was about omitting arguments entirely from function calls. The overloading solution Cindy showed allows you to omit arguments, as long as they're omitted at the end. The method you show requires you to substitute a null rather than omit, but you can do this anywhere.
I'm looking at your previous comment about how to distinguish between
(value, value, null)
(value, null, value)
How would you make this distinction in C++? I.e., what would the function calls look like? Is it legal in C++ to omit an argument which has a default, and then follow with another explicit argument? E.g. method(a,,c) where the second argement is intentionally omitted, but not the third? (I don't have ready access to a C++ compiler at the moment, unfortunately.) If this is legal, then I understand where you're coming from - but if not, then I'm still .


"I'm not back." - Bill Harding, Twister
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Ya, it's been quite some time for me as well, but I think it looked like:
[ January 31, 2002: Message edited by: Mike Curwen ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

CMethod(21,31,,4); //3rd argument left out, will use the default

That will not compile. Only the right-most parameters can "default" as in.
[ccode]
int method(int n = 0, int h = 1, int i = 2, int j = 7) { return n + h + i + j; }
method(6) // method(6, 1, 2, 7);
method(6, 5) // method(6, 5, 2, 7);
method(0, 1, 2, 5) // method(0, 1, 2, 5);
[/ccode]
So if you want to set the right-most parameter you'd have to set the left ones too or override the method ;-)

Regards,
S. Bro
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hey yah, that's right.

That's why the VB way of doing the same thing seemed familiar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: C++ style default values for method parameters
 
Similar Threads
Default constructor
not relevant to scjp
exeption handling
POST method parameters
Storing ServletContext init param in <distributable> webapp