String对象为什么是不可变的

    科技2024-07-27  9

    String对象为什么是不可变的

    String内部成员

    /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0

    从String内部成员来看, String底层是基于byte数组实现的, 修饰符为 private final类型,外部不可以直接访问, 并且String类中并没有对外提供对数组内部数值进行修改的方法,因此对String的每次赋值都会重新生成一个新的String对象

    Processed: 0.016, SQL: 8