In the source code of the LocalDate class I saw that the private instance variables month and day were shorts instead of ints.
This is the Docs of the LocalDate class.
** A short part of the source code **
private final int year;
private final short month;
private final short day;
private LocalDate(int year, int month, int dayOfMonth) {
this.year = year;
this.month = (short) month;
this.day = (short) dayOfMonth;
}
public int getDayOfYear() {
return getMonth().firstDayOfYear(isLeapYear()) + day - 1;
}
public int getMonthValue() {
return month;
}
public int getDayOfMonth() {
return day;
}
As you can see beside the variables themselve, the int data type is used for month and day. Why make it a short then?
And why not this?
private final short year;
private final byte month;
private final byte day;