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)
|
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.
|
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.
|
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.
|
Another thing to keep in mind is that Android Java is a nonstandard collection of Java
classes. Java typically ships in:
|
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
|
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 fea-
tures 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.
|
No comments:
Post a Comment