Java section No. 08 data type conversions

Source: Internet
Author: User

2016-06-28

1 Automatic conversion

The destination type is larger than the original type
The two types are mutually compatible

Byte->short
Short->int
Char->int
Int->long
Int->double
float->double float 4-byte single-precision double 8-byte dual-precision

One of the two numbers is of type double, the other is also converted to a double type, and the result is a double type;
Otherwise, one of the two numbers is a float type, and the other I is also converted to float type
Otherwise, if there is a long type in two operands, the other is also converted to a long type
Otherwise, two numbers are converted to int type

 Package day08;  Public class Testchange {        publicstaticvoid  main (string[] args) {                  Double adouble=3.55555;         int aint= (int) adouble;                System.out.println (aint);    }}

2 Coercion of type conversions

Floating-point type to Integer: Truncate the decimal digit, preserving the integer digits
Long integer to short integer: Go to bit.

int 4 bytes 32 bits
Byte 1 bytes 8 bits

25700000000 00000000 00000001 00000001

 Packageday08; Public classTestchange { Public Static voidMain (string[] args) {//double adouble=3.55555;//int aint= (int) adouble;//        //System.out.println (aint);                intaint=257; byteAbyte= (byte) aint;    System.out.println (Abyte); }}

Resources

[1] Java Easy Start Classic tutorial "full version"

Java section No. 08 data type conversions

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.