Description of automatic type conversion in Java for Byte,short,char,int,long operations

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

As we all know, Java implicitly automates type conversions when it comes to operations, so what happens when it's converted? Summarized as follows:

First, arithmetic operators

Monocular operator:+ (positive)-(minus) + + (self-increment 1)-(self-minus 1)

1.1 + (take positive)-(take negative)

When the operand is Byte,short,char, it is automatically converted to int, and the returned result is int.

When the operand is int,long, it does not convert, what is the original type, or what type.

1.2 + + (self-increment 1) --(self-minus 1)

No matter what type of operand it is, do not convert.


Binocular operator: +-*/% (take surplus)

1.3 +-*/% (take more)

When there is no long type in the two operands, the non-int type of the two operands is automatically converted to the int type, then participates in the operation, and returns the result as int;

When the two operands contain a long type, the non-long type of the two operands is automatically converted to a long type, and then the operation is taken, and the result is long;


two-bit operators

Bitwise operators:& (with), | (or), ^ (XOR), ~ (non), << (with symbol left), >> (with symbol right), >>> (unsigned Right shift)

2.1 & (with), | (or), ^ (XOR)

Same as the 1.3 case

2.2 ~ (non)

Same as the 1.1 case

2.3 << (with symbol left shift), >> (with symbol right shift), >>> (unsigned Right shift )

Same as the 1.1 case


Summarize

Although there are so many cases listed above, it is easy to remember.

That is: In addition to the self-increment self-reduction does not convert, all other cases are no long type, all non-int types are converted to int type; when there is a long type, it is converted to a long type



Description of automatic type conversion in Java for Byte,short,char,int,long operations

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.