Object类
是所有类的顶层父类。
图中漏了一个非常重要的方法,toString()方法,具体用法会在后面代码中详细解释。
所有类都默认继承了Object类,所有具有Object类的这些方法,光说不练假把式。上栗子:
我创建了一个测试类Test,但是我并没有让他继承Object类,但是默认继承,我就可以调用Object类的非私有修饰的所有方法。
equals方法
返回值:布尔类型
由于没有重写Object的方法,所有默认比较的是两个Student类对象的地址值,故输出false
getClass方法
返回值:返回运行时类的方法,反射中会用到该方法
hashCode方法
返回值:一个int类型的数
该方法内部通过哈希算法计算返回一个调用者对应的哈希码值。
toString方法
返回值:String类型
没有重写该方法的时候会打印对应地址值与十六进制拼成的字符串,重写以后以重写的方法进行对应输出。请看栗子
//一个Student实体类,未重写toString方法
public class Student {
private String name;
private int age;
public Student() {
}
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 class Test {
public static void main(String[] args) {
Student student1 = new Student();
System.out.println(student1.toString());
}
}
执行结果:
//重写父类toString方法
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("张三",12);
System.out.println(student1.toString());
}
}
执行结果 :
String类
charAt方法(public char charAt(int index))
参数:字符串对应索引值(int类型)
返回值 :char类型
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.charAt(5));
}
}
执行结果: f
注意:跟数组一样下标从0开始,越界会抛异常 StringIndexOutOfBoundsException
length方法(public int length())
返回值 返回int类型的长度
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.length());
}
}
执行结果 : 7
concat方法(public String concat(String str))
参数:String类型
返回值:String类型
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.concat("sssss"));
}
}
执行结果: abcdefgsssss
isEmpty方法 (public boolean isEmpty())
返回值:布尔类型
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.isEmpty());//字符串不为空就返回false,反之返回true
}
}
执行结果:false
toUpperCase方法( public String toUpperCase())
返回值:String类型
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.toUpperCase());//将字符串中小写字母全部转换为大写字母
}
}
执行结果:ABCDEFG
toLowerCase方法(public String toLowerCase())
返回值:String类型
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
System.out.println(s.toLowerCase());//将字符串中大写字母全部转换为小写字母
}
}
执行结果:abcdefg
toCharArray方法(public char[] toCharArray())
返回值:字符数组char[]
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
char[] chars = s.toCharArray();//将字符串转为字符数组
}
}
getBytes方法(public byte[] getBytes())
返回值:字节数组
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
byte[] bytes = s.getBytes();//将字符串转为字节数组
}
}
replace方法(public String replace(char oldChar,char newChar))
参数:第一个参数是需要被替换的字符串,第二个参数是需要替换成的字符串
返回值:String类型
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
System.out.println(s.replace("A","*"));
}
}
执行结果:*BCDEFG
substring方法
public String substring(int beginIndex)
参数:截取的索引处的索引值 int类型
返回值:String类型
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
System.out.println(s.substring(2));
}
}