create object it doesn't mean that you must have a reference to it
LocalDate is an immutable date-time object
So
LocalDate d1 = LocalDate.now(); //1
d1.plusDays(1); //1
d1 = d1.minusMonths(2); //1
LocalDate d2 = d1.plusWeeks(3); //1
d2.minusDays(4); //1
d2 = null;
1+1+1+1+1=5 objects created
A:
LocalDate ld = LocalDate.now(); - This line creates the first LocalDate object, which represents the current date.
LocalDate ld2 = ld.plusWeeks(3); - This line creates the second LocalDate object. The plusWeeks method returns a new LocalDate object that represents the date three weeks after the date in ld.
The other methods called on ld and ld2 (plusDays, minusMonths, minusDays) also return new LocalDate objects, but these are not assigned to any variables, so they are not accessible after the method call and are eligible for garbage collection.
B. 3
Explanation:
1. Initially, d1 is assigned the current date using LocalDate.now(), creating the first LocalDate object.
2 d1.plusDays(1) returns a new LocalDate object that is one day ahead of d1. However, this new object is not assigned to any variable, so it is not counted as a created object.
3. d1 = d1.minusMonths(2) assigns a new LocalDate object that is two months before d1 to d1. This creates the second LocalDate object.
4. d1.plusWeeks(3) returns a new LocalDate object that is three weeks ahead of d1. However, this new object is not assigned to any variable, so it is not counted as a created object.
5. d2.minusDays(4) does not create a new LocalDate object because the return value is not assigned to any variable.
6. d2 = null assigns null to d2, but it does not create a new LocalDate object.
Therefore, the total number of LocalDate objects created is 3.
LocalDate is immutable (like String for example) and does not have public constructor but factory methods. When we create an obj, the proprieties cannot change. For example:
d1- now... today in other words
d1.plusDays(1)...tomorrow (...today is still today)
d1.minusMonth(2)...is the same date but two months ago...and so on.
Apparently I'm the only one answering this. BUT LocalDate is immutable so every method: plusDays(), minusMonths, plusWeeks, minusDays(). ALL return a new LocalDate Object. Resulting in 5 Objects being created.
Also d1.plusDays(1) on the second line would do nothing as this return value is not caught.
The correct answer is **A. 2**. In this example, two `LocalDate` objects are created. The first one is created when `d1` is initialized with the current date using the `now()` method. The second one is created when `d2` is assigned the value of `d1.plusWeeks(3)`. The other method calls, such as `plusDays(1)`, `minusMonths(2)`, and `minusDays(4)` do not create new objects because the `LocalDate` class is immutable and these methods return new objects instead of modifying the existing ones.
The correct answer is **A. 2**. In this example, two `LocalDate` objects are created. The first one is created when `d1` is initialized with the current date using the `now()` method. The second one is created when `d2` is assigned the value of `d1.plusWeeks(3)`. The other method calls, such as `plusDays(1)`, `minusMonths(2)`, and `minusDays(4)` do not create new objects because the `LocalDate` class is immutable and these methods return new objects instead of modifying the existing ones.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
ASPushkin
3 weeks agocathDev
2 months, 2 weeks agod7bb0b2
6 months agod7bb0b2
5 months agoduydn
7 months agoAPJ1
7 months, 2 weeks agoRoxyFoxy
7 months, 3 weeks agoRoxyFoxy
7 months, 3 weeks ago[Removed]
9 months, 2 weeks agoStavok
10 months, 2 weeks agoStavok
10 months, 2 weeks agobelal97
11 months ago