122

OK, this should really be asked to someone from Google, but I just want other opinions.

Even though Android supports Native code applications, the main development tool is still Java. But why? I mean, isn't it too slow to interpret code on a mobile device? When introducing Froyo, Google said that new JIT compiler can achieve 2-5 times faster applications. This means, that using Java over native code is 2-x times slower.

Yes, I know that using managed code applications is safer in terms of system stability, since virtual machines have better control over program execution, but still, this performance drop is huge, and I don't see any point why to use it.

12
  • 12
    Java code is not interpreted, at least not on Android -- it's compiled and ran on a virtual machine. Aug 24, 2010 at 21:00
  • 4
    I thought Sun proved Java can be (in a few areas, but quite often nearly) as fast as native code? Plus, the google guys are a smart pack - I'm confident that the JIT they recently introduced will sooner or later produce very good code.
    – user395760
    Aug 24, 2010 at 21:15
  • 1
    @b-gen-jack-o-neill The answer is actually no, because the VM can tell what code is being executed at runtime, and how it is being executed. For example, Apple uses LLVM in OS X for the explicit purpose of optimizing performance-critical graphics functions at runtime. This is done specifically because it is faster than native-code techniques. Aug 24, 2010 at 21:33
  • 1
    @b-gen-jack-o-neill, Java bytecode can be compiled to native code at runtime. Aug 24, 2010 at 21:36
  • 1
    Please, is there any article about this? I just cant understand the idea that something could actually be faster than direct machine code. Ok, maybe this optimalization on OS X, but this could be achived by well written graphic API too, isnt it? I mean, is there really any difference wheather virtual machine therefore another program optimizes code, or that system graphics library has this code already integrated? Aug 24, 2010 at 21:39

9 Answers 9

103

Some points:

  1. Java is a known language, developers know it and don't have to learn it

  2. it's harder to shoot yourself with Java than with C/C++ code since it has no pointer arithmetic

  3. it runs in a VM, so no need to recompile it for every phone out there and easy to secure

  4. large number of development tools for Java (see point 1)

  5. several mobile phones already used Java ME, so Java was known in the industry

  6. the speed difference is not an issue for most applications; if it was you should code in low-level language

13
  • 5
    Running on a VM (thus no recompiling) is a huge plus. Also, it easily separates processes from each other, preventing a rogue application from destroying your phone or interfering with other applications
    – Falmarri
    Aug 24, 2010 at 21:20
  • 1
    About the rogue application thing - this sounds interesting. Correct me if I am wrong, but x86 CPUs have biult in protection via paging and ring modes, therefore application cannot change its page in memory therefore cannot interfere with another app other than using OS API. But does this feature have ARM CPUs? I actually have no idea. If not, This would be great + for Java on this platform. Aug 24, 2010 at 21:42
  • The CPU has nothing to do with a malicious application doing nefarious things
    – Falmarri
    Aug 25, 2010 at 7:51
  • 4
    Memory protection is part of some cpu architectures. It prevents a malicious application from accessing memory assigned to a different application. en.wikipedia.org/wiki/Memory_protection
    – josefx
    Aug 25, 2010 at 8:24
  • 2
    @Falmarri: Yes, it does. Basically its very simple. Your app has assigned its own adress space. All adresses you want to access are translated by MMU. You want to access adress 0x0000 and MMU translates it into for example 0x0E21. And to prevent you to change the base adress, its priviledged instruction and your program when started by OS has assigned lowest priviledge level. If not, single CLI (disable interrupts) instruction would crash system.... Aug 25, 2010 at 18:53
42

On the byte-code level, Android doesn't use Java. The source is Java, but it doesn't use a JVM.

4
  • 7
    yes. Java is the source, but its not compiled to java virtual machine compatible byte code. This is why they'll probably most/all of the patent dispute with sun/oracle. They're only using the syntax of the language. Aug 24, 2010 at 21:02
  • 1
    It still has to support most functions of the java vm. So they can't optimize those out.
    – josefx
    Aug 24, 2010 at 21:39
  • 1
    Then why the need to install the JDK when developing in android? Is it just for the emulator?
    – jiggunjer
    Feb 18, 2016 at 1:39
  • @jiggunjer Android Studio is developed in Java, as a matter of fact. And so is the emulator. Feb 4, 2020 at 14:41
