|
In the definition of variables, there are many problems to be aware, accidentally there will be loss of accuracy or incompatibility types and other issues.
E.g:
1. When defining long data, you must add the suffix l or L
Long l = 123456789012345L
2. When defining a single precision type (7-8 significant digits), the suffix f or F must be added
Float f = 12.5F
3. boolean type can not be converted to other data types.
Of these, we often encounter data type conversion problem, the most common to be an implicit conversion and coercion, and we analyze.
Implicit conversion
feature:
From small to large, can be implicit conversion, the data type will automatically upgrade.
Byte, short, char -> int -> long -> float -> double
Note: long is 8 bytes, float is 4 bytes.
Long is an integer, float is a floating-point, integer and floating-point storage rules are not the same, remember that the scope of a little long is less than float.
example :
Byte a = 10;
Int b = a;
When compiling intb = a, a is implicitly converted to an int.
Forced conversion
feature:
From large to small (if you explicitly know that the data can be used to represent the data type, you can use the cast)
format:
(Converted data type) variable or value.
Note: Under normal circumstances, do not recommend the use of mandatory type conversion.
example 1 :
Int a = 10;
Byte b = (byte) a;
When compiling byte b = (byte) a, a is cast to byte.
EXAMPLE 2:
Class QiangZhiDemo
{
Public static void main (String [] args)
{
Byte b = (byte) 130;
System.out.println (b); // Print the result -126
}}
}}
Resolution:
Data 130 is the default type of int decimal data,
The first step: decimal 130 converted to binary data.
10000010
The second step: 130 in memory is represented as follows
Original: 0000000000000000 00000000 10000010
The third step: seeking int130 complement
Because 130 is a positive number, so the code and complement are the original code and consistent.
Complement: 0000000000000000 00000000 10000010
The fourth step: the complement code to intercept, leaving only the last 8.
(Byte) 130 of the complement is: 10000010
The fifth step: the complement code into the original code.
Since the sign bit (first bit) is 1, the number is negative,
Counter-code: 10000001 (complement-1)
Original code: 11111110 (sign bit unchanged data bit inversion)
Converted to decimal -126, so the final print -126.
Example 3:
Shorts = 1;
S = s + 1;
and
Shorts = 1;
S + = 1;
Is there a problem? Why?
Resolution:
The first program will report error: Error: Incompatible type: From int to short there may be losses
Reason: s = s + 1; s + 1 will be implicitly converted to int type, when an int type assignment to the short type is, may be lost.
The second program can compile and run.
S + = 1, although there can be seen as s = s +1, but there are differences, s + = 1 in a coercion, namely s = (short) (s + 1) Cast to short type, it will not error.
summary:
Data type conversion problems If some small program, we may be able to see at a glance, but when writing a large system, with a huge amount of data, these small problems may lead to system errors or even collapse, so the previous code The rigor of preparation must rely on our own grasp. |
|
|
|