Java zero foundation entry series – day3 java basic data type

The first two articles have built the development environment. If you have completed the deployment step by step according to the previous tutorial, Then one of the world's best programming languages and one of the world's best ides has appeared on your computer (there should be applause here). If you haven't started or are pacing on the steps, you may not understand the meaning of "the best". It doesn't matter. We need to chew and swallow this delicious meal carefully to taste its uniqueness.

Next, let's officially unveil Java.

Java is a simple, object-oriented, high-performance, high security, strong robustness Transplantable language (serious nonsense). Java's syntax is very similar to C + +. If you have C + + learning experience, it will be much easier to learn Java. At first, many features of Java are the same as C + +, such as basic program structure, classes, syntax rules, etc., but many complex things are deleted, such as header files, structures, unions, operator overloading, virtual base classes, etc Wait, see here, you may feel very happy to learn C + +, and finally you don't have to deal with those strange things. Don't be happy too soon, because, And what makes you happier (manual funny), the most troublesome pointer operation part is also removed. It can be said to be a pure and simplified version of C + +, but it also adds a lot of its own features. There is no multiple inheritance. Instead, the interface and internal classes are replaced. Since direct operation of the pointer is not allowed, there will be no magical bugs due to blind operation of the pointer. Of course, there are many fewer The fun (embarrassment) of finding bugs. Java can be trusted in security performance, because it will never access a bad pointer, resulting in memory allocation errors, and there is no memory leakage problem. All the security risks caused by pointers do not exist.

Maybe having said so much, you, a novice, can't understand what you're talking about. It doesn't matter. You just need to know that it's not difficult to get started with Java. It's only a little harder than running around the playground for ten times. But if you just want to take a walk, you may not have a chance to enjoy the satisfaction behind sweating.

To get down to business, Java can write desktop, Android, server, enterprise business and Internet business. If you want to develop in Android development, server development, website background development and maintenance in the future, Java will be your good choice. If you just want to worship this language, which has always been at the top of the list of programming and development languages, you can also take a look. As the saying goes, good skills don't weigh you down. When you meet friends who can't program in the future, you can also pretend to be an X and say that I have also studied Java (hold a smile).

After a brief introduction to the functions of Java, it shows that you are a little excited. If you want to continue learning, please come with me for systematic learning.

To learn Java, we should naturally start with the basic data types. So, what is a data type? As the name suggests, different data types are different, just like people are divided into men and women according to gender, old, middle-aged and young according to age, and attack and accept according to temperament (cough, this is just to give a chestnut). In Java, there are four basic data types, integer, floating point, Boolean and character. Let's introduce them one by one.

Integers are naturally used to store integers, such as 1,2,3,4100, etc. generally integers can be stored and operated by int. integers can also be subdivided into int, short, long and byte according to the requirements of the value range. These brothers are arranged in order, from large to small, which are long, int, short and byte. Long type is the largest, Occupied 8 bytes (in Java, the smallest storage unit discussed is bit, that is, bit, byte is byte, and the conversion from bit to bit is: 1byte = 8bit, 1KB = 1024bytes = 2 ^ 10bytes). As the largest integer, it occupies 8 * 8 = 64 bits. Therefore, except that the first bit is used to store symbol bits, the other 63 bits can be used to represent the size of numerical values, so its representation range is: - 2 ^ 63-2 ^ 63-1. It is about 10 billion So if it's used to deposit your bank card balance, Don't bother with the long type (funny). As the most commonly used integer type, the int type is well known and has almost become the representative of the integer type. It only takes up 4 bytes and the value range is - 2 ^ 31-2 ^ 31-1, a little more than 2.1 billion. The third short, as the name suggests, is relatively "short" , it can't be compared with the 18cm long type. It only takes 2 bytes, indicating that the range is - 2 ^ 15 -- 2 ^ 15-1, that is - 32768 -- 32767. It's enough to add, subtract, multiply and divide in a small range. Finally, the old four, byte type, occupies only one byte, which can be described as quite tight. The representation range is - 128-127. Therefore, if the deposit balance is too short for byte and too long for long, it is most economical to use int.

Beginners of programming may ask, why is it so fine to have a good integer type. You don't understand. Take chestnuts for example, because the memory space used to store data is based on bits, which can be understood as many, many, many, the same small boxes. The same data type needs to use the same size of space, otherwise the program doesn't know the start and end positions of data. It's obviously a waste of space to use the data header to store size and length information, So the question now is, how much space is appropriate for an integer? You may think that it's not good to use the same size of space. It's all stored in 8 bytes. It's easy to use when it's used to store large values, but when it's used to store your age, it seems to waste a little more space. For example, now you have a basketball, a tennis ball, a football and a table tennis ball, which need to be stored separately in a box of the same size, Naturally, the specification can only be calculated according to the largest one. Don't you think it's a waste of space when you put a table tennis ball in that big box. Therefore, in order to avoid such unnecessary waste, it is necessary to design a variety of integer types. Of course, there is no need to be too fussy about memory. If you are not doing algorithm research and demanding time and space efficiency, basically use int. After all, in general, operation effect is more important than operation efficiency.

Next, let's talk about floating-point type. Why is it called floating-point type? Naturally, it is used to represent floating-point number (nonsense). What is floating-point number? This is another article. In computer, there are two methods to represent decimal, one is fixed-point and the other is floating-point.

Take the 32-bit program as an example. Fixed point means that the decimal point is fixed at a certain position in the 32-bit program. The first is an integer and the last is a decimal. Where the decimal point is fixed can be specified in the program. For example, in the above example, the decimal point is 23 bit. Whether you are 124.25, 0.5, or 100, the decimal point is fixed at 23 bit.

Floating point numbers are relative to fixed-point numbers. The representation of floating-point numbers is:

  5 DEC = 101 BIN = 1.01 x 2^2

  100 DEC = ‭01100100‬ BIN = 1.100100 x 2^6

  0.125 DEC = 0.001 BIN = 1 x 2^-3

Now, the position of the decimal point is floating. If you want to know about the storage of floating-point numbers, you can see this article, https://www.cnblogs.com/jillzhang/archive/2007/06/24/793901.html

Of course, if you are a novice, it is recommended not to read it first to avoid being scared away. You only need to know that floating point is used to access numbers with decimal points.

Floating point numbers are also divided into two types, float and double. Float occupies four bytes and the value range is - 2 ^ 128 ~ + 2 ^ 128, that is -3.40e + 38 ~ + 3.40e + 38; Double occupies eight bytes, and the value range is - 2 ^ 1024 ~ + 2 ^ 1024, that is -1.79e + 308 ~ + 1.79e + 308.

Float: 2 ^ 23 = 8388608, a total of seven digits, which means that there can be up to seven significant digits, but the absolute guarantee is 6 digits, that is, the accuracy of float is 6 ~ 7 significant digits;

Double: 2 ^ 52 = 4503599627370496, 16 bits in total. Similarly, the precision of double is 15 ~ 16 bits.

If you are confused about the two types, just use the double type. The most commonly used floating-point type in Java is the double type.

It's a little too much. Booleans can't wait. Don't worry. Booleans are the simplest. There are only two states: true and false.

The last is the character type (char), which is used to store the data type of characters. It occupies 2 bytes and is encoded in Unicode. Its first 128 bytes are encoded and compatible with ASCII. The storage range of characters is \ u0000 ~ \ uFFFF. When defining character type data, pay attention to adding ',' for example, '1' indicates the character '1' rather than the value 1.

Well, that's all for basic data types. In general, integer type is used to store integers, floating point type is used to store decimals, boolean type is used to store true and false, mainly for logical judgment, and character type is used to store single characters, such as: 'a', 'B', 'C', etc. In Java, integer is of type int by default, and decimal is of type double by default.

Finally, different data types can also be converted to each other. Java also has the law of the jungle. Whoever occupies more land is bigger. When an integer int is operated with a floating-point double, it will be automatically converted to double, so that numerical operations can be carried out without precision loss. The following are legal conversions between data types:

What should I do if I have to carry out "illegal conversion"? What should I do if I have to convert an 8-byte double type into a 4-byte int type? I can only use forced type conversion, but this may cause the loss of precision. I won't introduce more details for the time being, otherwise I'll have to say it for a long time.

Well, that's the end of today's content. I've read all the basic data types. If I still don't understand it, it doesn't matter. I'll be born twice. It's a long time coming. Let's talk about it later. (slip away)

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
分享
二维码
< <上一篇
下一篇>>