Assertion in Java

In Java, assertions are a way to test assumptions about your program during runtime. They are primarily used for debugging purposes and help catch programming errors early. An assertion evaluates a boolean expression, and if the expression evaluates to false, the JVM throws an AssertionError. Syntax assert condition; assert condition; or assert condition :…

0 Comments

KISS in Java

The KISS principle says: write the simplest code that solves the problem without unnecessary complexity. 🚨 Without KISS (Over-Engineered Example) public class DiscountCalculator { // Overcomplicated discount logic public double calculateDiscount(double price, String customerType) { if (customerType.equals("REGULAR")) { if (price > 100) { return price * 0.9; } else { return price * 0.95;…

0 Comments

YAGNI in Java

In Java, this often happens when developers add extra classes, methods, or features “just in case.” 🚨 Without YAGNI (Over-engineering) public class ReportGenerator { // Feature not needed right now, but added "just in case" public String generatePDFReport(String data) { // Pretend to generate PDF (not needed yet) return "PDF Report: " + data;…

0 Comments

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: 1. Heap Area Where all objects are stored. Divided into:…

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

End of content

No more pages to load