英文:
Does serialization in java make it platform independent?
问题
"Serialization in Java is a mechanism of writing the state of an object into a byte-stream and Deserialization is vice versa of this." 所以我在想,是不是序列化使Java平台独立?
英文:
Since "Serialization in Java is a mechanism of writing the state of an object into a byte-stream and Deserialization is vice versa of this". So it got me thinking that is it Serialization that makes Java platform independent?
答案1
得分: 3
不是。
Java平台的独立性来自于在许多不同平台上存在与Java兼容的运行时环境。更具体地说,存在于您选择的平台上能够执行Java字节码的Java运行时环境,这是Java编译器输出的常见中间语言。
序列化/反序列化的作用是提供一种在不同系统之间通信的平台无关方式。您只需要在每个系统上安装能够使用相同通信协议的序列化/反序列化软件。不需要在每一侧都使用Java。
英文:
Nope.
What makes Java platform-independent is the existence of Java-compatible runtimes on many different platforms. More specifically, the existence of Java runtimes for your platform of choice that are capable of executing Java bytecode, the common intermediate language that the Java compiler outputs.
What Serialization/Deserialization does is give you a platform-independent way of communicating between different systems. All you need is serialization/deserialization software on each system that speaks the same communication protocol. Using Java on each side is not required.
答案2
得分: 1
序列化/反序列化涉及数据在您的运行程序中进出的过程。在此时,由于您的程序已经在运行,平台依赖性/独立性已经发生。
平台独立性是由于将Java程序编译为字节码格式而发生的。这个字节码对于所有平台都是相同的,然后由一个平台相关的引擎来执行。平台相关的引擎/运行时在您安装JVM/JRE时安装在您的计算机上。所以Java代码如下:
Java代码 -> 字节码 -> 平台相关运行时
英文:
Serialization/deserialization is about data moving in and out of your running program. At this point, because your program is already running, platform dependence/independence has already happened.
Platform independence happens because of compilation of a Java program to a bytecode format. This bytecode is same for all platforms which is then executed by a platform dependent engine. The platform dependent engine/runtime is installed on your machine when you install the JVM/JRE. So the Java code goes:
Java code -> Bytecode -> platform dependent runtime
答案3
得分: 0
我认为,在某个层面上,为了使Java具备跨平台性,内置的序列化技术(就像它所拥有的那样)是一个必不可少的组成部分。拥有可移植的应用程序是没有意义的,除非它们的数据也是可移植的,这就是标准化序列化所实现的。
平台依赖性
现在我将深入讨论,可能会对真正的平台独立性提出过于苛刻的看法!
我认为Java出问题的地方在于(就像许多其他编程语言一样),序列化是代码优先的;您需要装饰或以其他方式注释类的源代码,以指示它应该如何(或是否应该)被序列化。这没问题,但然后Java语言是我所谓的封闭的;Java应用程序的数据很难或至少不方便地移植到所有其他编程语言。即使有库支持(例如XML库)来导航数据,它们仍然需要开发等效的类。如果您有一堆以这种方式序列化的数据,然后稍后需要在C等其他语言中访问它,某位开发人员将需要大量工作!
此外,如果您想以某种方式更改类,该更改还必须合并到C源代码中。这会导致混淆、重复工作和错误,并且没有一个单一的实现(您的Java,他们的C代码)是关于数据应该是什么的"唯一真正的来源"。这随后产生了对接口控制文档等东西的需求,这变成了难以维护、遵循等问题。
真正的平台独立性?
更灵活的方法是首先定义模式 - 在其中以与语言无关的方式描述数据 - 然后从中生成特定语言的类的源代码。这就是像Google Protocol Buffers、ASN.1(除非使用了使其再次成为代码优先的Python库)、dBus等所做的事情。
真的重要吗?
可能不重要。内置的代码优先序列化的"封闭"性质是否重要,这是一个项目与项目之间的问题。有些可以完全使用Java编写,而有些则不能。使用模式优先序列化的吸引力在于,如果不确定必须能够读取数据的内容,那么如果稍后需要语言/平台互操作性,模式将使其变得容易。而且注意到互操作性经常作为一个需求出现。
总结
因此,我认为Java本身并不完全是平台无关的,因为它鼓励非可移植的数据定义(如果"平台无关"包括其他编程语言的话)。
英文:
I'd say that, at one level, for Java to be platform independent, a built-in serialisation technology (such as it has) is an essential component. There's no point having portable applications unless their data is also portable, which is what a standardised serialisation achieves.
Platform Dependence
I'll now go down a rabit hole of a discussion, where I may be being too pedanctic on what true platform independence really means!
Where things go a bit wrong with Java I think is that (like a lot of other languages) the serialisation is code-first; you decorate or otherwise annotate the source code for a class to indicate how (or if) it should be serialisable. That's fine, but then the Java language is what I call closed; it's difficult or at least inconvenient for a Java application's data to be portable to all other languages. They have to develop equivalent classes, even if there is library support (e.g. an XML library) to navitgate the data. If you have a bunch of data serialised this way, and later it has to be accessible in, say, C, some developer has a lot of work to do!
Also, if you're wanting to change a class in some way, the change has also to be folded into the C source code to. This is a recipe for confusion, duplicate work and error, and no one single implementation (your Java, their C code) is "the one source of truth" as to what the data should be. This then generates the need for things like Interface Control Documents, which become a nightmare to maintain, adhere to, etc.
True Platform Independence?
A more flexible approach is schema-first - where data is described in a language independent way - and language-specific classes' source code generated from it. This is what one does with things like Google Protocol Buffers, ASN.1 (unless one is using the Python libs which have made it code-first again), dBus.
Does it Really Matter?
Possibly not. Whether or not the "closed" nature of built in code-first serialisation matters or not is a project by project matter. Some can be written in all Java, some not. The attraction of using schema-first serialisation is that, if one is uncertain as to what must be able to read data, the schema makes it easy if language / platorm interop become a requirement later on. And it's noticable how often interop does emerge as a requirement.
Summary
So I'd argue that Java alone is not completely platform independent, because it encourages non-portable data definitions, (if by "platform independent" one includes other languages).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论