Automatic type conversion for Java

Source: Internet
Author: User
Tags arithmetic operators bitwise

The package Basetype;/*java automatic type promotion and narrowing conversion * * 1. When a variable of type char,short,byte is assigned to a number that exceeds the maximum range that he can represent, Java automatically converts that number to int. * * 2. When the variables of the Char,short,byte type are arithmetic, the bitwise operations are automatically promoted to the int type * But the arithmetic operation, the bitwise operation operation and their compound operation are different, the code is the following. * Arithmetic operators include: +,-, *,/,%,+ (unary operator),-(unary operator) * Bitwise operator operators include shift operations (&,^,|), bitwise operations (<<,>>,>>>) * */public Class Autoconversion {public static void main (String args[]) {//because the int type is assigned to a type that is smaller than the range he represents, the compiler type check will give an error, prompting for a forced type conversion, which is the narrowing conversion byte a = 129; Because byte is 8 bytes, the range represented is from 128 to +127, so the assignment 128 is automatically converted to int type, the compiler will error the type check, it must be narrowed to bytebyte a = (byte) 129;} public void Bytetest (byte x, byte y) {x = (byte) (x*y);//Because the result of X*y is automatically promoted to int, the narrowing conversion must be used when the value is re-assigned to X, and the other arithmetic operators and bitwise operations are the same x = ( BYTE) x = (byte) (x+y); x = (Byte) (x-y); × = (Byte) (x%y); x--; x = (byte) +y; x = (byte)-y; x = (byte) ~y; x = (byte) (x&y); x = (byte) (x|y); x = (byte) (x^y); x = (byte) (x>>y); x = (byte) (x<<y); x = (byte) (X&gt ; >>y); x + = y;//The composite operation is different, although the type promotion is not required, but the result is the same as the result of using arithmetic operations or bit operations,But the specific why does not need, the book also did not say that the future in contact with the supplementary x-= y; x *= y; x/= y; x%= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x |= y; x ^= y;}}

"Thingking in Java" notes

Automatic type conversion for Java

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.