Java compilation targets: are Java programs compiled into portable bytecode that runs on a Java Virtual Machine (JVM)? Evaluate this foundational statement.
-
AValid (Java source compiles to bytecode executed by the JVM)
-
BInvalid (Java compiles directly to native machine code only)
-
CDepends on whether GraalVM native image is used
-
DValid only for applets, not applications
-
EApplies exclusively to Android apps
Answer
Correct Answer: Valid (Java source compiles to bytecode executed by the JVM)
Explanation
Introduction / Context: Java’s “write once, run anywhere” promise originates from compiling source code into a platform-neutral intermediate form called bytecode. This bytecode executes on any compatible Java Virtual Machine (JVM), enabling broad portability across operating systems and hardware.
Given Data / Assumptions:
- The statement says Java programs are compiled into bytecode.
- We focus on the standard javac toolchain and conventional deployment.
- Alternative toolchains (for example, ahead-of-time native image) do not negate the general rule.
Concept / Approach: The javac compiler converts .java files into .class files containing JVM bytecode. The JVM loads these classes, verifies them, and executes them using interpretation and/or just-in-time compilation. While newer technologies can compile Java to native binaries, the canonical model remains bytecode to JVM execution, which underpins Java’s portability and security model.
Step-by-Step Solution:
Write Java source (.java).Compile with javac to produce .class bytecode files.Run with java (the JVM) which loads and executes bytecode.Benefit from cross-platform portability through consistent JVM implementations.Verification / Alternative check: Inspect compiled output: .class files contain bytecode; tools like javap display bytecode instructions, confirming the compilation target.
Why Other Options Are Wrong:
- Java does not “only” compile to native machine code; that is optional via specialized tools.
- Bytecode applies broadly (apps, libraries, tests), not only applets or Android.
Common Pitfalls: Confusing JVM bytecode with CPU-specific machine code; assuming Android uses standard JVM bytecode (Android uses DEX/ART but the principle of intermediate bytecode remains similar).
Final Answer: Valid (Java source compiles to bytecode executed by the JVM)