但是为什么说类用的内存多,不适合用于少量数据struct Point { int x, y; } As a struct, it requires 8 bytes (an integer is 4 bytes).class Point { int x, y; } As a class instance, it incurs some overheads: pointer to type (4 bytes) synchronization block (4 bytes) 'pointer'(多了个地址用内存, 4 bytes)----> x (4 bytes) y (4 bytes)但是结构不是要复制值才能操作吗 Yes.But in the case of '少量数据', it is ok. Even passing a reference requires the copy of the 'pointer'. Plus, you have all the advantages of passing by value.
{
int x, y;
}
As a struct, it requires 8 bytes (an integer is 4 bytes).class Point
{
int x, y;
}
As a class instance, it incurs some overheads:
pointer to type (4 bytes)
synchronization block (4 bytes)
'pointer'(多了个地址用内存, 4 bytes)----> x (4 bytes)
y (4 bytes)但是结构不是要复制值才能操作吗
Yes.But in the case of '少量数据', it is ok. Even passing a reference requires the copy of the 'pointer'.
Plus, you have all the advantages of passing by value.