day08-常用类_String的概述和特点

This commit is contained in:
2026-05-16 11:41:14 +08:00
parent 55ad571736
commit d65005955a
2 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
package com.inmind.string01;
/*
常用类_String的概述和特点
1.String类代表字符串。 Java程序中的所有字符串文字例如"abc" )都被实现为此类的实例。
""双引号字符串就是String类的对象所以就可以直接调用String类的方法
2.字符串不变; 它们的值在创建后不能被更改。 因为String对象是不可变的它们可以被共享。
*/
public class StringDemo01 {
public static void main(String[] args) {
//创建2个学生对象
Student s1 = new Student("小王", 18);
Student s2 = new Student("小王", 18);
System.out.println(s1);
System.out.println(s2);
System.out.println(s1 == s2);//false 注意:引用数据类型中保存的是地址,当前地址不同
//定义2个字符串对象
String str1 = "abc";//str1中保存是地址所有引用数据类型保存的永远是地址
System.out.println(str1);//这里由于String类的底层源码重写了toString方法所以打印的是对应地址指向的内容
String str2 = "abc";
System.out.println(str1 == str2);//true
}
}