为什么不对所有整数值使用long [英] Why not use long for all integer values

查看:139
本文介绍了为什么不对所有整数值使用long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java类中,我们刚刚了解了以下每种原始数据类型:




  • byte

  • short

  • int

  • long



由于 long 数据类型包含的位数最多,因此独占使用 long 数据类型以避免限制?



问题




  • 仅使用 long 数据类型是否存在特殊情况?

  • 使用例如 int 数据类型而不是 long 数据类型?


解决方案


是吗?有意义使用例如 int 数据类型,而不是 long 数据类型?


绝对是。






内存/磁盘使用



只使用一个或两个变量,您将看不到性能差异,但是当应用程序增长时,它将提高您的应用程序速度。 / p>

检查这个问题以获取更多信息



同时期待 Oracle原始类型文档你可以看到一些建议和内存使用情况:

 
建议输入内存使用情况------- ------ --------- ----------------------------------------- ----------
字节8位有符号字节数据类型对于节省大数组中的内存非常有用,其中节省的内存实际上很重要。
short 16位signed with byte
int 32位signed
long 64-bit当你需要的值范围比int
提供的值宽时,请使用此数据类型float如果需要在大型浮点数数组中保存内存,请使用float(而不是double)。绝不应将此数据类型用于精确值,例如货币。

byte


字节数据类型是 8位有符号二进制补码整数。它的最小值为-128,最大值为127(含)。 字节数据类型可用于在大型阵列中保存内存,其中节省的内存实际上很重要。




短数据类型为 16位带符号的二进制补码整数。它的最小值为-32,768,最大值为32,767(含)。与字节一样,适用相同的准则:在内存节省实际上非常重要的情况下,您可以使用短路来节省大型阵列中的内存

int


默认情况下,int数据类型是 32位有符号二进制补码整数,其最小值为-2³¹,最大值为2³¹-1。在Java SE 8及更高版本中,您可以使用int数据类型来表示无符号32位整数,其最小值为0,最大值为2³²-1。


long



< blockquote>

长数据类型是 64位二进制补码整数。带符号的长度最小值为-2⁶³,最大值为2⁶³-1。在Java SE 8及更高版本中,您可以使用长数据类型来表示无符号64位长,其最小值为0,最大值为2⁶⁴-1。 当您需要的值范围比int 提供的值更宽时,请使用此数据类型。


float


浮点数据类型是单精度 32位IEEE 754浮点。其值范围超出了本讨论的范围,但在Java语言规范的浮点类型,格式和值部分中指定。与字节和短的建议一样,如果需要在大型浮点数数组中保存内存,使用float(而不是double)。此数据类型绝不能用于精确值,例如货币。







代码可读性



此外,它会澄清您的想法和您的代码,比方说,您有一个表示对象ID的变量,此对象ID将永远不要使用小数,所以,如果你在代码中看到:

  int id; 

您现在可以确定此ID的外观,否则

  double id; 

不会。



另外,如果你见:

  int quantity; 
双倍价格;

你会知道数量将不允许小数(只有完整的对象),但价格会...这使你的工作(和其他程序员将阅读你的代码)更容易。


In my Java class we have just learned about of each of the following primitive data types:

  • byte
  • short
  • int
  • long

Since the long data type contains the most bits, wouldn't it make sense to exclusively use the long data type to avoid restrictions?

Questions

  • Is there a particular downfall with only using the long data type?
  • Does it make sense to use for example, an int data type, instead of a long data type?

解决方案

Does it make sense to use for example, an int data type, instead of a long data type?

ABSOLUTELY YES.


MEMORY / DISK USAGE

Using only one variable or two you won't see difference of performance, but when apps grow it will increase your app speed.

Check this question for further info.

Also looking to Oracle primitive type documentation you can see some advices and the memory usage:

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.


CODE READABILITY

Also, it will clarify your mind and your code, lets say, you have a variable that represents the ID of an object, this object ID will never use decimals, so, if you see in your code:

int id;

you will now for sure how this ID will look, otherwise

double id;

wont.

Also, if you see:

int quantity;
double price;

you will know quantity won't allow decimals (only full objects) but price will do... That makes your job (and others programmers will read your code) easier.

这篇关于为什么不对所有整数值使用long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