Which is the better way to simulate optional parameters in Java?
- by froadie
I have a Java method that takes 3 parameters, and I'd like it to also have a 4th "optional" parameter. I know that Java doesn't support optional parameters directly, so I coded in a 4th parameter and when I don't want to pass it I pass null. (And then the method checks for null before using it.) I know this is kind of clunky... but the other way is to overload the method which will result in quite a bit of duplication.
Which is the better way to implement optional method parameters in Java: using a nullable parameter, or overloading? And why?