Java Immutability
An immutable data type is a data type whose instances cannot change in any observable way after instantiation. 比如String是immutable, Array是mutable. Immutable 的类型: String, Integer, Double, Color, Vector, Transaction, Point2D. Mutable: StringBuilder, Stack, Counter, Java array. public final class Vector { private final int N; private final double[] data; public Vector(double[] data) { this.N = data.length; this.data = new double[N]; for (int i = 0; i < N; i++) { // defensive copy of mutable instance variables this.data[i] = data[i]; } } ... } 如何防止变量在第一次赋值后被更改: ...