Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1429 :

Posted: Sun Nov 04, 2018 5:34 am
by flex567
How many LocalDate objcts were created during this statement?:

Code: Select all

java.time.LocalDate dt = java.time.LocalDate.parse("2015-01-01").minusMonths(1).minusDays(1).plusYears(1);
I would say only 1, from the parse method.

Re: About Question enthuware.ocajp.i.v8.2.1429 :

Posted: Sun Nov 04, 2018 6:12 am
by admin
And what about the chained method calls .minusMonths(1).minusDays(1).plusYears(1); ?

Re: About Question enthuware.ocajp.i.v8.2.1429 :

Posted: Sun Nov 04, 2018 7:10 am
by flex567
Don't they work on the same reference?

Re: About Question enthuware.ocajp.i.v8.2.1429 :

Posted: Sun Nov 04, 2018 8:09 am
by admin
This is what the explanation says,
Observe that most of the methods of LocalDate (as well as LocalTime and LocalDateTime) return an object of the same class. This allows you to chain the calls as done in this question. However, these methods return a new object. They don't modify the object on which the method is called.
You might want to go through "Creating a date/time object using an existing date/time object" paragraph of Section 12.3.2 Creating date/time objects from OCAJP Fundamentals book by Hanumant Deshmukh..

Re: About Question enthuware.ocajp.i.v8.2.1429 :

Posted: Sun Nov 04, 2018 11:38 am
by flex567
So this is like with String class.
And not like with StringBuilder class.

How do you know if a method returns a new object or modifies the existing from Java API documentation?

Re: About Question enthuware.ocajp.i.v8.2.1429 :

Posted: Sun Nov 04, 2018 8:15 pm
by admin
Yes, from the javadoc API description.