The eight basic data types are: 1. 4 integer types (byte, short, int, long); 2. 2 floating point types (float, double); 3. 1 character type" char"; 4. 1 Boolean type "boolean".
The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.
There are 8 basic data types in Java, which are used to store integers, floating point numbers, character data and Boolean type data respectively. It should be noted that: What is introduced now is only the basic data types, and many non-basic data types will be introduced later. The basic data types are shown in Figure – 1:
Figure- 1
From Figure- 1 It can be seen that basic data types are mainly divided into 4 major categories (integer type, floating point type, char, boolean), integer types are further divided into 4 subcategories (byte, short, int, long), and floating point types are also divided into There are 2 small categories (float, double). What are the differences between these data types? Figure-2 shows the storage space and usage scenarios of these 8 data types:
Figure- 2
Among the 8 data types above, 5 are the most commonly used, namely int, long, double, char, and boolean. The rest of the data types are almost not used. It is required to focus on mastering these five basic data types. If you are interested in learning about the other data types, you can learn about them.
The so-called integer literals (literal) are integers written directly, for example: the following In the statement, 100 is the direct quantity.
int a = 100; int a = 100;Regarding integer direct quantities, you need to pay attention to the following points:
The type of integer direct quantities defaults to the int type. If the directly written integer exceeds the int If the expression range is exceeded, a compilation error will occur. The following statement is a compilation error caused by exceeding the range of integers.
int d = 10000000000; // 编译错误10000000000这个数值写出来就是错误的,因为Java认为所有直接写出的整数都是int类型,而这个数值超过了int的表达范围。 int d = 10000000000; // 编译错误10000000000这个数值写出来就是错误的,因为Java认为所有直接写出的整数都是int类型,而这个数值超过了int的表达范围。
In addition to the usual decimal number form, integer literals can also be written in hexadecimal form (starting with 0X or 0x) or octal form (starting with 0), Please see the following three expressions of direct quantities:
int a = 100000; // 10进制 int b = 0x186a0; // 16进制 int c = 0303240; // 8进制 int a = 100000; // 10进制 int b = 0x186a0; // 16进制 int c = 0303240; // 8进制
int c = 5/3; System.out.println(c); // c的值为1,取整 int total = 87; int error = 23; int percent = error / total * 100; System.out.println(percent+"%"); //结果为0%,23除以87整数部分为0,乘以100,为0 percent = 100 * error / total; System.out.println(percent + "%"); // 结果为26%,230除以87整数部分为26 int c = 5/3; System.out.println(c); // c的值为1,取整 int total = 87; int error = 23; int percent = error / total * 100; System.out.println(percent+"%"); //结果为0%,23除以87整数部分为0,乘以100,为0 percent = 100 * error / total; System.out.println(percent + "%"); // 结果为26%,230除以87整数部分为26
When two integers are operated on, The result may exceed the range of the integer and cause overflow. If the positive number is too large, the result will be a negative number; if the negative number is too large, the result will be a positive number. The sample code is as follows:
int a = 2147483647; //int类型整数的上限 int b = -2147483648; //int类型整数的下限 a = a + 1; b = b - 1; System.out.println("a=" + a); //输出结果: a=-2147483648 溢出,结果错误。 System.out.println("b=" + b); //输出结果: b=2147483647溢出,结果错误。 int a = 2147483647; //int类型整数的上限 int b = -2147483648; //int类型整数的下限 a = a + 1; b = b - 1; System.out.println("a=" + a); //输出结果: a=-2147483648 溢出,结果错误。 System.out.println("b=" + b); //输出结果: b=2147483647溢出,结果错误。
When representing integers, if the range of the int type is not enough, you can use the long type. A long type variable occupies 8 bytes (that is, 64 bits), and the maximum representation range is: -263 ~ 263-1, that is -9223372036854775808 ~ 9223372036854775807.
If you want to express a long direct quantity, it needs to end with L or l. The sample code is as follows:
long a = 10000000000; //会有编译错误,因为10000000000编译器认为是int类型,而这个值,已经超出了int的范围 long b = 10000000000l; //正确 long a = 10000000000; //会有编译错误,因为10000000000编译器认为是int类型,而这个值,已经超出了int的范围 long b = 10000000000l; //正确
For larger integer operations (more than int Expression range), long type can be used. The sample code is as follows:
long distance1 = 10000 * 365 * 24 * 60 * 60 * 299792458l; //必须有一个long型数据参与的运算结果才是long型 System.out.println("distance1="+distance1);//distance1=547836957965889536 结果正确 long distance2 = 10000 * 365 * 24 * 60 * 60 * 299792458; System.out.println("distance2="+ distance2); //distance2=-1973211136 溢出,=号后面的数据默认为int类型,超出了范围,发生溢出。 long distance1 = 10000 * 365 * 24 * 60 * 60 * 299792458l; //必须有一个long型数据参与的运算结果才是long型 System.out.println("distance1="+distance1);//distance1=547836957965889536 结果正确 long distance2 = 10000 * 365 * 24 * 60 * 60 * 299792458; System.out.println("distance2="+ distance2); //distance2=-1973211136 溢出,=号后面的数据默认为int类型,超出了范围,发生溢出。
long time = System.currentTimeMillis(); System.out.println(time); //输出的结果为: 1383835712828 long time = System.currentTimeMillis(); System.out.println(time); //输出的结果为: 1383835712828
It can be seen from the above code that the output result has exceeded the maximum value of the int type. Therefore, the return type designed by the JDK is long. This method often is used for timing operations.
前面所学习的int、long都是用于存储整数的,小数即为浮点数,包括: float(单精度)和double(双精度),double类型的精度值是float类型的两倍,因此而得名双精精,在实际的应用开发中,float应用极少,大多数场合使用double表示浮点数。示例代码如下:
double pi = 3.14; double r = 8; double s = pi * r * r; System.out.println("s=" + s); // 输出的结果为:s=200.96 double pi = 3.14; double r = 8; double s = pi * r * r; System.out.println("s=" + s); // 输出的结果为:s=200.96
浮点数的直接量有两种写法:1)通常写法,如:3.14、314、0.1、.5。 2)科学计数法,如:1.25E2、1.25e2、1.25E-2。其中,1.25E2表示1.25乘以10的2次方。
默认的浮点直接量为double型,如果需要表示float类型的直接量,需要加“f”或“F”后缀。例如:
float f1 = 3.14 //编译错误,应该写成3.14f float f1 = 3.14 //编译错误,应该写成3.14f
2进制系统中无法精确的表示1/10,就好像十进制系统中无法精确的表示1/3一样,
所以,2进制表示10进制会有一些舍入误差,对于一些要求精确运算的场合会导致代码的缺陷。示例代码如下所示:
double money = 3.0; double price = 2.9; System.out.println(money - price); //输出的结果是: 0.10000000000000009 double money = 3.0; double price = 2.9; System.out.println(money - price); //输出的结果是: 0.10000000000000009
如果需要精确的运算可以考虑放弃使用double或float而采用BigDecimal 类来实现。关于这一点,将在后续的章节中介绍。
字符类型char事实上是一个16位无符号整数(都是正数),这个值是对应字符的编码,Java字符类型采用Unicode字符集编码(通用码、统一码、万国码),而Unicode是世界通用的定长字符集,所有的字符都是16位来表示。例如:字符a实际的值为97,字符A实际的值为65,字符0实际的值为48。
字符直接量可以采用诸如:‘中’的形式,也可以采用16进制的表示形式,例如: ‘\u4e2d’,代码如下所示:
char c1 = ‘中’; //c1中存的是”中”的编码 char c2 = '\u4e2d'; //‘4e2d’为‘中’所对应的16位Unicode编码的16进制表示形式 System.out.println(c1); System.out.println(c2); char c1 = ‘中’; //c1中存的是”中”的编码 char c2 = '\u4e2d'; //‘4e2d’为‘中’所对应的16位Unicode编码的16进制表示形式 System.out.println(c1); System.out.println(c2);
如上代码的输出结果:c1的值为中,c2值也为中,但c1和c2内部存储的其实是”中”这个字符所对应的Unicode码,即:一个无符号的整数。
在对char型变量赋值时,可以采用如下三种方式:
方式一:
字符直接量:形如‘A’,变量中实际存储的是该字符的Unicode编码(无符号整数值),一个char型变量只能存储一个字符。示例如下:
char c1 = 'A'; char c1 = 'A';
方式二:
整型直接量:范围在0~65535之间的整数,变量中实际存储的即该整数值,但表示的是该整数值所对应的Unicode字符。示例如下:
char c2 = 65; char c2 = 65;
Unicode形式:形如‘\u0041’,Unicode字符的16进制形式。示例如下:
char c3 = '\u0041'; char c3 = '\u0041';
字符直接量需要包含在一对’’单引号之中,那如果想表示单引号’的字符时,需要怎么表示?想表示回车、换行符时,怎么表示?
因为单引号为特殊意义的字符, 那么,对于不方便输出的字符可以采用转义字符来表示,示例代码如下:
char c = '\\'; System.out.println(c); //输出的结果为:\ char c = '\\'; System.out.println(c); //输出的结果为:\
常用转义字符如下图 – 2所示:
图- 2
boolean类型适用于关系、逻辑运算, 表示某个条件是否成立, 只允许取值true或false,true表示条件成立, 而false表示条件不成立。
boolean型变量经常用于存储关系运算的结果,所谓关系运算就是比较两个变量的大小相等等关系(此知识点,后续详细介绍)。boolean示例代码如下所示:
int age = 18; boolean isChild = age<16; System.out.println(isChild); // isChild的值为false boolean running = true; boolean closed = false; int age = 18; boolean isChild = age<16; System.out.println(isChild); // isChild的值为false boolean running = true; boolean closed = false;
不同的基本类型直接可以相互转化,主要有两种方式:
自动类型转化(隐式类型转换):从小类型到大类型可以自动完成。类型的大小关系如下图 - 3所示:
图- 3
强制转化:从大类型到小类型需要强制转换符,语法如下:
(需要转换成的类型)变量
因为大类型的精度值大于小类型,取值范围大于小类型,所以,当使用强制转化时,有可能会造成精度的损失或者溢出,所以,在使用强制转化时要求显式的告诉编译器,正在进行强制转换。
基本类型转化如下示例所示,注意强制转换时可能会造成的精度丧失和溢出。
int a = 100; int b = 200; long c = a + b; //自动将int转化为long long l1 = 1024l; int i = (int) l1; //需要加强制转化符由于1024在int的范围内,所以没有产生溢出 long l = 1024L * 1024 * 1024 * 4; int j = (int) l; //会产生溢出 System.out.println(j); // 结果为:0 double pi = 3.1415926535897932384; float f = (float) pi; //会造成精度的损失,因为单精度的精确度小于double System.out.println(f); //结果为:3.1415927 int a = 100; int b = 200; long c = a + b; //自动将int转化为long long l1 = 1024l; int i = (int) l1; //需要加强制转化符由于1024在int的范围内,所以没有产生溢出 long l = 1024L * 1024 * 1024 * 4; int j = (int) l; //会产生溢出 System.out.println(j); // 结果为:0 double pi = 3.1415926535897932384; float f = (float) pi; //会造成精度的损失,因为单精度的精确度小于double System.out.println(f); //结果为:3.1415927
如果在一个表达式中出现了多种数据类型,则运算结果会自动的向较大的类型进行转化,
示例如下:
//由于有long型的直接量参与,整个表达式的结果为long long distance = 10000 * 365 * 24 * 60 * 60 * 299792458l; //由于有double型的直接量599.0参与,整个表达式的结果为 double double change = 800 - 599.0; //结果为0.0,右边都是int型数据运算结果也为int类型,结果为0,再赋值给double 将0转化为 0.0 double persent1 = 80 / 100; //结果为0.8,右边表达式有double型直接量参与, 运算结果为double型 double persent2 = 80.0 / 100; //由于有long型的直接量参与,整个表达式的结果为long long distance = 10000 * 365 * 24 * 60 * 60 * 299792458l; //由于有double型的直接量599.0参与,整个表达式的结果为 double double change = 800 - 599.0; //结果为0.0,右边都是int型数据运算结果也为int类型,结果为0,再赋值给double 型,将0转化为 0.0 double persent1 = 80 / 100; //结果为0.8,右边表达式有double型直接量参与, 运算结果为double型 double persent2 = 80.0 / 100;
在前面所介绍的8种数据类型中,byte、char、short、int、long都表示整数类型,而整型的直接量为int,在实际使用中,为了方便使用,遵循了如下的规则:
int直接量可以直接赋值给byte、char和short,只要不超过其表示范围。示例如下:
byte b = 97; short s = 97; char c = 97; byte b = 97; short s = 97; char c = 97;
byte、char、short三种类型参与运算时,先一律转换成int类型再进行运算。示例如下:
byte b = 97; int num = b + b; //num的值为194
相关视频教程推荐:Java视频教程
The above is the detailed content of What are the eight basic data types?. For more information, please follow other related articles on the PHP Chinese website!