Data types in Java--Sico software Training Center

Source: Internet
Author: User

Introduction to Java Data types:

Java is a strong type of language, numeric types are mainly divided into basic data types and reference data types, where the basic data types are divided into 8 kinds, specific details and some points of attention are as follows.

Basic data types in Java:

Character type:

Keywords: char occupies bytes: 2 bytes
Details: 1. The character types in Java are Unicode encoded, plus 2 bytes, so data types in Java can store a single character directly, for example: char sex = "male";
Note: 1. When declaring a character type, use single quotation marks instead of double quotation marks.

Boolean type:

Keyword: boolean occupied bytes: 1 bytes
Point of detail: 1, only two values: TRUE or False
NOTE: In conditional statements, Java is not allowed to act as true or false for numeric types 0 or 0, as in the C/s + + language, but only to allow true or false.

Numeric type:

The numeric types in Java are also classified as integers and floating-point, where integer types can be understood as integers, and floating-point type can be generally understood as decimals.

Integer type:

Keyword: byte occupied by byte: 1 bytes Abbreviation: byte type
Keyword: short is a byte: 2 bytes english abbreviation: shorter integer type
Note: You should be aware of the range of numeric types when using the short type, avoiding overflow overruns.
Keywords: int occupies bytes: 4 bytes abbreviation: integer type
Details: 1, in Java for an integral type if there is no specific type designation, the default is declared as int type 2, the integer is usually declared as int type
Keyword: Long occupies bytes: 8 bytes Abbreviation: Long integer type
Note: Since Java is defined as int by default, the long type should be prefixed with l or lowercase l, but lowercase l is usually confused with 1, usually with uppercase L as the suffix

Floating-point types:
The floating-point type in Java is divided into two types, single-precision floating-point and double-precision floating-point, where the accuracy of the double-longitude float is higher than the accuracy of the single-longitude floating-point type.

Single-precision floating-point type:

Keywords: float occupies bytes: 4 bytes Abbreviation: Single precision floating point type
Detail point: Since the default for floating-point values in Java is stored with a double type, when using the float type it should be followed by an uppercase F or lowercase f as the suffix.
Note the point: short a = 1.35; Short B = A + 12.56; note here because 12.56 does not perform a numeric type for it, so the default is double type, and + will automatically increase the precision of the previous a to double type, then the whole result of the calculation is double type, It does not match the result declaration of the short type.

Double-precision floating-point type:

Keywords: double bytes: 8 bytes Abbreviation: Double precision floating point type
Note: In Java, the default definition of a decimal number type is double, which can be followed by an uppercase D or lowercase d as a suffix when using a double type.

Automatic type promotion in Java:

When calculating, Java automatically makes numeric type promotion for the type, specifically the promotion of a numeric type that represents a small range to the largest range of data types in the entire expression, often occurring in the following two areas:
1, the expression as an operand or an integral type of operation is increased: 12.3f + 12.5 Where 12.5 does not explicitly specify a data type for it, the floating-point value of Java by default is specified as a double type, and then participate in the operation of the expression, but preceded by the float type, Java in order not to lose the overall accuracy of the float type of operator is automatically promoted to a higher precision double type and let it continue Continue to participate in the operation.
2. Automatic type promotion for the result in the expression: double A = 12.5f + 12.3f, for example, although the operation part of the whole expression is of type float but the result to be given is a double type, then Java will increase its precision to double type.

Forced type conversions for Java:

Some aspects of automatic type conversion in Java occur in the case of low precision → high accuracy, while some aspects of forced type conversion in Java are described in the case of high precision → low accuracy.
Automatic type conversion in Java is automatic, while the high-precision type conversion in Java is manually enforced by the programming staff, because it is clear that improving accuracy does not cause harm and reduces accuracy, which often results in damage, such as the amount of money or interest deposited in a bank. So a Java automatic one to the programmer manually forced.
The syntax for coercion type conversions in Java is: (The data type to convert to) the parameter or value to convert for example: (int) 12.5 + 12.3 Where 12.5 is automatically converted to 12, and for a positive number of Java, whether rounded, converts it to the nearest positive integer downward, and the negative number converts it upward to the nearest negative integer, for example: (int)-12.5 is converted to-12, note that this is only the closest parameter to the distance (int). In this example, only 12.5 casts are cast and not even 12.3 are converted.

Other:

Improper conversion will result in numerical overflow, the specific situation of overflow can be referred to the blog Park Other friends of the article. In addition, the length of numeric types in Java is fixed on whatever machine, rather than on the 16-bit machine where int is the number of bits on a 32-bit machine, and the adaptive fixation of how many bits the int becomes.

Data types in Java--Sico software Training Center

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.