Web-android Engineer first form automatic type conversion in Java -2-9

Source: Internet
Author: User

Source: http://www.imooc.com/code/1236

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

, the int variable score1 in the code can do the assignment directly for the double variable Score2, and the result is: This conversion is called an 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

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

Task

Little friends, let's feel the automatic type conversion.

In the editor, the code function is: Define three variables, respectively, to save: Test average, growth value, adjusted average score

The expected run result is:

Now that there is an error in line 5 , can you find and correct it?

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         Doubleavg1=78.5;4         intRise=5;5         intavg2=avg1+Rise;6System.out.println ("Average test score:" +avg1);7SYSTEM.OUT.PRINTLN ("Adjusted average score:" +avg2);8     }9}

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         Doubleavg1=78.5;4         intRise=5;5         Doubleavg2=avg1+Rise;6System.out.println ("Average test score:" +avg1);7SYSTEM.OUT.PRINTLN ("Adjusted average score:" +avg2);8     }9}

Web-android Engineer first form automatic type conversion in Java -2-9

Related Article

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.