全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-09 6 分钟 ✍️ juanwangdev

Java字符串基础

字符串是Java中最常用的数据类型之一。

字符串概念

什么是字符串

字符串是由字符组成的序列,在Java中是String类对象。

Java
// 字符串字面量
String str1 = "Hello";
String str2 = "Java编程";

// 字符串是对象
String str3 = new String("Hello");

字符串特点

  • String是引用类型(不是基本类型)
  • 字符串使用双引号包裹
  • 字符串是不可变的
  • 底层存储是字符数组

创建字符串

字面量创建

直接使用双引号创建字符串。

Java
String name = "张三";
String message = "欢迎来到Java世界";

// 字面量存储在字符串常量池
String s1 = "Hello";
String s2 = "Hello";  // s1和s2指向同一对象(常量池)
System.out.println(s1 == s2);  // true

new关键字创建

使用new创建字符串对象。

Java
String s1 = new String("Hello");
String s2 = new String("Hello");

// new创建的对象在堆内存
System.out.println(s1 == s2);  // false(不同对象)
System.out.println(s1.equals(s2));  // true(内容相同)

其他创建方式

Java
// 从字符数组创建
char[] chars = {'H', 'e', 'l', 'l', 'o'};
String str = new String(chars);  // "Hello"

// 从字节数组创建
byte[] bytes = {65, 66, 67};
String str = new String(bytes);  // "ABC"

// 从部分字符数组创建
char[] chars = {'H', 'e', 'l', 'l', 'o'};
String str = new String(chars, 0, 3);  // "Hel"(从索引0取3个)

字符串存储

字符串常量池

字符串常量池存储字符串字面量,避免重复创建。

Java
// 字面量存入常量池
String s1 = "Java";       // 常量池创建
String s2 = "Java";       // 常量池已存在,直接引用
String s3 = new String("Java");  // 堆内存新对象

// s1和s2指向常量池同一对象
System.out.println(s1 == s2);  // true

// s3是堆内存新对象
System.out.println(s1 == s3);  // false

内存位置示意

Java
栈内存           字符串常量池(方法区)
s1 → 地址 ──→   "Java"对象

栈内存           堆内存
s3 → 地址 ──→   "Java"对象(新创建)

intern方法

intern()将字符串加入常量池。

Java
String s1 = new String("Java");
String s2 = s1.intern();  // 加入常量池

String s3 = "Java";  // 常量池已有
System.out.println(s2 == s3);  // true

字符串底层结构

Java 8及之前

底层是char数组。

Java
// Java 8 String底层
private final char value[];

// 每个字符占2字节(UTF-16)

Java 9及之后

底层改为byte数组 + 编码标识。

Java
// Java 9+ String底层
private final byte[] value;
private final byte coder;  // 编码标识(LATIN1或UTF16)

// 优化:纯ASCII字符只占1字节

字符串与基本类型

字符串与其他类型区别

类型分类示例
String引用类型"Hello"
char基本类型'A'(单字符)
int基本类型100

字符串与字符

Java
// 字符串(多个字符)
String str = "Hello";

// 字符(单个字符)
char c = 'A';

// 字符数组
char[] chars = {'A', 'B', 'C'};

字符串长度

length方法

获取字符串长度(字符数)。

Java
String str = "Hello";
int len = str.length();  // 5

String chinese = "中文";
int len = chinese.length();  // 2(按字符计数)

// 空字符串
String empty = "";
int len = empty.length();  // 0

空字符串与null

Java
// 空字符串:有对象,长度为0
String empty = "";
empty.length();  // 0

// null:无对象
String nullStr = null;
// nullStr.length();  // NullPointerException

// 判断方式
if (str != null && str.length() > 0) {  // 非空判断
    // 处理字符串
}

// 或使用isEmpty()
if (str != null && !str.isEmpty()) {
    // 处理字符串
}

字符串常用操作

获取字符

Java
String str = "Hello";

// 获取指定位置字符
char c = str.charAt(0);  // 'H'
char c2 = str.charAt(4);  // 'o'

// 错误:索引越界
// char c3 = str.charAt(10);  // StringIndexOutOfBoundsException

遍历字符串

Java
String str = "Hello";

// 方式1:charAt遍历
for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    System.out.println(c);
}

// 方式2:增强for循环(转为char数组)
for (char c : str.toCharArray()) {
    System.out.println(c);
}

// 方式3:Java 8+ stream
str.chars().forEach(c -> System.out.println((char) c));

字符串基本属性

判断空字符串

Java
String str = "";

// isEmpty():判断长度是否为0
boolean empty = str.isEmpty();  // true

// null不能调用isEmpty()
String nullStr = null;
// nullStr.isEmpty();  // NullPointerException

// 安全判断
if (str == null || str.isEmpty()) {
    // 空字符串或null
}

判断空白字符串

text
String str = "   ";

// Java 11+ isBlank()
boolean blank = str.isBlank();  // true(纯空白)

// 手动判断
boolean blank = str.trim().isEmpty();  // true

要点总结

  • 字符串是String类对象,使用双引号创建
  • String是引用类型,不是基本类型
  • 字面量存储在字符串常量池
  • new创建的对象在堆内存
  • 字符串底层是char数组(Java 8)或byte数组(Java 9+)
  • length()获取字符串长度
  • charAt()获取指定位置字符
  • isEmpty()判断是否空字符串
  • null需要单独判断,否则NPE
  • intern()将字符串加入常量池

📝 发现内容有误?点击此处直接编辑

← 上一篇 递归方法
下一篇 → StringBuilder与StringBuffer
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库