Data type conversion in Java-6 rules of basic data type conversion


Preface

By studying the four types and eight data types in Java, we know that there are three different situations when a data is assigned to a variable:

  • The type is consistent, there is no type conversion
  • Automatic type conversion, automatic assignment of small capacity to large capacity
  • Forcing type conversion, assigning large capacity to small capacity, the possible result is a loss of precision

One, basic data type conversion

  There are fixed conversion rules between basic data types, and the following 6 rules are summarized. No matter which program it is, apply these 6 rules and the problem will be solved.

  1. Among the eight basic data types, except the boolean type cannot be converted, the remaining seven types can be converted

  2. If the integer literal does not exceed the value range of byte, short, char, you can directly assign it to a variable of type byte, short, char

  3. The conversion from small capacity to large capacity is called automatic type conversion . The order of capacity from small to large is: byte<short(char)<int<long<float<double , where short and char both occupy two bytes, but char can represent more Large positive integer.

  4. Converting large capacity to small capacity is called forced type conversion . You must add a "compulsory type conversion operator" when writing, but there may be a loss of precision during runtime, so use it with caution.

  5. When the byte, short, and char types are mixed, first convert each to int type and then perform the operation

  6. When multiple data types are mixed together, they should be converted to the one with the largest capacity before the calculation

Two, exercises

  1. What is wrong with the following program?
short s1=1;
s1=s1+1;

The error is reported
Insert picture description here
  because s1 is a short type, and 1 is an int type. When short and int are mixed, short will be automatically converted to int type, so the s1+1 compiler detects that it is of type int, and int type cannot be assigned to variable s1 of type short . So you need to force type conversion, namely:

short s1 = 1;
//注意强制类型转换的格式
s1 =(short) (s1 + 1) ;
  1. Why can a char type variable store a Chinese character and why?

  The text in java uses unicode encoding, a Chinese occupies 2 bytes, and the char type occupies two bytes in java, so the char type in java can completely accommodate a Chinese character.

  1. What's wrong with flaot f=1.0? What's wrong with long a=2147483648?

  In the Java language, there is a provision that as long as it is a floating-point literal, such as 1.0, 3.14, etc., it will be treated as a double by default . If you want the program to treat it as a float, you need to add it after the literalf\F, Or use forced type conversion. Integer literal value is treated as int type by default , if it is expressed in long form, it needs to be added after the literal valueL\l

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/110110982