compiler construction - Why same piece of (simple) Java code behaves very differently on different Android devices? -


why same piece of (simple) java code behaves differently on different android devices?

that simple piece of code use of 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 -j-u-s-t-_-a-_-s-t-r-i-n-g- on lg optimus 3d p920 (android 2.3.3), , sister's samsung galaxy s2 (android 4.1.2), , guess on of devices well.

but halts (a suspect of infinite looping) on lg optimus chic (android 2.2).

the old lg optimus chic , android 2.2 may buggy. (string.replace() indeed has a bug.) the piece of code in string.replace() relatively simple - "simple" means no dynamic binding, no threads, etc...

isn't that piece of code should finalised during compile time? how java compiler work (as know java cross-platform language, may work differently)?

p.s. ensure same piece of compiled code, transferred compiled .apk usb android phones, rather using eclipse run them directly in devices.


i have found source code of android 2.2 froyo:

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 do-while loop, string.indexof("", tail) never return -1).

doubts has been cleared bit. but...

i still don't know why different versions of string class loaded, upon running in different devices. meant cross-platform?

let me close question.

i not find concrete documentary references. after repeated trials, , few researches of "differences between java vs c compiler". yes, java behaviour - compile once, run everywhere (& debug everywhere).

this why need java vm. why java compiles faster c/c++. why java runs slower c/c++.

(i guess) while java compiles, records class signatures. upon running on vm, matches signatures real implementations of corresponding classes, compile them machine codes just-in-time. why java can compile once, run on different machines, because different vms have own implementations of classes. lead problem that, if there bug in version, programmer can nothing it. because actual buggy implementation @ client side end-user running program. need wait user update own vm.

p.s. in fact can re-invent wheel, re-write our own classes. classes attached deployed program.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -