JAVA Data Type

Source: Internet
Author: User

JAVA Data Type

Data Types in JAVA

Java data type introduction:

Java is a strongly typed language. The numeric types are mainly divided into basic data types and reference data types. The basic data types are divided into eight types. The details and notes are as follows.

Basic Data Types in java:

Character Type:

Keyword: char occupied byte: 2 bytes
Details: 1. The character type in java adopts Unicode encoding and 2 bytes. Therefore, the Data Type in java can directly store a Chinese character, for example, char sex = "male ";
Note: 1. When declaring the character type, use single quotation marks ''instead of double quotation marks "".

Boolean Type:

Keyword: boolean byte: 1 byte
Details: 1. There are only two values: true or false.
Note: In a Condition Statement, java does not allow 0 or non-0 values to be true or false in C/C ++, but only true or false.

Value Type:

The value types in java are classified into integer and floating point types. The integer type can be interpreted as an integer, and the floating point type can be interpreted as a decimal.

Integer:

Keywords: byte: 1 byte Chinese abbreviation: byte type
Keyword: short byte: 2 bytes Chinese abbreviation: short integer
Note: when using the short type, pay attention to the value type range to avoid cross-border overflow.
Keywords: int occupied byte: 4 bytes Chinese abbreviation: integer
Details: 1. If the integer type is not specified in java, it is declared as int type by default. 2. Generally, the integer is declared as int type.
Keyword: long occupied byte: 8 bytes Chinese abbreviation: long Integer
Note: Because java defines the integer type as int by default, the suffix L or lower-case l should be added when the long type is used, but the lower-case l is usually confused with 1, generally, uppercase L is used as its suffix.

Floating point type:
The floating point type in Java is divided into two types: Single-precision floating point type and double-precision floating point type. The double-Longitude floating point type has a higher accuracy than the single-Longitude floating point type.

Single precision floating point type:

Keywords: float byte: 4-byte Chinese abbreviation: Single-precision floating point type
Verbose: in Java, float type values are stored in Double type by default. When float Type values are used, uppercase F or lowercase f should be added after the float type is declared as its suffix.
Note: short a = 1.35; short B = a + 12.56; note that because 12.56 does not execute the numerical type for it, the default value is double, + If the accuracy of a is automatically upgraded to the double type, the entire calculation result is of the double type, which is inconsistent with the declared result of the short type.

Double Precision Floating Point Type:

Keywords: double occupied byte: 8 bytes Chinese abbreviation: double Precision Floating Point Type
Note: in Java, the default numeric type of decimals is double. When the double type is used, you can add an uppercase D or lowercase d to the end of the decimal number as its suffix.

Automatic type upgrade in java:

During calculation, java automatically upgrades the value type. Specifically, it increases the value type that indicates a small range to the largest value type in the entire expression, it often occurs in the following two aspects:
1. Raise the integer type in the expression as the operand or operation sub-integer: 12.3f + 12.5 Where 12.5 does not explicitly specify the data type for it, the floating point value java is designated as double by default, this operation is involved in computational operations, but the float type is in the front, in order not to lose the overall precision, java automatically upgrades the float type operator to a double type with higher precision by default and allows it to continue to participate in the operation.
2. Automatic type upgrade for the result in the expression: for example, double a = 12.5f + 12.3f; although the operation part of the entire expression is of the float type, the result to be given is of the double type, java will upgrade its precision to the double type.

Java forced type conversion:

In java, automatic type conversion occurs in some aspects: low precision> high precision, while in java, forced type conversion occurs in some aspects: High Precision> low precision.
In java, automatic type conversion is automatically implemented by the system, while in java, high-precision type conversion is manually forced by programmers, because it is obvious that precision improvement will not cause any harm, and precision reduction will often cause harm, such as the amount of money or interest stored in the bank. Therefore, a Java program must be manually forced by programmers.
The syntax for force type conversion in java is: (the data type to be converted to) the parameter or value to be converted, for example: (int) 12.5 + 12.3 where 12.5 is automatically converted to 12, for a positive number of java, No matter rounding, it is converted to the downward nearest positive integer, negative number is converted to the upward to the nearest negative integer, for example: (int) -12.5 is converted to-12. Note that only one parameter closest to (int) is converted here. In this example, only 12.5 is forcibly converted without 12.3 conversion.

Others:

Improper conversion may cause overflow of values. For details about overflow, refer to the articles of other garden friends in the blog garden. In addition, the length of the Value Type in Java is fixed on any machine, instead of the number of digits of the int on a 16-bit machine like the C/C ++ language, the number of digits of the int on a 32-bit machine is fixed.

 

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.