In Java, type conversion can often be encountered, from the definition of variables to copying, calculation of numerical variables to parameter transfer of methods, modeling between base classes and derived classes, etc. Type conversion can be seen everywhere. Type conversion in Java plays an important role in Java encoding.
There are many issues to pay attention to when defining variables. If you are not careful, you will lose accuracy or incompatible types.
For example:
1. When defining long integer data, the suffix l or L must be added.
long l =123456789012345L
2. When defining a single precision type (7-8-bit significant digit), the suffix f or F must be added.
float f = 12.5F
3. The boolean type cannot be converted to other data types.
Among them, we often encounter data type conversion problems. The most common ones are implicit conversion and cast conversion. Let’s analyze it.
Implicit conversion
feature:
From small to large, it can be converted implicitly, and the data type will be automatically improved.
byte, short, char -->int -->long -->float -->double
Note: long is 8 bytes and float is 4 bytes.
long is an integer, float is a floating point type. The storage rules for integers and floating point numbers are different. Remember that the range of long is smaller than float.
example:
byte a=10;
int b=a;
When intb=a is compiled, a is implicitly converted to int type.
Cases
feature:
From large to small (if you know clearly that the data can be represented by this data type, you can use casting)
Format:
(Converted data type) variable or value.
Note: In general, casting is not recommended at all.
Example 1:
int a=10;byte b=(byte)a;
When byte b=(byte)a is compiled, a is cast to the byte type.
Example 2:
class QiangZhiDemo { public static void main(String[] args) { byte b=(byte)130; System.out.println(b); //Print result-126 } }Analysis:
Data 130 defaults to int type decimal data,
Step 1: Convert decimal 130 into binary data.
10000010
Step 2: The representation of 130 in memory is as follows
Original code: 000000000000000000000 00000000 10000010
Step 3: Find the complement code of int130
Because 130 is a positive number, the inverse code and complement code are consistent with the original code.
Complement code: 00000000000000000000000 00000000 100000010
Step 4: Intercept the complement code, leaving only the last 8 digits.
(byte)130's complement code is: 10000010
Step 5: Convert this complement into the original code.
Since the sign bit (first bit) is 1, the number is a negative number.
Inverse code: 10000001 (complement code-1)
Original code: 111111110 (the symbol bit remains unchanged, the data bit is inverted)
Convert to decimal to -126, so finally print -126.
Example 3:
shorts = 1;s= s +1;
and
shorts = 1;s+=1;
Is there any problem? Why?
Analysis:
The first program will report an error: Error: Incompatible type: There may be losses when converting from int to short
Cause: s=s+1;s+1 will be implicitly converted to int type. When an int type is assigned to the short type, it may be lost.
The second program can be compiled and run.
Reason: s+=1, although it can be regarded as s=s+1, there is still a difference. There is a cast in s+=1, that is, s=(short)(s+1), which will force the value of s+1 to short type, so there will be no error.
summary:
If the problem of data type conversion occurs in some mini programs, we may be able to see at a glance. However, when writing a huge system and having a huge amount of data, these small problems may cause system errors or even crashes, so we have to grasp the rigor of early code writing.
The above content introduces the Java basic implicit conversion vs cast conversion, I hope you like it.