Java language learning 3-Variables

Source: Internet
Author: User

Variables are the most basic thing in a language and are no exception in Java. In terms of variable naming, Java allows a variable to start with a letter, underscore, or $, but generally only starts with a lower-case letter, and rarely uses the underscore or $ symbol. In addition, you need to take a closer look at the keywords and reserved words of Java. I often ask in the pen questions whether const is a Java keyword. So pay attention to it.

The next step is the Java variable type. There are eight types of Java variable types in the original database: byte, short, int, long, float, double, boolean, char.

Byte type

An 8-bit binary represents a signed integer, which must be emphasized here.

Byte indicates that the range is-128 to 127, because for positive numbers, the maximum value is 0111 1111, where the first digit is the symbol bit, 0 indicates the positive number, and the last seven 1 indicates the value, it Exactly indicates 127. For negative numbers, why is the minimum value-128?

Because the computer uses the complement code to represent a negative number, the method to calculate a negative number is: first write the original code of a negative number, for example,-127 can be expressed as 1111 1111, where 1 of the first digit represents the symbol bit, first, retrieve all the characters except the symbol bit to get 0000, and then add 1 to the number to get 0001, and then add the symbol bit to get the complement code of this number: 1000 0001.

Using the same method, we can find that the complement code of-126 is 1000 0010, And the complement code of-1 is 1111 1111. From this, we can see that for negative numbers, the smaller the last seven digits, the larger the number, so the maximum negative number should be 1000, so what does it mean? We reverse the 7-bit bitwise operators except the symbol and Add 1 to get the original code of this number: 000 0000 to get the inverse 111 1111, plus 1 to get 1000
0000, the number is exactly 128, so the minimum value is-128.

Short Type

It is represented by a 16-bit binary signed integer. The derived short representation range is similar to that of byte, which is not described here.

Int type

It is represented by a 32-bit binary signed integer, which is the default expression of the integer, but here we need to emphasize what happens when the type is automatically upgraded and forcibly downgraded.

For example:

byte num1 = -126;int num2 = num1;

In this case, num1 will automatically upgrade the type, as shown below:

-126 of the complement code is 1000 0010

When the type is upgraded, the rule indicates that the symbol bit is automatically extended to the high bit, that is, 1111 1111 1111 1111 1111 1111 1000. The original code of this number is * 000 0010 0000 0000 0000 0000 0000 0111, the value is exactly 126, and the symbol is added to indicate-126, which is no problem.

However, if it is forced downgrade, problems may occur, as shown below:

int num1 = -132;byte num2 = (byte)num1;

In this case, the int type of-132 is: 000 0000 0000 0000 0000 0000 1000 0100

Bitwise inversion and 1 complement: 111 1111 1111 1111 1111 1111 0111 1100 0111 <= (1011 + 1)

Add the symbol bits to the front side: 1111 1111 1111 1111 1111 1111 0111 1100

When it is forcibly converted to byte, the value is 0111 1100

The value above is changed to positive 124, not only the value has changed, but also the symbol.

To sum up, this forced type conversion is very dangerous and requires caution.

Long TYPE

An integer in 8 bytes.

Float Type

The number of single precision expressed in 32 bits. Since decimal places are double by default, the suffix f must be added after the decimal places to indicate the float type.

Double Type

64-bit double precision.

For the double type (including the float type), pay special attention to the inaccuracy in the representation of decimal places. Therefore, pay special attention to the exact operation. Not only that, but you should pay attention to this problem even when performing common calculations. For example, the following program:

double num1 = 0.1;double num2 = 0.2;System.out.println("num1+num2=" + (num1+num2) + "!");

When running this program, the output result is num1 + num2 = 0.30000000000000004, rather than 0.3, which we take for granted. If we use if (0.3 = num1 + num2) for determination, the result will always be false, which is very different from the expected result.

Boolean Type

There are only two values true or false, but the bucket is not fixed.

Char type

A 16-bit Unicode character, \ u0000 to \ uffff, indicating 65536 characters.

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.