day08-常用类-常用类_String的概述和特点
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
|
||||||
|
字符串不变; 它们的值在创建后不能被更改。 因为String对象是不可变的,它们可以被共享。
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义一个字符串变量(对象)
|
||||||
|
String str1 = "abc";//str1存放的是地址,str1是String类的一个对象,可以调用很多成员方法
|
||||||
|
System.out.println(str1);//虽然str1存放的是地址,但是底层源码在打印的时候,重载toString,直接打印了地址对应的内容
|
||||||
|
|
||||||
|
//定义2个自定义类的对象
|
||||||
|
Student s1 = new Student();
|
||||||
|
Student s2 = new Student();
|
||||||
|
System.out.println(s1 == s2);//false
|
||||||
|
//再定义一个字符串
|
||||||
|
String str2 = "abc";
|
||||||
|
System.out.println(str1 == str2);//true:str1和str2是同一个对象“abc”,地址相同的
|
||||||
|
str1 = "123";
|
||||||
|
System.out.println(str1 == str2);//false,str1中保存的地址改变了,和str2地址不同
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user