DRY in Java

🚨 Without DRY (Bad Practice – Code Duplication) public class Calculator { // Duplicated logic for addition public int addIntegers(int a, int b) { return a + b; } // Duplicated logic for doubles public double addDoubles(double a, double b) { return a + b; } public static void main(String[] args) { Calculator calc…

0 Comments

SOLID in Java

S – Single Responsibility Principle (SRP) A class should have only one reason to change, meaning it should only have one responsibility. // Bad Example: Single class has multiple responsibilities class User { void saveUserToDatabase() { /* code to save user */ } void sendEmail() { /* code to send email */ } }…

0 Comments

Profiling in Java

Profiling in Java is the process of analyzing a Java application to measure its performance characteristics such as CPU usage, memory consumption, object creation, garbage collection, thread activity, and method execution time. It helps in identifying performance bottlenecks, memory leaks, and inefficient code paths. 🔹 Why Profile a Java Application? To detect slow methods…

0 Comments

Memory management in Java

Java manages memory automatically using a Garbage Collector (GC), which frees developers from explicitly allocating and deallocating memory like in C or C++. Let’s break it down clearly with an example. 🔹 Memory Management in Java Java memory is mainly divided into two parts: Stack Memory Stores primitive variables (int, double, etc.) and references…

0 Comments

Parallel Stream in Java

Parallel streams in Java are part of the Java Streams API (introduced in Java 8) and allow you to perform parallel data processing in a declarative way, taking advantage of multiple CPU cores for better performance on large datasets. 🔹 What is a Stream? A Stream is a sequence of elements that supports aggregate…

0 Comments

Java Streams API

The Java Streams API (introduced in Java 8) provides a modern, functional-style way to process collections of data. It allows you to work with sequences of elements (like List, Set, or arrays) using declarative operations such as filtering, mapping, reducing, collecting, and more, while supporting parallel execution. Key Concepts Stream: A sequence of elements…

0 Comments

Lambda expression in Java

A lambda expression in Java is a way to write shorter code for implementing interfaces that have a single abstract method (called functional interfaces). It was introduced in Java 8 to make code more concise and readable, especially when working with APIs like Streams or event listeners. General Syntax: (parameters) -> expression (parameters) ->…

0 Comments

Functional Interface in Java

A Functional Interface in Java 8 is an interface that has exactly one abstract method (SAM – Single Abstract Method). It can have any number of default and static methods, but only one abstract method. Functional interfaces are the foundation of Lambda Expressions and Method References in Java 8. ✅ Key Points Only one…

0 Comments

Method Reference in Java

In Java, method references provide a concise way to refer to methods or constructors without executing them. They are part of Java 8’s lambda expressions feature and are used mainly with functional interfaces. Method references make code shorter and more readable. Syntax There are four types of method references: Reference to a static method…

0 Comments

Optional in Java

In Java, Optional is a container object introduced in Java 8 that may or may not contain a non-null value. It is primarily used to avoid NullPointerException and to make code more readable by explicitly handling the presence or absence of values. Here’s a thorough breakdown: 1. Creating an Optional import java.util.Optional; public class…

0 Comments

End of content

No more pages to load