Java Basic data types

Source: Internet
Author: User

Integral type
Integral type
Type Storage requirements Range of values
Byte 1 bytes -128 ~ 127
Short 2 bytes -32768 ~ 32767
Int 4 bytes -2147483648 ~ 2147483647
Long 8 bytes -9223372036854775808 ~ 9223372036854775807

1, the long integer value has a suffix l or L.

2, hexadecimal value has a prefix of 0X or 0x.

3. The octal value has a prefix of 0.

4, starting from Java7, plus the prefix 0b or 0B can write binary number, but also in the digital literal underlined, easy to read, the Java compiler will automatically remove these underscores when compiling.

Example code:

 Public classTest01 { Public Static voidMain (string[] args) {intA = 100_0000;//underline position can be arbitrary, easy to read on the lineSystem.out.println ("a=" + a);//a=1000000        Longb = 1_000_000l;//Long Integer typeSystem.out.println ("b=" + b);//b=1000000        intc = 0xCAFE;//hexadecimalSystem.out.println ("c=" + C);//c=51966        intD = 010;//octalSystem.out.println ("d=" + D);//d=8        inte = 0b1111_0100_0010_0100_0000;// BinarySystem.out.println ("e=" + e);//e=1000000    }}
Floating-point types
Floating-point types
Type Storage requirements Range of values
Float 4 bytes Approximately ±3.40282347e+38f (valid digits are 6~7 bits)
Double 8 bytes Approx. ±1.79769313486231570e+308 (15-bit significant number of digits)

1, float, single-precision floating point, rarely used, only for example: a library that requires single-precision data, or need to store large amounts of data in the case of the need to use float. Suffix f or f.

2, double, dual-precision floating point, most programs use double. A suffix D or d, or a floating-point number without a suffix, represents a double.

3. Floating-point values cannot be used in financial calculations where rounding errors cannot be accepted, and the BigDecimal class should be used.

4. Three special floating-point values that indicate overflow and error conditions:

    • Zheng Infinity
    • Negative infinity
    • NaN (not a number)

These three special floating-point values can be represented using a constant in Double and float types:

 Public classTest02 { Public Static voidMain (string[] args) {//The following is a constant representation of a double, and float has the same constantSystem.out.println (double.positive_infinity);//InfinitySystem.out.println (double.negative_infinity);//-infinitySystem.out.println (Double.NaN);//NaN//determine if a value is a number        if(! Double.isnan (12) {System.out.println ("This is a number"); }    }}
Character type
Character type
Type Storage requirements Range of values
Char 2 bytes \u0000 ~ \UFFFF

1, Char is a single character, accounting for 2 bytes, Java Unified using Unicode "2 bytes, 16bit" to represent a character.

2, char c = ' A ';

Because the English alphabet is special, it derives from the 8-bit (1-byte) ASCII code. The use of a low 8-bit (1-byte) in Unicode means that a high 8-bit is not used, and that only 1 bytes are used for storage in memory.

3, char d = ' I ';

The kanji uses a full 16-bit Unicode, and 2 bytes are used in memory.

4, it is not recommended to use char in the program unless you do need to process the UTF-16 code unit.

5. \u escape sequences are special, and can appear in quoted string constants and strings, all other escape sequences are not available.

Escape sequences Description
\b Backspace
\ n Line break. Position the screen cursor at the beginning of the next line
\ t Move the screen cursor to the next tab stop
\ r Carriage return character. Position the screen cursor at the beginning of the next line in the current row, without moving forward to the next line. Characters that are output after the carriage return overwrite the characters that were output on the previous line.
\\ Backslash symbol. Used to print a backslash symbol.
\" Double quotation marks. Used to print double quotes.
\‘ Single quotation marks

Boolean type
Boolean type
Type Storage requirements Take value
Boolean 1 bytes True or False

Java Basic data types

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.