Summary of variables used in Java

Source: Internet
Author: User
Tags integer numbers

Integers in Java default to int, and decimals default to double.

float n5=1.3; This sentence will be error, should be modified into such a float n5=1.3f;

Eight basic types

Variable type

Number of digits

Range

Note

Byte

8 Guests

-27-------27-1

Signed integer

Short

-digit

-215-------215-1

Signed integer

Int

level

-231-------231-1

Signed integer

Long

position

-263------263-1

Signed integer

Char

-digit

0-----216-1

unsigned integer

Float

level

Single-precision character points

Double

position

Double-precision character points

Boolean

1 Guests

Value: TRUE or False

The release of JDK1.7 has introduced some useful features, although most of them are syntactic sugars, but still greatly improves the readability and quality of the code. One feature is the underscore of literal constant numbers. Starting with Java7, you can write long integer numbers like 10000000000 in your Java code into a more readable 10_000_000_000. An important reason to underline in literal constant numbers is to avoid subtle errors that are difficult to find by looking at the code. In contrast to 10000000000 and 1000000000, it's hard to find a 0 or more 0 less, but not for 10_000_000_000 and 1_000_000_000. So if you're working with large numbers in the Java source, you can add underscores to the numbers to improve readability . When using the note: The literal constant number is underlined in a certain rule, the underscore can only be between the numbers, at the beginning or end of the number must not use underscores. In the following sections of this section, we will learn how to use underscores in literal constant numbers and the rules for using them in literal constant numbers.

How to use underline for numbers in Java

As I said before, this is just a syntactic sugar, much like the implementation of strings in a switch scenario, which is also accomplished with the help of a compiler. During compilation, the compiler removes the underscore and assigns the actual number to the variable. For example, 10_000_000 will be converted to 10000000 during compilation. Since the CPU has no pressure to deal with long numbers, we don't have to worry about the poor people who have trouble dealing with long numbers. This feature is especially useful in dealing with large amounts of money, credit card numbers, bank accounts, and other banking and financial areas that require long accounts. Although it is frustrating to write sensitive information in a Java file, we should never do this at the time of coding. But using underscores in numbers makes our life easier than ever.

Rules for underlining numbers in Java

The Java encoding language has strict rules for adding underscores to numeric literals. As mentioned above, you can only underline between numbers. You can't use a number to start with an underscore, or an underscore to end. Here are some other places that cannot be underlined on numeric literals:

    • At the beginning or end of a number
    • Decimal attachment to floating-point numbers
    • The front of the F or L subscript
    • When the numeric literal is of type string

Here are some examples to show which places are added to the numbers underline valid, which places the numbers underline invalid

float pi1 = 3_.1415f; null and void; cannot be underlined before the decimal point
float pi2 = 3._1415f; null and void; Cannot be underlined after a decimal point
Long socialSecurityNumber1 = 999_99_9999_l; Invalid, cannot underline before l subscript
int a1 = _52; This is an identifier that begins with an underscore, not a number
int a2 = 5_2; Effective
int a3 = 52_; Invalid, cannot end with an underscore
int a4 = 5_______2; Effective in
int a5 = 0_x52; Invalid, cannot underline between 0x
int a6 = 0x_52; Invalid, cannot be underlined at the beginning of a number
int a7 = 0x5_2; Valid (16 binary digits)
int a8 = 0x52_; Invalid, cannot end with an underscore
int a9 = 0_52; Valid (8 binary number)
int a10 = 05_2; Valid (8 binary number)
int A11 = 052_; Invalid, cannot end with an underscore

Here are some other examples that are underlined in numbers:

Long creditcardnumber = 6684_5678_9012_3456l; It's best to never do this when coding.
Long socialsecuritynumber = 333_99_9999l; It's best to never do this when coding.
float pi = 3.14_15f;
Long hexbytes = 0xff_ec_de_5e;
Long hexwords = 0xcafe_babe;
Long Maxlong = 0X7FFF_FFFF_FFFF_FFFFL;
byte nybbles = 0b0010_0101;
Long bytes = 0b11010010_01101001_10010100_10010010;

With underscores, you'll find your code is more readable than before. Incidentally, in Java you should always use L to represent a long integer number. Although it is legal to use the lowercase l to denote a long integer, he looks too much like 1, so it should never be used. Tell me if you can find the difference between 12l and 121, I guess there are not many people to find. But between 12L and 121?

In short, you should develop the habit of using underscores in numbers , especially for long integers, which can increase readability. I know that this function only works from Java1.7, and has not been widely used.

Summary of variables used in Java

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.