• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Why is the immutable LocalDate objects all evaluated to false when == is executed

 
Ranch Hand
Posts: 110
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all, please can someone explain why the immutable LocalDate objects are all false when == is evaluated

/* What will be the result of compiling and executing the below program?
  A) true:true:true
  B) false:false:true
  C) false:false:false         [CORRECT]
  D) true:false:true
*/

public class _13_Question_LocalDate_Object_Equality {
   public static void main(String [] args) {
       LocalDate d1 = LocalDate.parse("1999-09-09");
       LocalDate d2 = LocalDate.parse("1999-09-09");
       LocalDate d3 = LocalDate.of(1999, 9, 9);
       LocalDate d4 = LocalDate.of(1999, 9, 9);
       System.out.println((d1 == d2) + ":" + (d2 == d3) + ":" + (d3 == d4));

       System.out.println(d1.hashCode());     // 4094537
       System.out.println(d2.hashCode());     // 4094537
       System.out.println(d3.hashCode());     // 4094537
       System.out.println(d4.hashCode());     // 4094537
   }
}
 
Clemonte Johnstone
Ranch Hand
Posts: 110
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, the source is the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry
 
Clemonte Johnstone
Ranch Hand
Posts: 110
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I check the source for == or what source should I check for this question?
 
Clemonte Johnstone
Ranch Hand
Posts: 110
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The source is the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry
 
Marshal
Posts: 26591
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 3705
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Marshal
Posts: 72909
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Clapham
Marshal
Posts: 26591
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . The API documentation doesn't mention that feature . . . .

That allows them to keep their options open...



And to make the answer to that exam question incorrect. This is why I'm unimpressed, let's say, by the question.
 
Master Rancher
Posts: 3889
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 2864
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For having a question mentioned in the January 2021 CodeRanch Journal, congratulations: this question earns you a cow
 
Look ma! I'm selling my stuff!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic