Interview Q&A · Java · 4 questions in this topic
Java Compared to Other Languages — Java Interview Q&A
How Java differs from C and C++ — memory model, pointers, generics and portability. Part of Core Java Basics · 294 questions in the Java bank.
Comparing Java with C and C++ is really a comparison of runtime models. Java compiles to bytecode and runs on a JVM that manages memory, verifies bytecode and provides reflection and portable threading; C and C++ compile to native code and expose manual allocation, pointer arithmetic and undefined behaviour. That gives C++ more control and often lower overhead, while Java trades some control for safety and portability. Modern Java has generics, lambdas, records and pattern matching, so claims that it lacks those features are out of date.
What is the difference between C++ and Java?
Answer
Java runs on a virtual machine with automatic garbage collection and no direct pointer arithmetic, while C++ compiles to native code and gives the programmer manual memory control. Java code is more portable; C++ can be faster and allows features Java forbids. Many old "Java has no X" claims are now wrong — Java has generics (since 5), lambdas and streams (since 8), records and pattern matching (17–21).
Key points
- 1Memory: Java has a garbage collector; C++ uses new/delete and RAII (smart pointers).
- 2Pointers: Java has references, not arithmetic pointers; C++ has raw pointers.
- 3Operator overloading: C++ allows it (Java allows only + for String concatenation).
- 4Generics: both support type parameters now; Java generics use type erasure.
- 5Inheritance: Java has single class inheritance plus interfaces; C++ allows multiple class inheritance.
- 6Both support multithreading; Java builds it into the language and standard library.
- 7Arrays: Java checks bounds and throws ArrayIndexOutOfBoundsException; C++ overruns are undefined behaviour.
- 8Build model: Java has no headers or preprocessor — classes compile to bytecode loaded by the JVM.
How does Java achieve platform independence?
Answer
javac translates source into an intermediate instruction set — bytecode — instead of machine code for one CPU. A JVM written for each platform loads that bytecode, verifies it, and interprets or just-in-time compiles it to native instructions at run time. The same .class file therefore runs on any platform that has a compatible JVM; only the JVM implementation is platform-specific. The catch is that the target machine must have a JVM of the right version, and JNI or native libraries reintroduce platform dependence.
Key points
- 1Source compiles once to bytecode (.class files), not to native binaries.
- 2The JVM is the platform adapter: one implementation per operating system and CPU.
- 3Bytecode verification at load time is part of Java’s safety model.
- 4Anything touching native code or the filesystem loses some portability.
What is the difference between the JDK, the JRE and the JVM?
Answer
The JVM executes bytecode: it loads classes, manages memory and runs the program. The JRE is the JVM plus the core class libraries needed to run existing programs. The JDK is the JRE plus development tools such as javac, jar, javadoc and jshell. You need the JDK to write and compile Java, and only the JRE to run a finished application — which is why server images are often built on a smaller runtime base.
Key points
- 1JVM: the execution engine, one per platform.
- 2JRE: JVM + standard libraries, enough to run but not to compile.
- 3JDK: JRE + compiler and tooling, what developers install.
- 4Modern JDKs can build a trimmed custom runtime with jlink.
Why is Java usually described as not purely object-oriented?
Answer
In a purely object-oriented language every value would be an object. Java keeps eight primitive types — byte, short, int, long, float, double, char and boolean — that are not objects: they have no methods, are not heap-allocated, and cannot be used where an Object is expected without boxing. Java also allows static members, which belong to the type rather than to any instance. Both choices exist for performance and simplicity, and both are reasons purists call Java a hybrid rather than a pure OO language.
Key points
- 1Primitives are values, not objects, and are exempt from inheritance.
- 2Boxing ({ Integer }) wraps a primitive but creates a separate object.
- 3static methods and fields are reachable without any instance.
- 4The trade-off buys speed and predictable memory layout.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission