Skip to content

基本数据类型

Java 提供 8 种基本数据类型,分为整数、浮点数、字符和布尔四类。

基本类型一览

类型关键字位数默认值取值范围
字节型byte80-128 ~ 127
短整型short160-32768 ~ 32767
整型int320-2147483648 ~ 2147483647
长整型long640L-9223372036854775808 ~ 9223372036854775807
单精度浮点float320.0f±3.4E+38(7位精度)
双精度浮点double640.0d±1.8E+308(15位精度)
字符型char16'\u0000''\u0000' ~ '\uFFFF'
布尔型boolean1falsetrue / false

整数类型

byte:8 位有符号整数,取值范围 -128 ~ 127。

java
byte minValue = -128;
byte maxValue = 127;
byte b = 100;

// 超出范围会编译错误
// byte b1 = 128;  // 编译错误

short:16 位有符号整数,取值范围 -32768 ~ 32767。

java
short minValue = -32768;
short maxValue = 32767;
short s = 1000;

int:32 位有符号整数,取值范围 -2147483648 ~ 2147483647,是最常用的整数类型。

java
int minValue = -2147483648;
int maxValue = 2147483647;
int age = 25;
int count = -100;

long:64 位有符号整数,取值范围约 ±9.2×10^18。声明时需在数字后加 Ll

java
long minValue = -9223372036854775808L;
long maxValue = 9223372036854775807L;

// 常见用法
long population = 1400000000L;  // 人口数据
long timestamp = System.currentTimeMillis();  // 时间戳

浮点类型

float:32 位单精度浮点数,精度约 6-7 位有效数字,声明时必须加 fF 后缀。

java
float pi = 3.14f;
float e = 2.71828F;
float negValue = -1.5f;

// 科学计数法
float bigValue = 3.14e2f;  // 314.0
float smallValue = 3.14e-2f;  // 0.0314

double:64 位双精度浮点数,精度约 15 位有效数字,是默认的浮点类型。

java
double pi = 3.1415926;
double e = 2.718281828;
double price = 99.99;

// 科学计数法
double bigValue = 3.14e+10;  // 3.14 * 10^10
double smallValue = 3.14e-10;  // 3.14 * 10^-10

浮点数不适合精确计算,典型问题:

java
// 0.1 + 0.2 的结果并非精确的 0.3
System.out.println(0.1 + 0.2);  // 0.30000000000000004

// 精确计算应使用 BigDecimal
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
System.out.println(a.add(b));  // 0.3

字符类型

char:16 位 Unicode 字符,使用单引号包裹。

java
char letter = 'A';
char digit = '5';
char chinese = '中';
char unicode = '\u4E2D';  // Unicode 表示

// ASCII 码
char ascii = 65;  // 'A'
System.out.println((int)'A');  // 65

// 转义字符
char tab = '\t';  // 制表符
char newline = '\n';  // 换行符
char quote = '\'';  // 单引号
char backslash = '\\';  // 反斜杠

布尔类型

boolean:只能是 truefalse,不能使用 0/1 代替。

java
boolean isActive = true;
boolean isEmpty = false;
boolean isLoggedIn = (age >= 18);

// 在条件判断中
if (isActive) {
    System.out.println("激活状态");
}

// 在三元运算符中
String status = isActive ? "在线" : "离线";

类型转换

自动类型转换(隐式):容量小的类型自动转换为容量大的类型。

byte → short → int → long → float → double

         char
java
byte b = 100;
int i = b;  // byte 自动转换为 int

int i2 = 100;
long l = i2;  // int 自动转换为 long

long l2 = 100L;
double d = l2;  // long 自动转换为 double

强制类型转换(显式):容量大的类型转换为容量小的类型,需要显式声明。

java
double d = 99.9;
int i = (int) d;  // i = 99,小数部分被截断

long l = 100L;
byte b = (byte) l;  // b = 100

// 溢出示例
int i2 = 130;
byte b2 = (byte) i2;  // b2 = -126(溢出)

默认值

类成员变量(实例变量/静态变量)有默认值:

类型默认值
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
booleanfalse
java
public class DataTypes {
    static int staticInt;
    int instanceInt;

    public static void main(String[] args) {
        System.out.println(staticInt);  // 0
    }
}

局部变量没有默认值,必须先赋值再使用。

常见问题

整数溢出:整数运算可能超出取值范围,导致结果错误。

java
int max = Integer.MAX_VALUE;
System.out.println(max + 1);  // -2147483648(溢出)

// 解决方案:使用 long 或检查溢出
long safe = (long) max + 1;

float 和 double 精度差异:浮点数精度有限,运算结果可能存在微小误差。

java
System.out.println(1.0f - 0.9f);  // 0.100000024
System.out.println(1.0 - 0.9);    // 0.09999999999999998

char 与 String 的区别:char 是单个字符(单引号),String 是字符串(双引号)。

java
char c = 'A';      // 单个字符
String s = "A";    // 字符串

基于 VitePress 构建