Conversion between 8 basic data types in Java language


Preface

Type conversion is a common operation in computers, such as input and output, comparison of data with each other, etc. This article introduces the conversion between 8 basic data types.


1. What is data type conversion?

Data type conversion is to convert a data type to another data type. For example, in the calculation 1+2.3=3.2, the int type is converted to the float type, and then the operation is performed.

Two, data type conversion method

1. Automatic data type conversion

Automatic type conversion refers to the data type conversion automatically performed inside the system, and the data type conversion method can be completed automatically by the user without adding any related operations. Generally refers to from small to large, such as: byte—>short—>int—>long in integer type, float—>double in floating point type, etc.

1. Automatic data type conversion between integers

1.byte—>short、int、long

The code is as follows (example):

byte a = 10;
short b = a;   // b = 10;
//byte c = b;   此时会报错,因为short类型长度为2字节,大于byte的1字节
int c = a;    // c = 10
long d = a;   // d = 10
//同样不可以反过来写byte e = c;原因同上
2.short—>int、long

The code is as follows (example):

short a = 10;
int b = a;      // b = 10
long c = a;     // c = 10
//同样不可以反过来写,原因是int,long类型的长度大于short
3.int—>long

The code is as follows (example):

int a = 10;
long b = a;    // b = 10
//同样不可以反过来写,原因是long类型的长度大于int

2. Automatic data type conversion between floating point types

1.float—>double

The code is as follows (example):

float a = 10.0f;
double b = a;    // b = 10.0
//float c = b;   此时会报错,因为double类型的数据精度大于float

3. Integer -> Floating point automatic data type conversion

Integer —> Floating-point data type automatic conversion refers to the process of automatically converting integer data into floating-point data and then processing the data. The most typical example is that the result of adding a decimal to an integer is a decimal. For example: 2 + 3.6 = 5.6.
The code is as follows (example):

int i = 5;
float a = 10.0f;
float b = a + i;    // b = 15.0
//int b = a + i;   此时会报错,因为float类型的数据精度大于int

Here is only an example of automatic conversion of int type to float type. The principle of automatic type conversion between other integral data types and float types is the same, just change the data type from int to byte, short, long Etc., the same is true for the automatic data type conversion of integral and double types, so I won’t list them all.

2. Force data type conversion

Mandatory data type conversion refers to the process of data type conversion that the system cannot perform automatically and must rely on a prescribed grammar.
The syntax format is:
required data type variable name = (required data type) data of the converted data type

1. Forced data type conversion between integers

1.short、int、long—>byte

The code is as follows (example):

byte a = 10;
short b = a;   // b = 10;
//byte c = b;   此时会报错,因为short类型长度为2字节,大于byte的1字节
byte c = (byte)b;    // c = 10
//此时,先进行强制数据类型转换,把short类型的b强制转换为byte,再赋值给c

Other situations are similar, so I won't list them all.

2. Forced data type conversion between floating point types

1.double—>float

The code is as follows (example):

double a = 10.0;
//float b = a;   报错
float c = (float)a;   // c = 10.0
//此时,先进行强制数据类型转换,把double类型的a强制转换为float类型,再赋值给c

3. Forced data type conversion between integer and floating point

1.float—>int

The code is as follows (example):

float a = 10.5;
//int b = a;   报错
int c = (int)a;   //此时,先进行强制数据类型转换,把float类型的a强制转换为int类型,再赋值给c
2.double—>int

The code is as follows (example):

double a = 10.5;
//int b = a;   报错
int c = (int)a;   //此时,先进行强制数据类型转换,把double类型的a强制转换为int类型,再赋值给c

This case is similar to the above float. The
conversion of floating-point types to other integer types is similar, so I won’t enumerate them one by one.

3. Data type conversion between char and int

There is a separate code table between the character type char and int type so that each character corresponds to an integer, so I take it out separately.

1.Char is automatically converted to int

The code is as follows (example):

char a = 'c';
int b = 10;
System.out.println((a+b));   //109
//因为小写字母c在ASCII编码表中对应的数为99,所以,做加法运算时,用的是c对应的数字,可以理解为系统内部进行了自动数据类型转换
2. Force data type conversion

The code is as follows (example):

Example of forced data type conversion between character type and int type data
Insert picture description here

to sum up

For example: The above is what we are going to talk about today. This article only briefly introduces the two methods of data type conversion between Java8 basic data, and the examples given are simple and clear.

Guess you like

Origin blog.csdn.net/ly_1998_/article/details/109366475