引用如何在Java中工作

raj*_*aju 5 java serialization reference

我正在读一本有效Java的书,它有以下例子.在下面的示例中,作者通过以下行复制ObjectOutputStream中存在的对象的引用

byte[] ref = {0x71, 0, 0x7e, 0, 5}; // Ref #5 
Run Code Online (Sandbox Code Playgroud)

为什么这个引用指向ObjectOutputStream中存在的日期对象?什么存储在参考?

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

final class Period {
    private final Date start;
    private final Date end;

    /**
     * @param start the beginning of the period
     * @param end the end of the period; must not precede start * @throws IllegalArgumentException
     *        if start is after end
     * @throws NullPointerException if start or end is null
     */
    public Period(Date start, Date end) {
        this.start = new Date(start.getTime());
        this.end = new Date(end.getTime());
        if (this.start.compareTo(this.end) > 0)
            throw new IllegalArgumentException(start + " after " + end);
    }

    public Date start() {
        return new Date(start.getTime());
    }

    public Date end() {
        return new Date(end.getTime());
    }

    public String toString() {
        return start + " - " + end;
    }
    // Remainder omitted
}


public class MutablePeriod {
    // A period instance
    public final Period period;
    // period's start field, to which we shouldn't have access
    public final Date start;
    // period's end field, to which we shouldn't have access
    public final Date end;

    public MutablePeriod() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bos);

            // Serialize a valid Period instance
            out.writeObject(new Period(new Date(), new Date()));
            /*
             * Append rogue "previous object refs" for internal * Date fields in Period. For
             * details, see "Java Object Serialization Specification," Section 6.4.
             */
            byte[] ref = {0x71, 0, 0x7e, 0, 5}; // Ref #5 
            bos.write(ref); // The start field
            ref[4] = 4; // Ref#4
            bos.write(ref); // The end field
            // Deserialize Period and "stolen" Date references
            ObjectInputStream in =
                    new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            period = (Period) in.readObject();
            start = (Date) in.readObject();
            end = (Date) in.readObject();

        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Her*_*eri 2

这两个流式“ref”与java中的引用无关。(顺便说一句:字节数组有 5 个字节,而不是您在评论中提到的只有 4 个字节)。这十个字节是序列化输出中的一些内部魔法。关于此流格式的确切细节在第 1 章中进行了描述。“Java 对象序列化规范”的 6.4(即使在底部的示例中也会出现值“71 00 7e 00”)。

java 程序中的引用包含 4 或 8 个字节,具体取决于平台(32 位与 64 位)。它是指向对象实例数据开始的(java 管理的)内存的指针。但值得庆幸的是,您永远不必在 java 中处理这个值。我认为你甚至无法访问它。