Jump to content

Comparison of Java and Android API

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gracefool (talk | contribs) at 23:29, 2 October 2016 (Class library: java.beans package). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This article compares the Java and Android API and virtual machines.

While most Android applications are written in Java-like language, there are some differences between the Java API and the Android API, and Android does not run Java bytecode by a traditional Java Virtual Machine but Dalvik virtual machine is used in older versions of Android, but Android Runtime (ART), is used in recent versions, that compiles the same code that Dalvik runs to ELF executables containing machine code.

Java bytecode in JAR-files is not executed by Android devices. Instead Java classes are compiled into a proprietary bytecode format and run on Dalvik (or compiled version of with newer ART), a specialized virtual machine (VM) designed specifically for Android. Unlike Java VMs, which are stack machines, the Dalvik VM is a register-based architecture.

Dalvik has some specific characteristics that differentiate it from other standard VMs:[1]

  • The VM was designed to use less space.
  • The constant pool has been modified to use only 32-bit indexes to simplify the interpreter.
  • Standard Java bytecode executes 8-bit stack instructions. Local variables must be copied to or from the operand stack by separate instructions. Dalvik instead uses its own 16-bit instruction set that works directly on local variables. The local variable is commonly picked by a 4-bit "virtual register" field.

Because the bytecode loaded by the Dalvik virtual machine is not Java bytecode and due to the specific way Dalvik loads classes, it is not possible to load libraries packages as JJAR-files. A different procedure must be used to load Android libraries, in which the content of the underlying dex file must be copied in the application private internal storage area before it is loaded.[2]

System properties

As is the case for the Java SE class System, the Android System class allows the retrieval of system properties. However, some mandatory properties defined with the Java Virtual Machine have no meaning or a different meaning on Android. For example:

  • "java.version" property returns 0 because it is not used on Android,
  • "java.specification.version" invariably returns 0.9 independently of the version of Android used,
  • "java.class.version" invariably returns 50 independently of the version of Android used,
  • "user.dir" has a different meaning on Android,
  • "user.home" and "user.name" properties do not exist on Android

Class library

Current versions of Android use the latest Java language and its libraries (but not full GUI frameworks), not the Apache Harmony Java implementation, that older versions used. Java 8 source code that works in latest version of Android, can be made to work in older versions of Android.

java.lang package

By default, the default output stream System.out and System.err do not output anything,[3] and developers are encouraged to use the Log class, which logs Strings on the LogCat tool.[4] (this has changed at least from HoneyComb, and they now output to the log console as well.)

Graphics and Widget library

Android does not use the Abstract Window Toolkit nor the Swing library. User Interface is built using View objects. Android uses a framework similar to Swing-based around Views rather than JComponents. However, Android widgets are not JavaBeans: the Android application Context must be provided to the widget at creation.

Look and feel

Android widget library does not support a pluggable look and feel architecture; The Look and Feel of Android widgets must be embedded in the widgets themselves. There is, however, a limited capability to set styles and themes for an application.[5]

Layout manager

Contrary to Swing where layout managers can be applied to any container widget, Android layout behavior is encoded in the containers.[6]


java.beans package

Android includes only a small subset of the java.beans package (PropertyChangeEvent and related classes).

See also

References

  1. ^ Rose, John (2008-05-31). "with Android and Dalvik at Google I/O". Retrieved 2008-06-08.
  2. ^ Fred Chung (2011-07-28). "Custom Class Loading in Dalvik". Google. Retrieved 2011-11-27.
  3. ^ "Android Debug Bridge". Google. Retrieved 2009-05-31. By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null.
  4. ^ "Reading and Writing Logs". Google. Retrieved 2011-11-27.
  5. ^ "Applying Styles and Themes". Google. Retrieved 2011-09-03.
  6. ^ "Common Layout Objects". Google. Retrieved 2011-09-03.