Java SE native data type usage trap

Source: Internet
Author: User
Tags float range
Java SE 3:

1. There are eight native Data Types in Java:
1) Integer type: int type. (32-bit)
2) byte: byte. (-128 ~ An integer between 127 and 8 digits ).
3) Short INTEGER: represented by short. (16 bits)
4) Long Integer: Long is used. (64-bit)
5) Single-precision floating point: float is used. Float refers to decimal places, also called real numbers, such as 1.2.
6) double-precision floating point: double. The double-precision floating-point data range is larger than that of the single-precision floating-point type.
7) character type: Char (char is the abbreviation of character ). A character represents a single character,
For example, a is a letter or a Chinese character, which is enclosed by single quotation marks. For example, char a = 'B'; char B = 'zhang ';
8) boolean type, represented by Boolean. Boolean has only two possible values: true and false.

2. All floating point types in Java are double by default. Values of the double type cannot be assigned to the float type.
Even if the value of the double type is within the float range. In short, can it be
The assignment of power is determined by whether the value type on the right of the equal sign is consistent with the variable type on the left of the equal sign.
3. How to assign a value of the double type to a variable of the float type? The answer is (1) Forced type conversion.
Type value is forcibly converted to float type. (2) Support for Java predictions.
Syntax for forced conversion: type variable name = (type) variable value;
4. A variable must be assigned a value before use. A variable must declare its type before use. A variable must be defined before use,
And can only be defined once.
5. Code Compilation fails:
Int A = 1;
Short B =;

A is of the int type and B is of the short type. The data range of the int type is larger than that of the short type.
A value with a large range is assigned to a variable with a small range.
6. the following code can be compiled:
Short A = 1;
Int B =;
A is of the short type, B is of the int type, and the data range of the int type is larger than that of the short type.
A value with a small range is assigned to a variable with a large range.
7. Conclusion: You can assign a value with a small range to a variable with a large range, but not directly assign a value with a large range.
Variables with a small scope can only be implemented through forced type conversion.

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.