Java 中的二进制兼容性是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14973380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:02:58  来源:igfitidea点击:

What is binary compatibility in Java?

java

提问by Sam

I was reading Effective Javaby Joshua Bloch.

我正在阅读Joshua Bloch 的Effective Java

In Item 17: "Use interfaces only to define types", I came across the explanation where it is not advised to use Interfaces for storing constants. I am putting the explanation below.

在第 17 条:“仅使用接口来定义类型”中,我遇到了不建议使用接口来存储常量的解释。我把解释放在下面。

"Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility."

“更糟糕的是,它代表了一种承诺:如果在未来的版本中修改了类,使其不再需要使用常量,它仍然必须实现接口以确保二进制兼容性。”

What does binary compatibility mean here?

二进制兼容性在这里意味着什么?

Can someone guide me with an example in Java to show that code is binary compatible.

有人可以用 Java 中的示例指导我说明代码是二进制兼容的。

采纳答案by Evgeniy Dorofeev

In short, binary compatibility means that when you change your class, you do not need to recompile classes that use it. For example, you removed or renamed a public or protected method from this class

简而言之,二进制兼容性意味着当你改变你的类时,你不需要重新编译使用它的类。例如,您从此类中删除或重命名了公共或受保护的方法

public class Logger implements Constants {
   public Logger getLogger(String name) {
         return LogManager.getLogger(name);
   }
}

from your log-1.jar library and released a new version as log-2.jar. When users of your log-1.jar download the new version it will break their apps when they will try to use the missing getLogger(String name) method.

来自您的 log-1.jar 库并发布了一个新版本作为 log-2.jar。当您的 log-1.jar 的用户下载新版本时,当他们尝试使用缺少的 getLogger(String name) 方法时,会破坏他们的应用程序。

And if you remove Constants interface (Item 17) this will break binary compatibility either, due to the same reason.

如果您删除 Constants 接口(第 17 项),由于相同的原因,这也会破坏二进制兼容性。

But you can remove / rename a private or package private member of this class without breaking the binary compatibility, because external apps cannot (or should not) use it.

但是您可以在不破坏二进制兼容性的情况下删除/重命名此类的私有或包私有成员,因为外部应用程序不能(或不应)使用它。

回答by Sumit Singh

Binary compatibility

二进制兼容性

Java binary compatibility prescribes conditions under which modication and re-compilation of classes does not necessitate re-compilation of further classes import- ing the modied classes. Binary compatibility is a novel concept for language design.

Java 二进制兼容性规定了修改和重新编译类不需要重新编译导入修改后的类的其他类的条件。二进制兼容性是语言设计的一个新概念。

The Java language specication [7]describes binary com- patible changes as follows:

Java语言specication [7]描述了如下二进制相兼容的变化:

A change to a type is binary compatible with (equivalently, does not break compatibility with) pre-existing binaries if pre-existing binaries that previously linked without error will con- tinue to link without error.

如果先前无错误链接的预先存在的二进制文件将继续无错误链接,则对类型的更改与(等效地,不会破坏与)预先存在的二进制文件的二进制兼容。

回答by BIPIN SHARMA

If in future, we wish to change the interface that some classes are implementing (e.g., addition of some new methods).

如果将来我们希望更改某些类正在实现的接口(例如,添加一些新方法)。

If we add abstract methods(additional methods), then the classes (implementing the interface) must implementsthe additional method creating dependency constraint and cost overhead to perform the same.

如果我们添加抽象方法(附加方法),那么类(实现接口)必须实现附加方法,创建依赖约束和成本开销来执行相同的操作。

To overcome this, we can add defaultmethods in the interface.

为了克服这个问题,我们可以在接口中添加默认方法。

This will remove the dependency to implement the additional methods.

这将消除实现附加方法的依赖性。

We do not need to modify the implementing class to incorporate changes. This is called as Binary Compatibility.

我们不需要修改实现类来合并更改。这称为二进制兼容性。

Please refer the example below:

请参考以下示例:

The interface that we are going to use

我们将要使用的界面

    //Interface       
    interface SampleInterface
            {
                // abstract method
                public void abstractMethod(int side);

                // default method
                default void defaultMethod() {
                   System.out.println("Default Method Block");
                }

                // static method
                static void staticMethod() {
                    System.out.println("Static Method Block");
                }
            }


//The Class that implements the above interface.

    class SampleClass implements SampleInterface
    {
        /* implementation of abstractMethod abstract method, if not implemented 
        will throw compiler error. */
        public void abstractMethod(int side)
        {System.out.println(side*side);}

        public static void main(String args[])
        {
            SampleClass sc = new SampleClass();
            sc.abstractMethod(4);

            // default method executed
            sc.defaultMethod();

            // Static method executed
            SampleInterface.staticMethod();

        }
    }

Note:For more detailed information, please refer default methods

注意:更多详细信息,请参考默认方法

回答by LGAP

To make things look simple:

为了让事情看起来简单:

A computer that can run the same binary code intended to be run on another computer is said to be binary-compatible. This differs from source code compatibility, where recompilation may be necessary.

可以运行旨在在另一台计算机上运行的相同二进制代码的计算机被称为二进制兼容。这与源代码兼容性不同,后者可能需要重新编译。

Binary-compatibility is a major benefit when developing computer programs that are to be run on multiple operating systems.

在开发要在多个操作系统上运行的计算机程序时,二进制兼容性是一个主要优势。