java - 从接口(interface)中省略公共(public)抽象会损害字节码兼容性吗?

标签 java interface compatibility backwards-compatibility

在浏览 SO 问题时,我遇到了 Runnable 的定义:

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

如您所见,方法定义中有 public abstract,这是多余的,据我所知,不应包含在方法声明中。

我希望 JDK 团队看到这一点,并希望他们还在这里是有原因的。

因此问题来了,从接口(interface)声明中删除 public abstract 会破坏字节码兼容性吗?请记住,即使使用 JDK1.0 java 代码编写的代码,这个 Runnable 在技术上仍然必须工作。

最佳答案

我找不到更好的来源,但是 here's the JLS for Java 1. (也许 here 。)

与此后的所有其他 JLS 一样,它指出。

AbstractMethodDeclaration:

    AbstractMethodModifiers opt ResultType MethodDeclarator Throwsopt ;

AbstractMethodModifiers:

    AbstractMethodModifier

    AbstractMethodModifiers AbstractMethodModifier

AbstractMethodModifier: one of

    public abstract

注意 opt。它还指出

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block. For compatibility with older versions of Java, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.

Every method declaration in the body of an interface is implicitly public. It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.

它不会损害字节码兼容性。

他们可能只是想尽可能明确。

关于java - 从接口(interface)中省略公共(public)抽象会损害字节码兼容性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961756/

相关文章:

c++ - Matlab 2017a 无法识别编译器(Error using mex No supported compiler or SDK was found)

java - 为什么我的二进制数据从网络服务器获取后会变大?

java - Java nanoTime 的多线程

java - 正则表达式和带有元字符的匹配 URL

c# - 使类依赖项出现在界面中而不是属性

c# - C# 中的继承与接口(interface)

php - 在这种情况下是使用接口(interface)还是抽象类更好?

c - fgets() linux 与 mac

web - 我可以使用哪种键盘快捷键而不与现代浏览器发生冲突?

java - 循环绘制字符串未出现在 JFrame 中