JAVA type conversions: implicit conversions and casts

Source: Internet
Author: User

This essay is a review of the Java type conversion, convenient to forget the time of the query, but also want to help people with this need friends

First, data type classification:

Simple data types: integer, Real, character ((Byte-short-char)-int-long-float-double)

Second, simple type conversion

(1) Low-level to advanced conversion---automatic conversion: implicit conversion

byte i = 0;
int a =i;
Long B = i;
float c =b;
Double d =c;
Short J =i;

(2) advanced to low-level conversion---casts

int i=99;
byte b= (byte) i;
Char c= (char) i;
float f=i;
Double d = f;
f = (float) D;

(3) Packaging type transition

JAVA has a total of 6 wrapper classes: Boolean, Integer, Long, Float, Double, Character

Iii. Examples of conversions

Shorts = 1;

s= s +1;

And

Shorts = 1;

S+=1;

Is there a problem with the analysis?

Analysis: The first one will error, because s = S+1 will be implicitly converted to the int type, when an int type is assigned to the short type, it may be lost.

The second one is correct, because S+=1 has a cast, S = (short) (s+1), then no error is given.




JAVA type conversions: implicit conversions and casts

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.