<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gihan Benaragama</title>
    <description>The latest articles on DEV Community by Gihan Benaragama (@gihan_benaragama).</description>
    <link>https://dev.to/gihan_benaragama</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3958332%2F9674e807-9fe8-43e3-9d5c-7b5067bdbefa.jpg</url>
      <title>DEV Community: Gihan Benaragama</title>
      <link>https://dev.to/gihan_benaragama</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gihan_benaragama"/>
    <language>en</language>
    <item>
      <title>Understanding WebSockets in Spring Boot: A Beginner-Friendly Guide</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Wed, 02 Dec 2026 04:22:07 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/understanding-websockets-in-spring-boot-a-beginner-friendly-guide-18nd</link>
      <guid>https://dev.to/gihan_benaragama/understanding-websockets-in-spring-boot-a-beginner-friendly-guide-18nd</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When building web applications, we often need to communicate between the frontend and backend.&lt;/p&gt;

&lt;p&gt;For example, imagine you are using a messaging application. When someone sends you a message, you expect to receive it immediately without refreshing the page.&lt;/p&gt;

&lt;p&gt;But how does that happen?&lt;/p&gt;

&lt;p&gt;Spring Boot provides excellent support for WebSocket communication through Spring WebSocket and Spring Messaging.&lt;/p&gt;

&lt;p&gt;In this article, we will explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What WebSockets are&lt;/li&gt;
&lt;li&gt;Why WebSockets are useful&lt;/li&gt;
&lt;li&gt;How WebSockets work&lt;/li&gt;
&lt;li&gt;What STOMP is and why we use it&lt;/li&gt;
&lt;li&gt;WebSockets in Spring Boot&lt;/li&gt;
&lt;li&gt;WebSocket security&lt;/li&gt;
&lt;li&gt;Real-world use cases&lt;/li&gt;
&lt;li&gt;Important things to consider when developing WebSocket applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;




&lt;h2&gt;
  
  
  1. What is a WebSocket?
&lt;/h2&gt;

&lt;p&gt;A WebSocket is a communication protocol that allows a client and server to communicate with each other through a single, long-lived connection.&lt;/p&gt;

&lt;p&gt;Unlike traditional HTTP communication, WebSockets support &lt;strong&gt;two-way communication&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client can send messages to the server.&lt;/li&gt;
&lt;li&gt;The server can send messages to the client.&lt;/li&gt;
&lt;li&gt;Both can communicate without repeatedly creating new HTTP requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, consider a normal HTTP application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The frontend sends a request to the backend.&lt;/li&gt;
&lt;li&gt;The backend processes the request.&lt;/li&gt;
&lt;li&gt;The backend sends a response.&lt;/li&gt;
&lt;li&gt;The connection is no longer used for that request.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With WebSockets, the connection stays open, allowing both sides to exchange messages whenever necessary.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP vs WebSocket
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HTTP&lt;/th&gt;
&lt;th&gt;WebSocket&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Request-response communication&lt;/td&gt;
&lt;td&gt;Two-way communication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usually a separate request for each operation&lt;/td&gt;
&lt;td&gt;Persistent connection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Client normally initiates requests&lt;/td&gt;
&lt;td&gt;Both client and server can send messages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Suitable for regular API operations&lt;/td&gt;
&lt;td&gt;Suitable for real-time communication&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; WebSockets do not replace HTTP. Both technologies are useful for different purposes.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcf81a3i0owcx83kwhhez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcf81a3i0owcx83kwhhez.png" alt="simple" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. How Does WebSocket Communication Work?
&lt;/h2&gt;

&lt;p&gt;WebSocket communication starts with an HTTP request called a &lt;strong&gt;WebSocket handshake&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The basic process is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The client requests a WebSocket connection.&lt;/li&gt;
&lt;li&gt;The server accepts the connection and upgrades it from HTTP to WebSocket.&lt;/li&gt;
&lt;li&gt;A persistent connection is established.&lt;/li&gt;
&lt;li&gt;Both client and server can exchange messages.&lt;/li&gt;
&lt;li&gt;The connection remains open until it is closed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Imagine a student using a university Lost &amp;amp; Found application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A student submits a claim for a lost item.&lt;/li&gt;
&lt;li&gt;An administrator approves the claim.&lt;/li&gt;
&lt;li&gt;The backend sends a notification through WebSocket.&lt;/li&gt;
&lt;li&gt;The student immediately receives the notification.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is no need to refresh the page or repeatedly request the server to check for updates.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. What is STOMP and Why Do We Use It?
&lt;/h2&gt;

&lt;p&gt;When working with WebSockets in Spring Boot, you will often encounter something called &lt;strong&gt;STOMP&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;STOMP stands for Simple Text Oriented Messaging Protocol.&lt;/p&gt;

&lt;p&gt;It is a messaging protocol that works over WebSocket and provides a structured way to exchange messages.&lt;/p&gt;

&lt;p&gt;But why do we need it?&lt;/p&gt;

&lt;p&gt;WebSocket itself provides the communication connection. However, it does not define an application-level messaging structure or destination system.&lt;/p&gt;

&lt;p&gt;STOMP helps solve this problem.&lt;/p&gt;

&lt;p&gt;It provides features such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sending messages to specific destinations.&lt;/li&gt;
&lt;li&gt;Subscribing to destinations.&lt;/li&gt;
&lt;li&gt;Publishing messages.&lt;/li&gt;
&lt;li&gt;Routing messages between clients and the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Suppose a user wants to receive notifications.&lt;/p&gt;

&lt;p&gt;The frontend subscribes to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/user/queue/notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the backend sends a notification to that user, the message broker routes it to the subscribed destination.&lt;/p&gt;

&lt;p&gt;This makes message communication easier to organize and manage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WebSocket:&lt;/strong&gt; Provides the communication connection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;STOMP:&lt;/strong&gt; Defines how messages are exchanged and routed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spring Messaging:&lt;/strong&gt; Handles messaging operations inside the application.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsdckdqusiozg4yjzjutd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsdckdqusiozg4yjzjutd.png" alt="concepts" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. WebSocket Architecture in Spring Boot
&lt;/h2&gt;

&lt;p&gt;Spring Boot provides WebSocket support through the &lt;code&gt;spring-boot-starter-websocket&lt;/code&gt; dependency.&lt;/p&gt;

&lt;p&gt;The main components include:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. WebSocket Configuration
&lt;/h3&gt;

&lt;p&gt;This is where we configure WebSocket endpoints, message brokers, and destination prefixes.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/ws&lt;/code&gt; – WebSocket connection endpoint.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/topic&lt;/code&gt; – Common destinations for messages published to multiple subscribers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/queue&lt;/code&gt; – Destinations commonly used for individual or point-to-point messages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/app&lt;/code&gt; – Prefix for messages sent from clients to application-level message handlers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/user&lt;/code&gt; – Prefix used for user-specific destinations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Message Broker
&lt;/h3&gt;

&lt;p&gt;A message broker manages message routing between publishers and subscribers.&lt;/p&gt;

&lt;p&gt;Spring provides a simple in-memory message broker that is suitable for smaller applications and development.&lt;/p&gt;

&lt;p&gt;For larger applications, external brokers such as RabbitMQ can be integrated.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Controller
&lt;/h3&gt;

&lt;p&gt;A WebSocket controller handles incoming messages from clients.&lt;/p&gt;

&lt;p&gt;In Spring Boot, we can use &lt;code&gt;@MessageMapping&lt;/code&gt; to define destinations that handle incoming STOMP messages.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Service Layer
&lt;/h3&gt;

&lt;p&gt;The service layer contains the application's business logic.&lt;/p&gt;

&lt;p&gt;For example, when an administrator approves a claim, the service can create a notification and send it to the appropriate user.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Frontend
&lt;/h3&gt;

&lt;p&gt;The frontend connects to the WebSocket endpoint, subscribes to destinations, and receives messages.&lt;/p&gt;

&lt;p&gt;For React applications, libraries such as &lt;code&gt;@stomp/stompjs&lt;/code&gt; can be used for STOMP communication.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu5vb59fntwc328wq0yyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu5vb59fntwc328wq0yyj.png" alt="architecure" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. How to Implement WebSockets in Spring Boot
&lt;/h2&gt;

&lt;p&gt;Let's look at a simple implementation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Add the Dependency
&lt;/h3&gt;

&lt;p&gt;Add the following dependency to your Spring Boot project:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maven:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-starter-websocket&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides Spring's WebSocket and messaging support.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Create WebSocket Configuration
&lt;/h3&gt;

