Default conversion and forced conversion of data types in Java, java Data Type

Source: Internet
Author: User

Default conversion and forced conversion of data types in Java, java Data Type

Default conversion:

A: from low to high :(Byte short char)---Int---Long---Float---Double

B: Note:Byte short charThey do not convert each other. They are involved in the calculation and are first converted to the int type.

Forced type conversion:

Format: Target Data Type Variable = (target data type) (converted data)

Among them, string data is good for other data+The result is of the string type,+Here is the connector

Classic example:

1. JudgmentShort s1 = 1; s1 = s1 + 1; AndShort s1 = 1; s1 + = 1; Difference

Resolution:

  Short s1 = 1; s1 = s1 + 1; Format Error

BecauseS1 + = 1The expression type is automatically upgraded during operation. The result isIntType, and then assignedShortType, the error of forced type conversion is reported.

  Short s1 = 1; s1 + = 1; The format is correct.

BecauseS1 + = 1; Is the specified operator and the encoder performs special processing;

2,Float f= 3.4Correct?

Resolution:

Error

If the precision is not accurate, forced type conversion should be used, for example:Float f = (flat) 3.4OrFloat f = 3.4 f;

In Java, the default value without a decimal point isIntType. The default value with a decimal point isDoubleType

Both questions are Java interview questions, which are very basic and important.

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.