目录
Object 是类的层次结构根,每个类都可以将 Object 作为超类,所有类都是直接或间接的继承 Object 类。
构造方法:public Object()
问题:回想面向对象中,为什么说子类的构造方法默认访问的是父类的无参构造方法?
∵ 它们的顶级父类(Object类中)只有无参构造方法
Object 类 有哪些方法呢?
Object类 中有一个无参构造方法
clone()——克隆方法(复制对象)
equals(Object obj)——比较两个对象是否相等
finalize()——jvm的垃圾回收机制(jdk11已过时)
getClass()——获取该对象的class
hashCode()——HashMap集合
toString()——返回对象的字符串表示形式
notify()、notifyAll()、wait()、wait(long timeoutMillis)、wait(long timeoutMillis, int nanos)——这些是 多线程 synchronized 多线程之间的通讯
Parent.java
- package com.api.Demo02;
-
- public class Parent {
- // Parent 间接的形式继承了 Object类
- }
Student.java
- package com.api.Demo02;
-
- public class Student extends Parent{
- // Student直接的形式继承了Parent
- }
Test01.java
- package com.api.Demo02;
-
- public class Test01 {
- public static void main(String[] args) {
- // 多态机制 通过父类引用 指向 子类对象
- Object student = new Student();
- }
- }

Student.java
- package com.api.Demo02;
-
- public class Student extends Parent{
- // Student直接的形式继承了Parent
- private String name;
- private int 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;
- }
- }
Test02.java
- package com.api.Demo02;
-
- public class Test02 {
- public static void main(String[] args) {
- Student stu = new Student();
- stu.setName("mayikt");
- stu.setAge(22);
- System.out.println(stu); //输出该类的完整路径地址(包名+类的名称@...)com.api.Demo02.Student@4554617c
- System.out.println(stu.getClass().getName() + "@" + Integer.toHexString(stu.hashCode()));
- // 上面这行代码是下面的toString()方法里的 return
-
- /**
- * 底层是如何实现的?
- * ============================================================================
- * public void println(Object x) {
- * String s = String.valueOf(x);
- * synchronized (this) {
- * print(s);
- * newLine();
- * }
- * }
- * ============================================================================
- * public static String valueOf(Object obj) {
- * return (obj == null) ? "null" : obj.toString();
- * }
- * ============================================================================
- * public String toString() {
- * return getClass().getName() + "@" + Integer.toHexString(hashCode());
- * }
- * ============================================================================
- * 1.String.valueOf(x)
- * String.valueOf 底层是如何实现的呢?
- * 通过三元表达式判断,该对象是否为null,如果是null 则返回一个空字符串 否则 调用obj.toString
- * 2.toString() 底层如何实现?
- * getClass().getName()获取该类的完整路径地址 @ hashCode()值 转换成字符串
- */
- }
- }
在Student类中重写toString()
Student.java 加上如下代码
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
Test02.java
- package com.api.Demo02;
-
- public class Test02 {
- public static void main(String[] args) {
- Student stu = new Student();
- stu.setName("mayikt");
- stu.setAge(22);
- System.out.println(stu); //com.api.Demo02.Student@4554617c(未重写toString()之前的)
- System.out.println(stu); //Student{name='mayikt', age=22}(重写toString()之后的)
- System.out.println(stu.getClass().getName() + "@" + Integer.toHexString(stu.hashCode()));
- //com.api.Demo02.Student@4554617c
- System.out.println(stu.toString());//Student{name='mayikt', age=22}
- }
- }
综上:Object 类中的 toString() 目的 就是给我们对象重写的,输出对象中所有程序的属性值
String 对象用于保存字符串,也就是一组字符序列;
字符串常量对象是用双引号括起来的字符序列;
字符串的字符使用Unicode 字符编码,一个字符(不区分字母还是汉字)占2个字节
String 类比较常用的构造器
String s1 = new String()
String s2 = new String(String original)
String s3 = new String(char[] c)
String s4 = new String(char[] c, int startIndex, int count)
String 实现了Serializable,说明String可以串行化
String 实现了 Comparable 接口,说明String 对象可以比较
String 是final 不可以被继承,不可以被修改(内存地址不可以修改,内容是可以修改的)底层实际上通过char数组实现
Test01.java
- package com.api.Demo03;
-
- public class Test01 {
- public static void main(String[] args) {
- // JDK8 String 底层基于 char[] 实现的
- // JDK9 String 底层基于 byte[] 实现的
- String str = "mayikt";
- char c = 'm';
- /**
- * char 只能保存单个字符
- * String保存多个字符——保存字符串
- * String 底层如何实现的呢?保存字符串——底层是基于数组实现
- */
- char[] value = {'m', 'a', 'y', 'i', 'k', 't'};
-
- System.out.println("这是使用 char[] 封装存放的字符串: " + value); //这样只能输出内存地址
- System.out.print("这是使用 char[] 封装存放的字符串: ");
- System.out.println(value);
- }
- }
Stirng的底层

Test02.java
- package com.api.Demo03;
-
- public class Test02 {
- public static void main(String[] args) {
- String str1 = "mayikt1";
- String str2 = "mayikt2";
- boolean result1 = str1.equals(str2);
- System.out.println(result1); //false
-
- String str3 = "mayikt";
- String str4 = "mayikt";
- boolean result2 = str3.equals(str4);
- System.out.println(result2); //true
-
- String str5 = null;
- System.out.println(str1.equals(str5));//false
- // System.out.println(str5.equals(str1));//报错,NullPointerException 空指针
- // equals 比较时 左边 如果是为 null, 报错, 空指针
- }
- }
下一篇文章: 三种不同的方式实现用户登录