🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## Java编程那些事儿17——基本数据类型 作者:陈跃峰 出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb) **3.5 基本数据类型** 程序中最核心的就是一系列的数据,或者叫程序状态,计算机为了方便的管理数据,就为数据设定了一组类型,这样在为数据分配内存以及操作数据时都比较方便,这就是数据类型的由来。其实现实生活中也存在各种数据类型,例如数字型,字符型等,数字型又可以划分为整数型和小数型,只是没有很可以的划分罢了。 在数据类型中,最常用也是最基础的数据类型,被称作基本数据类型。可以使用这些类型的值来代表一些简单的状态。 **3.5.1 概述** 学习数据类型的目的就是在需要代表一个数值时,能够选择合适的类型。当然,有些时候好几种类型都适合,那就要看个人习惯了。 学习数据类型需要掌握每种数据类型的特征,以及对应的细节知识,这样会有助于对类型的选择。所以在初次学习时,需要记忆很多的内容 Java语言的基本数据类型总共有以下8种,下面是按照用途划分出的4个类别: 1. 整数型:byte(字节型)、short(短整型)、int(整型)、long(长整型) 2. 小数型:float(单精度浮点型)、double(双精度浮点型) 3. 字符型 4. 布尔型 **3.5.2 整数型** 整数型是一类代表整数值的类型。当需要代表一个整数的值时,可以根据需要从4种类型中挑选合适的,如果没有特殊要求的话,一般选择int类型。4种整数型区别主要在每个数据在内存中占用的空间大小和代表的数值的范围。具体说明参看下表:  整数型参数表 <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 40.85pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"><tbody><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">类型名称</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">关键字</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">占用空间(字节)</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">取值范围</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">默认值</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">字节型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">byte</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">-2<sup>7</sup>—2<sup>7</sup>-1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">0</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">短整型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">short</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">2</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">-2<sup>15</sup>—2<sup>15</sup>-1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">0</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">整型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">int</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">4</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">-2<sup>31</sup>—2<sup>31</sup>-1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">0</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">长整型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">long</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">8</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63.8pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="85"> <div align="center">-2<sup>63</sup>—2<sup>63</sup>-1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 49.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="66"> <div align="center">0</div> </td> </tr></tbody></table> 说明:1、Java中的整数都是有符号数,也就是有正有负。 2、默认值指在特定的情况下才自动初始化,具体的情况后续将有叙述。 3、程序中的整数数值默认是int以及int以下类型,如果需要书写long型的值,则需要在数值后面添加字母L,大小写均可。 4、程序中默认整数是十进制数字,八进制数字以数字字符0开头,例如016、034等,十六进制数字以数字字符0和字母x(不区分大小写)开头,例如0xaf、0X12等。 **3.5.3 小数型** 小数型是一类代表小数值的类型。当需要代表一个小数的值时,可以根据需要从以下2种类型中挑选合适的。如果没有特殊要求,一般选择double类型。 由于小数的存储方式和整数不同,所以小数都有一定的精度,所以在计算机中运算时不够精确。根据精度和存储区间的不同,设计了两种小数类型,具体见下表: 小数型参数表 <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 26.7pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"><tbody><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">类型名称</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">关键字</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">占用空间(字节)</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">取值范围</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">默认值</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">单精度浮点型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">float</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">4</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">-3.4E+38—3.4E+38</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">0.0f</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">双精度浮点型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">double</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">8</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">-1.7E+308—1.7E+308</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">0.0</div> </td> </tr></tbody></table> 说明:1、取值范围以科学计数法形式进行描述。 2、在程序中,小数的运算速度要低于整数运算。 3、float类型的小数,需要在小数后加字母f,不区分大小写,例如1.01f。 **3.5.4 字符型** 字符型代表特定的某个字符,按照前面介绍的知识,计算机中都是以字符集的形式来保存字符的,所以字符型的值实际只是字符集中的编号,而不是实际代表的字符,由计算机完成从编号转换成对应字符的工作。 Java语言中为了更加方便国际化,使用Unicode字符集作为默认的字符集,该字符集包含各种语言中常见的字符。 在程序代码中,字符使用一对单引号加上需要表达的字符来标识,例如’A’、’a’等,当然也可以直接使用字符编码,也就是一个非负整数进行表示。 字符型参数表 <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 26.7pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"><tbody><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">类型名称</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">关键字</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">占用空间(字节)</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">取值范围</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">默认值</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">字符型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">char</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">2</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">0-2<sup>16</sup>-1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">0</div> </td> </tr></tbody></table> 说明:1、字符型的编号中不包含负数。 2、字符型由于存储的是编号的数值,所以可以参与数学运算。 3、字符型可以作为Java语言中的无符号整数使用。 4、字符型的默认值是编号为0的字符,而不是字符0 **3.5.5 布尔型** 布尔型代表逻辑中的成立和不成立。Java语言中使用关键字true代表成立,false代表不成立。布尔型是存储逻辑值的类型,其实很多程序中都有逻辑值的概念,Java把逻辑的值用布尔型来进行表达。 布尔型参数表 <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 26.7pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"><tbody><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">类型名称</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">关键字</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">占用空间(字节)</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">取值范围</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">默认值</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 77.95pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="104"> <div align="center">布尔型</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">boolean</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center"> </div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 106.35pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="142"> <div align="center">true或false</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 42.5pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="57"> <div align="center">false</div> </td> </tr></tbody></table> 说明: 1、布尔型占用的空间取决于Java虚拟机(JVM)的实现,可能是1位也可能是1个字节。 **3.5.6 小结** 这里简单的介绍了8种基本数据类型的基本特征,在实际的程序设计中,可以根据需要选择对应的类型。 由于Java语言是一种强类型的语言,所以在使用数据类型是需要小心。