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
posted
0
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.
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
posted
0
Well, OK - I will conceed that you have a MINOR point . . .
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
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: 18670
posted
0
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 .
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
posted
0
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 ;-)