1. String概述
java.lang.String类代表字符串,Java程序中的所有字符串文字(例如“abc”)都为此类的对象。
字符串的内容是不会发生改变的,它的对象在创建之后不能被修改。
总结:
- String是Java定义好的一个类。定义在java.lang中,所以使用的时候不用导包
- Java程序中的所有字符串文字都被实例化为此类的对象
- 字符串不可变,它们的值在创建后不能被更改
2. 创建String对象的两种方式
直接赋值和new
构造方法 | 说明 |
public String() | 创建空白字符,不含任何内容 |
public String(String original) | 根据传入的字符串,创建字符串对象 |
public String(char[ ] chs) | 根据字符数组,创建字符串对象 |
public String(byte[ ] chs) | 根据字节数组,创建字符串对象 |
public class demo {public static void main(String[] args) {// 1. 直接赋值的方式获取一个字符串对象String a = "abc";System.out.println(a);// 2. 使用new的方式获取一个字符串对象// 空参构造String s2 = new String();System.out.println(s2);// 传递一个字符串,根据传递的字符串内容再创建一个新的字符串对象String s3 = new String("abc");System.out.println(s3);// 传递一个字符数组,根据字符数组的内容再创建一个新的字符串对象// 需求: 用来修改字符串的内容char[] chs = {'a','b','c'};String s4 = new String(chs);System.out.println(s4);// 传递一个字节数组,根据字节数组的内容再创建一个新的字符串对象// 应用场景: 以后在网络中传输的数据其实都是字节信息// 我们一般要把字节信息进行转换,转成字符串byte[] bytes = {97,98,99,100};String s5 = new String(bytes);System.out.println(s5);}
}
3. String内存分布
字符串常量池(StringTable)的内存分布:
JDK7之前:
JDK7开始:
3.1 直接赋值的内存模型
当使用双引号直接赋值时,系统会检查该字符串在字符串常量池中是否存在。
- 不存在:创建新的
- 存在:复用
3.2 new赋值的内存模型
如果相同的字符串比较多,会浪费内存。所以我们要采用直接赋值的方式,不仅简单还能节约内存。
4. 字符串的比较
== 比较的是栈内存的值
equals 比较的是引用的对象的值
public class demo {public static void main(String[] args) {// ==: 比较栈内存的值// equals: 比较引用对象的值String s1 = "abc";String s2 = "abc";System.out.println(s1 == s2); // trueString s3 = new String("abc");String s4 = new String("abc");System.out.println(s3 == s4); // falseSystem.out.println(s3.equals(s4)); // trueSystem.out.println(s3.equals(s1)); // true}
}
5. String练习
5.1 练习1
需求:已知正确的用户名和密码,请用程序实现模拟用户登录。总共三次机会,登录之后,给出相应的提示。
import java.util.Scanner;public class demo {public static void main(String[] args) {// 已知的用户名和密码String username = "admin";String password = "123456";// 总共三次机会for (int i = 0; i < 3; i++) {Scanner sc = new Scanner(System.in);System.out.println("Enter your username: ");String user = sc.next();System.out.println("Enter your password: ");String pass = sc.next();// 登录成功if (user.equals(username) && pass.equals(password)) {System.out.println("You are logged in!");break;}else {// 登录失败System.out.println("Invalid username or password!");continue;}}}
}
5.2 练习2
需求:键盘录入一个字符串,使用程序实现在控制台遍历该字符串
- public char charAt(int d):根据索引返回字符
- public int length(): 返回字符串的长度
- 数组的长度: arr.length
- 字符串的长度: str.length( )
import java.util.Scanner;public class demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("Please enter string: ");String str = sc.next();for (int i = 0; i < str.length(); i++) {System.out.print(str.charAt(i)+" ");}}
}
需求:键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
import java.util.Scanner;public class demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串:");String str = sc.next();int bigCount = 0;int smallCount = 0;int numberCOunt = 0;for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c >= '0' && c <= '9') {numberCOunt++;}else if (c >= 'A' && c <= 'Z') {bigCount++;}else if (c >= 'a' && c <= 'z') {smallCount++;}}System.out.println("大写字母出现次数:"+bigCount);System.out.println("小写字母出现次数:"+smallCount);System.out.println("数字出现次数:"+numberCOunt);}
}