day08-常用类_String的概述和特点
This commit is contained in:
26
day08/src/com/inmind/string01/StringDemo01.java
Normal file
26
day08/src/com/inmind/string01/StringDemo01.java
Normal 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
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user