0

I heard that Java is becoming faster on newer versions of Android that it's almost as fast as C++, and the performance depends on the type of code/operations. Is this true? If it is, which Java version is the fastest on Android and is supported for Android development?

I currently probably have Java 11; it said so when I entered java --version into CMD:

java 11.0.11 2021-04-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)

But, I got this when I opened the "About Java" app:

enter image description here

Would the upgrade be worth it, and is it different from Java 8/11 in how it builds/syntax/features? Would changing the Java/JDK version affect the Gradle/other build tools' versions or the minimum Android version a device would need to run my apps?

Also, if there's any difference in the speed/performance between using different compilers, JDKs and optimizations, how faster/better is it?

Any comparisons would be helpful.

The Amateur Coder
  • 789
  • 3
  • 11
  • 33
  • 6
    "I heard that Java is becoming faster on newer versions of Android" -- Android does not run Java. Android executes Dalvik bytecodes, which we get from compiling Java (or Kotlin, etc.) code to Java 8 bytecode, which then gets cross-compiled into Dalvik bytecode. How quickly Android executes that Dalvik bytecode depends on the version of Android, along with hardware characteristics (CPU speed, etc.). – CommonsWare May 06 '22 at 16:54
  • 1
    @CommonsWare, yes, I'm aware of that. But does the Java version matter too? The Dalvik bytecodes we get by compiling Java (no, I've not tried Kotlin) would change with the Java version used, as it probably would generate different/more optimized bytecodes, right? – The Amateur Coder May 06 '22 at 16:59
  • 1
    No, it really doesn't. Java (or kotlin, etc) are just languages to make it easier to express dalvik bytecodes. There may be a few tiny differences that later versions enable, but they'd be minor shavings of a few microseconds in some cases. The truly important thing is the interpreter underneath and the compiler that turns it into bytecode. – Gabe Sechan May 06 '22 at 17:02
  • 1
    As for being facter and aproaching C- no. Java has been claiming that since I was in college 20+ years ago. It still hasn't happened. The real question is if you actually need that speed, or just think you need it. – Gabe Sechan May 06 '22 at 17:03
  • 1
    @GabeSechan, oh, I thought the Android team is improving Java apps' performance a lot somehow. So, Java is not actually as fast as C/C++. And yes, I only think I need that speed. Do apps that are more dependent on user interaction need that speed? – The Amateur Coder May 06 '22 at 17:19
  • 1
    "I thought the Android team is improving Java apps' performance a lot somehow" -- any such improvements are at the level of the runtime engine (ART), not Java. "The Dalvik bytecodes we get by compiling Java (no, I've not tried Kotlin) would change with the Java version used" -- no, partly because right now the Android toolchain only supports Java 8 bytecodes, and partly because Android does not run Java bytecodes anyway. "Do apps that are more dependent on user interaction need that speed?" -- not usually. – CommonsWare May 06 '22 at 17:45
  • 1
    @CommonsWare, oh, but what is [this](https://stackoverflow.com/questions/49891730/invoke-customs-are-only-supported-starting-with-android-0-min-api-26#comment97221250_51690133) then? Is that something else? Thanks for confirming I don't need that speed. – The Amateur Coder May 06 '22 at 18:10
  • 1
    "but what is this then? Is that something else?" -- so long as unrecognized bytecodes are not emitted, that holds up, but adds no value. See https://jakewharton.com/androids-java-9-10-11-and-12-support/ for a deeper dive into what language options from newer versions of Java (through 12) are supported, and https://developer.android.com/studio/write/java8-support#library-desugaring for more on "desugaring". – CommonsWare May 06 '22 at 18:22
  • 3
    The fact that the Java compiler/SDK used for Android development has effectively zero impact on the performance of the finished Android application is much easier to understand when you realize that the Java compiler (i.e. literally `javac`) does basically no optimization at all and it's all up to the runtime (the JVM in Java and the Android Runtime ART for Dalvik/Android). The *bytecode* isn't optimized. In fact it's a very high-level representation that's *designed to be further optimized in later steps*. – Joachim Sauer May 06 '22 at 19:55

1 Answers1

9

I heard that Java is becoming faster on newer versions of Android that it's almost as fast as C++,

The Java platform has had performance around that of C++ for a couple decades. Sometimes faster, due to the dynamic runtime optimizations made by HotSpot or OpenJ9.

But Android does not run the Java platform.

When writing Android code in either the Kotlin language or the Java language, that code is compiled down to Dalvik bytecode. At runtime, that byte code is further interpreted/compiled by the Android Runtime in modern Android, or by the Dalvik Runtime in earlier Android.

which Java version is the fastest on Android and is supported for Android development?

Your choice of JDK and Java version will not affect the performance of your Android app. As explained above, the JDK is not involved with the runtime execution of your Android app.

Would the upgrade be worth it, and is it different from Java 8/11 in how it builds/syntax/features?

You can make use of some of the new Java language features found in later versions of Java. See links below.

I wouldn't want to simply install different Java versions and change Android Studio's settings.

You can certainly install multiple JDKs on your machine. For installing and managing multiple JDKs, I suggest using SDKMAN.

Within Android Studio, in your project's settings, you specify which of the installed JDKs to utilize. See Set the JDK version section in the manual.

Note however that the documentation recommends using the JDK that comes bundled with Android Studio.


For more info, use the links provided by CommonsWare:

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154