What are the new features introduced in Java 8? Answer: Java 8 introduced several significant features, including lambda expressions, functional interfaces, default methods in interfaces, the Stream API, the Date and Time API (java.time), and the Optional class. Elaborate on each feature and provide examples of their usage.
Explain the differences between checked and unchecked exceptions in Java. Answer: Checked exceptions are exceptions that the compiler requires the programmer to handle explicitly using a try-catch block or by declaring them in the method signature using the “throws” keyword. Unchecked exceptions, on the other hand, do not require explicit handling. They are usually related to programming errors, such as null pointer exceptions or array index out of bounds exceptions.
What is the difference between method overloading and method overriding? Answer: Method overloading occurs when multiple methods in the same class have the same name but different parameters. Method overriding, on the other hand, happens when a subclass provides a different implementation of a method already defined in its superclass. Overloading is determined at compile-time, while overriding is determined at runtime.
How does garbage collection work in Java? Answer: Java’s garbage collector automatically reclaims memory by identifying and freeing objects that are no longer referenced. It uses algorithms like Mark and Sweep or Copying to manage memory. Explain these algorithms and discuss concepts like the garbage collection roots and generations (Young Generation, Old Generation).
What is the difference between ArrayList and LinkedList in Java? Answer: ArrayList is implemented as a resizable array, providing fast random access and efficient iteration. LinkedList, on the other hand, is implemented as a doubly-linked list, allowing efficient insertion and deletion but slower random access. Discuss the trade-offs between the two data structures based on their specific use cases.
Explain the concept of multithreading in Java and how it can be achieved. Answer: Multithreading allows concurrent execution of multiple threads within a single program. In Java, multithreading can be achieved by extending the Thread class or implementing the Runnable interface. Discuss the advantages of multithreading, synchronization mechanisms, and potential issues like race conditions and deadlock.
What is the difference between the Comparable and Comparator interfaces in Java? Answer: The Comparable interface is used for defining the natural ordering of objects. It is implemented by the class of the objects being compared. The Comparator interface, on the other hand, provides a separate class to define the comparison logic. Compare and contrast the usage and scenarios for each interface.
How does Java handle memory management, and what are the differences between stack and heap memory? Answer: Java manages memory automatically through garbage collection. Objects are stored in the heap memory, while method calls and local variables are stored in the stack memory. Explain the differences between stack and heap memory, their respective uses, and how Java manages memory allocation and deallocation.
Explain the concept of serialization and deserialization in Java. Answer: Serialization is the process of converting an object into a byte stream, which can be stored in a file or transmitted over the network. Deserialization is the reverse process of reconstructing the object from the byte stream. Discuss the Serializable interface, transient keyword, and potential challenges with versioning and compatibility.
What is the Java Memory Model (JMM) and its importance in multithreaded programming? Answer: The Java Memory Model defines the rules for how threads interact through shared memory in a multithreaded environment. It ensures that changes made by one thread are visible to other threads. Discuss concepts like volatile keyword, happens-before relationship, and the importance of thread synchronization.