Java Essay: Multiple transformations

Source: Internet
Author: User

What is the output of the following code?

SYSTEM.OUT.PRINTLN ((int) (char) (byte) (-1));

3 transformations have been made here, and the results are not visible at once.

First time, -1,int

The second time,?, byte: Here do the truncation 0xFFFFFFFF turns into 0xFF.

Third time, huh? , Char: Here char is unsigned, Byte signed. When extended, the symbol bit is taken and the result is 0xffff.

Fourth time, huh? , int: Here an int with a symbol, but char is unsigned. The extension does not have a sign bit, and the result is 0xffff.

So the output is 65535,0xffff.

Summarize

When a numeric conversion occurs, "truncation" and "extension" occur.

  • Truncation is relatively simple: keep low
  • The extension is judged according to the current type: If it is signed, such as Byte, the extension needs to expand the symbol bit. If no symbols, such as char, are extended high position 0

In actual development, it needs to be handled according to its own requirements, such as adding Masks & 0xFF. Ensure that the results meet your own requirements.

Java Essay: Multiple transformations

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.