简体   繁体   English

为什么同一段(简单的)Java代码在不同的Android设备上的行为会大不相同?

[英]Why same piece of (simple) Java code behaves very differently on different Android devices?

Why same piece of (simple) Java code behaves very differently on different Android devices? 为什么同一段(简单的)Java代码在不同的Android设备上的行为会大不相同?

That simple piece of code is just the use of String.replace(CharSequence target, CharSequence replacement) with target == "" : 这段简单的代码只是将String.replace(CharSequence target, CharSequence replacement)target == ""

package com.example.stringreplacetest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String str = "just_a_string";

        System.out.println(str.replace("", "-"));
        ((TextView) findViewById(R.id.textView)).setText(str.replace("", "-"));
    }
}

It produces -just-_-a-_-string- on my LG Optimus 3D P920 (Android 2.3.3), and my sister's Samsung Galaxy S2 (Android 4.1.2) , and i guess on most of your devices as well. 它在我的LG Optimus 3D P920(Android 2.3.3)和我姐姐的Samsung Galaxy S2(Android 4.1.2)上产生-just-_-a-_-string- ,而且我猜在您的大多数设备上也是如此。

But it halts (a suspect of infinite looping) on my LG Optimus Chic (Android 2.2). 但这会使我的LG Optimus Chic(Android 2.2)停止运行(可能会产生无限循环)。

The old LG Optimus Chic and Android 2.2 may be buggy. 旧的LG Optimus Chic和Android 2.2可能有故障。 ( String.replace() indeed has a bug .) But the piece of code in String.replace() is relatively simple - "simple" means no dynamic binding, no Threads, etc... String.replace()确实有一个错误 。)但是String.replace() 的代码段相对简单-“简单”表示没有动态绑定,没有线程等。

Isn't that piece of code should be finalised during compile time? 那段代码不是应该在编译时完成的吗? How does Java compiler work (as i know Java is a cross-platform language, it may work differently)? Java编译器如何工作(据我所知Java是一种跨平台语言,它的工作方式可能有所不同)?

PS to ensure it is the same piece of compiled code, i actually transferred the compiled .apk by USB to my Android phones, rather than using Eclipse to RUN them directly in the devices. PS为确保它是同一段编译代码,我实际上是通过USB将已编译的.apk传输到我的Android手机上,而不是使用Eclipse在设备中直接运行它们。


i have found the source code of Android 2.2 Froyo: 我找到了Android 2.2 Froyo的源代码:

https://android.googlesource.com/platform/dalvik/+/froyo-release/libcore/luni/src/main/java/java/lang/String.java https://android.googlesource.com/platform/dalvik/+/froyo-release/libcore/luni/src/main/java/java/lang/String.java

It does cause infinite looping when target.length == 0 (because in the do-while loop , string.indexOf("", tail) will never return -1 ). target.length == 0时,它的确会引起无限循环(因为在do-while loopstring.indexOf("", tail) 永远不会返回-1 )。

Doubts has been cleared a bit. 疑虑已经清除了一点。 But... 但...

i still don't know why different versions of String class is loaded, upon running in different devices. 我仍然不知道为什么在不同的设备上运行时会加载不同版本的String类。 Is it this is what meant to be cross-platform ? 这是跨平台的意思吗?

Let me close this question. 让我结束这个问题。

i could not find concrete documentary references. 我找不到具体的文献参考。 But after my repeated trials, and a few researches of the "differences between Java vs C compiler". 但是经过反复的尝试,以及对“ Java vs C编译器之间的差异”的一些研究。 Yes, this is the Java behaviour - compile once, run everywhere (& debug everywhere). 是的,这是Java的行为- 编译一次,到处运行 (和到处调试)。

This is why we need the Java VM. 这就是为什么我们需要Java VM。 This is why Java compiles faster than C/C++. 这就是Java编译速度比C / C ++快的原因。 This is why Java runs slower than C/C++. 这就是Java运行速度比C / C ++慢的原因。

(i guess) While Java compiles, it only records the class signatures. (我猜)在Java编译时,它仅记录类签名。 Upon running on VM, it matches those signatures with the real implementations of the corresponding classes, compile them into machine codes just-in-time. 在VM上运行时,它将这些签名与相应类的实际实现进行匹配,并及时将它们编译为机器代码。 That is why Java can compile once, run on different machines , because different VMs have their own implementations of the classes. 这就是Java可以一次编译并在不同机器上运行的原因,因为不同的VM具有自己的类实现。 This also lead to the problem that, if there is a bug in a certain version, the programmer can do nothing about it. 这也导致了一个问题,如果某个版本中存在错误,程序员将对此无能为力 It is because the actual buggy implementation is at the client side where the end-user is running the program. 这是因为实际的bug实施是在最终用户运行程序的客户端。 We need to wait the user to update their own VM. 我们需要等待用户更新自己的VM。

PS in fact we can re-invent the wheel, re-write our own classes. PS实际上,我们可以重新发明轮子,重新编写我们自己的类。 So those classes will be attached into the deployed program. 因此,这些类将被附加到已部署的程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相同代码在不同设备上的行为不同? - Same code acting differently on different devices? 为什么Kotlin在非常简单的代码段中引发异常? - Why Kotlin throws an exception in a very simple piece of code? 代码在Android和Windows中的行为有所不同 - Code behaves differently in Android and Windows 使用Camera实例捕获的图像的方向在不同的Android设备上表现不同 - Orientation of Image captured using Camera instance behaves differently for different Android devices 为什么android代码中的这一段java这么慢? - Why is this piece of java in android code so slow? 代码在两种不同的网络类型上表现不同 - Code behaves differently on two different network type 同一代码在不同的android活动中的功能不同 - Same code is functioning differently in different android activities Android UI的行为与不同的显示硬件不同 - Android UI behaves differently with different display hardware Android createbitmap宽度在不同设备上表现不同 - Android createbitmap width behaves different on different devices Flutter_TTS package 在不同设备上表现不同 - Flutter_TTS package behaves differently on different devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM