Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Comparable vs Comparator

Comparable Comparator Key Differences Aspect Comparable Comparator Sorting Sequence Provides a single sorting sequence based on one element (e.g., id, name). Provides multiple sorting sequences based on multiple elements (e.g., id, name, price). Class Modification Affects the original class; the class must implement Comparable. Does not modify the original class; comparison logic can be in a separate class. Method Uses compareTo() method for sorting. Uses compare() method for sorting. Package…

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Thread: Differences between t.start() and t.run()

Tasks of start() Method **Overriding run() and **start() Warning: Overriding start() without calling super.start() will cause the thread to execute like a normal method and not in a new thread. Can a Thread Be Restarted? Solution: To restart the same logic, you need to create a new thread instance. Summary

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Covariant Return Type

Definition In Java, the covariant return type allows an overridden method to return a subtype of the return type declared in the parent class. This feature was introduced in Java 5 to make method overriding more flexible and precise. Key Points Example Advantages Key Differences from Regular Return Types Aspect Regular Return Type Covariant Return Type Return Type Must match exactly in parent and child. Can be a subtype in…

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Association vs Aggregation

Association Aggregation(Has-A Relationship) 3. Composition (Strong Has-A Relationship) Key Differences Aspect Association Aggregation Definition Describes a relationship between classes. Represents a whole-part relationship. Ownership No ownership between objects. Whole does not own the part. Object Lifecycle Objects have independent lifecycles. Parts can exist independently of the whole. Relationship Ownership Lifecycle Dependency Example Association No Independent Student ↔ Teacher Aggregation Shared Independent School ↔ Department Composition Strong Dependent Human → Brain…

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Iterator vs ListIterator

Both Iterator and ListIterator are used to traverse collections in Java, but they differ in their functionality and scope. Iterator ListIterator Key Differences Aspect Iterator ListIterator Traversal Forward only. Forward and backward. Modification Can remove elements. Can add, remove, or modify elements. Applicable To All collections. Only List collections. Additional Methods None. hasPrevious(), previous(), etc. Summary

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

HashMap vs WeakHashMap

Both HashMap and WeakHashMap are part of the java.util package and are used to store key-value pairs. However, they have distinct differences in their behavior and use cases. HashMap WeakHashMap Key Differences Aspect HashMap WeakHashMap Key Reference Strong references. Weak references. Garbage Collection Keys are not eligible for GC. Keys are eligible for GC if unreferenced. Use Case General-purpose key-value mapping. Temporary mappings, caches. Performance Faster. Slower due to GC…

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Understanding Lambda Expressions vs Method References in Java

Introduction In Java, lambda expressions and method references are both features introduced in Java 8 to support functional programming. While both serve the purpose of simplifying the way we write functional interfaces, there are specific scenarios where each is more suitable. This guide explains when to use a lambda expression and when to use a method reference. What is a Lambda Expression? A lambda expression is a concise way to…

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

How to Make a Class Immutable in Java?

An immutable class in Java is a class whose objects cannot be modified once created. This design ensures thread safety, simplifies concurrent programming, and provides better control over object states. Steps to Create an Immutable Class Complete Example of an Immutable Class Deep Copy vs. Shallow Copy Shallow Copy: Deep Copy: Other Copy Mechanisms 2. Serialization and Deserialization:

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Threads in Java

Threads in Java allow concurrent execution of two or more parts of a program, enabling multitasking and efficient resource usage. 1. What is a Thread in Java? 2. Lifecycle of a Thread A thread in Java goes through the following states: 3. Creating Threads in Java 1. Extending the Thread Class 2. Implementing the Runnable Interface 3. Using Lambda Expressions 4. Thread Class Methods Method Description start() Starts the thread…

Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

Interfaces in Java

Types of Interfaces in Java Java provides several types of interfaces to define behavior that classes can implement. These include marker interfaces, functional interfaces, and regular interfaces. Each serves a specific purpose and is used in different scenarios. 1. Marker Interface How Marker Interfaces Work Example: In this example, Employee can be serialized because it implements the Serializable marker interface. 2. Functional Interface Example: Chaining Functional Interfaces Functional interfaces can…