Java Fundamentals and Core Concepts
What is the difference between JVM, JRE, and JDK?
Answer:
JVM (Java Virtual Machine) is an abstract machine that provides the runtime environment to execute Java bytecode. JRE (Java Runtime Environment) is the implementation of JVM, providing the necessary libraries and files to run Java applications. JDK (Java Development Kit) includes JRE along with development tools like the compiler (javac) and debugger, used for developing Java applications.
Answer:
Java achieves platform independence through its 'Write Once, Run Anywhere' (WORA) principle. Java source code is compiled into bytecode, which is then executed by the JVM. Since a JVM is available for various operating systems, the same bytecode can run on any platform that has a compatible JVM, without needing recompilation.
What are the main differences between abstract class and interface in Java?
Answer:
An abstract class can have abstract and non-abstract methods, constructors, and instance variables, and supports single inheritance. An interface can only have abstract methods (before Java 8) or default/static methods (Java 8+), and only static final variables, supporting multiple inheritance. A class extends an abstract class but implements an interface.
What is method overloading and method overriding?
Answer:
Method overloading occurs when a class has multiple methods with the same name but different parameters (number, type, or order). Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass, maintaining the same method signature.
Explain the final keyword in Java.
Answer:
The final keyword can be used with variables, methods, and classes. A final variable's value cannot be changed once initialized. A final method cannot be overridden by subclasses. A final class cannot be subclassed, preventing inheritance.
What is the purpose of the static keyword in Java?
Answer:
The static keyword indicates that a member (variable or method) belongs to the class itself, rather than to any specific instance of the class. Static members can be accessed directly using the class name without creating an object. Static variables are shared among all instances, and static methods can only access static members.
Describe the Java Memory Model (Heap vs. Stack).
Answer:
The Heap memory is used for storing objects and their instance variables, and it's shared across all threads. The Stack memory is used for storing local variables, method calls, and primitive data types, and each thread has its own stack. Objects on the Heap are garbage collected when no longer referenced, while Stack frames are popped off when a method completes.
What is the difference between == and .equals() in Java?
Answer:
== is an operator used to compare references (memory addresses) for objects, checking if two references point to the same object. For primitive types, it compares values. The .equals() method, inherited from Object, is used to compare the content or value of objects. It should be overridden in custom classes to provide meaningful value comparison.
How does exception handling work in Java? Name some keywords.
Answer:
Exception handling in Java uses try, catch, finally, and throw/throws keywords. Code that might throw an exception is placed in a try block. If an exception occurs, it's caught by a catch block. The finally block executes regardless of whether an exception occurred. throw is used to explicitly throw an exception, and throws declares that a method might throw certain exceptions.
What are Wrapper Classes in Java?
Answer:
Wrapper classes provide a way to use primitive data types (like int, char, boolean) as objects. Each primitive type has a corresponding wrapper class (e.g., Integer, Character, Boolean). They are useful for collections that store objects, and for features like autoboxing/unboxing, which automatically convert between primitives and their wrapper objects.