Java Essay: Mixed-type expression type conversion issues

Source: Internet
Author: User

Mixed expressions handle a lot of "pits" and look at the following code:

Short x = 0;int y = 123456;x + = y; SYSTEM.OUT.PRINTLN (x);

According to the normal understanding x+=y is equivalent to x = X+y

This converts the Y-strong of int to short x, which reports an error:

Error: Incompatible type: conversion from int to short may be a loss

However, the above code actually did not error.

This is where the mixed-type expression is converted by default , converting the result to the type of the left variable, which is the X-type short.

Since 123456 crosses the border, the result is-7616.

Summarize

Hidden type conversions are likely to compile without an error, but the actual result is truncated.

When the right and left side types are not equal, be sure to pay attention.

Java Essay: Mixed-type expression type conversion issues

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.