2
在类LOCALDATE的源代码,我看到的是,私有实例变量的月和日是短裤,而不是整数。LOCALDATE的私有变量的月和日短数据类型
This is the Docs of the LocalDate class.
**的源代码**
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;
}
,你可以在旁边的变量看themselve,int数据类型用于月,日的一个简短的一部分。那为什么把它缩短呢?
为什么不呢?
private final short year;
private final byte month;
private final byte day;
这也是我的想法,但他们为什么不通过将年份,字节月,短日期缩短来节省更多空间? –
可能是一个设计考虑因素,即对低级设备进行编程时,字大小将为16位(对于非常低级别的嵌入式设备也可能为8位)。对于大多数计算机来说,它是32位字的大小。即使它短暂,我认为操作是在32位,即。寄存器将是32位大小,总线将是32位,寻址将是32位字。 所以在计算机的情况下,除了短阵外,我们不会获得内存优势。 –