• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

C++ style default values for method parameters

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those who aren't sure . . . . .
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, OK - I will conceed that you have a MINOR point . . .
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey yah, that's right.

That's why the VB way of doing the same thing seemed familiar.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic