In Java, you can often encounter types of conversion scenarios, from the definition of variables to replication, the calculation of numerical variables to the method of parameter transfer, base classes and derived classes between the shape, and so on, everywhere type conversion figure. Type conversions in Java play an important role in Java coding.
When defining variables, there are a number of issues to be aware of, inadvertently will appear loss accuracy or incompatible types and so on.
For example:
1. When defining long integer data, you must add the suffix L or l
Long L =123456789012345l
2. When defining a single precision type (7-8-digit valid number), you must add a suffix f or f
float F = 12.5F
3. A Boolean type cannot be converted to another data type.
Among these, we often encounter the conversion of data types, the most common is implicit conversion and forced conversion, let's analyze.
Implicit conversions
Characteristics:
From small to large, you can implicitly convert the data type will automatically ascend.
Byte,short,char-->int-->long-->float-->double
Note: Long is 8 bytes and float is 4 bytes.
A long is an integer, float is a floating-point type, and an integer is not stored with a floating-point number, and remember that a long range is less than float.
Cases:
BYTE a=10;
int b=a;
When Intb=a is compiled, a is implicitly converted to type int.
Forced conversions
Characteristics:
From big to small (if you know explicitly that data can be represented by that data type, you can use a cast)
Format:
(converted data type) variable or value.
Note: In general, it is not recommended to use coercion type conversions at all.
Example 1:
int a=10;
byte b= (byte) A;
When a byte b= (byte) 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
}
Analytical:
Data 130 defaults to the decimal data of type int,
The first step: decimal 130 is converted to binary data.
10000010
Step two: 130 the representation in memory is as follows
Original code: 0000000000000000 00000000 10000010
The third step: to find the complement of int130
Since 130 is a positive number, the inverse code and complement are consistent with the original code.
Complement: 0000000000000000 00000000 10000010
The fourth step: the complement to intercept, only the last 8 digits left.
The complement of (byte) 130 is: 10000010
The fifth step: Converts the complement to the original code.
Because the sign bit (first digit) is 1, the number is negative,
Anti-code: 10000001 (Complement-1)
Original code: 11111110 (symbol bit unchanged, data bit counter)
Converts the decimal to-126, so the final print-126.
Example 3:
And
Do you have a problem?
Analytical:
The first program will complain: Error: Incompatible type: conversion from int to short can be a loss
Reason: S=s+1;s+1 is implicitly converted to type int, and 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 can be regarded as s=s+1, but there is a difference, S+=1 has a cast, that is, s= (short) (s+1), will be forced to convert the value of s+1 to the short type, it does not error.
Summary:
Problems with data type conversions if they occur on some small program, we may be able to see at a glance, but when writing a large system, with a large amount of data, these small problems may lead to system errors or even crashes, so the rigor of the early code writing depends on our own grasp.
The above describes the Java foundation of the implicit conversion vs forced conversion, I hope you like.