Rules for Java data types

Rules for data types

Select the appropriate data type to store. For example, it is used to store the age. Its value cannot exceed 200. Then use short to store it. Only 2 bytes are wasted, which is very economical. If the value is never greater than 100, we will use byte to store it. If the value is not greater than 100, we have to use long integer to store it, which is a bit wasteful.

However, now it is no more than 10 years ago. The previous programming was done on a microcomputer. For programming on a microcomputer, its resources are very small, and each memory is very small, so we must care about the use of its resources at that time. We can't waste the resources inside, otherwise the resources will become smaller and smaller.

Today's random memory is more than 100 g, and the server is more than 100 g. So now you don't need to care about the use of data types. In Java, if your integer value is less than a few billion, only int is used. Int is a general integer type, which is not wasted for current machines. On the contrary, it will be much simpler and less troublesome. If you use short to store the age, byte to store the 100 value, and int to store the house price, it's so troublesome, and it will overflow during the transformation. As shown in Figure 3.5:

Figure 3.5

If you want to turn it upside down, it will overflow again. It's very messy. So Java says that as long as the integer defaults to int. If it is a decimal, it defaults to double. Even if you don't use int, he is int, and he is double without double.

Data type is a space size selected for data storage. Everyone's storage size is different, so we have to choose. However, in Java, he recommends that we use int for integer and double for decimal. If we have to replace it, it is also possible.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>