Java Note: Part 6 first understanding of data types (part I), part 6 of java
# Routine nagging 1 # the recent power outage in the residential area has caused me to doubt my life. From today on, we should be able to get back to the previous pace
# Daily nagging 2 # I wrote a blog in the previous article. From today on, I have come to the theory again in two days. Everyone is stable and there will be code in two days.
A convenient programming tool is available. Now we have everything we can, and we can start to explore Java.
The first concept we need to contact is the data type. What isData TypeWhat about it? Is the data type ). What is the data? From the perspective of computer science, data is information, and our computer is centered around this information, that is, Data. I will not give examples from other perspectives. I will say too much for fear of your side. That is to say, data is the core of our program and there is no program without data.
To distinguish the differences among several data types, the concept of data type emerged. With the data type, the program can be transferred to specialized management for different data types for different processing.
In Java, data types are dividedBasic Data TypeAndReference data type. Basic data types are dividedInteger(Integer ),Floating PointType(Decimal type ),Character Type,Boolean, The referenced data types are dividedClass,Interface,Enumeration,Array.
The reference data type is not our focus. We will detail the reference data type later. Here we only need to pay attention to the basic data types.
First, let's talk about integer. integer is the type of data whose content is an integer. There are four types of integer types:Byte,Short,Int,Long(Large programming languages are so troublesome ). These four different types can be used to represent integer data, but they have the difference in the number range of tables (that is, the difference in the occupied storage space ), the following table lists the storage space used by various types and the table quantity range:
Data Type |
Occupied storage space |
Table quantity range |
Byte |
1 byte |
-128 ~ 127 |
Short |
2 bytes |
-32768 ~ 32767 |
Int |
4 bytes |
-2147483648 ~ 2147483647 |
Long |
8 bytes |
-263 ~ (263-1) |
Why are all positive numbers in the table's number range smaller than negative numbers? Because there is another 0. Generally, we use the int type as the default Integer type, although sometimes we do not need such a large table number range. Java also agrees with this point, so the default type of the integer is set to int (that is, I randomly create an integer in Java, which is the int type ), but what if we want to use another type? If you want to use byte and short, there is no relationship, because int can be directly converted to byte and short (We will write about the transformation later), but what if you use the long type? You need to use the ending mark by adding an L to the end of the integer (Note: Do not use lower-case L here, because lower-case L is similar to the numbers 1 and I in upper case, we will write about question L later ). For example, 123 is an int type data while 123L is a long type data.
Some people have to ask, what should I do if I want to use an integer to represent a larger number than (263-1? Java provides a class called BigInteger, which can represent an integer larger than long, but this is a suffix (the BigInteger class will be written later ).
In Java, integers can be expressed in four hexadecimal notation: binary, octal, decimal, and hexadecimal. By adding 0 B (or ob) to the front of the integer, it is proved that the number you are playing is a 2-digit integer, and the 8-digit is added to the front with 0, 10 in the hexadecimal format, add 0x or ox to the front in hexadecimal notation.
Now let's talk about the integer type, the floating point type, which is the type of data whose content is decimal. The floating point type is okay. It can only be divided into two types,FloatAndDouble. Their differences are the same as those of integer types, except that the table's number range (occupied storage space) is different:
Data Type |
Occupied storage space |
Table quantity range |
Float |
4 bytes |
-3.403e38 ~ 3.403e38 |
Double |
8 bytes |
-1.798e308 ~ 1.798e308 |
If you have learned mathematics, you should know that you can write an infinite number of digits after the decimal point. Therefore, the floating point type is just a rough estimation, which may lead to errors. A float type can contain only seven valid digits. It is called a single-precision decimal. The precision of the double type is twice that of the float, so it is also called the double precision decimal point (the double English intention is to double, do you know whether you have heard of this word: double kill ). Java floating point type is of the double type by default. If float is to be used, the tail mark f or F must be added to the end (I suggest using uppercase F). For example, 3.14 is of the double type, 3.14F is of the float type (adding d or D after the decimal point can indicate that the decimal point is of the double type. Although this is meaningless, you must know it ).
Decimal is so inaccurate, so we should avoid using decimal. If we really need to perform a strict decimal operation, we can use the BigDecimal class (which will be written later ).
There are two methods to represent Decimals in Java: normal decimals and scientific notation decimals. You do not need to write special annotations. We should know about scientific notation. For example, 3.14e2 is 3.14 × 102 = 314, and 3.14e-2 is 3.14 × 10-2 = 0.0314.