-3

I'm currently studying the java.time API and I have encountered the following phrase on Java LocalDateTime Documentation

This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of LocalDateTime may have unpredictable results and should be avoided. The equals method should be used for comparisons.

So, I was just wondering, why it is not recommended to use identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of LocalDateTime?

Thor
  • 9,638
  • 15
  • 62
  • 137
  • 6
    This sort of question has been asked many times on this site including in [these links](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+equals+vs+%3D%3D&*). How do these not answer your question? Your next step should be to check the source code to see exactly what is in their equals and hashCode override, if you want to see how this applies to this particular class. – Hovercraft Full Of Eels Mar 14 '17 at 00:34
  • @HovercraftFullOfEels thank you for your comment. I understand "==" is a reference comparator whereas "equals()" method compare if two instances are equal in value. But none of the post explained what "value based class" meant? And they also doesn't explain why "identity hash code, synchronisation" should be avoided with "LocalDateTime" class. So I was just wondering, if you could reopen my question based on these reasons? Thanks for your time. – Thor Mar 14 '17 at 03:36
  • @HovercraftFullOfEels i have edited question, so that it is more specific and less related to "==" and "equals()" – Thor Mar 14 '17 at 03:39
  • Re-opened. Note that I did not down-vote the question, only closed it. – Hovercraft Full Of Eels Mar 14 '17 at 11:02

1 Answers1

1

On the javadoc, "value-based" has a link to this page, which explains in more details what it means.

The rest essentially says that you are not supposed to assume anything about the instance identity. For example, LocalDate.of(y, m, d) called twice may return the same instance or not.

That blurb is probably there with Java 10 in mind which will introduce value objects.

assylias
  • 321,522
  • 82
  • 660
  • 783