Core Java Tutorial

Top 10 System Design Implementations
Modern enterprise applications must handle millions of users and high request volumes. Senior Java developers are expected to design systems that are scalable, reliable, and fault tolerant. In this guide we explain 10 common system design problems, their architecture, Spring Boot implementation approaches, interview questions, and strategies to scale systems to high TPS (Transactions Per Second). 1. URL Shortener System Problem Convert long URLs into short URLs and redirect users quickly. Example: https://example.com/products/12345 ↓ https://7ds.in/a1B2c Core Components URL generation service API service Database Cache (Redis) Load balancer Spring Boot Implementation... Read more...
Fail-Fast vs Fail-Safe Iterator in Java
Iterators in Java are used to traverse elements of a collection. Depending on how they behave when a collection is modified during iteration, they are classified as Fail-Fast and Fail-Safe iterators. Fail-Fast Iterator Fail-fast iterators immediately throw an exception if the collection is modified while iterating. They detect structural modification and throw ConcurrentModificationException. Example import java.util.*; public class Test { public static void main(String[] args){ List list = new ArrayList<>(); list.add("Java"); list.add("Python"); for(String s : list){ list.add("C++"); } } } Output: ConcurrentModificationException Fail-fast iterators check a modification counter internally and throw... Read more...
Marker Interfaces in Java
A Marker Interface in Java is an empty interface (without methods) used to “mark” a class. This marker provides special behavior to the JVM or a framework. The presence of the marker interface signals that the class should be treated differently. Definition A marker interface is simply an interface with no methods. interface MarkerInterface { } The interface does not define methods but acts as a marker for special behavior. Example: Serializable Interface One of the most common marker interfaces in Java is Serializable. import java.io.Serializable; class Person implements Serializable... Read more...
If You Override equals() but Not hashCode() in Java?
In Java, the equals() and hashCode() methods work together to determine object equality, especially when objects are stored in collections such as HashMap or HashSet. equals() Method The equals() method is used to compare the logical equality of two objects. public boolean equals(Object obj) Example: class Person { String name; Person(String name){ this.name = name; } public boolean equals(Object obj){ Person p = (Person) obj; return this.name.equals(p.name); } } hashCode() Method The hashCode() method returns an integer value used by hash-based collections to locate objects quickly. public int hashCode() The Contract... Read more...
Stack vs Heap vs Thread Memory in Java
Understanding memory structure is important when working with multithreaded applications. Java memory mainly consists of Stack Memory, Heap Memory, and Thread Memory. 1. Stack Memory Stack memory stores method calls and local variables. Each thread in Java has its own stack. Stores method execution frames Stores primitive variables Stores object references Memory is automatically removed when a method finishes public class Example { public static void main(String[] args) { int x = 10; calculate(x); } static void calculate(int value){ int result = value * 2; System.out.println(result); } } Stack Execution... Read more...
Java Memory Model (JMM)
The Java Memory Model (JMM) defines how threads interact with memory in Java. It specifies how variables are stored in memory and how threads access and modify those variables in a multi-threaded environment. Why Java Memory Model is Needed Modern CPUs use caching and reordering optimizations. Without a memory model, different threads might see inconsistent data. JMM ensures predictable behavior in concurrent programs. JMM defines rules for visibility, ordering, and atomicity of shared variables. Main Components of JMM Main Memory Thread Working Memory Main Memory Stores all shared variables. Every... Read more...
OutOfMemoryError in Java
OutOfMemoryError occurs when the JVM cannot allocate memory for new objects. It usually happens due to memory leaks, excessive object creation, or improper memory configuration. 1. Heap Space OutOfMemoryError This occurs when the Java heap is full and no more objects can be allocated. import java.util.*; public class HeapOOM { public static void main(String[] args) { List list = new ArrayList<>(); while(true){ list.add(new int[1000000]); } } } Real-world example: Caching large datasets in memory without eviction strategy. Solutions: Increase heap size (-Xmx) Use caching frameworks with eviction (LRU) Avoid storing... Read more...
Pass by Value vs Pass by Reference in Java
Understanding how parameters are passed in Java is important for writing correct programs. In Java, arguments are always passed using pass by value. However, when objects are passed, the value of the reference is copied. 1. Pass by Value In pass by value, a copy of the variable value is passed to the method. Changes made inside the method do not affect the original variable. public class Test { static void changeValue(int x){ x = 50; } public static void main(String[] args) { int num = 10; changeValue(num); System.out.println(num); } }... Read more...
Java 11 Features
Java 11 is a Long-Term Support (LTS) release that introduced several important API enhancements, performance improvements, and new utilities for developers. It became the most widely adopted enterprise Java version after Java 8. 1. New String Methods Java 11 introduced useful methods in the String class. String text = " Java "; System.out.println(text.strip()); System.out.println(text.isBlank()); System.out.println("Java\nPython".lines().count()); New methods include strip(), isBlank(), and lines(). 2. HTTP Client API Java 11 introduced a modern HTTP client to replace the older HttpURLConnection. HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://example.com")) .build(); HttpResponse response... Read more...
Java 17 Features
Java 17 is a Long-Term Support (LTS) release that introduced several important language enhancements and JVM improvements. It became widely adopted in enterprise applications. 1. Sealed Classes Sealed classes restrict which classes can extend or implement them. public sealed class Shape permits Circle, Rectangle {} final class Circle extends Shape {} final class Rectangle extends Shape {} Sealed classes help control inheritance and improve code safety. Interview Question: Why use sealed classes?Answer: To restrict which classes can extend a class or interface. 2. Records Records provide a concise way to... Read more...
Java 21 Features
Java 21 is a Long-Term Support (LTS) release introducing major improvements in concurrency, pattern matching, and collections. These features help developers build scalable and modern applications. 1. Virtual Threads (Project Loom) Virtual threads allow applications to run millions of lightweight threads with minimal resources. Thread.startVirtualThread(() -> { System.out.println("Running in virtual thread"); }); Virtual threads make concurrent programming easier and more scalable. Interview Question: What problem do virtual threads solve?Answer: They allow handling thousands of concurrent tasks without creating heavy OS threads. 2. Record Patterns Record patterns allow destructuring of records... Read more...
Java Versions History
Java has evolved significantly since its first release. Each version introduced features that improved performance, developer productivity, and scalability. Below is a quick overview of major Java versions. Java 1.0 (1996) First official Java release Introduced JVM (Java Virtual Machine) Write Once Run Anywhere concept Applet support for web applications Java 1.2 (1998) – Java 2 Introduced Collections Framework Swing GUI toolkit Improved performance and security Java 1.3 (2000) HotSpot JVM introduced Improved networking libraries Performance optimizations Java 1.4 (2002) Assertions Logging API NIO (New Input Output) XML processing support... Read more...