Java Data types

Source: Internet
Author: User

Java Data types

Java is a strongly typed language, so you must declare a type for each variable.

There are 8 basic types of Java, including: integer 4, floating-point 2, Unicode character 1, and Boolean-value type 1 .

Integral type

Java integral type
Type Storage space Range of values

Int

4 bytes -2147483648 ~ 2147483647
Short 2 bytes -32768 ~ 32767
Long 8 bytes -9223372036854775808 ~ 9223372036854775807
Byte 1 bytes -128 ~ 127

Floating-point type: Used to represent a numeric value with a decimal part.

Java floating-point type
type storage space value range
float 4 bytes approximately ±3.40282347e + 38F (number of significant digits is 6~7 bit)
double 8 bytes approximately ±1.79769313486231570e + 308 (significant digits 15 bits)

A value of type float has a suffix f, (for example, 3.14F), and if a floating-point value without an F-suffix defaults to a double type, a double-type floating-point value can be appended with a suffix D, such as 3.14D.

Floating-point values do not apply to financial calculations with incoming errors, for example: 2.0-1.1 will appear = 0.89999999999999 instead of 0.9, which is mainly due to the fact that floating-point values are represented by a binary system, while in binary systems it is not possible to accurately represent a fraction of 1/ 10, it is as if the decimal does not accurately represent 1/3. If you need to have no rounding errors in the numerical calculations, you should use the BigDecimal class.

Char type

The char type is used to represent a single character, and its Unicode encoding unit can be represented as a hexadecimal value ranging from \u0000 to \UFFFF. A char type is a deprecated numeric type, preferably a string that needs to be processed in an abstract data type.

Boolean type (Boolean type)

A Boolean (Boolean) type has only two values: True and False. These two values are used to determine whether the logic is true or false. An integer value and a Boolean value cannot be converted to each other in Java.

Java 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.