Default parameters
If you have experience with Java, then you probably have seen a class with multiple overloaded methods, or a class with multiple constructors that differ in a parameter count to provide better usability to users. String or Thread classes from the Java standard library are good examples of this. The problem is that, a lot of code has to be duplicated, and if you are writing a library, you'll probably want to have Java documents on each method, which again have to be duplicated.
Let's take a look at an example from the Java standard library. The String class has overloaded indexOf methods; this one accepts two integers as arguments. The first one is the Unicode character representation, and the second one is the index to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access