Java data type conversion (automatic conversion and forced conversion)
Data type conversion is divided into automatic conversion and forced conversion. Automatic conversion is a conversion performed "quietly" during the execution of the program. It does not require the user to declare it in advance. It usually converts from a low-digit type to a high-digit type. Forced type conversion must be declared in the code, and the conversion order does not matter. Restricted.
Automatic data type conversion
Automatic conversion is performed in order from low to high. The priority relationship between different types of data is as follows:
Low---------------------------------------- -------->高
byte, short, char-> int -> long -> float -> double
In the operation, different types of data are converted first For the same type, and then perform operations, the conversion rules are as follows:
Forced data type conversion
The format of forced conversion is to add before the data that needs to be converted "( )", and then add the data type to be converted within the brackets. After some data is transformed, the accuracy will be lost, while some will be more accurate. The following example can illustrate this problem.
public class Demo { public static void main(String[] args){ int x; double y; x = (int)34.56 + (int)11.2; // 丢失精度 y = (double)x + (double)10 + 1; // 提高精度 System.out.println("x=" + x); System.out.println("y=" + y); } }
Running results:
x=45 y=56.0
Carefully analyze the above program segment: Since it is in 34.56 There was a forced type conversion of int before, so 34.56 became 34. Likewise 11.2 becomes 11, so the result of x is 45. There is a double type 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 final value of y becomes 56.
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 you represent the energy released by the atomic bombs dropped on Hiroshima and Nagasaki, you need to use the long type. The byte and short types are mainly used in specific applications, such as low-level file processing or large arrays that need to control the amount of storage space they occupy.
In Java, the length of integer data has nothing to do with the platform, which solves many problems caused to programmers when software is transplanted from one platform to another. In contrast, the length of C/C++ integer data is platform-related, and programmers need to choose appropriate integer types for different platforms. This may cause programs that run stably on 64-bit systems to be corrupted on 32-bit systems. type overflow.
Octal has a prefix 0, such as 010 corresponding to 8 in decimal; hexadecimal has a prefix 0x, such as 0xCAFE; starting from Java 7, you can use the prefix 0b to represent binary data, such as 0b1001 corresponding to decimal 9 out of 9. Also starting from Java 7, you can use underscores to separate numbers, similar to how English numbers are written. For example, 1_000_000 means 1,000,000, which is one million. Underscores are only used to make the code more readable, and the compiler removes them.
In addition, unlike C/C++, Java does not support unsigned types (unsigned).
The valid number of the float type is up to 7 digits, and the length of the valid number includes the integer part and the decimal part. For example:
float x = 223.56F; float y = 100.00f;
Note: There is a sign "F" or "f" after each float type. This sign means it is a float type.
The maximum number of valid digits for double type is 15 digits. Like the float type, double is followed by the flag "D" or "d". For example:
double x = 23.45D; double y = 422.22d; double z = 562.234;
Note: For floating-point data without any flag, the system defaults to double type.
In most cases, the double type is used, and the precision of float is difficult to meet the demand.
Application examples of different data types:
public class Demo { public static void main(String[] args){ // 字符型 char webName1 = '微'; char webName2 = '学'; char webName3 = '苑'; System.out.println("网站的名字是:" + webName1 + webName2 + webName3); // 整型 short x=22; // 十进制 int y=022; // 八进制 long z=0x22L; // 十六进制 System.out.println("转化成十进制:x = " + x + ", y = " + y + ", z = " + z); // 浮点型 float m = 22.45f; double n = 10; System.out.println("计算乘积:" + m + " * " + n + "=" + m*n); } }
Running results:
网站的名字是:微学苑 转化成十进制:x = 22, y = 18, z = 34 计算乘积:22.45 * 10.0=224.50000762939453
It can be seen from the running results that even if floating Point-type data only has integers and no decimals. When output on the console, the system will automatically add decimal points and set all decimal places to 0.
Explanation of Boolean type
If you have programming experience and understand the Boolean type, please skip the following tutorial. The following tutorial is for readers who only have a basic foundation in C language (C language does not have Boolean type).
In C language, if the judgment condition is true, 1 will be returned, otherwise 0 will be returned, 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 result:
100>10 = 1 100<10 = 0
But it is different in Java. If the condition is met, it returns true, otherwise it returns false, which is a Boolean type. For example:
public class Demo { public static void main(String[] args){ // 字符型 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是对的"); }else{ System.out.println("100<10是错的"); } } }
Running result:
100>10 = true 100<10 = false 100<10是对的
In fact, true is equivalent to 1 and false is equivalent to 0, but the name is changed. , and becomes a data type in its own right.
For more in-depth analysis of data types and variables in Java, please pay attention to the PHP Chinese website!