Rules for data types
Choose the right data type to store, for example, if it is used to store age, its value cannot exceed 200, so just use short to store it, which is very cost-effective. If the value is never greater than 100, then we will use byte to store it. If the value is not greater than 100, we have to use a long-term integer to store it, which is a bit wasteful.
However, now it is no more than 10 years ago, and the previous programming was done on microcomputers. The resources for programming on the microcomputer are very small, one memory and one memory are very small, so at that time, you must care about the use of its resource issues and cannot waste the resources inside, otherwise the resources will become smaller and smaller.
Nowadays, any memory is G, and the server is more than hundreds of 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, then just use int. Int is a general integer type, which is not wasted at all for today’s machines. On the contrary, this will be much simpler and will not be so troublesome. If you use short to store age, byte to store 100 values, and use int to store the price of a house as troublesome, and it will overflow during transformation. As shown in Figure 3.5:
Figure 3.5
It's going to be reversed, and it will overflow again when it comes back, which is quite messy. So Java says that as long as integers default to int. If it is a decimal, the default is double. Even if you don’t need to int, he is still int, and if you don’t need to double, he is also double.
The data type is the space size selected by the data storage. Everyone's storage size is different, so we have to choose, but in Java it recommends that we use int in integers and double decimals. It is also OK if we have to replace them.