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
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
66
day08/src/com/inmind/string01/Student.java
Normal file
66
day08/src/com/inmind/string01/Student.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
private String name;
|
||||||
|
private int age = 18;
|
||||||
|
private int id;
|
||||||
|
private String gender;
|
||||||
|
private double score;
|
||||||
|
|
||||||
|
|
||||||
|
public Student(){
|
||||||
|
//对所有的属性设置默认值
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(String name){
|
||||||
|
//针对指定的属性赋值,其他的属性默认
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(String name, int age){
|
||||||
|
//针对指定的属性赋值,其他的属性默认
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(String gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getScore() {
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(double score) {
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user