Learning java together [5] --- traps for primitive data types

Source: Internet
Author: User

The first four data types of native data are discussed;
This article mainly talks about the last four data types:
Single-precision float: float;
Double-precision floating point: double;
Character Type: char representation: 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 ';
Boolean: boolean indicates that the boolean type has only two possible values: true and false.
 
What is float: Refers to decimals. It is also called a real number. For example, 1.2; double-precision floating point data range is larger than that of single-precision floating point data range.
 
Supplement: byte: 8 bits;
 
 
Traps for native data types:
All floating point types in java are double by default. A value of the double type cannot be assigned to a variable of the float type, even if the value of the double type is within the range of the float type. In short, whether the value is successfully assigned depends on whether the value type on the right of the equal sign is consistent with the variable type on the left of the equal sign.
Example: float B = 1.2;
System. out. println (B );
 
How can I assign a value of the double type to a variable of the float type? The answer is to forcibly convert the double type to the float type.
Syntax for forced conversion: type variable name = (type) variable value;
For example, float B = (float) 1.2;
System. out. println (B );
Supported by java;
Float B = 1.2F;
System. out. println (B );
This can also solve this usage trap.
 
Before using a variable, you must assign a value, that is, initialize the variable. The variable must declare its type before it can be used. The variable cannot be initialized multiple times.
 
 
Summary: You can assign a value with a small range to a variable with a large range, but you cannot directly assign a value with a large range to a variable with a small range, only accept is implemented through forced type conversion.
Author: "Li Yuan Cao"

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.