Java Data type conversions

Source: Internet
Author: User


Grammar:
A. Automatic type conversion: Under certain conditions, the system auto-complete type conversion is also called the implicit conversion (implicit casting)
A. Two types of compatibility
B. Destination type is greater than source type
C. For an expression, if one operand is of type double, the entire expression can be promoted to type double

B. Coercion type conversion (Explicit Casting): Under certain conditions, the programmer manually completes the type conversion
A. Two types of compatibility
B. Generic target type small fish source type
IMPORTANT: 1. Forcing type conversions will lose precision


Example: 1. Automatic type conversion:
int i = 10;
Double d = i;
2. Force type conversions:
Double d = 10.0;
int i = (int) d;

Example code:

1  PackageCom.sanlianxueuan;2 3  Public classDemo03 {4 5     /**6 * Conversion of data types7      */8      Public Static voidMain (string[] args) {9         //Automatic type conversion (implicit conversion)Ten         byteNUM1 = 10; One         intnum2 =NUM1; A System.out.println (num2); -         LongNUM3 =num2; - System.out.println (num3); the          -         floatf = 10.5f; -         DoubleD =F; - System.out.println (d); +          -         intNUM4 = 100; +         floatNUM5 =num4; A System.out.println (NUM5); at          -         CharC1= ' a '; -         intNUM6 = C1;//character assigned value to shaping variable - System.out.println (NUM6); -System.out.println (c1+10); -System.out.println ((Char) (c1+10)); in          -          to         //Forcing type conversions (explicit conversions) +         intNUM7 = 10; -          ShortNUM8 = ( Short) Num7; the System.out.println (NUM8); *          $         floatNUM9 = (float) 10.25;Panax Notoginseng System.out.println (NUM9); -          the         intNUM10 = 65; +         CharNUM11 = (Char) NUM10; A System.out.println (NUM11); the          +         intNUM12 = 10; -         DoubleNum13 =10.6; $         floatres = (float) (num12+num13); $ System.out.println (res); -          -         /*Summary: Byte<short (char) <int<long<float<double*/ the          -     }Wuyi  the}

Java Data type conversions

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.