Java基本数据类型 #
一、数据类型概述 #
Java是强类型语言,每个变量必须声明类型。Java数据类型分为两大类:
text
数据类型
├── 基本数据类型(Primitive Type)
│ ├── 整数类型: byte, short, int, long
│ ├── 浮点类型: float, double
│ ├── 字符类型: char
│ └── 布尔类型: boolean
└── 引用数据类型(Reference Type)
├── 类(Class)
├── 接口(Interface)
└── 数组(Array)
二、八种基本数据类型 #
2.1 类型一览表 #
| 类型 | 关键字 | 字节数 | 位数 | 取值范围 | 默认值 |
|---|---|---|---|---|---|
| 字节型 | byte | 1 | 8 | -128 ~ 127 | 0 |
| 短整型 | short | 2 | 16 | -32768 ~ 32767 | 0 |
| 整型 | int | 4 | 32 | -2^31 ~ 2^31-1 | 0 |
| 长整型 | long | 8 | 64 | -2^63 ~ 2^63-1 | 0L |
| 单精度浮点 | float | 4 | 32 | ±3.4E38 | 0.0f |
| 双精度浮点 | double | 8 | 64 | ±1.7E308 | 0.0d |
| 字符型 | char | 2 | 16 | 0 ~ 65535 | ‘\u0000’ |
| 布尔型 | boolean | 1 | - | true/false | false |
三、整数类型 #
3.1 byte(字节型) #
java
byte b = 10;
byte max = 127; // 最大值
byte min = -128; // 最小值
// byte b2 = 128; // 错误:超出范围
用途:处理二进制数据、文件IO、网络传输。
3.2 short(短整型) #
java
short s = 1000;
short max = 32767; // 最大值
short min = -32768; // 最小值
用途:较少使用,主要用于节省内存。
3.3 int(整型) #
最常用的整数类型。
java
int num = 100;
int max = 2147483647; // 最大值
int min = -2147483648; // 最小值
// 使用下划线分隔大数字(Java 7+)
int bigNum = 1_000_000_000;
// 不同进制表示
int decimal = 100; // 十进制
int binary = 0b1100100; // 二进制
int octal = 0144; // 八进制
int hex = 0x64; // 十六进制
3.4 long(长整型) #
用于大整数。
java
long num = 100L; // 建议加L后缀
long max = 9223372036854775807L;
long min = -9223372036854775808L;
// 大数字使用下划线分隔
long bigNum = 1_000_000_000_000L;
// 注意:不加L后缀,默认为int
// long big = 2147483648; // 错误:超出int范围
long big = 2147483648L; // 正确
3.5 整数类型选择 #
| 类型 | 使用场景 |
|---|---|
| byte | 二进制数据处理、字节数组 |
| short | 节省内存、特定算法 |
| int | 一般整数计算(首选) |
| long | 大整数、时间戳、ID |
四、浮点类型 #
4.1 float(单精度浮点) #
java
float f1 = 3.14f; // 必须加f或F后缀
float f2 = 3.14F;
float f3 = 100f;
// float f4 = 3.14; // 错误:默认是double
精度:约6-7位有效数字。
4.2 double(双精度浮点) #
java
double d1 = 3.14; // 默认是double
double d2 = 3.14d; // 可以加d或D后缀
double d3 = 3.14D;
double d4 = 100.0;
// 科学计数法
double scientific = 1.5e10; // 1.5 × 10^10
double tiny = 1.5e-10; // 1.5 × 10^-10
精度:约15-16位有效数字。
4.3 浮点数精度问题 #
java
// 精度问题示例
double a = 0.1 + 0.2;
System.out.println(a); // 0.30000000000000004
// 解决方案:使用BigDecimal
import java.math.BigDecimal;
BigDecimal bd1 = new BigDecimal("0.1");
BigDecimal bd2 = new BigDecimal("0.2");
BigDecimal result = bd1.add(bd2);
System.out.println(result); // 0.3
4.4 特殊浮点值 #
java
// 正无穷大
double positiveInfinity = Double.POSITIVE_INFINITY;
System.out.println(1.0 / 0.0); // Infinity
// 负无穷大
double negativeInfinity = Double.NEGATIVE_INFINITY;
System.out.println(-1.0 / 0.0); // -Infinity
// 非数字
double nan = Double.NaN;
System.out.println(0.0 / 0.0); // NaN
// 判断方法
System.out.println(Double.isInfinite(positiveInfinity)); // true
System.out.println(Double.isNaN(nan)); // true
五、字符类型 #
5.1 char基本使用 #
char用于存储单个字符,使用单引号包围。
java
char c1 = 'A';
char c2 = '中';
char c3 = '9';
char c4 = '@';
5.2 char的本质 #
char本质是16位无符号整数,存储Unicode编码。
java
char c1 = 'A';
char c2 = 65; // 与'A'相同
System.out.println(c1); // A
System.out.println(c2); // A
// 字符与编码转换
int code = 'A';
System.out.println(code); // 65
char c = (char) 20013;
System.out.println(c); // 中
5.3 转义字符 #
| 转义字符 | 说明 | Unicode |
|---|---|---|
| \n | 换行 | \u000a |
| \r | 回车 | \u000d |
| \t | 制表符 | \u0009 |
| \ | 反斜杠 | \u005c |
| ' | 单引号 | \u0027 |
| " | 双引号 | \u0022 |
| \b | 退格 | \u0008 |
java
char newline = '\n';
char tab = '\t';
char quote = '\'';
char backslash = '\\';
String message = "Hello\nWorld\t!";
5.4 Unicode表示 #
java
char c1 = '\u0041'; // 'A'
char c2 = '\u4e2d'; // '中'
System.out.println(c1); // A
System.out.println(c2); // 中
六、布尔类型 #
6.1 boolean基本使用 #
java
boolean isTrue = true;
boolean isFalse = false;
// 条件判断
if (isTrue) {
System.out.println("条件为真");
}
// 逻辑运算
boolean result = isTrue && !isFalse;
6.2 布尔运算 #
java
boolean a = true;
boolean b = false;
// 逻辑与
boolean and = a && b; // false
// 逻辑或
boolean or = a || b; // true
// 逻辑非
boolean not = !a; // false
// 异或
boolean xor = a ^ b; // true
6.3 注意事项 #
java
// 错误:不能与整数转换
// boolean flag = 1;
// boolean flag = 0;
// 正确方式
boolean flag = true;
if (flag) {
System.out.println("真");
}
七、类型默认值 #
成员变量有默认值,局部变量没有默认值。
java
public class DefaultValues {
byte b; // 0
short s; // 0
int i; // 0
long l; // 0L
float f; // 0.0f
double d; // 0.0d
char c; // '\u0000'
boolean bool; // false
public void method() {
// 局部变量必须初始化
// int local; // 错误:未初始化
int local = 0; // 正确
}
}
八、类型选择建议 #
| 场景 | 推荐类型 |
|---|---|
| 一般整数 | int |
| 大整数、ID、时间戳 | long |
| 小数计算 | double |
| 精确计算(金额) | BigDecimal |
| 单个字符 | char |
| 是/否判断 | boolean |
| 二进制数据 | byte[] |
九、示例代码 #
java
public class PrimitiveTypesDemo {
public static void main(String[] args) {
// 整数类型
byte byteVar = 127;
short shortVar = 32767;
int intVar = 2147483647;
long longVar = 9223372036854775807L;
// 浮点类型
float floatVar = 3.14f;
double doubleVar = 3.14159265358979;
// 字符类型
char charVar = 'A';
char chineseChar = '中';
// 布尔类型
boolean boolVar = true;
// 输出
System.out.println("byte: " + byteVar);
System.out.println("short: " + shortVar);
System.out.println("int: " + intVar);
System.out.println("long: " + longVar);
System.out.println("float: " + floatVar);
System.out.println("double: " + doubleVar);
System.out.println("char: " + charVar);
System.out.println("chinese: " + chineseChar);
System.out.println("boolean: " + boolVar);
// 类型信息
System.out.println("int最大值: " + Integer.MAX_VALUE);
System.out.println("int最小值: " + Integer.MIN_VALUE);
System.out.println("double最大值: " + Double.MAX_VALUE);
}
}
十、总结 #
| 类型 | 字节 | 用途 |
|---|---|---|
| byte | 1 | 二进制数据 |
| short | 2 | 节省内存 |
| int | 4 | 一般整数 |
| long | 8 | 大整数 |
| float | 4 | 单精度浮点 |
| double | 8 | 双精度浮点(默认) |
| char | 2 | 单个字符 |
| boolean | 1 | 真假值 |
基本数据类型是Java编程的基础,理解每种类型的特点和适用场景非常重要。
最后更新:2026-03-26