JDK1.8源码分析

1、Arrays.toString()

package test.javase.array;
import java.util.Arrays;
/**
 * @author zhangxuhui
 * @email zxh_1633@163.com
 * @create 2020-03-13 16:51
 */
public class ShowArray {
    public static void main(String[] args) {
        //数组声明的三种方式
        int [] arr = new int [5];
        String [] str = new String [] {"a","b"};
        char [] ch = {'a','b','c'};
        
        String s = Arrays.toString(str);
        System.out.println(s);

        /**
         * 源码分析:
         *
         * public static String toString(Object[] a) {
         *          为空直接返回null
         *         if (a == null)
         *             return "null";
         *          获取数组索引的最大值
         *         int iMax = a.length - 1;
         *           如果数组索引的最大值为-1,此数组为空数组,直接返回[]
         *         if (iMax == -1)
         *             return "[]";
         *            线程不安全,但是效率较高的字符序列
         *         StringBuilder b = new StringBuilder();
         *         b.append('[');
         *         没有判断条件,不进行判断,默认为true,循环的结束条件在循环内控制
         *         for (int i = 0; ; i++) {
         *             字符序列追加->将Object类型转为String
         *             b.append(String.valueOf(a[i]));
         *             如果i等于了数组的最大索引则追加结束符号],return返回,结束程序。
         *             if (i == iMax)
         *                 return b.append(']').toString();
         *             如果i还没有等于数组的最大索引,则追加","
         *             b.append(", ");
         *         }
         *     }
         *
         *     程序输出效果为:
         *          [1,2,3,4]
         */
    }
}

2、对象的拷贝

方式一:clone方法
方式二:序列化和反序列化

package test.javase.object;

/**
 * @author zhangxuhui
 * @email zxh_1633@163.com
 * @create 2020-04-02 15:35
 */
public class Car implements Cloneable {

    private String name;
    private String price;

    public Car() {
    }

    public Car(String name, String price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price='" + price + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
package test.javase.object;

/**
 * @author zhangxuhui
 * @email zxh_1633@163.com
 * @create 2020-04-02 15:32
 */
public class Person implements Cloneable{

    private String name;
    private String age;
    private Car car;

    public Person() {
    }

    public Person(String name, String age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }


    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", car=" + car +
                '}';
    }

    /**
     * 重写克隆方法
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Person clone() throws CloneNotSupportedException {

        Person p= (Person)super.clone();

        p.setCar((Car) p.getCar().clone());

        return p;
    }
}
package test.javase.object;

/**
 * @author zhangxuhui
 * @email zxh_1633@163.com
 * @create 2020-04-02 14:34
 */
public class ObjectTest {
    public static void main(String [] args) throws CloneNotSupportedException{
        //Object o = new Object();
        //System.out.println(o);
        //System.out.println(o.hashCode());
        Car car = new Car("长城H6", "15万");
        Person p = new Person("bob", "18",car);

        System.out.println(p);

        Person p1 = p.clone();

        System.out.println(p1);

        /**
         *  克隆对象与原始对象的引用对象是同一对象为浅拷贝
         *  此时,person的克隆方法,没有克隆其内引用的对象。
         *  true
         */
        System.out.println(p1.getCar() == p.getCar());

        /**
         * 当被引用的对象也克隆后,那么克隆对象与原始对象中的引用对象
         * 为不用的对象。
         * Person p= (Person)super.clone();
         * p.setCar((Car) p.getCar().clone());
         */
    }
}

 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值