&lt;p&gt;Create a configuration class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableWebSocketMessageBroker&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WebSocketConfig&lt;/span&gt;
        &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;WebSocketMessageBrokerConfigurer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;configureMessageBroker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;MessageBrokerRegistry&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enableSimpleBroker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/topic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/queue"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setApplicationDestinationPrefixes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/app"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setUserDestinationPrefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/user"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;registerStompEndpoints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;StompEndpointRegistry&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEndpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/ws"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAllowedOrigins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:5173"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand the important annotations and methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@Configuration&lt;/code&gt; tells Spring that this class contains configuration.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@EnableWebSocketMessageBroker&lt;/code&gt; enables WebSocket messaging support.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;enableSimpleBroker()&lt;/code&gt; enables Spring's built-in message broker.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setApplicationDestinationPrefixes()&lt;/code&gt; defines the prefix for application message destinations.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;registerStompEndpoints()&lt;/code&gt; registers the endpoint used by clients to establish WebSocket connections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Create a Message Controller
&lt;/h3&gt;

&lt;p&gt;We can use &lt;code&gt;@MessageMapping&lt;/code&gt; to receive messages from clients.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChatController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@MessageMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/chat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@SendTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/topic/messages"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@MessageMapping("/chat")&lt;/code&gt; receives messages sent to &lt;code&gt;/app/chat&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@SendTo("/topic/messages")&lt;/code&gt; broadcasts the returned message to subscribers of that destination.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 4: Connect from the Frontend
&lt;/h3&gt;

&lt;p&gt;The frontend can connect using a STOMP client.&lt;/p&gt;

&lt;p&gt;For React, install:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install @stomp/stompjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@stomp/stompjs"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;brokerURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ws://localhost:8080/ws"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="n"&gt;onConnect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/topic/messages"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

            &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/app/chat"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello from React!"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple example of sending and receiving messages using Spring Boot WebSockets and STOMP.&lt;/p&gt;

&lt;p&gt;For production applications, you should also handle connection errors, reconnection, authentication, and authorization.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. WebSocket Security in Spring Boot
&lt;/h2&gt;

&lt;p&gt;Security is one of the most important topics when developing WebSocket applications.&lt;/p&gt;

&lt;p&gt;Just because a user has established a WebSocket connection does not mean they should be allowed to access every message destination.&lt;/p&gt;

&lt;p&gt;For example, imagine a university application where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Students should receive their own notifications.&lt;/li&gt;
&lt;li&gt;Administrators can manage claims.&lt;/li&gt;
&lt;li&gt;Students should not receive another student's private messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need proper authentication and authorization.&lt;/p&gt;

&lt;h3&gt;
  
  
  6.1 Authentication
&lt;/h3&gt;

&lt;p&gt;Authentication answers the question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Who is the user?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Spring Security, authentication is generally established during the initial HTTP WebSocket handshake.&lt;/p&gt;

&lt;p&gt;For example, if your application uses JWT authentication, you need to make sure the JWT is validated and the authenticated user identity is associated with the WebSocket session.&lt;/p&gt;

&lt;p&gt;An important point: WebSocket connections do not automatically reuse a JWT sent in an ordinary REST API request.&lt;/p&gt;

&lt;p&gt;With browser-based STOMP clients, a JWT can be sent in STOMP CONNECT headers. In that case, you need to implement a server-side STOMP &lt;code&gt;ChannelInterceptor&lt;/code&gt; to validate the token and establish the authenticated user.&lt;/p&gt;

&lt;h3&gt;
  
  
  6.2 Authorization
&lt;/h3&gt;

&lt;p&gt;Authorization answers the question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the user allowed to do?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Students can subscribe to their own notifications.&lt;/li&gt;
&lt;li&gt;Administrators can access administrator-specific messaging destinations.&lt;/li&gt;
&lt;li&gt;Users cannot subscribe to another user's private notification destination.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring Security supports WebSocket message authorization.&lt;/p&gt;

&lt;p&gt;You can configure authorization rules to restrict access to specific message destinations.&lt;/p&gt;

&lt;h3&gt;
  
  
  6.3 Protecting Private Messages
&lt;/h3&gt;

&lt;p&gt;Consider this destination:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/user/queue/notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This destination is commonly used to deliver notifications to individual authenticated users.&lt;/p&gt;

&lt;p&gt;In Spring Boot, we can send a message using &lt;code&gt;SimpMessagingTemplate&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;messagingTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convertAndSendToUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"/queue/notifications"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;notification&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frontend subscribes to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/user/queue/notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring resolves the user destination and routes the message to the appropriate authenticated user's session.&lt;/p&gt;

&lt;p&gt;However, using &lt;code&gt;/user&lt;/code&gt; does not automatically guarantee security. You must also configure authentication, authorization, and destination access correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Real-World Uses of WebSockets
&lt;/h2&gt;

&lt;p&gt;WebSockets are useful whenever applications need real-time communication.&lt;/p&gt;

&lt;p&gt;Here are some common examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Real-Time Notifications
&lt;/h3&gt;

&lt;p&gt;Applications can immediately notify users about important events.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Claim approval notifications.&lt;/li&gt;
&lt;li&gt;Order status updates.&lt;/li&gt;
&lt;li&gt;New friend requests.&lt;/li&gt;
&lt;li&gt;Payment status notifications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Chat Applications
&lt;/h3&gt;

&lt;p&gt;Messaging applications use WebSockets to exchange messages in real time.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Private messaging.&lt;/li&gt;
&lt;li&gt;Group chats.&lt;/li&gt;
&lt;li&gt;Customer support chat.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Live Tracking Systems
&lt;/h3&gt;

&lt;p&gt;WebSockets can deliver frequent location or status updates.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delivery tracking.&lt;/li&gt;
&lt;li&gt;Vehicle tracking.&lt;/li&gt;
&lt;li&gt;Live logistics monitoring.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Collaborative Applications
&lt;/h3&gt;

&lt;p&gt;Multiple users can work on the same information simultaneously.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collaborative document editing.&lt;/li&gt;
&lt;li&gt;Shared project boards.&lt;/li&gt;
&lt;li&gt;Live dashboards.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Online Gaming
&lt;/h3&gt;

&lt;p&gt;WebSockets can exchange real-time game events between players and servers.&lt;/p&gt;

&lt;p&gt;They are useful for multiplayer games that need frequent communication.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. WebSocket vs Polling
&lt;/h2&gt;

&lt;p&gt;Before WebSockets became widely used, applications often used polling to check for updates.&lt;/p&gt;

&lt;p&gt;Polling means the client repeatedly sends HTTP requests to ask the server whether new information is available.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send a request every 5 seconds.&lt;/li&gt;
&lt;li&gt;Check whether a new notification exists.&lt;/li&gt;
&lt;li&gt;Repeat the process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This can create unnecessary requests, especially when there are no updates.&lt;/p&gt;

&lt;p&gt;WebSockets solve this by allowing the server to push messages to connected clients whenever new information is available.&lt;/p&gt;

&lt;p&gt;However, polling is not always bad. For applications that do not need immediate updates, regular HTTP requests or polling may be simpler and sufficient.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Important Things to Consider When Using WebSockets
&lt;/h2&gt;

&lt;p&gt;Although WebSockets are powerful, there are a few important things to remember.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Connection Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;WebSocket connections remain open, so applications must handle disconnections, reconnections, and inactive sessions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Always authenticate users and authorize access to message destinations. Never trust a username supplied by the client when sending private messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Scalability&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring's simple in-memory message broker is suitable for smaller applications, but it may not be sufficient for large-scale systems.&lt;/p&gt;

&lt;p&gt;External message brokers can help distribute messages across multiple application instances.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Offline Users&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A WebSocket connection only delivers messages while a suitable connection is available.&lt;/p&gt;

&lt;p&gt;If a user is offline, important notifications should be stored in a database so they can retrieve them later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Message Validation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Always validate incoming messages. Do not blindly trust data received from clients.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;WebSockets are an important technology for building modern real-time applications.&lt;/p&gt;

&lt;p&gt;Spring Boot makes WebSocket implementation easier through Spring WebSocket and Spring Messaging.&lt;/p&gt;

&lt;p&gt;By combining WebSocket, STOMP, and Spring Security, developers can build applications that support real-time communication while protecting private information.&lt;/p&gt;

&lt;p&gt;Whether you are building a chat application, notification system, live dashboard, or tracking application, understanding WebSockets can be a valuable skill for a backend developer.&lt;/p&gt;

&lt;p&gt;If you are learning Spring Boot, I recommend trying a small real-time notification feature in one of your projects. It is a great way to understand how WebSocket communication works in practice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Coding! 🚀&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>websocket</category>
      <category>java</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Introduction to Spring Security</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Thu, 26 Nov 2026 17:09:38 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/introduction-to-spring-security-1jgb</link>
      <guid>https://dev.to/gihan_benaragama/introduction-to-spring-security-1jgb</guid>
      <description>&lt;h1&gt;
  
  
  Spring Security Explained: How to Secure Your Spring Boot Applications
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When developing a web application, security is one of the most important things to consider. Applications often contain sensitive information such as user accounts, passwords, personal details, and payment information. Therefore, we need a way to protect these resources from unauthorized access.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;Spring Security&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is Spring Security?
&lt;/h2&gt;

&lt;p&gt;Spring Security is a powerful framework used to secure Java applications, especially Spring Boot applications.&lt;/p&gt;

&lt;p&gt;It provides built-in features to handle authentication, authorization, password encryption, and protection against common security attacks.&lt;/p&gt;

&lt;p&gt;Simply, Spring Security helps us control who can access our application and what they are allowed to do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxon3f3i0itdeaq15u58t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxon3f3i0itdeaq15u58t.png" alt="security" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Why Do We Need Spring Security?
&lt;/h2&gt;

&lt;p&gt;Imagine you are developing an online banking application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normal users should only access their own account details.&lt;/li&gt;
&lt;li&gt;Administrators should be able to manage user accounts.&lt;/li&gt;
&lt;li&gt;Users who are not logged in should not access private information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without proper security, anyone might access sensitive resources.&lt;/p&gt;

&lt;p&gt;Spring Security helps solve these problems by providing a structured way to manage application security.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Authentication vs Authorization
&lt;/h2&gt;

&lt;p&gt;These are two important concepts in Spring Security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Authentication means verifying the identity of a user.&lt;/p&gt;

&lt;p&gt;For example, when a user enters their email and password to log in, the application checks whether those credentials are valid.&lt;/p&gt;

&lt;p&gt;Simply: &lt;strong&gt;Who are you?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authorization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Authorization determines what an authenticated user is allowed to access.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An admin can manage all users.&lt;/li&gt;
&lt;li&gt;A normal user can view only their own profile.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simply: &lt;strong&gt;What are you allowed to do?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Important Features of Spring Security
&lt;/h2&gt;

&lt;p&gt;Spring Security provides several useful security features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Password Encoding:&lt;/strong&gt; Stores passwords securely using hashing instead of saving plain-text passwords.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Role-Based Access Control:&lt;/strong&gt; Controls access based on user roles, such as ADMIN and USER.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CSRF Protection:&lt;/strong&gt; Helps protect applications against Cross-Site Request Forgery attacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session Management:&lt;/strong&gt; Helps manage authenticated user sessions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request Security:&lt;/strong&gt; Controls which application endpoints are publicly accessible and which require authentication.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Common Spring Security Components
&lt;/h2&gt;

&lt;p&gt;When working with Spring Security, you will frequently encounter these components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SecurityFilterChain:&lt;/strong&gt; Defines security rules for incoming HTTP requests, including which endpoints require authentication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UserDetailsService:&lt;/strong&gt; Loads user information from a data source, such as a database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PasswordEncoder:&lt;/strong&gt; Encodes passwords and verifies submitted passwords against stored hashes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AuthenticationManager:&lt;/strong&gt; Coordinates the authentication process by checking the provided credentials.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these components will help you work with Spring Security more effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. What additional security features does Spring Security provide compared to MERN Stack?
&lt;/h2&gt;

&lt;p&gt;The main difference is that Spring Security provides a dedicated, comprehensive security framework, whereas MERN developers typically combine different Node.js libraries and middleware to implement security.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F67t4gn0m5jn59nnuor80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F67t4gn0m5jn59nnuor80.png" alt="Table" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Where Is Spring Security Used?
&lt;/h2&gt;

&lt;p&gt;Spring Security is commonly used in many real-world applications, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Banking and financial applications.&lt;/li&gt;
&lt;li&gt;E-commerce platforms.&lt;/li&gt;
&lt;li&gt;Student management systems.&lt;/li&gt;
&lt;li&gt;Employee management systems.&lt;/li&gt;
&lt;li&gt;REST APIs and enterprise applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any application that needs to protect user information or control access to resources can benefit from Spring Security.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Spring Security is an essential part of developing secure Spring Boot applications. It provides powerful features to manage authentication, authorization, and other security requirements.&lt;/p&gt;

&lt;p&gt;If you are planning to become a Spring Boot developer, understanding Spring Security is an important step towards building secure, reliable, and real-world applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Coding! 🚀&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>springsecurity</category>
      <category>springboot</category>
      <category>java</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Understanding Spring Boot Annotations: What They Are and Why We Use Them</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Wed, 25 Nov 2026 04:31:53 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/understanding-spring-boot-annotations-what-they-are-and-why-we-use-them-4l98</link>
      <guid>https://dev.to/gihan_benaragama/understanding-spring-boot-annotations-what-they-are-and-why-we-use-them-4l98</guid>
      <description>&lt;p&gt;When learning Spring Boot, you will notice that many classes, methods, and fields contain annotations such as &lt;code&gt;@RestController&lt;/code&gt;, &lt;code&gt;@Service&lt;/code&gt;, &lt;code&gt;@Autowired&lt;/code&gt;, and &lt;code&gt;@Entity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At first, these annotations may look confusing. But once you understand what annotations are and why we use them, Spring Boot becomes much easier to understand.&lt;/p&gt;

&lt;p&gt;In this article, we will explore what annotations are, their advantages, and the most commonly used Spring Boot annotations in simple language.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is an annotation?
&lt;/h2&gt;

&lt;p&gt;An annotation is a way of providing additional information (metadata) about our Java code.&lt;/p&gt;

&lt;p&gt;Annotations begin with the &lt;code&gt;@&lt;/code&gt; symbol. They can be applied to classes, methods, fields, constructors, and other elements.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;@Service&lt;/code&gt; tells Spring that a particular class is a service component. &lt;code&gt;@GetMapping&lt;/code&gt; tells Spring which HTTP GET requests a method should handle.&lt;/p&gt;

&lt;p&gt;Annotations don't usually perform the task themselves. Instead, frameworks like Spring read these annotations and perform the required actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Why do we use annotations?
&lt;/h2&gt;

&lt;p&gt;Annotations provide several advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Less configuration:&lt;/strong&gt; We don't need to write large configuration files for many common tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Less boilerplate code:&lt;/strong&gt; Spring handles many tasks automatically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better readability:&lt;/strong&gt; We can easily understand the purpose of a class or method by looking at its annotations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy development:&lt;/strong&gt; They simplify configuring application components, handling requests, database mapping, and dependency injection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability:&lt;/strong&gt; They help organize application behavior and configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Common Spring Boot annotations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Application startup
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@SpringBootApplication&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Marks the main application class and enables Spring Boot configuration, auto-configuration, and component scanning.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Component annotations
&lt;/h3&gt;

&lt;p&gt;These annotations tell Spring to manage the objects of the annotated classes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Component&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Marks a general-purpose class as a Spring-managed component.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Service&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Identifies a class that contains business logic.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Repository&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Identifies a class responsible for database operations.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Controller&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles web requests and usually returns webpage views.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@RestController&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles REST API requests and returns response data, commonly JSON.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Dependency injection
&lt;/h3&gt;

&lt;p&gt;These annotations help Spring connect objects and provide required dependencies.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Autowired&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Tells Spring to inject a required dependency.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Qualifier&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Specifies which bean to inject when multiple beans of the same type exist.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Primary&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Marks a bean as the preferred choice when multiple beans of the same type exist.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  HTTP request handling
&lt;/h3&gt;

&lt;p&gt;These annotations are commonly used when building REST APIs.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@RequestMapping&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Maps HTTP requests to a controller or method.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@GetMapping&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles HTTP GET requests, usually to retrieve data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@PostMapping&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles HTTP POST requests, usually to create data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@PutMapping&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles HTTP PUT requests, usually to update data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@DeleteMapping&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles HTTP DELETE requests, usually to delete data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@PathVariable&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Extracts a value from the URL path.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@RequestParam&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Extracts a query parameter from the URL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@RequestBody&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Converts request body data, such as JSON, into a Java object.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@CrossOrigin&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allows cross-origin requests from permitted origins.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Database and JPA annotations
&lt;/h3&gt;

&lt;p&gt;These annotations help map Java classes and fields to database tables and columns.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Entity&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Marks a Java class as a database entity.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Table&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Specifies the database table name for an entity.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Identifies the primary key field.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@GeneratedValue&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Configures how primary key values are generated.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Column&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Configures a database column, such as its name or constraints.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@OneToMany&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Defines a one-to-many relationship between entities.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@ManyToOne&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Defines a many-to-one relationship between entities.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@OneToOne&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Defines a one-to-one relationship between entities.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@ManyToMany&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Defines a many-to-many relationship between entities.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@JoinColumn&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Specifies a database column used to join or reference related entities.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Enumerated&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Specifies how enum values are stored in the database.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Configuration annotations
&lt;/h3&gt;

&lt;p&gt;These annotations help configure the application and its objects.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Configuration&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Marks a class as a configuration class.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Bean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Registers an object returned by a method as a Spring-managed bean.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Value&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Injects values from configuration files or supported property sources.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Other useful annotations
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Annotation&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@Transactional&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Manages database transactions, allowing related operations to succeed or roll back together.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  4. How Spring uses annotations
&lt;/h2&gt;

&lt;p&gt;When a Spring Boot application starts, Spring scans the configured packages and reads relevant annotations.&lt;/p&gt;

&lt;p&gt;Based on those annotations, Spring can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create and manage objects (beans).&lt;/li&gt;
&lt;li&gt;Connect dependencies between objects.&lt;/li&gt;
&lt;li&gt;Configure HTTP endpoints.&lt;/li&gt;
&lt;li&gt;Map Java entities to database tables.&lt;/li&gt;
&lt;li&gt;Apply configurations and other framework behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, when Spring finds &lt;code&gt;@Service&lt;/code&gt; on a class, it can register that class as a bean. When it finds &lt;code&gt;@GetMapping&lt;/code&gt;, it registers the method as a handler for a particular GET request.&lt;/p&gt;

&lt;p&gt;This is how annotations help Spring Boot handle many tasks automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Annotations are an important part of Spring Boot because they simplify configuration and reduce the amount of code developers need to write.&lt;/p&gt;

&lt;p&gt;You don't need to memorize all annotations at once. Start by understanding the annotations you encounter in your projects, their purpose, and how Spring uses them.&lt;/p&gt;

&lt;p&gt;Once you become familiar with the common annotations, reading and writing Spring Boot applications becomes much easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 🚀&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>annotations</category>
      <category>java</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Spring Boot Project Structure Explained: Folders, Files, and How They Connect</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Sun, 22 Nov 2026 17:33:08 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/spring-boot-project-structure-explained-folders-files-and-how-they-connect-2fhb</link>
      <guid>https://dev.to/gihan_benaragama/spring-boot-project-structure-explained-folders-files-and-how-they-connect-2fhb</guid>
      <description>&lt;p&gt;Spring Boot Project Structure Explained: Folders, Files, and How They Connect&lt;br&gt;
This is Part 2 of my Spring Boot series. In Part 1, I explained what Spring Boot is and why we use it. Now let's open a real project and understand what is inside.&lt;br&gt;
When you create a Spring Boot project, you get many files and folders. It looks confusing at first. This post answers three simple questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; What are all these files?&lt;/li&gt;
&lt;li&gt; Where do I write my code?&lt;/li&gt;
&lt;li&gt; How do my classes work together?
Step 1: What you get in a new project&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiu41eolie3vbfgei9s40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiu41eolie3vbfgei9s40.png" alt="folder struct" width="799" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the start, you only work with three things:&lt;br&gt;
What&lt;br&gt;
Why&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pom.xml&lt;/strong&gt;&lt;br&gt;
Add libraries here. Example: you need a database, so you add a database library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/main/java&lt;/strong&gt;&lt;br&gt;
Your Java code goes here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;application.properties&lt;/strong&gt;&lt;br&gt;
Your settings go here (port, database password, and so on).&lt;/p&gt;

&lt;p&gt;Everything else, you can ignore for now.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: The start button
&lt;/h2&gt;

&lt;p&gt;Open DemoApplication.java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this class, your whole application starts. The annotation @SpringBootApplication tells Spring: start here, and look for my other classes.&lt;/p&gt;

&lt;p&gt;There is one important rule. Spring only looks inside the package of this class, and below it.&lt;/p&gt;

&lt;p&gt;com.example.demo               &amp;lt;- DemoApplication is here&lt;br&gt;
├── controller/                  OK, Spring finds it&lt;br&gt;
├── service/                     OK, Spring finds it&lt;br&gt;
└── repository/                  OK, Spring finds it&lt;/p&gt;

&lt;p&gt;com.other.stuff                  NOT OK, Spring never looks here&lt;/p&gt;

&lt;p&gt;If you put a class outside this package, Spring ignores it and you get strange errors.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: The four layers
&lt;/h2&gt;

&lt;p&gt;To keep code clean, we split it into layers. Think of a restaurant:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faxz39sttftpxmvz8ubeq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faxz39sttftpxmvz8ubeq.png" alt="struct" width="800" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The waiter does not cook. The chef does not go to the storeroom himself. Each layer talks only to the next one.&lt;/p&gt;

&lt;p&gt;User  -&amp;gt;  Controller  -&amp;gt;  Service  -&amp;gt;  Repository  -&amp;gt;  Database&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F044i1wo9jyobrx0jycf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F044i1wo9jyobrx0jycf7.png" alt="folders" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your project, each layer is a package (a folder):&lt;/p&gt;

&lt;p&gt;com.example.demo&lt;br&gt;
├── controller/&lt;br&gt;
├── service/&lt;br&gt;
├── repository/&lt;br&gt;
└── entity/&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4: Follow one request
&lt;/h2&gt;

&lt;p&gt;Let's say a user opens this URL to get all customers:&lt;/p&gt;

&lt;p&gt;GET &lt;a href="http://localhost:8080/api/v1/customer/get-all-customers" rel="noopener noreferrer"&gt;http://localhost:8080/api/v1/customer/get-all-customers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is what happens, step by step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The controller receives the request. It calls the service.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@RequestMapping("/api/v1/customer")
public class CustomerController {

