Java 0 Basic Starter Series Day3 Java base data type

Source: Internet
Author: User

The first two have already completed the development environment, and if you have already completed the deployment according to the previous tutorial, then one of the best programming languages in the world and one of the best Ides in the world has already appeared on your computer (applause here), if you haven't started, or are pacing on the steps, You may not understand the meaning of the "best", it's OK, this delicacy needs to be chewed to taste its uniqueness.

Next, let us come together to formally uncover the veil of Java.

Java is a simple, object-oriented, high-performance, high-security, strong, and well-ported language (in the face of the nonsense). Java syntax is similar to C + +, if you have a C + + learning experience, learning Java is a lot easier to learn, Java at first many features are the same as C + +, such as basic program structure, classes, grammar rules and so on, but delete a lot of complex things, such as header file, structure, union, Operator overloading, virtual base class, and so on, see here, learn C + + You may feel very happy, and finally do not have to deal with those weird things. Do not be happy too early, because, there is more to make you Happy (manual funny), the most headache of the pointer operation part, also removed, can be said to be a pure simplified version of C + +, but also added a lot of their own features, without multiple inheritance, instead of the interface and internal classes, because not allowed to directly manipulate pointers, So there will not be a blind operation of the pointer to the Magic bug, of course, there is a lot less fun to find the bug (awkward), Java can be trusted in security, because never access a bad pointer, and cause memory allocation errors, there is no memory leak problem. All of those security risks that are caused by pointers do not exist.

Perhaps said so much, for the novice you, look at the foggy, unintelligible, it doesn't matter, you just need to know that Java is not difficult to get started, just a little more than ten laps around the playground, but if you just want to take a walk, then you may not have the opportunity to enjoy the sweat after the satisfaction.

On the other, Java can write desktop, can write Android, can write server, can write enterprise-class business, can write Internet business. If you want to develop in Android, server development, website background development and maintenance, then Java will be a good choice for you. If you just want to worship this door all year round the programming development language ranking top of the language, that can also come to see, as the saying goes, the technology is not pressure, see not programming friends, but also can put an X said I also learned Java people (laughter).

Simply introduced the Java function, can insist to see this shows that you have a little heart, want to continue to learn, then with me to carry out the system of learning it.

To learn Java, it is natural to start with the basic data types, so what is the data type? As the name implies, there are different types of data, just like men and women by gender, according to the age of the old less, according to the disposition (cough, here just to raise a chestnut), in Java, the basic data types are four, integer type, floating point type, Boolean type and character type. Here are a few of them.

Integer, Nature is used to store integers, such as: 1,2,3,4,100, and so on, the general integer can be used to store and operate int type, integer type can also be divided into int type, short type, long type, byte type, and so on according to the requirements of the range of values, the order of the brothers, The big to the small are long, int, short, and Byte, respectively. The long type is the largest and occupies 8 bytes (in Java, the smallest storage unit is a bit, that is, bit, byte is byte, the conversion of a bit is: 1byte=8bit,1kb=1024bytes=2^10bytes), as the eldest of the integer, Occupy 8*8=64 bit , so removing the first bit is used to store the symbol bit, and the other 63 bits can be used to represent the value size, so it is represented as: -2^63-2^63-1. It's probably a exascale level, so if you're storing your bank card balance, don't bother long (funny). As the most commonly used integer-type second, the int is well known, almost become the representative of the integer type, it occupies only 4 bytes, the value range is -2^31-2^31-1, a little more than 2.1 billion. And old three short, as the name implies, compared to the "shorter", not with the 18cm long type, only occupies 2 bytes, the range is -2^15-2^15-1, that is-32768-32767, to do a small range of subtraction words is sufficient. The last introduction of the old four, byte type, only occupies one byte, it is quite tight, said the range is-128-127. So deposit the balance, byte,short type is too short, long type is too lengthy, so the most economical with int type.

Novice programmers may ask, good integer type, but also sub-fine. You don't get it, give me a chestnut, because the memory space used to store the data is based on bits, which can be understood as a lot of the same small boxes, the same data types need to use the same size space, or the program will not know the beginning and end of the data, with the data head to hold the size of the length of information is obviously a waste , the question now is, how much space is appropriate for an integer? You may feel that the same size of the space is not good, it is used 8 bytes to store, used to store large values when very useful, but to store your age, as if the waste of space is a bit more, like now you have a basketball, a tennis, a football, a table tennis, All need to use the same size box to store alone, the size of nature can only be calculated according to the largest, when you put a table tennis in that big box, do not feel a waste of space. Therefore, in order to avoid such unnecessary waste, it is necessary to design multiple integer types. Of course, there is no need for memory too preoccupied, if not in the algorithm research, not to the time and space efficiency requirements are very harsh conditions, basically with the int type is good. After all, the operational effect is more important than the operational efficiency.

Next, the floating-point type, why it is called floating-point type, is naturally used to denote floating-point numbers (nonsense), and what is floating-point number, this. There is another piece of paper. In a computer, there are two ways to represent decimals, a call point, a floating point.

As an example of a 32-bit program, the point is that the decimal point is fixed somewhere in the 32-bit position, preceded by an integer, followed by a decimal. The decimal point is fixed exactly where you can specify it yourself in the program. For example, in the above example, the decimal point is at the fractional bit. Whether you are 124.25, 0.5, or 100, the decimal point is fixed in the position of the fractional bit.

Floating-point numbers are relative to the fixed-point number, which is represented by the highest 1 bits is the sign bit s, the next 8 bits are exponential e, and the remaining 23 bits are the valid number M. For example:

5 DEC = 101 BIN = 1.01 x 2^2

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 look at this article, https://www.cnblogs.com/jillzhang/archive/2007/06/24/793901.html

, of course, the novice suggested not to look at first, lest be frightened away, you just need to know, floating point is used to access the number with a decimal point can be.

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

float:2^23 = 8388608, a total of seven, which means that there can be up to 7 valid digits, but the absolute guarantee of 6-bit, that is, the precision of float is 6~7 bit valid number;

double:2^52 = 4503599627370496, altogether 16 bits, in the same vein, the precision of the double is 15~16 bit.

If you're not sure about the two types, it's just a double type, and the most common floating-point type in Java is the double type.

A little bit more, the Boolean can not wait, don't worry, the Boolean type (Boolean) is the simplest, only true (true) and False (false) two states.

The last is the character type (char), used to hold the character data type, occupies 2 bytes, using Unicode encoding, its first 128 byte encoding and ASCII compatible characters storage range in \u0000~\uffff, in the definition of character type of data should pay attention to ", such as ' 1 ' Represents the character ' 1 ' instead of the value 1.

Well, the basic data types are introduced so much, in general, the integer used to hold integers, floating-point type used to store decimals, Boolean used to store the true and false, mainly used for logical judgment, character type used to store a single character, such as: ' A ', ' B ', ' C ' and so on. In Java, integers are defaulted to int, and decimals default to type Double.

Finally, there are different data types that can be converted to each other. Java is also the rule of the jungle, who occupies a large number of people, an integer int with a floating-point double operation, will be automatically converted to double type, so that the loss of precision can be calculated numerically. The following is a legal conversion between data types:

  

Then I have to do "illegal conversion" what to do, a 8-byte double type is not to be converted to 4 bytes of int What to do, it can only be coerced type conversion, but this may result in the loss of precision. The specific situation is not much to do the introduction, otherwise said half a day to say the end.

Well, today's content to this end, the basic data types have been looked at, if still some do not understand, there is no relationship, a return to two back to ripe. More days, let's talk about it later. (Slipped away)

Java 0 Basic Starter Series Day3 Java base data type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.