Automatic type conversion problem in Java for Byte, short, char, int, long operations

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

--------------------------------------------------------------------------------------------------

★ Automatic (implicit, default) type conversion with force (Explicit) type conversion ★

1) Boolean type does not participate in conversions
2) Default Conversion
A: From small to large
B:byte,short,char--? int--? Long--? Float--? Double
C:byte,short,char do not convert each other, directly into the int type participates in the operation.
3) Cast
A: from big to small
B: There may be a loss of precision, generally not recommended for such use.
C: Format:
Target data type variable name = (target data type) (converted data);

★ The formula of the expression value ★

int a = 10;

int B = 20;

int c = (A = b); The assignment itself is also an expression, so it also has the formula value!!!

--------------------------------------------------------------------------------------------------

As you can see, Java implicitly automates type conversions when it comes to operations. So what are the circumstances that will translate? Summarized as follows:

(i) Arithmetic operators

1) monocular operator: + (positive),-(negative), + + (self-increment 1),--(auto minus 1)

A) + (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.

b) + + (self-increment 1),--(self-minus 1)

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

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

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;

(ii) Bitwise operators

1) & (Bitwise AND), | (bitwise OR), ^ (bitwise XOR)

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;

2) ~ (bitwise NON)

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.

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

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.

(c) Assignment operators

1) Basic assignment operator: =

a) variable = variable: Small to large, auto-turn, large-fu small, not automatic conversion;

b) Variable = constant: no automatic type conversion

2) Extended assignment operator: + =,-=, *=,/=,%=

In particular, the extended assignment operator implies a forced type conversion, that is: ※※s + = 1 is equivalent to S = (data type of s) (S + 1) ※※

★ Summary ★

※ In addition to self-increment and self-reduction do not convert, all other cases are no long type, all non-int type to int type; when there is a long type, it is converted to a long type (abbreviation: No long,int in operation ... )※

Automatic type conversion problem 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.