Java Data type conversions (automatic conversions and casts)
conversion of data types into automatic conversions and casts. Automatic conversion is a program in the implementation of the "quietly" conversion, do not require the user to advance the declaration, generally from the low bit of the type to the number of high type conversion; Coercion type conversions must be declared in code and the conversion order is unrestricted.
Automatic data type conversions
Automatic conversions are converted in order from lowest to highest. The priority relationships between different types of data are as follows:
Low---------------------------------------------> High
byte,short,char-> int-> long-> float-> double
In operations, different types of data are first converted to the same type, and then the conversion rules are as follows:
Force data type Conversions
The format of the cast is to add "()" before the data that needs to be transformed, and then add the data types that need to be converted in parentheses. Some data after transformation operation, the precision will be lost, and some will be more accurate, the following example can illustrate the problem.
public class Demo {public
static void Main (string[] args) {
int x;
Double y;
x = (int) 34.56 + (int) 11.2; Missing precision
y = (double) x + (double) 10 + 1;//Improve Precision
System.out.println ("x=" + x);
System.out.println ("y=" + y);
}
Run Result:
Careful analysis of the above program section: Because the 34.56 before the mandatory type of an int conversion, so 34.56 becomes 34. The same 11.2 turns out to be 11, so the result of X is 45. There is a cast with a double type before x, so the value of x changes to 45.0, and the front of 10 is also forced to double, so it becomes 10.0, so the last Y value becomes 56.
Java data types and the definition of variables
Java is a strongly typed language and must indicate the data type when declaring variables. The value of the variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes.
Java has 8 basic data types, including 4 integer, 2 floating-point, 1 character, 1 Boolean, see the table below.
For integer data, the int type is typically used. But if the energy released by the atom bomb that was put on Hiroshima Nagasaki, it would need to use a long type. The byte and short types are used primarily for specific applications, such as the underlying file processing or the need to control the large array of storage space consumption.
In Java, the length of the integer data is not platform-dependent, which solves many of the problems that the software brings to the programmer when it is ported from one platform to another. In contrast, the length of a/C + + Integer data is platform-dependent, and the programmer needs to select the appropriate integer for the different platforms, which could result in an integer overflow on a 32-bit system for programs that run stably on a 64-bit system.
Octal has a prefix of 0, for example, 010 corresponds to 8 in decimal; hexadecimal has a prefix of 0x, such as 0xCAFE; starting with Java 7, prefix 0b can be used to represent binary data, for example, 0b1001 corresponds to 9 in the decimal system. Also starting with Java 7, you can use underscores to separate numbers, similar to English numerals, such as 1_000_000 1,000,000, or 1 million. Underscores just to make your code easier to read, the compiler deletes these underscores.
Additionally, unsigned types (unsigned) are not supported like C/c++,java.
The maximum number of valid digits in float type is 7 digits, and the effective number length includes the integer part and the decimal part. For example:
float x = 223.56F;
Float y = 100.00f;
Note: There is a flag "F" or "F" behind each float type, and this flag represents a float type.
The double type is valid for a maximum of 15 digits. Like the float type, a double is followed by a flag "D" or "D". For example:
Double x = 23.45D;
Double y = 422.22d;
Double z = 562.234;
Note: floating-point data with no flags, the system defaults to the double type.
In most cases the double type is used, and the precision of float is difficult to meet the demand.
Examples of applications for different data types:
public class Demo {public
static void Main (string[] args) {
//char
char webName1 = ' micro ';
Char webName2 = ' learn ';
Char webName3 = ' yuan ';
System.out.println ("The name of the website is:" + webName1 + webName2 + webName3);
Integer short
x=22//decimal
int y=022;//Eight into
long z=0x22l;//16 into
System.out.println ("Convert to decimal: x =" + x + ", y =" + y + ", z =" + z ");
Floating-point float
m = 22.45f;
Double n = ten;
SYSTEM.OUT.PRINTLN ("Calculated product:" + M + "*" + n + "=" + M*n);
}
Run Result:
The name of the website is: the micro-learning court is
converted to decimal: x = z, y = = =
22.45: * 10.0=224.50000762939453
From the results of the operation can be seen, even if the floating-point data only integers without decimals, the console output when the system will automatically add a decimal point, and decimal places are all placed to 0.
Description of the Boolean type
If you have programming experience to understand the Boolean, skip the following tutorial, which is for readers with only C language base (C language without Boolean).
In the C language, if the condition is judged, it returns 1, or 0, for example:
#include <stdio.h>
int main () {
int x = 100>10;
int y = 100<10;
printf ("100>10 =%d\n", x);
printf ("100<10 =%d\n", y);
return 0;
}
Run Result:
In Java, however, the condition returns TRUE, otherwise it returns false, that is, a Boolean type. For example:
public class Demo {public
static void Main (string[] args) {
//character
Boolean a = 100>10;
Boolean B = 100<10;
System.out.println ("100>10 =" + a);
System.out.println ("100<10 =" + b);
if (a) {
System.out.println ("100<10 is Right");
} else{
System.out.println ("100<10 is Wrong");}
}
Run Result:
100>10 = True
100<10 = False
100<10 is right
In fact, true is equivalent to 1,false equivalent to 0, except for a different name and a separate data type.