为什么不对所有整数值使用 long
Why not use long for all integer values
在我的 Java class 中,我们刚刚了解了以下每种原始数据类型:
byte
short
int
long
由于 long
数据类型包含的位数最多,是否可以专门使用 long
数据类型来避免限制?
问题
- 仅使用
long
数据类型是否有特别的缺点?
- 使用
int
数据类型而不是 long
数据类型是否有意义?
Does it make sense to use for example, an int
data type, instead of a long
data type?
绝对是。
内存/磁盘使用
仅使用一两个变量您不会看到性能差异,但是当应用程序增长时它会提高您的应用程序速度。
检查this question for further info。
同时查看 Oracle primitive type documentation 您可以看到一些建议和内存使用情况:
type memory usage recommended for
------- --------------- ---------------------------------------------------
byte 8-bit signed The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
short 16-bit signed same as byte
int 32-bit signed
long 64-bit Use this data type when you need a range of values wider than those provided by int
float Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
byte
:
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
short
:
The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
int
:
By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2³¹ and a maximum value of 2³¹-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2³²-1.
long
:
The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2⁶³ and a maximum value of 2⁶³-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2⁶⁴-1. Use this data type when you need a range of values wider than those provided by int.
float
:
The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
代码可读性
另外,它会澄清你的想法和你的代码,比方说,你有一个代表对象 ID 的变量,这个对象 ID 永远不会使用小数,所以,如果你在你的代码中看到:
int id;
您现在可以确定此 ID 的外观,否则
double id;
不会。
此外,如果您看到:
int quantity;
double price;
你会知道 quantity
不允许小数(只有完整的对象)但价格可以...这使你的工作(和其他程序员会阅读你的代码)更容易。
除了范围(可以存储在任何特定数据类型中的最小值和最大值)之外,还有另一个方面,那就是变量的 size
。
您还必须了解以下内容:
byte = 1 byte
short = 2 bytes
int = 4 bytes
long = 8 bytes
因此使用 long
变量意味着您正在为其分配 8 bytes
内存。
有点像,
long var = 1000L
未显示内存的有效使用。如果你现在有 GB 的 RAM 并不意味着我们应该浪费它。
我想说的简单一点是,内存使用效率更高,应用程序速度更快。
内存需求和速度方面的性能会使 long
昂贵。
然而 int
还有一个优点:太容易与 int
子表达式混合,并且在组合操作错误时可能会丢失长信息。 50 左移一位实际上做 18 等等。仅使用带有 L
后缀的数字是一种措施。 long
也可以用作 int
乘法和其他此类运算的溢出捕获,在这种情况下,您可以长时间检测溢出。
请注意,对于 int
"all" byte 和 short 的操作正在传播到 int
。
在我的 Java class 中,我们刚刚了解了以下每种原始数据类型:
byte
short
int
long
由于 long
数据类型包含的位数最多,是否可以专门使用 long
数据类型来避免限制?
问题
- 仅使用
long
数据类型是否有特别的缺点? - 使用
int
数据类型而不是long
数据类型是否有意义?
Does it make sense to use for example, an
int
data type, instead of along
data type?
绝对是。
内存/磁盘使用
仅使用一两个变量您不会看到性能差异,但是当应用程序增长时它会提高您的应用程序速度。
检查this question for further info。
同时查看 Oracle primitive type documentation 您可以看到一些建议和内存使用情况:
type memory usage recommended for
------- --------------- ---------------------------------------------------
byte 8-bit signed The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
short 16-bit signed same as byte
int 32-bit signed
long 64-bit Use this data type when you need a range of values wider than those provided by int
float Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
byte
:
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
short
:
The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
int
:
By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2³¹ and a maximum value of 2³¹-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2³²-1.
long
:
The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2⁶³ and a maximum value of 2⁶³-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2⁶⁴-1. Use this data type when you need a range of values wider than those provided by int.
float
:
The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.
代码可读性
另外,它会澄清你的想法和你的代码,比方说,你有一个代表对象 ID 的变量,这个对象 ID 永远不会使用小数,所以,如果你在你的代码中看到:
int id;
您现在可以确定此 ID 的外观,否则
double id;
不会。
此外,如果您看到:
int quantity;
double price;
你会知道 quantity
不允许小数(只有完整的对象)但价格可以...这使你的工作(和其他程序员会阅读你的代码)更容易。
除了范围(可以存储在任何特定数据类型中的最小值和最大值)之外,还有另一个方面,那就是变量的 size
。
您还必须了解以下内容:
byte = 1 byte
short = 2 bytes
int = 4 bytes
long = 8 bytes
因此使用 long
变量意味着您正在为其分配 8 bytes
内存。
有点像,
long var = 1000L
未显示内存的有效使用。如果你现在有 GB 的 RAM 并不意味着我们应该浪费它。
我想说的简单一点是,内存使用效率更高,应用程序速度更快。
内存需求和速度方面的性能会使 long
昂贵。
然而 int
还有一个优点:太容易与 int
子表达式混合,并且在组合操作错误时可能会丢失长信息。 50 左移一位实际上做 18 等等。仅使用带有 L
后缀的数字是一种措施。 long
也可以用作 int
乘法和其他此类运算的溢出捕获,在这种情况下,您可以长时间检测溢出。
请注意,对于 int
"all" byte 和 short 的操作正在传播到 int
。