java - 为什么不对所有整数值使用 long

标签 java integer long-integer

在我的 Java 类(class)中,我们刚刚学习了以下每种原始数据类型:

  • 字节
  • int

由于 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.

字节:

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.

:

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.

:

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 不允许小数(只有完整的对象),但价格可以...这使您的工作(以及其他程序员将阅读您的代码)更容易。

关于java - 为什么不对所有整数值使用 long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32608153/

相关文章:

java - 如何在不同的类中获取2个ArrayList?

algorithm - 了解长除法算法

c++ - 无法创建动态整数数组

android - 类型转换为 Long

c - 8086/386 asm 与 bcc5 : returning long int from asm proc

java - 使用带有 POP3 协议(protocol)的 javamail 检索所有未读电子邮件

java - Selenium 网络驱动程序 : Extracting strings in a search result using a loop

java - 解析来自不受信任的 Java 序列化对象的数据

c - 如何将 16 位整数连续附加到固定的 8 位数组

assembly - 对一对有符号和无符号数字进行算术运算是否合法?