Special cases of type conversions in Java

Source: Internet
Author: User
Conversion first look at the following statement those are illegal.

BYTE b1=2;
BYTE b2=1+1;
BYTE b3=b1+1;
BYTE b4=b1++;

int i1=integer.max_value+1; integer.max_value=2147483647
int i2=2147483648;


Changing the byte to char and short is almost the same.
Because integer literal constants are int, that is, 1 and 2 are all int type.
It is not possible to assign an int to byte directly in an assignment statement.
int i=1;
BYTE b=i;
is not compiled, because the implicit type conversion is not allowed, change to the following is OK
int i=1;
byte b= (byte) i;

Implicit conversions are generally relaxed transformations (such as Byte to Int,string to object)
But one exception is that, in the assignment transformation,
Implicit transformations also include narrowing of the basic transformations.

is the conversion from int to Byte,char,short:
Like what:
BYTE b=1;//Legal
byte b=128;//illegal

The right must be an int literal constant, and the value must be represented by the left type


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.