Java data type conversion (automatic conversion and cast conversion)
The conversion of data types is divided into automatic conversion and cast conversion. Automatic conversion is a conversion that is "quietly" performed by the program during execution. It does not require the user to declare in advance. It is generally a conversion from a type with a low number of bits to a type with a high number of bits. The cast type conversion must be declared in the code, and the conversion order is not Restricted.
Automatic data type conversion
Automatic conversions are converted from low to high. The priority relationship between different types of data is as follows:
Low ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
byte,short,char-> int -> long -> float -> double
In operation, different types of data are first converted to the same type, and then the calculation is performed. The conversion rules are as follows:
Forced data type conversion
The cast format is to add "( )" before the data that needs to be transformed, and then add the data type that needs to be transformed in brackets. Some data will be lost after transformation operations, while others will be more accurate. The following examples can illustrate this problem.
public class Demo { public static void main(String[] args){ int x; double y; x = (int)34.56 + (int)11.2; // Lost precision y = (double)x + (double)10 + 1 ; // Improve accuracy System.out.println("x=" + x); System.out.println("y=" + y); }} Running results:
x=45y=56.0
Carefully analyze the above program segment: Since there is a forced type conversion of int before 34.56, 34.56 becomes 34. Similarly, 11.2 becomes 11, so the result of x is 45. There is a double cast before x, so the value of x becomes 45.0, and the front of 10 is also forced to double type, so it also becomes 10.0, so the value of y becomes 56 in the end.
Java data types and variable definitions
Java is a strongly typed language, and the data type must be specified when declaring variables. The value of a variable occupies a certain amount of memory space. Different types of variables occupy different sizes.
There are 8 basic data types in Java, including 4 integer types, 2 floating point types, 1 character type, and 1 Boolean type. Please see the table below.
For integer data, the int type is usually used. But if it means the energy released by the atomic bomb dropped from Hiroshima Nagasaki, you need to use the long type. The byte and short types are mainly used in specific application scenarios, such as underlying file processing or large arrays that need to control the amount of storage space.
In Java, the length of integer data is platform-related, which solves many problems that software brings to programmers when porting from one platform to another. In contrast, the length of C/C++ integer data is platform-related, and programmers need to select appropriate integers for different platforms, which may cause programs running stably on 64-bit systems to complete on 32-bit systems. type overflow.
Octal has a prefix of 0, for example, 010 corresponds to 8 in decimal; hexadecimal has a prefix of 0x, for example, 0xCAFE; starting from Java 7, the prefix of 0b can be used to represent binary data, for example, 0b1001 corresponds to 9 in decimal. Also starting from Java 7, underscores can be used to separate numbers, similar to English numerical writing, for example, 1_000_000 means 1,000,000, which is one million. Underscores are just to make the code more readable, and the compiler will remove these underscores.
In addition, unlike C/C++, Java does not support unsigned types.
The float type has a maximum length of 7 digits, and the length of the significant number includes the integer part and the decimal part. For example:
float x = 223.56F; float y = 100.00f;
Note: Each float type has a flag "F" or "f". With this flag, it means that it is the float type.
The double type has a maximum significant number of 15 digits. Like the float type, double also has the flag "D" or "d" after it. For example:
double x = 23.45D;double y = 422.22d;double z = 562.234;
Note: Floating point data without any flags, the system defaults to double type.
In most cases, the double type is used, and the accuracy of float is difficult to meet the needs.
Examples of different data types:
public class Demo { public static void main(String[] args){ // Character type char webName1 = 'micro'; char webName2 = 'science'; char webName3 = 'yuan'; System.ou t.println("The name of the website Yes: " + webName1 + webName2 + webName3); // Integer short x=22; // Decimal int y=022; // Octal long z=0x22L; // Hexadecimal System.out.println("Conversion Decimal: x = " + x + ", y = " + y + ", z = " + z); // Float type float m = 22.45f; double n = 10; System.out.println("calculate Product: " + m + " * " + n + "=" + m*n); }} Running results:
The name of the website is: Weixueyuan converted into decimal: x = 22, y = 18, z = 34 Calculate product: 22.45 * 10.0=224.50000762939453
From the running results, we can see that even if floating-point data only has integers and no decimals, the system will automatically add a decimal point when output on the console, and all decimal places are set to 0.
Description of Boolean
If you have programming experience and understand Boolean types, please skip the tutorial below. The tutorial below is for readers who only have C language basics (C language does not have Boolean types).
In C language, if the judgment condition is valid, it will return 1, otherwise it will return 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;} Running results:
100>10 = 1100<10 = 0
But it is different in Java. If the condition is true, it will return true, otherwise it will return false, that is, the 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 correct"); }else{ System.out.println("100<10 is Wrong"); } }} Running results:
100>10 = true100<10 = false100<10 is correct
In fact, true is equivalent to 1 and false is equivalent to 0, but it has changed its name and becomes a data type separately.