Home  >  Article  >  Java  >  Rules for Java data types

Rules for Java data types

高洛峰
高洛峰Original
2017-01-21 16:39:441504browse

Rules of data types

Select the appropriate data type to store, for example, if it is used to store age, its value cannot exceed 200, so use short storage, which wastes 2 bytes, which is very economical. 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 long to store it, which is a bit wasteful.

However, it is no different now than 10 years ago. In the past, programming was all done on microcomputers. The resources for programming on a microcomputer are very small, and each memory is very small. Therefore, at that time, you must care about the use of its resources and cannot waste the resources inside, otherwise the resources will become smaller and smaller.

Nowadays, any memory can exceed GB, and servers have hundreds of GB or more. So now you don’t need to worry about the use of data types. In Java, it is said that if your integer value is less than a few billion, then only use int. Int is a universal integer type, which is not a waste at all for today’s machines. On the contrary, it will be much simpler and less troublesome. If you use short to store age, byte to store 100 values, and int to store the price of a house, it will be troublesome, and it will overflow during transformation. As shown in Figure 3.5:

Rules for Java data types

If you have to turn it upside down again, it will overflow again, which is quite messy. So Java says that the default for integers is int. If it is a decimal, the default is double. Even if you don't use int, it's still int, and if you don't use double, it's still double.

The data type is the space size selected for data storage. Everyone’s storage size is different, so we have to choose. However, in Java, he recommends us to use int for integers and double for decimals. If you have to replace it, it is the same. OK.

For more articles related to the rules of Java data types, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn