dalvik java_2.1 比较Android上的Dalvik Java和Java SE

虽然远在Android出现之前,开发者就能用Java编程语言为移动设备编写应用程序,但它只是Java中功能极为有限的一个版本,称为Java ME(微型版)。不同的移动设备还需编写不同的代码,因此,写一个应用程序就能在支持Java ME的任何手机上运行是几乎不可能的。此外,由于当时不存在很好的在线商店,应用发布过程极其复杂。

Android的问世为开发者提供了构建智能手机强大应用的机会,开发者只需用Java编程语言以及他们熟知的标准Java API编写代码。然而,尽管Android开发者仍使用Java SE编译器来编译应用程序,你会发现,James Gosling开发的Java和Android设备上的Java存在许多不同之处。

在Android设备上运行的VM(虚拟机)称为Dalvik。它最初由谷歌的Dan Bornstein开发,适用于CPU和内存受限的移动设备。Java SE和Dalvik Java存在一些差异,主要体现在虚拟机上。Java SE使用了栈机设计,而Dalvik被设计成了基于寄存器的机器。Android SDK中有一个dx工具,它会把Java SE栈机器的字节码转换成基于寄存器的Dalvik机器字节码,该转换步骤由IDE自动完成。

基于栈的虚拟机和基于寄存器的虚拟机的定义以及差异将不列入本书的讨论范围。由于历史原因,Android使用基于寄存器的虚拟机。虽然基于寄存器的虚拟机最多可以比基于栈的虚拟机快32%,但这只限于执行时解释字节码的虚拟机(也就是说,解释型虚拟机)。在Android 2.2版本(也称为Froyo)之前,Dalvik虚拟机都是纯解释型的。Froyo版本引入了JIT编译器(即时编译),这是Java SE很早就有的一个优势。

JIT编译,也称为动态翻译。它在执行前把字节码翻译成本机代码(如图 2-1所示),这样主要有两个好处。首先,它消除了那些纯解释型虚拟机的开销;其次,它能对本机代码执行优化,这通常是静态编译代码无法做到的。例如,JIT编译器可以在它运行的CPU上选择最合适的优化,也可以根据应用程序的输入来分析代码是如何运行的,以便进行下一步的优化。

c4c2ef1bcfefe2ee17c019a5318fab05.png

图2-1 Android Java和Java SE翻译步骤

虽然Android的Dalvik JIT编译器有很大的发展前景,但要达到如Java SE的JIT编译器般稳定、成熟度尚需很长一段时间。不过,Dalvik JIT的出现为Android提供了巨大的性能优势,而且它也在不断得以改善。

JAVA SE虚拟机和Dalvik虚拟机的另一个区别是,后者进行了优化,可运行在同一个机器上的多个实例中。它在开机时会启动一个叫做zygote的进程,该进程会创建第一个Dalvik实例,由这个实例创建所有其他的实例。当应用程序启动时,zygote进程会收到一个创建新虚拟机实例的请求,并给该应用程序创建一个新进程(如图2-2所示)。如果开发者已习惯于Java SE开发,这样的设计可能看起来不切实际,但它有一个很大的优势,可以避免由一个应用程序运行失败导致Dalvik虚拟机崩溃,继而引发多应用程序崩溃。

fee99338b80d0e59e31206b123e5ca4b.png

图2-2 在Android中启动新Dalvik虚拟机实例

Android和Java SE除了运行的虚拟机不同之外,它们实现API的方式也不一样。Android中属于java和javax包中的API都来自Apache Harmony(这是一个开源项目,旨在重新实现Java SE软件栈,该项目从2011年11月不再维护)。在开发方面,这些API和Java SE包中的类似,但也存在一些差别。例如,谷歌对HttpUrlConnection类进行了Java SE版本中所没有的重大升级。

此外,Android平台移除了Java SE中无关的API。例如,Swing/AWT包被完全移除,因为Android使用不同的UI框架。其他被移除的API还有RMI、CORBA、ImageIO和JMX。它们或者被替换为特定的Android版本(在android包空间内),或者因为一些实际原因根本不存在。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android虚拟机Dalvik完整源码,宝贵资源,欢迎下载! This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments --------------------------------------------- Much of the code under this directory originally came from the Apache Harmony project, and as such contains the standard Apache header comment. Some of the code was written originally for the Android project, and as such contains the standard Android header comment. Some files contain code from both projects. In these cases, the header comment is a combination of the other two, and the portions of the code from Harmony are identified as indicated in the comment. Here is the combined header comment: /* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ---------- * * Portions of the code surrounded by "// BEGIN Harmony code" and * "// END Harmony code" are copyrighted and licensed separately, as * follows: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Native SH call bridge --------------------- Native SH call bridge is written by Shin-ichiro KAWASAKI and Contributed to Android by Hitachi, Ltd. and Renesas Solutions Corp.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值