26

The improvement to system stability is very important on a device like a cell phone.

Security is even more important. The Android environment lets users run semi-trusted apps which could exploit the phone in truly unpleasant ways without excellent security. By running all apps in a virtual machine, you guarantee that no app can exploit the OS kernel unless there is a flaw in the VM implementation. The VM implementation, in turn, is presumably small and has a small, well-defined security surface.

Perhaps most important, when programs are compiled to code for a virtual machine, they do not have to be recompiled for new hardware. The market for phone chips is diverse and rapidly-changing, so that's a big deal.

Also, using Java makes it less likely that the apps people write will be exploitable themselves. No buffer-overruns, mistakes with pointers, etc...

1
  • Another answer by David says android does not use jvm
    – Gilbert
    May 8, 2020 at 6:22
15

Native code is not necessarily any faster than Java code. Where is your profile data showing that native code could run faster?

Why Java?

  • Android runs on many different hardware platforms. You would need to compile and optimize your native code for each of these different platforms to see any real benefits.

  • There are a large number of developers already proficient in Java.

  • Java has huge open source support, with many libraries and tools available to make developers life easier.

  • Java protects you from many of the problems inherent in native code, like memory leaks, bad pointer usage, etc.

  • Java allows them to create sandbox applications, and create a better security model so that one bad App can't take down your entire OS.

7

First of all, according to Google, Android doesn't use Java. That's why Oracle is suing Google. Oracle claims that Android infringes on some Java technology, but Google says it's Dalvik.

Secondly, I haven't seen a Java byte code interpreter since 1995.

Can you back up your performance conjecture with some actual benchmarks? The scope of your presumptions don't seem justified given the inaccurate background information you provide.

5

Java has a pretty compelling argument for Google using it in Android: it has a huge base of developers. All these developers are (kind of) ready to develop for their mobile platform.

Keep in mind that, technically speaking, Android does not use pure Java.

1
  • 2
    I think that all the people interested in Mobile development are also interested in "cooler" languages than Java.
    – Earlz
    Aug 24, 2010 at 21:16
5

As touched on elsewhere, the main issue is that Android is designed as a portable OS, to run on a wide variety of hardware. It's also building on a framework and language familiar to many existing mobile developers.

Finally, I would say it is a bet against the future - whatever performance issues exist will become irrelevant as hardware improves - equally by getting developers to code against an abstraction, Google can rip-out and change the underlying OS far more easily, than if developers were coding to the POSIX/Unix APIs.

For most applications the overhead of using a VM-based language over native is not significant (the bottleneck for apps consuming web services, like Twitter, is mostly networking). The Palm WebOS also demonstrates this - and that uses JavaScript rather than Java as the main language.

Given that almost all VMs JIT compile down to native code, raw code speed is often comparable with native speed. A lot of delays attributed to higher-level languages are less to do with the VM overhead than other factors (a complex object runtime, 'safety' checking memory access by doing bounds checking, etc).

Also remember that regardless of the language used to write an application, a lot of the actual work is done in lower level APIs. The top level language is often just chaining API calls together.

There are, of course, many exceptions to this rule - games, audio and graphics apps that push the limits of phone hardware. Even on the iOS, developers often drop down to C/C++ to get speed in these areas.

1

The new JIT is running the applications 2 - 5 times faster than the old dalvikVM (both JAVA). So comparison is not C over JAVA, but JIT over dalvikVM.

1

First of all it's about the same thing will windows mobile or the iPhone, the .net framework needs its own VM as well as cocoa.

And even if the performance is not at the best, because it's an interpretation of byte code, android brings the entire java community as potential developers. More applications, more clients, etc.

To finish, no performance is not that bad, that's why java is used even on smaller devices (see JavaMe).

1
  • Cocoa's not VM based - it's all compiled native code - but unlike pure C/C++ it does have a dynamic runtime (similar to Smalltalk/Ruby/Python) - which has it's own performance issues and optimisations. It's notable that most iPhone games are largely C++ rather than Obj-C.
    – JulesLt
    Aug 24, 2010 at 21:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.