Java Foundation---Automatic type conversion in Java (vii)

Source: Internet
Author: User

Automatic type conversion in Java

In Java programs, it is often necessary to convert data between different basic data types. For example:

650) this.width=650; "src=" http://img.mukewang.com/5359d0c70001ffe202530066.jpg "/> in code int type variable Score1 can be directly for the double variable Score2 completion of the assignment operation, The result is: 650) this.width=650; src= http://img.mukewang.com/ 5359d10d0001d9c500540024.jpg "/> This conversion is called automatic type conversion .

Of course automatic type conversions are required to meet specific conditions :

1. The target type can be compatible with the source type, such as type double compatible int, but char type cannot be compatible with int

650) this.width=650; "src=" http://img.mukewang.com/53688a3200014fc803800109.jpg "/>

2. The target type is greater than the source type, such as the double type is 8 bytes long, the int type is 4 bytes, so the double type of the variable can directly hold the int type of data, but the reverse is not possible

650) this.width=650; "src=" http://img.mukewang.com/53688a460001539403740086.jpg "/>


Code:

public class helloworld{
public static void Main (string[] args) {
Double avg1=78.5;
int rise=5;
Double avg2=avg1+rise;
System.out.println ("Average test score:" +AVG1);
SYSTEM.OUT.PRINTLN ("Adjusted mean score:" +AVG2);
}
}


Operation Result:

Average Test score: 78.5
Adjusted average score: 83.5


1. Note The conditions for automatic type conversion Oh, theint type can be automatically converted to a double type, but the double type cannot be automatically converted to the INT type

2.5th line reference: Double avg2 = Avg1 + rise;


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547643

Java Foundation---Automatic type conversion in Java (vii)

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.