Java Learning notes-problem with type promotion

Source: Internet
Author: User

Type-promotion issues in expressions

int a = 3;

Long B = 4;

Double d = 5.3;

int c = (int) (A+B); Do all the two-dollar operators (+-*/%), there will be type promotion problem!

float f = (float) (a+b); A is int,b to Double,a+b will be promoted to double type, and when assigned to F, F is float type, the stock needs to be cast!

Error-prone exercises:

Earn 1 billion a year, 20 years to make how much money?

int money = 1000000000;

int years = 20;

Long total = (long) money*years; This ingenious use of the forced transformation of the type of promotion, solved the problem beyond the scope of int

SYSTEM.OUT.PRINTLN (total);

How many times a person's heart beats in 70?

Long times = 70l*60*24*365*70; Ditto, but cast to L, as far as possible after the first 70, because in some cases, if you put L after the last 70, you can not determine whether the previous value multiplication is out of range, the preceding multiplication has gone beyond the scope, and finally forced conversion type promotion, it will not help.

System.out.println (times);

Java Learning notes-problem with type promotion

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.