javaandroidjvmandroid-runtime

Is java’s WORA concept targeted at the OS or hardware and how does the Android Runtime manage it?


Although I’ ve practiced a lot on C/C++ , I’m still pretty new to programming and now I’m learning the basics of Java. Java’s ‘Write Once, Run Everywhere’ concept has always confused me.

  1. Are Languages such as C completely portable at a source level , with only the need to change the compiler for the cpu architecture?If yes , could we consider even C a WORA language over assembly level?

2.What are the abstractions that the jvm provides on a source level? For example , for a simple Hello World in C++ we have to include the header that is different for each OS, but in java this is not the case .

3.Because of the abstractions of the jvm , is java limited to high level ‘app’ development? Can you theoretically write an OS or driver in Java? What parts of Android are written in Java? When the Android Runtime ‘translates’ app jvm bytecode into native machine code does it add (link) other instructions that were simply not addable in the source?


Solution

  • Yes and no.

    Are Languages such as C completely portable at a source level , with only the need to change the compiler for the cpu architecture?If yes , could we consider even C a WORA language over assembly level?

    It depends on the program. If you used inline-assembly or intrinsics, you would have to rewrite this programs. Furthermore big/little-endian can be an issue.

    What are the abstractions that the jvm provides on a source level? For example , for a simple Hello World in C++ we have to include the header that is different for each OS, but in java this is not the case.

    The JVM/Java abstracts away a lot of hardware details like little/big endian, no inline-assembly, no intrinsics on source level (But at runtime, for example the JVM uses AES-NI instructions if available, for example). In Java we don't have to import something as the package java.lang is imported as default.

    Because of the abstractions of the jvm , is java limited to high level ‘app’ development? Can you theoretically write an OS or driver in Java? What parts of Android are written in Java? When the Android Runtime ‘translates’ app jvm bytecode into native machine code does it add (link) other instructions that were simply not addable in the source?

    Sun tried to write an OS in Java (https://en.wikipedia.org/wiki/JavaOS), other ones are for example JNode(https://sourceforge.net/projects/jnode/).

    A big part of the UI of android is written in java and a (smaller) parts of the system itself, while the kernel and things like the network stack are written in a lowlevel language like C.(Here an image from the android documentation: https://developer.android.com/guide/platform/images/android-stack_2x.png)

    The JVM of android (Android Runtime: https://en.wikipedia.org/wiki/Android_Runtime), is an AOT-compiler, that compiles your app to well-optimized machinecode for your device.