Use the literal value of the underline separator number in Java to explain _java

Source: Internet
Author: User

In Java SE 7, new binary notation is added, and you can use decimal notation to represent values in binary form as easily as possible.

For example:

A 8-bit byte value:
byte abyte = 0b100001;
A 16-bit short value: short
ashort = 0b1010010100101;
A 32-bit int value:
int anInt1 = 0b101000010100010110100101000101;
A 64-bit Long value (note suffix "l" at the end)
long along = 0b101000010l;
Binary literals begin with 0b or 0 B, case-insensitive
int anInt2 = 0b101;

In Java SE 7, the following underscore is also supported as a numeric representation of a delimiter join:

decimal form
int anInt1 = 123_45_6;
Binary, 16 and other forms also support
int anInt2 = 0b10_0110_100;
int anInt3 = 0xff_ec_de_5e;
The decimal form also supports
float pi = 3.14_15f;
Double adouble = 3.14_15;
Multiple underline connected
int chain = 5______2____0;

However, the following forms of writing are incorrect:

Underline cannot be placed at the last
int x = 52_;    Error
/underline cannot be near decimal point (not near decimal point)
float pi1 = 3_.1415f;  Error
Float pi2 = 3._1415f;  Error
//underline cannot be placed tightly in front of suffix "l" or "f"
float pi3 = 3.1415_f;  Error
Long aLong1 = 999_99_9999_l;//error
//underscore cannot be placed between the prefix characters representing the system
int x5 = 0_x52;    Error

Also, note the following, which puts the underscore on the front:

int _52 = 120; In Java, a variable name cannot begin with a number, but it can begin with an underscore
int x = _52;  So, the _52 here is not a literal form of a number, but rather a variable identifier (variable name)

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.