    private final CustomerService customerService;

    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @GetMapping("/get-all-customers")
    public List&amp;lt;Customer&amp;gt; getAll() {
        return customerService.getAllCustomers();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;The service does the work. It asks the repository for the data.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class CustomerService {

    private final CustomerRepository customerRepository;

    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public List&amp;lt;Customer&amp;gt; getAllCustomers() {
        return customerRepository.findAll();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;The repository reads the database. You write almost nothing:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Repository
public interface CustomerRepository extends JpaRepository&amp;lt;Customer, Integer&amp;gt; {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This empty interface already gives you findAll(), save(), findById() and deleteById(). Spring writes the code for you.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The answer goes back up. Database -&amp;gt; Repository -&amp;gt; Service -&amp;gt; Controller -&amp;gt; the user sees the customers as JSON.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notice the pattern: the controller has a service, and the service has a repository. That is how the files connect.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: Who creates these objects?
&lt;/h2&gt;

&lt;p&gt;Look again at the controller. It uses customerService, but we never wrote new CustomerService(). &lt;/p&gt;

&lt;p&gt;So where does it come from?&lt;/p&gt;

&lt;p&gt;Spring creates it for you. Here is how, in plain words:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; When the app starts, Spring looks for classes marked with &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;@RestController, @Service or @Repository.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It creates one object of each class. These objects are called beans.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a class needs another one in its constructor, Spring hands it over. This is called dependency injection.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of a manager who hires staff. The waiter does not hire the chef. The manager (Spring) gives the waiter a chef when he starts work.&lt;/p&gt;

&lt;p&gt;This is why those annotations matter. If you forget @Service, Spring never creates the chef, and your app fails to start with an error like No qualifying bean of type CustomerService.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 6: The settings file
&lt;/h2&gt;

&lt;p&gt;application.properties holds your settings. Here is a small example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=your_password
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You never write code to connect to the database. You only write these lines, and Spring Boot makes the connection for you. (Never upload your real password to GitHub.)&lt;/p&gt;

&lt;p&gt;Quick recap&lt;/p&gt;

&lt;p&gt;• pom.xml lists your libraries. application.properties holds your settings.&lt;br&gt;
• Your code lives in src/main/java, inside the package of the main class.&lt;br&gt;
• Four layers: Controller -&amp;gt; Service -&amp;gt; Repository -&amp;gt; Database. Each talks only to the next one.&lt;br&gt;
• Spring creates the objects and gives them to each other. You just write the constructor.&lt;/p&gt;

&lt;p&gt;What's next&lt;/p&gt;

&lt;p&gt;In Part 3 we will build a real REST API step by step and test it with Postman. If this post helped you, leave a comment and tell me what is still unclear. I will explain it better in the next post.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>backenddevelopment</category>
      <category>java</category>
    </item>
    <item>
      <title>Spring Boot for Beginners: Why Should We Choose It, and What Projects Is It Best For?</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Sat, 21 Nov 2026 15:42:32 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/spring-boot-for-beginners-why-should-we-choose-it-and-what-projects-is-it-best-for-1oh</link>
      <guid>https://dev.to/gihan_benaragama/spring-boot-for-beginners-why-should-we-choose-it-and-what-projects-is-it-best-for-1oh</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;1. Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When we start learning backend development, we come across many technologies and frameworks. One of the most popular technologies in the Java ecosystem is Spring Boot.&lt;/p&gt;

&lt;p&gt;But before learning how to create a Spring Boot application, we should understand a few important questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is Spring Boot?&lt;/li&gt;
&lt;li&gt;Why do developers choose Spring Boot?&lt;/li&gt;
&lt;li&gt;What problems does it solve?&lt;/li&gt;
&lt;li&gt;What types of projects can we build using it?&lt;/li&gt;
&lt;li&gt;Is Spring Boot suitable for beginners?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, let's understand these questions in a simple way.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. What Is Spring Boot?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Spring Boot is a Java-based framework built on top of the Spring Framework. Its main purpose is to make developing Spring applications easier, faster, and more convenient.&lt;/p&gt;

&lt;p&gt;The Spring Framework provides many powerful features for building applications, but configuring a Spring application manually can be time-consuming, especially when we are starting a new project.&lt;/p&gt;

&lt;p&gt;Spring Boot simplifies this process by providing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auto-configuration&lt;/li&gt;
&lt;li&gt;Starter dependencies&lt;/li&gt;
&lt;li&gt;Embedded servers&lt;/li&gt;
&lt;li&gt;Less manual configuration&lt;/li&gt;
&lt;li&gt;Production-ready features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This allows developers to focus more on writing application logic instead of spending too much time configuring the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. What Is the Difference Between Spring and Spring Boot?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Spring and Spring Boot are closely related, but they are not the same thing.&lt;/p&gt;

&lt;p&gt;Spring Framework provides the core functionality for building Java applications, such as Dependency Injection, Inversion of Control, Spring MVC, transaction management, and more.&lt;/p&gt;

&lt;p&gt;Spring Boot builds on top of the Spring Framework and simplifies the process of configuring and starting Spring applications.&lt;/p&gt;

&lt;p&gt;Think of it this way:&lt;/p&gt;

&lt;p&gt;Spring provides the tools and features to build an application, while Spring Boot makes it easier to use those tools by reducing configuration and providing sensible defaults.&lt;/p&gt;

&lt;p&gt;For example, in a traditional Spring application, we may need to configure many components manually. In Spring Boot, many of those configurations are automatically handled for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;4. Why Should We Choose Spring Boot?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;4.1 Less Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring Boot is that it reduces the amount of manual configuration required.&lt;/p&gt;

&lt;p&gt;In traditional Spring development, developers may need to configure different components individually.&lt;/p&gt;

&lt;p&gt;Spring Boot uses auto-configuration to automatically configure many components based on the dependencies available in our project.&lt;/p&gt;

&lt;p&gt;For example, when we add Spring Boot Web dependencies, it can automatically configure the components required to develop web applications.&lt;/p&gt;

&lt;p&gt;This saves time and makes development easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.2 Auto-Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring Boot examines the dependencies available in our project and automatically configures suitable components.&lt;/p&gt;

&lt;p&gt;For example, when we add Spring Data JPA and a database driver, Spring Boot can configure the required database-related components based on our configuration and dependencies.&lt;/p&gt;

&lt;p&gt;We don't need to manually configure every component from scratch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.3 Starter Dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring Boot provides starter dependencies that simplify dependency management.&lt;/p&gt;

&lt;p&gt;Instead of adding many individual libraries manually, we can add a starter dependency that includes the commonly required dependencies for a particular functionality.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;spring-boot-starter-webmvc – for developing web applications and REST APIs.&lt;/p&gt;

&lt;p&gt;spring-boot-starter-data-jpa – for working with databases using JPA.&lt;/p&gt;

&lt;p&gt;spring-boot-starter-validation – for validating data.&lt;/p&gt;

&lt;p&gt;spring-boot-starter-test – for testing applications.&lt;/p&gt;

&lt;p&gt;These dependencies help us get started quickly without manually searching for and adding every required library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.4 Embedded Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In traditional Java web application development, we may need to install and configure a web server separately and deploy our application into it.&lt;/p&gt;

&lt;p&gt;With Spring Boot, we can run our application directly.&lt;/p&gt;

&lt;p&gt;This makes application development and testing much easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.5 Easy Development of REST APIs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can easily create endpoints using annotations such as:&lt;/p&gt;

&lt;p&gt;@RestController&lt;br&gt;
@GetMapping&lt;br&gt;
@PostMapping&lt;br&gt;
@PutMapping&lt;br&gt;
@DeleteMapping&lt;br&gt;
@RequestBody&lt;br&gt;
@PathVariable&lt;br&gt;
@RequestParam&lt;/p&gt;

&lt;p&gt;These annotations help us handle HTTP requests and responses with less code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.6 Easy Database Integration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring Boot can easily integrate with different databases, such as:&lt;/p&gt;

&lt;p&gt;MySQL&lt;/p&gt;

&lt;p&gt;PostgreSQL&lt;/p&gt;

&lt;p&gt;Oracle&lt;/p&gt;

&lt;p&gt;Microsoft SQL Server&lt;/p&gt;

&lt;p&gt;Using Spring Data JPA, we can work with database records using Java objects and repositories.&lt;/p&gt;

&lt;p&gt;For example, instead of writing SQL queries for every basic database operation, we can use repository methods such as:&lt;/p&gt;

&lt;p&gt;userRepository.findAll();&lt;br&gt;
userRepository.findById(1);&lt;br&gt;
userRepository.save(user);&lt;br&gt;
userRepository.deleteById(1);&lt;/p&gt;

&lt;p&gt;This makes database operations easier and helps reduce repetitive code.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What Types of Projects Are Suitable for Spring Boot?
&lt;/h2&gt;

&lt;p&gt;Spring Boot is especially useful for backend development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.1 REST API Applications&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;p&gt;User management APIs&lt;br&gt;
Employee management systems&lt;br&gt;
Inventory management APIs&lt;/p&gt;

&lt;p&gt;These applications allow frontend applications to communicate with the backend through HTTP requests.&lt;/p&gt;

&lt;p&gt;For example, a React, Angular, or mobile application can communicate with a Spring Boot backend&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.2 Microservices&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring Boot makes it convenient to create standalone backend services that can communicate with other services.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. When Should We Not Choose Spring Boot?
&lt;/h2&gt;

&lt;p&gt;Spring Boot is powerful, but it doesn't mean it is the best choice for every project.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;A very simple static website does not need a Spring Boot backend.&lt;/p&gt;

&lt;p&gt;A small script or automation task may be easier with a simpler solution.&lt;/p&gt;

&lt;p&gt;If a project requires a specific ecosystem or technology, another framework may be more suitable.&lt;/p&gt;

&lt;p&gt;If you are building a frontend-only application, Spring Boot may not be necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Conclusion
&lt;/h2&gt;

&lt;p&gt;Spring Boot is one of the most useful frameworks for Java backend development.&lt;/p&gt;

&lt;p&gt;It simplifies the development process by reducing manual configuration, providing auto-configuration, offering starter dependencies, and supporting embedded servers.&lt;/p&gt;

&lt;p&gt;For beginners, Spring Boot is a great technology to learn after building a solid foundation in Java and Object-Oriented Programming.&lt;/p&gt;

&lt;p&gt;Learning Spring Boot is not just about learning annotations. It is about understanding how to design, build, and maintain real backend applications.&lt;/p&gt;

&lt;p&gt;This is the beginning of our Spring Boot journey. 🚀&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>springboot</category>
      <category>java</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>The 5 React hooks every beginner must know</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Tue, 16 Jun 2026 07:44:28 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/the-5-react-hooks-every-beginner-must-know-2kde</link>
      <guid>https://dev.to/gihan_benaragama/the-5-react-hooks-every-beginner-must-know-2kde</guid>
      <description>&lt;p&gt;&lt;strong&gt;A hook is just a special function in React that lets you "hook into" React features from inside a regular function component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this post&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useState — remembering things&lt;/li&gt;
&lt;li&gt;useEffect — reacting to changes&lt;/li&gt;
&lt;li&gt;useContext — sharing data everywhere&lt;/li&gt;
&lt;li&gt;useRef — grabbing DOM elements&lt;/li&gt;
&lt;li&gt;useMemo &amp;amp; useCallback — staying fast&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hooks were introduced in React 16.8 and completely changed how we write components. Instead of complex class-based components, we now write simple functions and "hook into" React features. Let's go through the five you'll use every single day.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. useState&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lets your component remember a value — and update the UI when that value changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it like a scoreboard. When a player scores, the number on the board updates automatically. useState is your scoreboard — it holds a value, and when it changes, React re-renders the component&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Import the useState Hook from React&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Define a functional component named Counter&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// useState(0):&lt;/span&gt;
  &lt;span class="c1"&gt;// - Creates a state variable called "count"&lt;/span&gt;
  &lt;span class="c1"&gt;// - Creates a function called "setCount" to update "count"&lt;/span&gt;
  &lt;span class="c1"&gt;// - Initializes count with the value 0&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// Return the JSX (UI) that will be rendered&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Display the current value of count */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* 
        When the button is clicked:
        1. Read the current value of count
        2. Add 1 to it
        3. Update the state using setCount()
        4. React re-renders the component with the new value
      */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Text shown on the button */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        Click me
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Export the component so it can be imported into other files&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Real-world uses&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Form inputs - Keep track of what the user is typing into an email or password field.&lt;/li&gt;
&lt;li&gt;Like button - Store the liked state and toggle it — exactly like a Twitter/X heart button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Key rule: Never modify state directly (e.g. count = count + 1). Always use the setter function (setCount) so React knows to re-render&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. useEffect — reacting to changes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runs code after your component renders — perfect for fetching data, setting up subscriptions, or talking to the outside world.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it like a notification trigger. When something happens (component loads, a value changes), a side effect kicks in — like fetching fresh data from an API.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Import the useState and useEffect Hooks from React&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Define a functional component named WeatherApp&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;WeatherApp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Create a state variable named "weather"&lt;/span&gt;
  &lt;span class="c1"&gt;// Initial value is null because no weather data has been loaded yet&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;weather&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setWeather&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// useEffect runs after the component renders&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Fetch weather data from the API&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.weather.com/today&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="c1"&gt;// Convert the response into JSON format&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

      &lt;span class="c1"&gt;// Store the fetched data in the weather state&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="c1"&gt;// Empty dependency array [] means:&lt;/span&gt;
          &lt;span class="c1"&gt;// Run this effect only once when the component first mounts&lt;/span&gt;

  &lt;span class="c1"&gt;// If weather is still null, show a loading message&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;weather&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Once weather data is available, display the temperature&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;It is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;weather&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;°C today&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Export the component so it can be used in other files&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;WeatherApp&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Avoid infinite loops: If you update state inside useEffect without a dependency array, it will keep running forever. Always add the [] or specific dependencies&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. useContext — sharing data everywhere&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lets any component access shared data — without passing props through every level of the tree&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Think of it like a WiFi network. You set up a router (the context provider) once, and any device in range (any child component) can connect and get the internet — without running a cable from device to device&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Create a Context object (shared data container)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ThemeContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// State to store the current theme&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTheme&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;// Provide theme and setTheme to all child components&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTheme&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Navbar&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Page&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Navbar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Consume (read) values from ThemeContext&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTheme&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;// Toggle between 'light' and 'dark' themes&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTheme&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      Toggle theme: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Real-world uses&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dark mode - Share the current theme across every component without passing it as a prop everywhere.&lt;/li&gt;
&lt;li&gt;Logged-in user - Once the user logs in, share their name and profile across the entire app.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;useRef — grabbing DOM elements&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Gives you a direct reference to a DOM element — or stores a value that doesn't trigger a re-render when it changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of it like a sticky note on a physical object. You stick a label on an input box, and later you can point to it directly — without going through React's re-render cycle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;SearchBar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create a ref object. Initially, current is null.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Access the actual &amp;lt;input&amp;gt; DOM element and place the cursor inside it&lt;/span&gt;
    &lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;focus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Attach the ref to the input DOM element */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Search..."&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* When clicked, call handleClick to focus the input */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Focus search&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Real-world uses&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auto-focus - Focus a login input the moment a modal opens — better UX without re-rendering.&lt;/li&gt;
&lt;li&gt;Store a timer ID - Save setTimeout's return value so you can cancel it later — without triggering a re-render.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;useState vs useRef:&lt;/strong&gt; Use useState when changing the value should update the UI. Use useRef when you just need to remember a value or grab a DOM element — no UI update needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. useMemo &amp;amp; useCallback — staying fast&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache expensive calculations or functions so React doesn't redo them on every render.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it like mental notes. If someone asks you "what's 1247 × 38?" you calculate it once and remember the answer. If they ask again with the same numbers — you just give the answer from memory instead of recalculating&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// State to store the user's search text&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// Memoize the filtered list and recalculate it&lt;/span&gt;
  &lt;span class="c1"&gt;// only when products or search changes&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filtered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Controlled input: value comes from state and updates state on typing */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Render only the filtered products */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filtered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Parent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Memoize the function and keep the same function reference&lt;/span&gt;
  &lt;span class="c1"&gt;// between renders unless dependencies change&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button clicked!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="c1"&gt;// [] = create the function once and reuse it on every render&lt;/span&gt;

  &lt;span class="c1"&gt;// Pass the memoized function to the child component&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExpensiveButton&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;useMemo — filtered / sorted lists&lt;br&gt;
Filtering 10,000 products every keypress is expensive. Cache the result until the data or search term changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useCallback — stable event handlers&lt;br&gt;
When passing a function to a child component wrapped in React.memo, keep the same reference to prevent unnecessary re-renders.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Don't over-use these. Wrapping everything in useMemo / useCallback adds its own cost. Only use them when you notice actual performance problems — measure first.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick reference summary&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useState - Store &amp;amp; update UI values&lt;/li&gt;
&lt;li&gt;useEffect - Side effects a fter render&lt;/li&gt;
&lt;li&gt;useContext - Share data app-wide&lt;/li&gt;
&lt;li&gt;useRef - Access DOM elements&lt;/li&gt;
&lt;li&gt;useMemo - Cache expensive values&lt;/li&gt;
&lt;li&gt;useCallback - Cache functions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Master these five and you'll be able to build almost any React app. The rest of the hooks exist for more specific or advanced scenarios — but these are the foundation. Happy coding! 🚀&lt;/p&gt;

</description>
      <category>reacthooks</category>
      <category>reactforbeginners</category>
      <category>usestate</category>
      <category>useeffect</category>
    </item>
    <item>
      <title>Understanding Queue in Data Structures with Real-Life Examples</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Tue, 09 Jun 2026 06:06:30 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/understanding-queue-in-data-structures-with-real-life-examples-3pnl</link>
      <guid>https://dev.to/gihan_benaragama/understanding-queue-in-data-structures-with-real-life-examples-3pnl</guid>
      <description>&lt;p&gt;Have you ever stood in a line at a supermarket? The person who arrived first gets served first, and newcomers join at the back. That simple idea is exactly what a Queue is in computer science. Let's break it down step by step.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is a Queue?
&lt;/h3&gt;

&lt;p&gt;A queue is a linear data structure that follows a simple rule:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First In, First Out (FIFO)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This means the first element added to the queue will be the first one to be removed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Think of it like a real-world queue at a ticket counter. The first person who joins the line will be served first.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb719zero9zy8qv1ygurz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb719zero9zy8qv1ygurz.png" alt="ticket counter" width="800" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does a Queue Work?
&lt;/h2&gt;

&lt;p&gt;A queue has two ends:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Front — where items are removed (think: the front of the checkout line)&lt;/li&gt;
&lt;li&gt;Rear — where items are added (think: the back of the line)&lt;/li&gt;
&lt;li&gt;You can only add to the rear and remove from the front. You cannot touch items in the middle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Three Basic Operations&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Insert (Enqueue)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Adding a new item to the rear of the queue.&lt;br&gt;
Example: The queue currently holds [15, 27, 39, 23]. When we insert 45, it goes to the rear:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Before: Front → [15, 27, 39, 23] ← Rear&lt;br&gt;
After:  Front → [15, 27, 39, 23, 45] ← Rear&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssj2q98utf632jinsvqn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssj2q98utf632jinsvqn.png" alt="insert" width="566" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove (Dequeue)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Taking an item out from the front of the queue.&lt;br&gt;
Example: From [15, 27, 39, 23], removing takes 15 (the front item):&lt;/p&gt;

&lt;p&gt;Before: Front → [15, 27, 39, 23] ← Rear&lt;br&gt;
After:  Front → [27, 39, 23] ← Rear&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftip89vfligpu9wsjrv7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftip89vfligpu9wsjrv7b.png" alt="remove" width="551" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PeekFront&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Reading the value at the front without removing it. You're just looking, not taking.&lt;/p&gt;

&lt;p&gt;Example: PeekFront on [15, 27, 39, 23] returns 15, and the queue stays the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fon43kzvnrqa12ak3c1ka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fon43kzvnrqa12ak3c1ka.png" alt="peek" width="552" height="559"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where Are Queues Used in Real Life (in Computers)?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Printer queue — When you send multiple documents to a printer, they line up and print one by one in the order they were sent.&lt;/li&gt;
&lt;li&gt;Keyboard input — When you type quickly, each keystroke is stored in a queue so none are lost.&lt;/li&gt;
&lt;li&gt;Pipelines — Data flowing through processes in an operating system uses queues to stay ordered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Do You Actually Need a Queue in a Project?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tasks pile up faster than they're processed&lt;/strong&gt;&lt;br&gt;
A user uploads a video. Encoding takes 2 minutes. You can't make them wait — so you drop the task in a queue and handle it in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You don't want to block the user&lt;/strong&gt;&lt;br&gt;
Sending a welcome email, generating a PDF, resizing a photo — these are slow. Push them to a queue. The user gets an instant response, the work happens quietly behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sending notifications at scale&lt;/strong&gt;&lt;br&gt;
Emails, SMS, push notifications — all go through a queue. You collect them, then deliver them without slowing down your main app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementing a Queue in Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's look at how we actually build a queue in code. We'll use a Java class called QueueX.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting Up the Queue
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;QueueX&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// maximum number of items the queue can hold&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;queArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the actual array storing items&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// index of the front item&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// index of the rear item&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;nItems&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// how many items are currently in the queue&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;QueueX&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// constructor: called when creating a new queue&lt;/span&gt;
        &lt;span class="n"&gt;maxSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;queArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;front&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;nItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Think of this as setting up an empty line — you reserve space for a certain number of people (maxSize), and right now nobody's in the queue (nItems = 0).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2ne926jt2s1esu24aao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2ne926jt2s1esu24aao.png" alt="setting up" width="457" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insert Method
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Queue is full"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;queArray&lt;/span&gt;&lt;span class="o"&gt;[++&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// move rear forward, then place the new item&lt;/span&gt;
        &lt;span class="n"&gt;nItems&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Before inserting, we check if the queue is already full - if (rear == maxSize - 1)&lt;/p&gt;

&lt;p&gt;If not, we move the rear pointer forward by one [++rear] and place the new item there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrbh6e5n12ltehzknd66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrbh6e5n12ltehzknd66.png" alt="insert" width="478" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove Method
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nItems&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Queue is empty"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// signal that queue was empty&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;nItems&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;queArray&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="o"&gt;++];&lt;/span&gt;  &lt;span class="c1"&gt;// return the front item, then move front forward&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We check if the queue is empty before removing - if (nItems == 0)&lt;br&gt;
If there are items, we return the one at front and move the front pointer forward.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1h7gqhp5bk61c161bdap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1h7gqhp5bk61c161bdap.png" alt="delete" width="746" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PeekFront Method
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;peekFront&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nItems&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Queue is empty"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;queArray&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;  &lt;span class="c1"&gt;// look at front item WITHOUT moving the pointer&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Notice the difference from remove() — here we do NOT do front++. We just look.&lt;/p&gt;

&lt;p&gt;Peek - We just print current front value&lt;/p&gt;
&lt;h2&gt;
  
  
  A Problem: The Queue Gets Stuck!
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Think You have a queue with 7 slots (index 0 to 6):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;[67, 12, 22, 55, 34, 70, 60]  ← full&lt;/p&gt;

&lt;p&gt;Front=0, Rear=6&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You remove 3 items. Now:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;[_, _, _, 55, 34, 70, 60]&lt;br&gt;
Front=3, Rear=6&lt;/p&gt;

&lt;p&gt;There are 3 empty slots at the beginning, but rear is already at the last index (6). If you try to insert a new item, the code thinks the queue is full — even though it's not!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmp6ug3xloapx79hnd36o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmp6ug3xloapx79hnd36o.png" alt="problem" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Solution: Circular Queue
&lt;/h2&gt;

&lt;p&gt;A circular queue (also called a ring buffer) fixes this problem by wrapping the rear pointer back to the beginning when it reaches the end.&lt;/p&gt;

&lt;p&gt;Think of it as a circular track — when you reach the end, you loop back to the start.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6920vxgwapncxsv5fzzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6920vxgwapncxsv5fzzh.png" alt="solution" width="799" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Insert Changes for a Circular Queue&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nItems&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Queue is full"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;rear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// wrap around to the beginning!&lt;/span&gt;
        &lt;span class="n"&gt;queArray&lt;/span&gt;&lt;span class="o"&gt;[++&lt;/span&gt;&lt;span class="n"&gt;rear&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;nItems&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How Remove Changes for a Circular Queue&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nItems&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Queue is empty"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queArray&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="o"&gt;++];&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;front&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// wrap front around too!&lt;/span&gt;
        &lt;span class="n"&gt;nItems&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now both front and rear wrap around, so we never waste those empty slots at the beginning!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Putting It All Together: A Full Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;QueueApp&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;QueueX&lt;/span&gt; &lt;span class="n"&gt;theQueue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;QueueX&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// create a queue that holds 10 items&lt;/span&gt;

        &lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;     &lt;span class="c1"&gt;// keep removing until empty&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;theQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:10 25 55 65 85&lt;/p&gt;

&lt;p&gt;The items come out in exactly the order they went in — that's FIFO in action!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A queue is like a real-world line — first come, first served.&lt;/li&gt;
&lt;li&gt;You add at the rear and remove at the front.&lt;/li&gt;
&lt;li&gt;You can only peek at the front item, not anything in the middle.&lt;/li&gt;
&lt;li&gt;A basic linear queue can run into a "false full" problem.&lt;/li&gt;
&lt;li&gt;A circular queue solves this by wrapping around the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it! Queues are one of the most practical data structures in computing, and now you know exactly how they work — from the idea all the way down to the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What if the person at the back of the line actually deserves to go first? That's exactly what a Priority Queue solves. See you in the next one!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>queue</category>
      <category>softwaredevelopment</category>
      <category>circulerqueue</category>
    </item>
    <item>
      <title>Stacks — The Simple Data Structure Every Developer Should Know</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Thu, 04 Jun 2026 13:52:16 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/stacks-the-simple-data-structure-every-developer-should-know-24ia</link>
      <guid>https://dev.to/gihan_benaragama/stacks-the-simple-data-structure-every-developer-should-know-24ia</guid>
      <description>&lt;h2&gt;
  
  
  What Is a Stack?
&lt;/h2&gt;

&lt;p&gt;A Stack is a linear data structure that follows the &lt;strong&gt;Last In, First Out (LIFO)&lt;/strong&gt; principle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LIFO&lt;/strong&gt; last item added to the stack is the first item removed from it.&lt;/p&gt;

&lt;p&gt;Think about a stack of plates in a kitchen. When you place a new plate on top, it becomes the first plate you remove later. You cannot easily take a plate from the middle or bottom without removing the plates above it first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Stack (the pile of plates)&lt;/strong&gt;&lt;br&gt;
You start with 4 plates already stacked, bottom to top:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Plate 1 — went in first (bottom)&lt;/li&gt;
&lt;li&gt;Plate 2&lt;/li&gt;
&lt;li&gt;Plate 3&lt;/li&gt;
&lt;li&gt;Plate 4 — went in last (top)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foht559xciue5qdhlmdpc.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foht559xciue5qdhlmdpc.jpeg" alt="Plate exsample" width="660" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push — adding a plate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A new blue plate is about to be placed on top. The arrow points downward, meaning it lands on the very top of the pile. You can never add a plate to the middle or bottom — only the top.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pop — removing a plate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Plate 4 is flying off upward (shown faded on the right). The arrow points upward, meaning it's being removed from the top. Again — you can only take from the top, not the middle or bottom.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The golden rule — LIFO&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The label at the bottom says it all:&lt;/p&gt;

&lt;p&gt;Last In, First Out&lt;/p&gt;

&lt;p&gt;Plate 4 was the last one added → it's the first one removed.&lt;br&gt;
Plate 1 was the first one added → it's the last one removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application of Stacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;• String Reverse&lt;br&gt;
• Page visited history in Web browser.&lt;br&gt;
• Undo sequence of text editor.&lt;br&gt;
• Recursive function calling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚙️ Three Core Operations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A stack has just three main things it can do:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. 🔼 Push — Add an item to the top&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stack before:    [ 27, 14, 3, 92, 64 ]  ← Top is 27&lt;br&gt;
Push(49)&lt;br&gt;
Stack after:     [ 49, 27, 14, 3, 92, 64 ]  ← Top is now 49&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fos9wh4yrkr6kga5fcxfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fos9wh4yrkr6kga5fcxfl.png" alt="push" width="507" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. 🔽 Pop — Remove the item from the top&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stack before:    [ 49, 27, 14, 3, 92, 64 ]  ← Top is 49&lt;br&gt;
Pop()  → returns 49&lt;br&gt;
Stack after:     [ 27, 14, 3, 92, 64 ]  ← Top is now 27&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flkmsgac9fmbxfbwkyrmb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flkmsgac9fmbxfbwkyrmb.png" alt="pop" width="502" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;👀 Peek — Look at the top item WITHOUT removing it
Stack:    [ 49, 27, 14, 3, 92, 64 ]
Peek()  → returns 49
Stack:    [ 49, 27, 14, 3, 92, 64 ]  ← Nothing changed!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe227yzffv4y9z0pt1jst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe227yzffv4y9z0pt1jst.png" alt="peek" width="505" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💻 Let's Build a Stack in Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a simple Stack class built using an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StackX {
    private int maxSize;        // maximum number of items
    private double[] stackArray; // the array holding items
    private int top;            // index of the top item

    // Constructor — creates an empty stack
    public StackX(int size) {
        maxSize = size;
        stackArray = new double[maxSize];
        top = -1; // -1 means the stack is empty
    }

    // Push — add item to top
    public void push(double item) {
        if (top == maxSize - 1) {
            System.out.println("Stack is full! Can't push.");
        } else {
            stackArray[++top] = item; // increment top, then insert
        }
    }

    // Pop — remove item from top
    public double pop() {
        if (top == -1) {
            System.out.println("Stack is empty!");
            return -99; // error value
        } else {
            return stackArray[top--]; // return item, then decrement top
        }
    }

    // Peek — look at top item without removing
    public double peek() {
        if (top == -1) {
            System.out.println("Stack is empty!");
            return -99;
        } else {
            return stackArray[top]; // just return, don't change top
        }
    }

    // isEmpty — returns true if stack has no items
    public boolean isEmpty() {
        return (top == -1);
    }

    // isFull — returns true if stack has no room left
    public boolean isFull() {
        return (top == maxSize - 1);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🧪 Using the Stack&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StackApp {
    public static void main(String[] args) {

        StackX myStack = new StackX(10); // stack with max size 10

        // Push items onto the stack
        myStack.push(30);
        myStack.push(80);
        myStack.push(100);
        myStack.push(25);

        // Pop all items and print them
        while (!myStack.isEmpty()) {
            double val = myStack.pop();
            System.out.print(val + " ");
        }
        // Output: 25.0 100.0 80.0 30.0
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🧩 Let's Trace Through an Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsnz5nocgtdge8ipbrsq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsnz5nocgtdge8ipbrsq.jpeg" alt="exsample" width="800" height="714"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚀 Where Are Stacks Used in Real Programming?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stacks are everywhere, even if you can't see them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function calls — &lt;em&gt;Every time your code calls a function, the computer pushes the return address and arguments onto a stack. When the function finishes, they're popped off.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Recursion — &lt;em&gt;Recursive functions use the call stack behind the scenes. Expression evaluation — Compilers use stacks to evaluate math expressions&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Backtracking algorithms — &lt;em&gt;Think maze solvers or game AI.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;📝Here is quick Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stack - &lt;em&gt;A data structure where you can only access the top item&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;LIFO - &lt;em&gt;Last In, First Out — last added is first removed&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Push - &lt;em&gt;Add an item to the top&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Pop - &lt;em&gt;Remove the top item&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Peek - &lt;em&gt;Read the top item without removing it&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;isEmpty() - &lt;em&gt;Check if there's nothing in the stack&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;isFull() - &lt;em&gt;Check if the stack has no room left&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this helped you understand stacks, drop a ❤️ and follow along — next up we'll explore Queues, where the rules are completely different!&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>datastructures</category>
      <category>stack</category>
    </item>
    <item>
      <title>Linked Lists — The Chain You'll See in Every Interview</title>
      <dc:creator>Gihan Benaragama</dc:creator>
      <pubDate>Fri, 29 May 2026 12:45:00 +0000</pubDate>
      <link>https://dev.to/gihan_benaragama/linked-lists-the-chain-youll-see-in-every-interview-12p6</link>
      <guid>https://dev.to/gihan_benaragama/linked-lists-the-chain-youll-see-in-every-interview-12p6</guid>
      <description>&lt;h2&gt;
  
  
  1. What is a linked list?
&lt;/h2&gt;

&lt;p&gt;A linked list is a chain of nodes, where each node holds a value and the address of the next node.&lt;br&gt;
think of a linked list like a train 🚂&lt;/p&gt;

&lt;p&gt;Each train compartment is connected to the next compartment.&lt;/p&gt;

&lt;p&gt;Every compartment (node) contains:&lt;/p&gt;

&lt;p&gt;Passengers or goods → &lt;strong&gt;the data&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;A connector to the next compartment →&lt;/em&gt; &lt;strong&gt;the next pointer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjun2r8gssvgxh25c32tr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjun2r8gssvgxh25c32tr.png" alt="Linked list nodes connected using pointers" width="800" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last compartment is not connected to anything, so it points to null.&lt;/p&gt;

&lt;p&gt;Just like you move through train compartments one by one, a linked list is also traversed node by node from the beginning to the end.&lt;/p&gt;
&lt;h2&gt;
  
  
  Array vs linked list — what's the difference?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Array&lt;/strong&gt;– each item occupies a particular position and can be directly accessed using an index number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked list&lt;/strong&gt; – need to follow along the chain of element to find a particular element. A data item cannot be accessed  directly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why do we even need linked lists?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You already know arrays. So why invent something new?&lt;/li&gt;
&lt;li&gt;The problem with arrays is that their size is fixed. When you create an array, you must say upfront how big it is. And inserting or deleting items in the middle means shifting everything else — which is slow.&lt;/li&gt;
&lt;li&gt;A linked list solves this. It grows and shrinks freely, and inserting or deleting is fast because you just change a pointer, not shift everything
.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Real world examples you already use:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;🌐 Browser back/forward — each page you visit is a node. Clicking back just follows the previous pointer&lt;br&gt;
🎵 Music player — each song links to the next and previous song&lt;br&gt;
🖼️ Image viewer — previous and next buttons work because images are linked in a chain&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Linked lists are probably the second most commonly used general purpose storage structures after arrays&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Operations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Find - Find a link with a specified key value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2pfhxiiwnxl4rdhi8hgh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2pfhxiiwnxl4rdhi8hgh.png" alt="Linked list find" width="800" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insert - Insert links anywhere in the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuad4urgzqpn9yb06rqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuad4urgzqpn9yb06rqw.png" alt="Linked list insert" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delete - Delete a link with the specified value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jcyh580b6efptgmrywl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jcyh580b6efptgmrywl.png" alt="Linked list delete" width="798" height="210"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Full Java implementation
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;iData&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Link&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;iData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;next&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displayLink&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkList&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// reference to the first node&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;LinkList&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// empty list at the start&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Returns true if the list has no nodes&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Print every node in the list&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displayList&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayLink&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// move to the next node&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Add a new node at the very beginning&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;insertFirst&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="n"&gt;newLink&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;newLink&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// new node points to old first&lt;/span&gt;
        &lt;span class="n"&gt;first&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newLink&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// new node becomes the first&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Remove and return the first node&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="nf"&gt;deleteFirst&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// save reference to return&lt;/span&gt;
        &lt;span class="n"&gt;first&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// second node becomes first&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;create a list, display it, then delete everything:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;myList&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;LinkList&lt;/span&gt; &lt;span class="n"&gt;theList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LinkList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// create empty list&lt;/span&gt;

        &lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insertFirst&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// insert four items&lt;/span&gt;
        &lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insertFirst&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;89&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// note: each goes to the FRONT&lt;/span&gt;
        &lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insertFirst&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// so the order will be reversed&lt;/span&gt;
        &lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insertFirst&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// prints: 55, 12, 89, 23&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="n"&gt;aLink&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;theList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deleteFirst&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Deleted: "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;aLink&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;displayLink&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Here is Bonus tip
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Two advanced types: double-ended and doubly linked&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzoighull4pxcb28lgqoo.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzoighull4pxcb28lgqoo.jpeg" alt="double ended and doubly linked" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick summary — what to remember
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A linked list is a chain of nodes. Each node holds data and a reference to the next node.&lt;/li&gt;
&lt;li&gt;first is the head. The last node's next is null.&lt;/li&gt;
&lt;li&gt;You cannot jump to index 5 directly — you must walk from first.&lt;/li&gt;
&lt;li&gt;Inserting at the front (insertFirst) is very fast — O(1).&lt;/li&gt;
&lt;li&gt;Searching takes O(n) — you check each node one by one.&lt;/li&gt;
&lt;li&gt;A doubly linked list lets you go both forward and backward.&lt;/li&gt;
&lt;li&gt;Linked lists are the foundation for stacks, queues, and graphs.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>linkedlist</category>
      <category>datastructures</category>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
