Clemonte Johnstone wrote:How can I check the source for == or what source should I check for this question?
The "CORRECT" answer means that the parse(String) method creates a new object every time it is called, and that the of(int, int, int) method likewise creates a new method every time it is called.
The API documentation doesn't mention that feature -- that would be a completely ordinary way to implement those two methods but it would be possible to write implementations which returned cached values, like the Integer class does. You could look at the API source code and see what you found, but I don't think that would be a good use of your time. Just accept that the given answer is correct and move on to something else.
I say this because, as you mentioned, LocalDate objects are immutable. Therefore it's of almost no importance at all whether those two methods return the same object for the same parameters or whether they return different objects. The only difference is that if they return different objects, your code might run out of memory sooner if you created millions of them and stored them in a list, but you shouldn't be shocked if that happened anyway.
Let me explain.
When you use parse or of, the code creates every time a new instance of the object, i.e. it uses "new" keyword and call constructor.
So, the == always returns false for any 2 objects created with new keyword.
Otherwise, it would be possible to hack any application: backend code created "account" and you created an account with "new", and it happened that your account points to the server-side account, and you have full control.
You can easily track the source code of LocalDate, for example "of" factory method:
So, for any 2 LocalDates created with "of" comparison with == will give false (because every time new object created on the heap with the new keyword).
Needless to say yhat equals() method overriden for LocalDate and compares logical content, so for those it will be "true"
Exam 1Z0-817: Upgrade OCP Java 6, 7 and 8 to Java SE 11 Developer Study Guide and Quiz
Paul Clapham wrote:. . . The API documentation doesn't mention that feature . . . .
That allows them to keep their options open. In Java29 they are going to change that behaviour to cache up to 1,048,576 LocalDate instances per application. Of course, by then, 1TB will be an average size for the RAM in a posh laptop.
And I challenge you to prove my prediction wrong
I say this because, as you mentioned, LocalDate objects are immutable. . . .
It also says they are value types value types and there are warnings against using == or similar.
Mikalai Zaikin wrote:When you use parse or of, the code creates every time a new instance of the object, i.e. it uses "new" keyword and call constructor.
That's true for LocalDate, as currently implemented. It's not necessarily true for other classes, nor for possible future implementations of LocalDate, as Paul and Campbell mention. There's a difference between how it's implemented currently, and what the API guarantees.