add()is invoked one time, ten times, or a million times with a given value for
b, the result of
add()will never change. However, consider the following function:
add()is no longer an idempotent function. Every time it is invoked, it changes the value of the
sumvariable and returns it. Invoking it with the same value of
bmultiple times will return a different result each time.
Dateconstructor here implicitly uses the current system time at the millisecond precision for getting the current date. If we invoke this function multiple times with the same input value for
days, the returned
Dateinstance will still be different every millisecond.
Dateclass before might be aware that the actual time of the
Datecan be changed via the
addDaysToDatefunction mutable. This will generally be a problem in multi-threaded environments if the
dateinstance being passed to the
addDaysToDatemethod is modified from another thread while the
addDaysToDatemethod is executing.
addDaysToCurrentDatefunction, it gets the current date implicitly by creating a new
Dateinstance which internally uses the
System.currentTimeMillis()call to get the current timestamp.