Android和Java,AndroidJava
Android和Java,AndroidJava
译注:节选自O'REILLY出版社的<Learning Android>一书第二章一个小节---简单介绍了在编译及类库方面,Android与Java的区别。
In Java, you write your Java source file, compile it into a Java byte code using the Java compiler, and then run this byte code on the Java VM. In Android, things are different. You still write the Java source file, and you still compile it to Java byte code using the same Java compiler. But at that point, you recompile it once again using the Dalvik compiler to Dalvik byte code. It is this Dalvik byte code that is then executed on the Dalvik VM. Figure 2-2 illustrates this comparison between standard Java (on the left) in Android using Dalvik (on the right).
在Java中,你写好Java源文件,用Java编译器将其编译成Java字节代码,然后在Java虚拟机中运行这些字节码。在Android中,情况就有些不同。您编写Java源文件,仍就使用相同的Java编译器将其编译为Java字节代码。但在这之后,你还得再一次使用的Dalvik编译器重新编译成为Dalvik字节码。正是这些Dalvik字节代码,在Dalvik虚拟机上运行。图2-2展示了在标准Java中(左)与在Android中使用Dalvik(右)编译过程的区别。
Figure 2-2. Java versus Dalvik
图2-2. Java与Dalvik
It might sound like you have to do a lot more work with Android when it comes to Java. However, all these compilation steps are automated by tools such as Eclipse or Ant, and you never notice the additional steps.
看起来你在Android编程工作中会比在Java中做更多的工作。然而,这些编译工作都会由工具软件自动完成,比如说Eclipse和Ant,你永远不会注意到这些额外的步骤。
You may wonder, why not compile straight from Java into the Dalvik byte code? There are a couple of good reasons for the extra steps. Back in 2005, when work on Dalvik started, the Java language was going through frequent changes, but the Java byte code was more or less set in stone. So, the Android team chose to base Dalvik on Java byte code instead of Java source code.
你可能会奇怪,为什么不直接从Java源文件编译成Dalvik字节码?有一堆原因造成这一额外的步骤。早在2005年,当Dalvik创建之初,Java语言正在经历频繁的变化,但是Java字节码相对来说是“坚如磐石”。因此,Android团队选择相应的Java字节代码而不是Java源代码用于Dalvik。
A side effect of this is that in theory you could write Android applications in any other language that compiles down to Java byte code. For example, you could use Python or Ruby. I say “in theory” because in practice the appropriate libraries that are part of the SDK would need to be available. But it is likely that the open source community will come up with a solution to that in the future.
由此产生的“副作用”是,在理论上你可以用任何其他能够编译成Java字节码的语言来编写Android应用。例如,你可以使用Python或Ruby。我说“在理论上“,因为在实践中需要作为SDK组成部分的相应库文件支持。但开源社区很有可能在将来想出了一个解决方案。
Another thing to keep in mind is that Android Java is a nonstandard collection of Java classes. Java typically ships in:
另外一个值得注意的事情是,Android中的Java类并非一个标准的Java类集合。Java通常按以下形式发布:
Java Standard Edition
Used for development on basic desktop-type applications
Java Enterprise Edition (aka J2EE or JavaEE)
Used for development of enterprise applications
Java Micro Edition (aka J2ME or JavaME)
Java for mobile applications
Java标准版本
用于普通桌面应用的开发
Java企业版本
用于企业应用的开发
Java微版本
用于移动应用开发
Android’s Java set of libraries is closest to Java Standard Edition. The major difference is that Java user interface libraries (AWT and Swing) have been taken out and replaced with Android-specific user interface libraries. Android also adds quite a few new features to standard Java while supporting most of Java’s standard features. So, you have most of your favorite Java libraries at your disposal, plus many new ones.
Android的Java库更接近于Java标准版本。最主要的区别是Java用户界面类库(AWT和Swing)被摒弃了,取而代之的是Android特定的用户界面类库。Android还在标准Java类库上增添了不少新属性,能够支持绝大多数的Java标准属性。于是,你就可以任意使用绝大多数的Java类库,同时还会有一些新的可用。
相关文章
- 暂无相关文章
用户点评