为什么 Android 使用 Java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3560963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:52:00  来源:igfitidea点击:

Why does Android use Java?

javaandroid

提问by B.Gen.Hyman.O.Neill

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

好吧,这真的应该问谷歌的人,但我只想要其他意见。

Even Android supports Native code applications, the main development tool is 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.

甚至Android也支持Native代码应用,主要的开发工具是Java。但为什么?我的意思是,在移动设备上解释代码是不是太慢了?Google 在介绍 Froyo 时表示,新的 JIT 编译器可以实现 2-5 倍的应用程序速度。这意味着,在本机代码上使用 Java 会慢 2-x 倍。

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

是的,我知道使用托管代码应用程序在系统稳定性方面更安全,因为虚拟机对程序有更好的控制,但这种性能下降仍然很大,我不明白为什么要使用它。

采纳答案by josefx

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

  1. Java 是一种众所周知的语言,开发人员知道它并且不必学习它

  2. 用 Java 比用 C/C++ 代码更难自救,因为它没有指针算法

  3. 它在虚拟机中运行,因此无需为那里的每部手机重新编译它,并且易于保护

  4. 大量的 Java 开发工具(见第 1 点)

  5. 好几部手机已经用上了Java ME,所以Java在业界享有盛誉

  6. 对于大多数应用程序来说,速度差异不是问题;如果是,你应该用低级语言编码

回答by Colin Hebert

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.

首先,它与 windows mobile 或 iPhone 大致相同,.net 框架需要自己的 VM 以及可可。

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.

而且即使性能不是最好的,因为它是对字节码的一种解释,android 将整个 java 社区作为潜在的开发者。更多的应用程序、更多的客户端等。

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

最后,没有性能还不错,这就是为什么即使在较小的设备上也使用 java(请参阅 JavaMe)。

回答by keyboardsurfer

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.

新的 JIT 运行应用程序的速度比旧的 dalvikVM(均为 JAVA)快 2-5 倍。所以比较的不是 C 对 JAVA,而是 JIT 对 dalvikVM。

回答by David Thornley

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

在字节码级别,Android 不使用 Java。源代码是 Java,但它不使用 JVM。

回答by Pablo Santa Cruz

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.

谷歌在 Android 中使用 Java 有一个非常有说服力的论据:它拥有庞大的开发人员基础。所有这些开发人员都(有点)准备为他们的移动平台进行开发。

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

请记住,从技术上讲,Android 不使用Java。

回答by PeterAllenWebb

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

对于手机这样的设备来说,系统稳定性的提升是非常重要的

Securityis 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.

安全更重要。Android 环境允许用户运行半信任的应用程序,这些应用程序可能以真正令人不快的方式利用手机,而没有出色的安全性。通过在虚拟机中运行所有应用程序,您可以保证没有应用程序可以利用操作系统内核,除非 VM 实现中存在缺陷。反过来,VM 实现可能很小,并且具有很小的、定义明确的安全表面。

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...

此外,使用 Java 可以降低人们编写的应用程序本身被利用的可能性。没有缓冲区溢出,指针错误等......

回答by erickson

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.

首先,根据谷歌的说法,Android 不使用 Java。这就是甲骨文起诉谷歌的原因。甲骨文声称 Android 侵犯了一些 Java 技术,但谷歌称它是 Dalvik。

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

其次,我从 1995 年以来就没有见过 Java 字节码解释器。

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.

你能用一些实际的基准测试来支持你的性能猜想吗?鉴于您提供的背景信息不准确,您的假设范围似乎不合理。

回答by Cheryl Simon

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

本机代码不一定比 Java 代码快。您的个人资料数据在哪里显示本机代码可以运行得更快?

Why Java?

为什么是 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.

  • Android 在许多不同的硬件平台上运行。您需要为这些不同平台中的每一个编译和优化您的本机代码,才能看到任何真正的好处。

  • 已经有大量的开发人员精通Java。

  • Java 有大量的开源支持,有许多库和工具可以让开发人员的生活更轻松。

  • Java 可以保护您免受本机代码中固有的许多问题的影响,例如内存泄漏、错误的指针使用等。

  • Java 允许他们创建沙箱应用程序,并创建一个更好的安全模型,这样一个坏应用程序就不能破坏你的整个操作系统。

回答by JulesLt

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.

正如其他地方提到的,主要问题是 Android 被设计为便携式操作系统,可以在各种硬件上运行。它还建立在许多现有移动开发人员熟悉的框架和语言之上。

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.

最后,我想说这是对未来的赌注——无论存在的任何性能问题都将随着硬件的改进而变得无关紧要——同样,通过让开发人员针对抽象进行编码,谷歌可以更容易地删除和更改底层操作系统,而不是如果开发人员正在编写 POSIX/Unix API。

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.

对于大多数应用程序,使用基于 VM 的语言而不是本机语言的开销并不大(使用 Web 服务的应用程序的瓶颈,如 Twitter,主要是网络)。Palm WebOS 也展示了这一点——它使用 JavaScript 而不是 Java 作为主要语言。

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).

鉴于几乎所有 VM 都 JIT 编译为本地代码,原始代码速度通常与本地速度相当。与其他因素(复杂的对象运行时、通过边界检查来检查内存访问的“安全性”等)相比,高级语言造成的许多延迟与 VM 开销的关系不大。

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.

还要记住,无论用于编写应用程序的语言是什么,很多实际工作都是在较低级别的 API 中完成的。顶级语言通常只是将 API 调用链接在一起。

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.

当然,这条规则有很多例外——突破手机硬件极限的游戏、音频和图形应用程序。即使在 iOS 上,开发人员也经常使用 C/C++ 来加快这些领域的速度。