Which simulates the optional parameters in Java better?

I have a Java method with three parameters, and I hope it also has the fourth "optional" parameter I know that Java does not directly support optional parameters, so I code in the fourth parameter. When I don't want to pass it, I pass null (the method then checks for null before use.) I know it's a little clumsy... But another way is to overload the method, which will lead to considerable repetition

Which is a better way to implement optional method parameters in Java: use nullable parameters or overloads? Why?

Solution

Write a separate 3 - parameter method and forward it to the 4 - parameter version Don't destroy it

With so many parameters, you may need to consider a builder or similar

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>