<?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: Hariharan S J</title>
    <description>The latest articles on DEV Community by Hariharan S J (@hariharan_sj_2003).</description>
    <link>https://dev.to/hariharan_sj_2003</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3695479%2Fbaea2418-be77-4c08-b2c8-6ab0e5325e7a.png</url>
      <title>DEV Community: Hariharan S J</title>
      <link>https://dev.to/hariharan_sj_2003</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hariharan_sj_2003"/>
    <language>en</language>
    <item>
      <title>What Really Happens When You Create an Object in Java? Understanding Constructors and Packages</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Wed, 03 Jun 2026 13:21:54 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/what-really-happens-when-you-create-an-object-in-java-understanding-constructors-and-packages-38ae</link>
      <guid>https://dev.to/hariharan_sj_2003/what-really-happens-when-you-create-an-object-in-java-understanding-constructors-and-packages-38ae</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever created an object in Java and wondered what happens behind the scenes the moment that object comes to life?&lt;/p&gt;

&lt;p&gt;When you write something as simple as:&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="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java doesn't just magically create an object. It first invokes a special mechanism called a constructor, which prepares the object and gets it ready for use. Whether you're assigning default values or passing custom data during object creation, constructors play a crucial role in every Java application.&lt;/p&gt;

&lt;p&gt;But creating objects is only part of the story.&lt;/p&gt;

&lt;p&gt;As applications grow from a few classes to hundreds or even thousands, managing code becomes a challenge. Imagine trying to find a single class in a project where every file is stored in the same folder. That's where packages come in. Packages help developers organize code, avoid naming conflicts, and build scalable applications that are easier to maintain.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore the different types of constructors in Java, understand how they work, and learn how packages help structure real-world Java projects. By the end, you'll have a solid understanding of two fundamental concepts that every Java developer uses daily.&lt;/p&gt;

&lt;p&gt;In my previous blog i had discussed what is constructor? , why constructor is needed in this blog we are going to discuss about the Types of Constructor and Packages&lt;/p&gt;

&lt;h2&gt;
  
  
  2.Types of Constructors in Java
&lt;/h2&gt;

&lt;p&gt;Java mainly provides three types of constructors:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Default Constructor&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No-Argument Constructor&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parameterized Constructor&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. Default Constructor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A default constructor is automatically provided by the Java compiler when no constructor is explicitly written.&lt;/p&gt;

&lt;p&gt;If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program.&lt;/p&gt;

&lt;p&gt;This constructor is called the default constructor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;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;Student&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&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;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&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;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&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;Output&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;0
null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since no constructor was defined, Java automatically generated a default constructor and assigned default values to instance variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. No-Argument Constructor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A no-argument constructor is created by the programmer and does not accept any parameters&lt;/p&gt;

&lt;p&gt;Similar to methods, a Java constructor may or may not have any parameters (arguments).&lt;/p&gt;

&lt;p&gt;If a constructor does not accept any parameters, it is known as a no-argument constructor. For example,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;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;Student&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;()&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="mi"&gt;101&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hari"&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="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;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&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;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&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;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&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;Output&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;101
Hari
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Use It?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A no-argument constructor allows us to initialize objects with custom default values instead of Java's built-in defaults.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Parameterized Constructor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A parameterized constructor accepts values during object creation.&lt;/p&gt;

&lt;p&gt;A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructors with parameters).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;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;Student&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Student&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&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="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;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hari"&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;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&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;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&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;Output&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;101
Hari
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Use It?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Parameterized constructors allow each object to have different values during creation.&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="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hari"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;s2&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;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;s3&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;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"David"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each object stores unique data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constructor Overloading&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java allows multiple constructors within the same class as long as their parameter lists are different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;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;Student&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Student&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;"No Argument Constructor"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;"Parameterized Constructor"&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="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;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;s2&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;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hari"&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;Output&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;No Argument Constructor
Parameterized Constructor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This feature is known as Constructor Overloading.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.What is a Package in Java?
&lt;/h2&gt;

&lt;p&gt;A package is a collection of related classes, interfaces, and sub-packages.&lt;/p&gt;

&lt;p&gt;Think of a package as a folder in your computer.&lt;/p&gt;

&lt;p&gt;Just as folders help organize files, packages help organize Java classes.&lt;/p&gt;

&lt;p&gt;A package in Java is a mechanism to group related classes, interfaces, and sub-packages into a single unit. Packages help organize large applications, avoid naming conflicts, provide access protection, and make code modular and maintainable.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoiding name conflicts (two classes with the same name can exist in different packages)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Providing access control using public, protected, and default access&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reusability: packaged code can be imported and used anywhere&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encouraging modular programming&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4.Why Do We Need Packages?
&lt;/h2&gt;

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

&lt;p&gt;&lt;strong&gt;1. Better Code Organization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Student.java
Employee.java
Customer.java
Order.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com.company.student
com.company.employee
com.company.order
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything becomes easier to manage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Prevent Naming Conflicts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two developers might create classes with the same name.&lt;/p&gt;

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

&lt;p&gt;student.Student&lt;br&gt;
employee.Student&lt;/p&gt;

&lt;p&gt;Packages help Java identify which class is being used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Access Protection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Packages help control visibility of classes and members using access modifiers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Packages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java provides two types of packages:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Built-in Packages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are packages provided by Java itself.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java.lang(TBD)
java.util(TBD)
java.io(TBD)
java.sql(TBD)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. User-Defined Packages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Developers can create their own packages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Package&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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;college&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&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;display&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;"Inside Student Class"&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;Using the Package&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;college.Student&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;class&lt;/span&gt; &lt;span class="nc"&gt;Main&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;Student&lt;/span&gt; &lt;span class="n"&gt;s&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;Student&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="na"&gt;display&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;Package Naming Convention&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java package names usually follow reverse domain naming.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com.google
com.microsoft
com.amazon
org.apache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps avoid naming conflicts in large applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  5.Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Constructors and packages may seem like small Java concepts at first, but they play a huge role in real-world application development.&lt;/p&gt;

&lt;p&gt;Constructors ensure that objects are properly initialized when created, while packages help organize code into meaningful structures.&lt;/p&gt;

&lt;p&gt;As your Java projects become larger and more complex, understanding these two concepts will help you write cleaner, more maintainable, and professional-quality code.&lt;/p&gt;

&lt;p&gt;Master constructors to control object creation. Master packages to control project organization. Together, they form a strong foundation for becoming an effective Java developer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://www.geeksforgeeks.org/java/packages-in-java/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/java/packages-in-java/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://www.programiz.com/java-programming/constructors" rel="noopener noreferrer"&gt;https://www.programiz.com/java-programming/constructors&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
      <category>learning</category>
    </item>
    <item>
      <title>One While Loop, Seven Patterns, Endless Learning</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Wed, 03 Jun 2026 10:06:11 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/one-while-loop-seven-patterns-endless-learning-2acj</link>
      <guid>https://dev.to/hariharan_sj_2003/one-while-loop-seven-patterns-endless-learning-2acj</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever noticed that most beginners can write a while loop, but struggle when asked to create a simple number pattern?&lt;/p&gt;

&lt;p&gt;The problem isn't the syntax.&lt;/p&gt;

&lt;p&gt;It's understanding how numbers change from one iteration to the next.&lt;/p&gt;

&lt;p&gt;A sequence like 1 2 3 4 5 may look different from 1 3 5 7 9 or 15 12 9 6 3, but behind the scenes, they all follow the same fundamental principle: start with a value, repeat a process, and update that value in a predictable way.&lt;/p&gt;

&lt;p&gt;This is exactly why pattern programs are so popular among programmers. They don't just teach loops—they train your brain to think logically, identify sequences, and understand how a program evolves step by step.&lt;/p&gt;

&lt;p&gt;In this article, we'll use Python's while loop to build a variety of number patterns, from simple counting sequences to reverse and alternating patterns. More importantly, we'll focus on the logic behind each pattern so that by the end, you won't just know how to print these patterns—you'll know how to create your own.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.Why Learn Patterns?
&lt;/h2&gt;

&lt;p&gt;Many beginners try to memorize pattern programs.&lt;/p&gt;

&lt;p&gt;That's the wrong approach.&lt;/p&gt;

&lt;p&gt;Instead, focus on understanding:&lt;/p&gt;

&lt;p&gt;What is changing in each iteration?&lt;/p&gt;

&lt;p&gt;Once you understand that, any pattern becomes easy to build.&lt;/p&gt;

&lt;p&gt;Let's look at some examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 1: Repeating the Same Number
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;1 1 1 1 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The loop runs 5 times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We always print 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The variable no is only used to count the iterations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pattern 2: Counting Numbers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;1 2 3 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We start from 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After every iteration, we increase the value by 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The loop stops when the value becomes greater than 5.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pattern 3: Odd Numbers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;1 3 5 7 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Odd numbers have a difference of 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of increasing by 1, we increase by 2.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pattern 4: Multiples of 3
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;3 6 9 12 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We start from 3.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every next value is 3 more than the previous value.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pattern 5: Reverse Multiples of 3
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;15 12 9 6 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We start from the largest value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of incrementing, we decrement by 3.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pattern 6: Reverse Even Numbers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;10 8 6 4 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We begin with 10.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every iteration subtracts 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This produces even numbers in reverse order.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pattern 7: Reverse Odd Numbers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Output&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;9 7 5 3 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We start from the largest odd number.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We subtract 2 in every iteration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The sequence continues until 1.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Hidden Pattern Behind All Patterns
&lt;/h2&gt;

&lt;p&gt;If you look carefully, every program follows the same structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;starting_value&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;update_value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only things that change are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Starting value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Condition&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increment or decrement&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;Once you understand these three components, creating new patterns becomes much easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Pattern programs are not about printing numbers on the screen. They are exercises that train your brain to recognize sequences, understand loop behavior, and think logically.&lt;/p&gt;

&lt;p&gt;The moment you stop memorizing patterns and start identifying what changes in each iteration, you'll be able to create your own patterns confidently.&lt;/p&gt;

&lt;p&gt;Learn the logic, not the pattern. That's where real programming begins.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
      <category>learning</category>
    </item>
    <item>
      <title>Java Constructors Demystified: What Happens Before Your Object Exists?</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Tue, 02 Jun 2026 14:50:36 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/java-constructors-demystified-what-happens-before-your-object-exists-5h9c</link>
      <guid>https://dev.to/hariharan_sj_2003/java-constructors-demystified-what-happens-before-your-object-exists-5h9c</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered what actually happens when you write:&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="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Noodles"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We all know that an object gets created. But what happens behind the scenes? Where does the memory come from? When are the values assigned? And why does Java automatically call a constructor without us explicitly invoking it?&lt;/p&gt;

&lt;p&gt;When I first learned constructors, I thought they were just special methods used to initialize objects. But after experimenting with object creation, default values, the this keyword, and constructor parameters, I discovered that constructors are much more than that—they are the starting point of an object's lifecycle.&lt;/p&gt;

&lt;p&gt;In this blog, we'll go beyond the textbook definition of constructors. We'll explore what really happens when an object is created, why this is important, how Java assigns default values, and some beginner mistakes that can silently break your code.&lt;/p&gt;

&lt;p&gt;By the end of this article, you'll not only know what a constructor is, but also why it behaves the way it does behind the scenes.&lt;/p&gt;

&lt;p&gt;Let's create our first object and uncover the magic that happens the moment we use the new keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.What is a Constructor?
&lt;/h2&gt;

&lt;p&gt;A constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.&lt;/p&gt;

&lt;p&gt;A constructor in Java is a special member that is called when an object is created. It initializes the new object’s state. It is used to set default or user-defined values for the object's attributes&lt;/p&gt;

&lt;p&gt;A constructor in Java is a special method that is used to initialize objects.&lt;/p&gt;

&lt;p&gt;The constructor is called when an object of a class is created.&lt;/p&gt;

&lt;p&gt;It can be used to set initial values for object attributes:&lt;/p&gt;

&lt;p&gt;A constructor is a special block of code that runs automatically whenever an object is created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&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;SuperMarket&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;SuperMarket&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;"Constructor Called"&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;Creating an Object&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="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product&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;SuperMarket&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;Output&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;Constructor Called
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The moment the object is created using the new keyword, Java automatically invokes the constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Rules of a Constructor
&lt;/h2&gt;

&lt;p&gt;A constructor has a few important rules:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Constructor name must be the same as the class name&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;SuperMarket&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;SuperMarket&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;&lt;strong&gt;2. Constructors do not have a return type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Correct&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="nc"&gt;SuperMarket&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;Incorrect&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SuperMarket&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;The second example is not a constructor. It is a normal method.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.Why Do We Need Constructors?
&lt;/h2&gt;

&lt;p&gt;Imagine creating an object and manually assigning values every time.&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="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product1&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;product1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Noodles"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;product1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product2&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;product2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Shampoo"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;product2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but it becomes repetitive.&lt;/p&gt;

&lt;p&gt;Instead, constructors allow us to initialize object-specific values immediately during object creation.&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;SuperMarket&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&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 we can simply write:&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="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product1&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Noodles"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product2&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Shampoo"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cleaner, safer, and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  5.Why Is the Constructor Called Twice?
&lt;/h2&gt;

&lt;p&gt;Consider this code:&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="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product1&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"abc"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="n"&gt;product2&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;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"xyz"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Constructor:&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="nc"&gt;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&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;"Are you constructor?"&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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Are you constructor?
Are you constructor?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Many beginners get confused here.&lt;/p&gt;

&lt;p&gt;The reason is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;One object = One constructor call&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Two objects = Two constructor calls&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every time you use the new keyword, Java creates a new object and invokes the constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  6.The Mystery of this
&lt;/h2&gt;

&lt;p&gt;Consider the following code:&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;SuperMarket&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&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;Here:&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;refers to the instance variable.&lt;/p&gt;

&lt;p&gt;While:&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;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;refers to the constructor parameter.&lt;/p&gt;

&lt;p&gt;So:&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Means:&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;instanceVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7.What Happens If We Remove this?
&lt;/h2&gt;

&lt;p&gt;Suppose we write:&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="nc"&gt;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&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;At first glance, it looks correct.&lt;/p&gt;

&lt;p&gt;But Java interprets it as:&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;parameter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;parameter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The instance variables never receive the values.&lt;/p&gt;

&lt;h2&gt;
  
  
  8.Then Why Do We See null and 0?
&lt;/h2&gt;

&lt;p&gt;Let's look at the 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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SuperMarket&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&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;Even though we never assigned values, Java still prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;null
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because Java automatically assigns default values to instance variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  9.What Actually Happens During Object Creation?
&lt;/h2&gt;

&lt;p&gt;When Java executes:&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Noodles"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JVM follows these steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Memory is allocated for the object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Default values are assigned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name = null
price = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Field initializers are executed (if any).&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Python"&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;Step 4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Constructor executes.&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="nc"&gt;SuperMarket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&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;Final flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object Creation
       ↓
Default Values
       ↓
Field Initializers
       ↓
Constructor Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This order is extremely important for understanding how Java objects are initialized.&lt;/p&gt;

&lt;h2&gt;
  
  
  10.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;A constructor is not just a special method—it is the starting point of an object's life cycle.&lt;/p&gt;

&lt;p&gt;Whenever an object is created:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Memory is allocated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Default values are assigned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Field initializers run.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The constructor executes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The object becomes ready for use.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Understanding constructors also helps you understand other important concepts like this, object initialization, inheritance, and &lt;strong&gt;super() TBD&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you're learning Java, mastering constructors is one of the best investments you can make before moving deeper into Object-Oriented Programming.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
      <category>backend</category>
    </item>
    <item>
      <title>Strong Java Starts with Strong Fundamentals</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Fri, 29 May 2026 06:26:01 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/strong-java-starts-with-strong-fundamentals-10ke</link>
      <guid>https://dev.to/hariharan_sj_2003/strong-java-starts-with-strong-fundamentals-10ke</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Programming languages come and go. Technologies change every year.&lt;br&gt;
But even after decades, Java still powers millions of applications around the world — from banking systems and Android apps to enterprise software used by giant companies.&lt;/p&gt;

&lt;p&gt;The interesting part?&lt;br&gt;
Every advanced Java application starts with just a few basic concepts.&lt;/p&gt;

&lt;p&gt;Before learning frameworks, APIs, or backend development, every Java developer must first understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How classes are declared&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What data types actually mean&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How fields store and manage data inside objects&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These may look like beginner topics, but they form the backbone of Java programming. If these fundamentals are strong, learning advanced concepts becomes much easier.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll break down Java classes, primitive and non-primitive data types, and fields in a simple and beginner-friendly way with clear examples.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.Classes, Data Types, and Fields in Java
&lt;/h2&gt;

&lt;p&gt;Java is one of the most popular programming languages in the world. From Android apps to enterprise software, Java is used everywhere because of its simplicity, security, and platform independence.&lt;/p&gt;

&lt;p&gt;Before building real-world applications, every Java developer must understand three important fundamentals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How to declare a class correctly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Types of data types in Java&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What fields are and how they work&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s explore them one by one.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Rules for Declaring a Class in Java
&lt;/h2&gt;

&lt;p&gt;A class is the blueprint of an object.&lt;br&gt;
In Java, everything revolves around classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Syntax&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;Student&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;h2&gt;
  
  
  4.Important Rules for Declaring a Class
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Class Name Should Start with a Letter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Correct:&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;Employee&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;Wrong:&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="err"&gt;123&lt;/span&gt;&lt;span class="nc"&gt;Employee&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;2. Java is Case Sensitive&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;Student&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;student&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;Both are considered different classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Spaces Are Not Allowed in Class Names&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Wrong:&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;My&lt;/span&gt; &lt;span class="nc"&gt;Class&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;Correct:&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;MyClass&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;4. Special Characters Are Not Allowed (Except _ and $)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Correct:&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;Student_Data&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;Wrong:&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;Student&lt;/span&gt;&lt;span class="nd"&gt;@Data&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;5. File Name and Public Class Name Must Be Same&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&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;The file name must be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise Java will throw an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.Data Types in Java
&lt;/h2&gt;

&lt;p&gt;Data types define what kind of value a variable can store.&lt;/p&gt;

&lt;p&gt;Java mainly has two categories of data types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Primitive Data Types&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Non-Primitive Data Types&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Primitive Data Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primitive data types are predefined by Java.&lt;br&gt;
They store simple values directly.&lt;/p&gt;

&lt;p&gt;Java has 8 primitive data types.&lt;/p&gt;

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

&lt;p&gt;The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.&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="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;age&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 1 byte&lt;/p&gt;

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

&lt;p&gt;The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.&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="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 2 bytes&lt;/p&gt;

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

&lt;p&gt;By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 4 bytes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. long&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.&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="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;population&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8000000000L&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 8 bytes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. float&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.&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="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;99.5f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 4 bytes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. double&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.&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="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.141592653&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 8 bytes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. char&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).&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="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Size: 2 bytes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. boolean&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.&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="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;isJavaFun&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;Non-Primitive Data Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Non-primitive data types are created by programmers or provided by Java.&lt;/p&gt;

&lt;p&gt;They store references instead of actual values.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;String&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Arrays (TBD)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Classes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interfaces (TBD)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Objects&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Non-primitive data types are called reference types because they refer to objects.&lt;/p&gt;

&lt;p&gt;The main differences between primitive and non-primitive data types are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Primitive types in Java are predefined and built into the language, while non-primitive types are created by the programmer (except for String).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Non-primitive types can be used to call methods to perform certain operations, whereas primitive types cannot.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Primitive types always hold a value, whereas non-primitive types can be null.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;String 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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hari"&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;Class Object 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;Student&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;s1&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;Student&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Every expert Java developer once started with the basics.&lt;br&gt;
Understanding classes, data types, and fields may seem simple at first, but these concepts are the foundation of everything you build in Java.&lt;/p&gt;

&lt;p&gt;A class gives structure to your program.&lt;br&gt;
Data types help Java understand and manage data efficiently.&lt;br&gt;
Fields define the properties that make every object unique.&lt;/p&gt;

&lt;p&gt;When your fundamentals are strong, learning advanced topics like objects, constructors, inheritance, collections, JDBC, Spring Boot, and backend development becomes much easier.&lt;/p&gt;

&lt;p&gt;So don’t rush through the basics.&lt;br&gt;
Master them.&lt;/p&gt;

&lt;p&gt;Because strong Java developers are built on strong fundamentals.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" rel="noopener noreferrer"&gt;https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://www.w3schools.com/java/java_data_types_non-prim.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/java/java_data_types_non-prim.asp&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
      <category>backend</category>
    </item>
    <item>
      <title>From Railway Stations to Police Chase — Master Python While Loops</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Thu, 28 May 2026 11:17:26 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/from-railway-stations-to-police-chase-master-python-while-loops-2cc7</link>
      <guid>https://dev.to/hariharan_sj_2003/from-railway-stations-to-police-chase-master-python-while-loops-2cc7</guid>
      <description>&lt;h2&gt;
  
  
  1.Mastering while Loops in Python with Real-Time Examples
&lt;/h2&gt;

&lt;p&gt;Programming becomes interesting when we connect logic with real-world situations.&lt;br&gt;
Instead of learning loops with boring numbers, let’s understand Python while loops using practical examples like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Finding railway stations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Detecting common multiples&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Catching a thief&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this blog, we’ll break down multiple Python programs step-by-step and understand how loops actually work behind the scenes.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.Example 1 — Finding Numbers Divisible by 3 and 5
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;station_no&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;15
30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;station_no starts from 1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The loop runs until 30&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We check:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AND&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If both are true, the number is printed.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.What Does % Mean?
&lt;/h2&gt;

&lt;p&gt;% is called the modulus operator.&lt;/p&gt;

&lt;p&gt;It returns the remainder.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;15 % 3 = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because 15 is perfectly divisible by 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.Example 2 — Finding the First Matching Station using break
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;station_no&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;

    &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5.What is break?
&lt;/h2&gt;

&lt;p&gt;The break statement in Python is used to exit or "break" out of a loop (either for or while loop) prematurely, before the loop has iterated through all its items or reached its condition. When the break statement is executed, the program immediately exits the loop, and the control moves to the next line of code after the loop.&lt;/p&gt;

&lt;p&gt;The break keyword is used to break out a for loop, or a while loop.&lt;/p&gt;

&lt;p&gt;Without break, the loop would print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;15
30
45
60
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with break, the loop stops after finding the first matching value.&lt;/p&gt;

&lt;h2&gt;
  
  
  6.Example 3 — Unknown Number of Stations using while True
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# Total no. of stations unknown
&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;62&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;station_no&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;

    &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;station_no&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;558
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why while True?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes we don't know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;how many times the loop should run&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;where the answer exists&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we create an infinite loop using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then stop it manually using break.&lt;/p&gt;

&lt;h2&gt;
  
  
  7.Example 4 — Police Catching a Thief
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="n"&gt;thief&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thief&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;thief&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;police&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Concept Behind the Program&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a beautiful real-world simulation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Police speed increases by 5&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thief speed increases by 2&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The loop runs continuously.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;thief&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The thief gets caught.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before running this program, initialize the variables.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;thief&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete Program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;thief&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="n"&gt;thief&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thief&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;police&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;thief&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;police&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;35
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8.Why These Programs Matter
&lt;/h2&gt;

&lt;p&gt;Most beginners think loops are difficult.&lt;/p&gt;

&lt;p&gt;But once you connect loops with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;trains&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;stations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;police chase&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;real-time logic&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Programming becomes much easier and more fun.&lt;/p&gt;

&lt;h2&gt;
  
  
  9.Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The while loop is one of the most powerful concepts in Python.&lt;/p&gt;

&lt;p&gt;Whether you're:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;searching for values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;building games&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;creating simulations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;solving real-world problems&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;while loops are everywhere.&lt;/p&gt;

&lt;p&gt;The best way to master loops is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Practice&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Visualize&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build logic gradually&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because programming is not about memorizing syntax —&lt;br&gt;
it’s about training your thinking process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://www.geeksforgeeks.org/python/python-break-statement/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/python/python-break-statement/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://www.w3schools.com/python/ref_keyword_break.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/python/ref_keyword_break.asp&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>coding</category>
      <category>python</category>
    </item>
    <item>
      <title>The One Concept That Powers Every Java Application</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Wed, 27 May 2026 11:53:57 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/the-one-concept-that-powers-every-java-application-3p3o</link>
      <guid>https://dev.to/hariharan_sj_2003/the-one-concept-that-powers-every-java-application-3p3o</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Imagine trying to build an application like Instagram, Amazon, or a banking system using thousands of random functions and unorganized code. Sounds impossible, right? &lt;/p&gt;

&lt;p&gt;As software applications started becoming larger and more complex, developers needed a smarter way to organize code — something that was secure, reusable, scalable, and easier to maintain.&lt;/p&gt;

&lt;p&gt;That’s where Object-Oriented Programming (OOPs) changed everything. &lt;/p&gt;

&lt;p&gt;OOPs is not just a programming concept in Java.&lt;br&gt;
It is the foundation behind how modern software is designed.&lt;/p&gt;

&lt;p&gt;From mobile apps  to enterprise systems , Java uses OOPs to model real-world entities like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Users&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cars&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bank Accounts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Employees&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shopping Carts&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;into reusable software objects.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll deeply explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Why OOPs is needed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The four pillars of OOPs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Internal working of classes and objects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-world examples&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How Java uses OOPs internally&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of this article, you won’t just memorize OOPs concepts — you’ll actually understand why they are the backbone of Java development.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.What is OOPs?
&lt;/h2&gt;

&lt;p&gt;Object-Oriented Programming System (OOPs) is a programming approach where everything is organized around objects and classes.&lt;/p&gt;

&lt;p&gt;Instead of writing long procedural code, OOPs allows developers to model real-world entities like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cars&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Students&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bank Accounts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Employees&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;as software objects.&lt;/p&gt;
&lt;h2&gt;
  
  
  3.Why OOPs Concept is Needed in Java?
&lt;/h2&gt;

&lt;p&gt;Imagine building a very large application like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Instagram&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amazon&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Banking Systems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hospital Management Systems&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without proper structure, the code would become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Messy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Difficult to manage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hard to debug&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unsafe&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Impossible to scale&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is exactly why Object-Oriented Programming (OOPs) was introduced.&lt;/p&gt;

&lt;p&gt;OOPs helps developers organize software in a smarter and more realistic way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Main Problem Before OOPs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before OOPs, many programming languages mainly followed:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Procedural Programming&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In procedural programming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Everything is written as functions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data is shared globally&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Programs become harder to maintain as size increases&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of Procedural Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine creating a banking application.&lt;/p&gt;

&lt;p&gt;You may have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deposit()
withdraw()
checkBalance()
transferMoney()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All functions directly access shared data.&lt;/p&gt;

&lt;p&gt;As the application grows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Bugs increase&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Security issues appear&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code duplication happens&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Maintenance becomes difficult&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For small programs this is okay.&lt;/p&gt;

&lt;p&gt;But for real-world applications with millions of users, this becomes a disaster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why OOPs Was Introduced&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;OOPs was introduced to solve these real software development problems.&lt;/p&gt;

&lt;p&gt;It helps developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Organize code properly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reuse existing code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Improve security&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce complexity&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build scalable applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Model real-world entities&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4.Understanding Class and Object
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Class&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A class is a blueprint or template for creating objects.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String brand;
    int speed;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, Car is a class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An object is an instance of a class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car c1 = new Car();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, c1 is an object of the Car class.&lt;/p&gt;

&lt;h2&gt;
  
  
  5.The Four Pillars of OOPs
&lt;/h2&gt;

&lt;p&gt;These are the core concepts that define Object-Oriented Programming:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Encapsulation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inheritance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polymorphism&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Abstraction&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;strong&gt;What is Encapsulation?&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Wrapping data and methods together into a single unit.&lt;/p&gt;

&lt;p&gt;It also means restricting direct access to sensitive data.&lt;/p&gt;

&lt;p&gt;In Java, encapsulation is achieved using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Private variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Public getter and setter methods&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why Encapsulation is Important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a bank account.&lt;/p&gt;

&lt;p&gt;If everyone could directly access your balance variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;balance = -100000;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the system would become unsafe.&lt;/p&gt;

&lt;p&gt;Encapsulation protects data from unauthorized modification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Encapsulation&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 Student {

    private int marks;

    public void setMarks(int m) {

        if(m &amp;gt;= 0 &amp;amp;&amp;amp; m &amp;lt;= 100) {
            marks = m;
        }
    }

    public int getMarks() {
        return marks;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;strong&gt;What is Inheritance?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inheritance allows one class to acquire the properties and behaviors of another class.&lt;/p&gt;

&lt;p&gt;The existing class is called:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Parent Class&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Super Class&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Base Class&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The new class is called:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Child Class&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sub Class&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Derived Class&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why Inheritance?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without inheritance, developers would repeatedly write the same code.&lt;/p&gt;

&lt;p&gt;Inheritance promotes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Code Reusability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Maintainability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hierarchical Relationships&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of Inheritance&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 Animal {

    void eat() {
        System.out.println("Animal eats food");
    }
}

- 
class Dog extends Animal {

    void bark() {
        System.out.println("Dog barks");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;strong&gt;What is Polymorphism?&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;One entity behaving in multiple forms.&lt;/p&gt;

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

&lt;p&gt;A person can behave differently as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Student&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Employee&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customer&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similarly, Java methods can behave differently depending on the situation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Polymorphism&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Method Overloading (TBD)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Method Overriding (TBD)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;4. Abstraction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Abstraction?&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Hiding internal implementation and showing only essential features.&lt;/p&gt;

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

&lt;p&gt;You drive a car using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Steering&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Brake&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accelerator&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But you don’t see internal engine mechanisms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Abstraction?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without abstraction, systems become extremely complex.&lt;/p&gt;

&lt;p&gt;Abstraction reduces complexity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class Vehicle {

    abstract void start();

    void stop() {
        System.out.println("Vehicle stopped");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6.Final Conclusion
&lt;/h2&gt;

&lt;p&gt;OOPs is not just a programming topic.&lt;/p&gt;

&lt;p&gt;It is a software design philosophy that helps developers create clean, maintainable, reusable, and scalable applications.&lt;/p&gt;

&lt;p&gt;To truly master Java, understanding these concepts deeply is essential:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Encapsulation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inheritance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polymorphism&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Abstraction&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once these concepts become clear, writing advanced Java applications becomes much easier.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Java Isn’t Just a Language — It’s an Ecosystem</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Tue, 26 May 2026 12:42:07 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/java-isnt-just-a-language-its-an-ecosystem-1o62</link>
      <guid>https://dev.to/hariharan_sj_2003/java-isnt-just-a-language-its-an-ecosystem-1o62</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered how the same Java program runs perfectly on Windows, Linux, and macOS without changing a single line of code?&lt;/p&gt;

&lt;p&gt;That’s the magic of Java Architecture.&lt;/p&gt;

&lt;p&gt;Behind every Java application, powerful components like JVM, JDK, JRE, Bytecode, and JIT Compiler work together to make Java secure, portable, and incredibly powerful.&lt;/p&gt;

&lt;p&gt;But most beginners use Java without actually understanding what happens internally when a Java program runs.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll break down the complete architecture of Java in a simple and beginner-friendly way. From the history of Java to understanding how bytecode works and why Java follows the famous principle:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Write Once, Run Anywhere”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You’ll learn everything you need to build a strong foundation in Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.Origin of Java
&lt;/h2&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%2Ffdh0kceoswgihexjfcfz.jpg" 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%2Ffdh0kceoswgihexjfcfz.jpg" alt=" " width="669" height="607"&gt;&lt;/a&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%2Feq4n7jjt9vb5yaqptiia.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%2Feq4n7jjt9vb5yaqptiia.png" alt=" " width="631" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Java was developed by James Gosling and his team at Sun Microsystems in the early 1990s.&lt;/p&gt;

&lt;p&gt;The project started in 1991.&lt;/p&gt;

&lt;p&gt;The main members of the Java team were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;James Gosling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mike Sheridan&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Patrick Naughton&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This team was later called the Green Team.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Why Was Java Created?
&lt;/h2&gt;

&lt;p&gt;Initially, Java was developed for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Television systems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Embedded devices&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Electronic appliances&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal was to create a language that could run on multiple devices without modification.&lt;/p&gt;

&lt;p&gt;At that time, existing languages were platform dependent.&lt;/p&gt;

&lt;p&gt;The Green Team wanted a language that followed:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Write Once, Run Anywhere”&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4.What is Java?
&lt;/h2&gt;

&lt;p&gt;Java is a high-level, object-oriented programming language developed by Oracle Corporation.&lt;/p&gt;

&lt;p&gt;Java is designed to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simple&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Portable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Platform Independent&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Java Program Execution Flow&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%2Ffxj13050bqck14wuwtsd.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%2Ffxj13050bqck14wuwtsd.png" alt=" " width="634" height="497"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Java Source Code (.java)
        ↓
Compiler (javac)
        ↓
Bytecode (.class)
        ↓
JVM
        ↓
Machine Code
        ↓
Output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5.What is JDK?
&lt;/h2&gt;

&lt;p&gt;JDK → Java Development Kit&lt;/p&gt;

&lt;p&gt;JDK is a complete package used for developing Java applications.&lt;/p&gt;

&lt;p&gt;It provides all the tools required to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Write Java programs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compile Java code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run Java applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Debug programs&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Components of JDK&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JDK contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JRE&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java Compiler (javac)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Debugging tools&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Documentation tools&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Development utilities&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Formula&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;JDK = JRE + Development Tools
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6.Why JDK is Important?
&lt;/h2&gt;

&lt;p&gt;Without JDK, developers cannot create Java applications because it contains the compiler needed to convert source code into bytecode.&lt;/p&gt;

&lt;h2&gt;
  
  
  7.What is Bytecode?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Bytecode Definition&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bytecode is the intermediate code generated after compiling a Java program.&lt;/p&gt;

&lt;p&gt;When we compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;javac Hello.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java creates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello.class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This .class file contains bytecode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Bytecode is Important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bytecode is platform independent.&lt;/p&gt;

&lt;p&gt;This means the same bytecode can run on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Windows&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Linux&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;macOS&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the core reason behind Java’s portability.&lt;/p&gt;

&lt;h2&gt;
  
  
  8.What is JVM?
&lt;/h2&gt;

&lt;p&gt;JVM → Java Virtual Machine&lt;/p&gt;

&lt;p&gt;JVM is the heart of Java Architecture.&lt;/p&gt;

&lt;p&gt;It converts bytecode into machine code and executes the program.&lt;/p&gt;

&lt;p&gt;JVM acts as a bridge between Java applications and the operating system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Responsibilities of JVM&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Loads class files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verifies bytecode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Executes code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allocates memory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performs Garbage Collection&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9.What is JIT Compiler?
&lt;/h2&gt;

&lt;p&gt;JIT → Just-In-Time Compiler&lt;/p&gt;

&lt;p&gt;The JIT Compiler improves Java performance.&lt;/p&gt;

&lt;p&gt;Instead of interpreting the same bytecode repeatedly, JIT converts frequently used bytecode into native machine code.&lt;/p&gt;

&lt;p&gt;This makes Java programs faster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How JIT Works?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without JIT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bytecode → Interpreter → Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With JIT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bytecode → JIT Compiler → Native Machine Code → Faster Execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of JIT Compiler&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Improves performance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduces execution time&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimizes frequently used code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Makes Java applications faster&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10.What is JRE?
&lt;/h2&gt;

&lt;p&gt;JRE → Java Runtime Environment&lt;/p&gt;

&lt;p&gt;JRE provides the environment required to run Java applications.&lt;/p&gt;

&lt;p&gt;It contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JVM&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Core libraries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supporting files&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Formula&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;JRE = JVM + Libraries
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why JRE is Important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you only want to run Java applications and not develop them, JRE is enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  11.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Understanding concepts like JDK, JVM, JRE, Bytecode, and JIT Compiler is essential for every Java developer.&lt;/p&gt;

&lt;p&gt;These concepts explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How Java programs execute&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Why Java is platform independent&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How Java manages memory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How Java achieves performance&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you understand Java Architecture deeply, learning advanced Java concepts becomes much easier.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
      <category>backend</category>
    </item>
    <item>
      <title>How a Simple Frog Story Helped Me Understand Python Loops</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Tue, 26 May 2026 09:51:15 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/how-a-simple-frog-story-helped-me-understand-python-loops-4njb</link>
      <guid>https://dev.to/hariharan_sj_2003/how-a-simple-frog-story-helped-me-understand-python-loops-4njb</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;Learning programming becomes easier when we connect code with real-world stories.&lt;/p&gt;

&lt;p&gt;Instead of directly jumping into theory, I tried solving a small story-based Python problem about a frog trapped inside a well.&lt;/p&gt;

&lt;p&gt;Even though the code is very small, it teaches some powerful beginner concepts like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Loops&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conditions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Logical thinking&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this blog, let’s understand how this simple frog problem works step by step.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.The Story Problem
&lt;/h2&gt;

&lt;p&gt;Imagine a frog accidentally falls into a 50-feet deep well.&lt;/p&gt;

&lt;p&gt;Now the frog tries to escape:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It climbs 2 feet upward&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But slips 1.25 feet downward&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This process repeats every single day.&lt;/p&gt;

&lt;p&gt;The question is:&lt;/p&gt;

&lt;p&gt;“How many days will the frog take to escape the well?”&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Thinking Like a Programmer
&lt;/h2&gt;

&lt;p&gt;Instead of solving this manually day by day, we can automate it using Python.&lt;/p&gt;

&lt;p&gt;We need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A variable to store the remaining depth&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A loop because the process repeats&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A counter to track the number of days&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python Code&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;feet = 50
up = 2
down = 1.25
day = 0

while feet &amp;gt; 0:
    feet = feet - up + down
    day = day + 1

print(day)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4.Step-by-Step Explanation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Storing the Initial Values&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;feet = 50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This represents the total depth of the well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;up = 2
down = 1.25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;up → how much the frog climbs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;down → how much the frog slips&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Counting the Days&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;day = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initially, no days have passed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the while Loop&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;while feet &amp;gt; 0:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loop keeps running until the frog escapes the well.&lt;/p&gt;

&lt;p&gt;As long as the remaining depth is greater than 0, the loop continues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Main Logic&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;feet = feet - up + down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what happens every day:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Frog climbs 2 feet&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Frog slips 1.25 feet&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the net movement becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 - 1.25 = 0.75 feet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frog effectively climbs 0.75 feet per day.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Increasing the Day Count&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;day = day + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every loop execution represents one full day.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Output&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;67
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frog escapes the well in 67 days.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concepts Learned From This Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This simple program teaches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;while loops&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Arithmetic operations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Problem-solving&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Logic building&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5.Why Story-Based Problems Are Great
&lt;/h2&gt;

&lt;p&gt;Story-based coding problems are excellent for beginners because they make programming feel practical and interesting.&lt;/p&gt;

&lt;p&gt;Instead of memorizing syntax, you start understanding:&lt;/p&gt;

&lt;p&gt;“Why are we using this logic?”&lt;/p&gt;

&lt;p&gt;That is where real programming learning begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  6.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Even a tiny Python program can teach powerful programming concepts.&lt;/p&gt;

&lt;p&gt;The frog escape problem may look simple, but it helps beginners understand how loops work in real scenarios.&lt;/p&gt;

&lt;p&gt;Sometimes the best way to improve coding skills is by solving small and fun logical problems consistently&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
      <category>learning</category>
    </item>
    <item>
      <title>The Backbone of Python: Conditions and Loops</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Sun, 24 May 2026 11:52:41 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/the-backbone-of-python-conditions-and-loops-182j</link>
      <guid>https://dev.to/hariharan_sj_2003/the-backbone-of-python-conditions-and-loops-182j</guid>
      <description>&lt;h2&gt;
  
  
  1.Python Conditional Statements and Looping Statements
&lt;/h2&gt;

&lt;p&gt;Python is one of the most popular programming languages because of its simple syntax and powerful features. Two of the most fundamental concepts in Python are Conditional Statements and Looping Statements. These concepts help programmers build logical, interactive, and efficient applications.&lt;/p&gt;

&lt;p&gt;Conditional statements help Python make decisions, while looping statements help Python repeat tasks multiple times without writing the same code again and again.&lt;/p&gt;

&lt;p&gt;Understanding these concepts is essential for every beginner because they form the foundation of real-world programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.What are Conditional Statements in Python?
&lt;/h2&gt;

&lt;p&gt;Conditional statements are used to execute specific blocks of code based on conditions.&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;p&gt;Conditional statements help Python make decisions.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if the password is correct → Login successful&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If marks are above 90 → Grade A&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If age is above 18 → Eligible to vote&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python uses conditional statements to perform these decision-making operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Why Conditional Statements are Used in Python?
&lt;/h2&gt;

&lt;p&gt;Conditional statements are used in Python to make decisions based on conditions.&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;p&gt;Conditional statements help Python decide which code should execute and which code should not execute.&lt;/p&gt;

&lt;p&gt;They allow programs to behave differently in different situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.Why Are Conditional Statements Important?
&lt;/h2&gt;

&lt;p&gt;Without conditional statements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Programs cannot make decisions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every line of code would execute in the same order&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Applications would not become interactive or smart&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Conditional statements make programs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Intelligent&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dynamic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interactive&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flexible&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5.Types of Conditional Statements
&lt;/h2&gt;

&lt;p&gt;Python mainly provides four types of conditional statements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;if statement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if-else statement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if-elif-else statement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nested if statement&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. if Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The if statement executes code only when the condition becomes true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;statement&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Eligible to vote&lt;/span&gt;&lt;span class="sh"&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;Output&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;Eligible to vote
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python checks whether the condition is true.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If true → Code executes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If false → Code is skipped&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. if-else Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The if-else statement is used when there are two possible outcomes.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Even Number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Odd Number&lt;/span&gt;&lt;span class="sh"&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;Output&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;Odd Number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the condition is true → if block executes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Otherwise → else block executes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. if-elif-else Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When multiple conditions need to be checked, Python uses elif.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Grade A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Grade B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Grade C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Fail&lt;/span&gt;&lt;span class="sh"&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;Output&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;Grade B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python checks conditions one by one from top to bottom.&lt;/p&gt;

&lt;p&gt;Once a condition becomes true, the remaining conditions are skipped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Nested if Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An if statement inside another if statement is called a nested if.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="n"&gt;has_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;has_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Entry Allowed&lt;/span&gt;&lt;span class="sh"&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;Output&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;Entry Allowed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6.What are Looping Statements in Python?
&lt;/h2&gt;

&lt;p&gt;Looping statements are used to execute a block of code repeatedly.&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;p&gt;Loops help Python repeat tasks automatically.&lt;/p&gt;

&lt;p&gt;Instead of writing the same code multiple times, loops perform repetition efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  7.Why Looping Statements are Used in Python?
&lt;/h2&gt;

&lt;p&gt;Looping statements are used in Python to execute a block of code repeatedly without writing the same code again and again.&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;p&gt;Loops help Python repeat tasks automatically.&lt;/p&gt;

&lt;p&gt;Without loops, programmers would need to write repetitive code manually, which makes programs lengthy and inefficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  8.Real-Life Example
&lt;/h2&gt;

&lt;p&gt;Imagine you want to print "Hello" 5 times.&lt;/p&gt;

&lt;p&gt;Without loops&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using loops&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&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="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  9.Types of Looping Statements in Python
&lt;/h2&gt;

&lt;p&gt;Python mainly provides two looping statements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;for loop (TBD)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;while loop&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;while Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The while loop executes as long as the condition remains true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;statement&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&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="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The loop continues until the condition becomes false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Does end=" " Mean?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Normally, print() moves to the next line automatically.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;tells Python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Do not move to next line&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print a space after the output&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is why all numbers appear in a single line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&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="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&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;1 2 3 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10.Real-World Importance of Conditional and Looping Statements
&lt;/h2&gt;

&lt;p&gt;These concepts are used everywhere in programming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Login authentication systems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;E-commerce websites&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Banking applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chat applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Games&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Artificial Intelligence systems&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are the building blocks of programming logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  11.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Conditional statements and looping statements are two of the most important concepts in Python programming.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Conditional statements help programs make decisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Looping statements help programs repeat tasks efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By mastering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if, else, and elif&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;for and while loops&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Logical and comparison operators&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you build a strong foundation for advanced Python concepts and real-world software development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
      <category>learning</category>
    </item>
    <item>
      <title>From Props to Global State: Understanding Redux</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Thu, 21 May 2026 11:34:35 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/from-props-to-global-state-understanding-redux-2k6d</link>
      <guid>https://dev.to/hariharan_sj_2003/from-props-to-global-state-understanding-redux-2k6d</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;If you’ve worked on a React project before, you’ve probably faced this frustrating situation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A parent component has some data…&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A child component needs that data…&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then another child needs it too…&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what do we do?&lt;/p&gt;

&lt;p&gt;We keep passing props from one component to another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Parent → Child → Grandchild → Great Grandchild
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, this seems manageable.&lt;/p&gt;

&lt;p&gt;But as your application grows, your component structure slowly turns into a nightmare of endless prop passing.&lt;/p&gt;

&lt;p&gt;This problem is called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prop Drilling
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is exactly where Redux enters the picture.&lt;/p&gt;

&lt;p&gt;Redux is one of the most powerful state management libraries used in React applications. It helps developers manage shared data in a centralized and predictable way without passing props through multiple layers of components.&lt;/p&gt;

&lt;p&gt;Instead of scattering state across different parts of your application, Redux gives you a single global store where your application data lives.&lt;/p&gt;

&lt;p&gt;This makes your applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Easier to scale&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easier to debug&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easier to maintain&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More predictable&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From authentication systems and shopping carts to large enterprise dashboards, Redux is widely used in modern React applications where managing complex state becomes difficult.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll break down Redux in the simplest way possible, understand how it works internally, and explore concepts like stores, actions, reducers, dispatching, and global state with practical examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.What is Redux in React?
&lt;/h2&gt;

&lt;p&gt;Redux is a popular state management library used in React applications to manage and share data across multiple components in a predictable way.&lt;/p&gt;

&lt;p&gt;In simple terms, Redux acts like a central storage system for your application’s data.&lt;/p&gt;

&lt;p&gt;Instead of passing data manually from one component to another using props, Redux allows all components to access shared data directly from a single global store.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Why Do We Need Redux?
&lt;/h2&gt;

&lt;p&gt;In small React applications, managing state using useState and passing props between components works perfectly fine.&lt;/p&gt;

&lt;p&gt;But as applications grow larger, problems start appearing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Too many props being passed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complex state management&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Difficult data sharing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hard-to-maintain component structure&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This problem is called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prop Drilling
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redux solves this problem by creating one centralized store where all shared data is managed.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.Understanding Redux with a Simple Analogy
&lt;/h2&gt;

&lt;p&gt;Think of Redux like a bank locker system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redux Store = Central Locker
Components = Customers
Actions = Requests
Reducers = Bank Employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A component requests a change&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redux processes the request&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store updates the data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All connected components receive updated data automatically&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  5.How to Install Redux in React
&lt;/h2&gt;

&lt;p&gt;When building small React applications, Hooks like useState and useContext are often enough for managing state. But as applications grow larger and more complex, managing shared data across multiple components becomes difficult.&lt;/p&gt;

&lt;p&gt;This is where Redux comes into the picture.&lt;/p&gt;

&lt;p&gt;Redux is a powerful state management library that helps developers manage global application state in a centralized and predictable way.&lt;/p&gt;

&lt;p&gt;In modern React applications, developers mainly use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redux Toolkit (RTK)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because it simplifies Redux setup and reduces unnecessary boilerplate code.&lt;/p&gt;

&lt;h2&gt;
  
  
  6.Installing Redux in React
&lt;/h2&gt;

&lt;p&gt;To use Redux in a React application, we mainly need two packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;@reduxjs/toolkit&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;react-redux&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install them using npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @reduxjs/toolkit react-redux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7.Understanding the Packages
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@reduxjs/toolkit&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Simplifies Redux setup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;react-redux&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Connects Redux with React&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  7.Core Concepts of Redux
&lt;/h2&gt;

&lt;p&gt;Redux mainly works with four important concepts:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Store&lt;/td&gt;
&lt;td&gt;Central place that stores application state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Action&lt;/td&gt;
&lt;td&gt;Object that describes what happened&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reducer&lt;/td&gt;
&lt;td&gt;Function that updates state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dispatch&lt;/td&gt;
&lt;td&gt;Sends actions to reducers&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;The store is the central place where all application data is stored.&lt;/p&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of it like the brain of Redux.&lt;/p&gt;

&lt;p&gt;All shared state lives inside the store.&lt;/p&gt;

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

&lt;p&gt;Actions describe what should happen.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;type:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"INCREMENT"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An action is simply a JavaScript object.&lt;/p&gt;

&lt;p&gt;It tells Redux:&lt;/p&gt;

&lt;p&gt;“Hey, something happened. Update the state.”&lt;/p&gt;

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

&lt;p&gt;Reducers decide how the state should change.&lt;/p&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counterReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INCREMENT&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="nx"&gt;state&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="nl"&gt;default&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;state&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;Reducer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Receives current state&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Receives action&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Returns updated state&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Dispatch&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dispatch sends actions to Redux.&lt;/p&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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INCREMENT&lt;/span&gt;&lt;span class="dl"&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 tells Redux to update the store.&lt;/p&gt;

&lt;h2&gt;
  
  
  8.How Redux Flow Works
&lt;/h2&gt;

&lt;p&gt;Redux follows a one-way data flow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component
   ↓
Dispatch Action
   ↓
Reducer
   ↓
Store Updates
   ↓
UI Re-renders
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This predictable flow makes debugging easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Counter App Using Redux&lt;/strong&gt;&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INCREMENT&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="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&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="nl"&gt;default&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;state&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;Dispatching action:&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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INCREMENT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redux updates the store automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  9.What is Global State?
&lt;/h2&gt;

&lt;p&gt;Global state means data that multiple components need access to.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Authentication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shopping cart&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Theme mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;User profile&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Notifications&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Redux helps manage this shared data efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  10.Redux vs useState
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Redux&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Local component state&lt;/td&gt;
&lt;td&gt;Global application state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best for small apps&lt;/td&gt;
&lt;td&gt;Best for large apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Simple state handling&lt;/td&gt;
&lt;td&gt;Complex state management&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data shared using props&lt;/td&gt;
&lt;td&gt;Data shared through store&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  11.Real-World Use Cases of Redux
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;E-commerce shopping carts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authentication systems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dashboard applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Social media apps&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-time notifications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Theme management&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Large-scale enterprise applications&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  12.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Redux is a powerful state management library that helps React applications manage shared data in a centralized and predictable way.&lt;/p&gt;

&lt;p&gt;While React Hooks like useState are perfect for local component state, Redux becomes extremely useful when applications grow larger and multiple components need access to the same data.&lt;/p&gt;

&lt;p&gt;Understanding Redux is an important step toward building scalable React applications because it teaches developers how to manage complex application state efficiently and maintain a clean architecture.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The Hidden Power of useRef in React</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Wed, 20 May 2026 10:45:49 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/the-hidden-power-of-useref-in-react-5d2m</link>
      <guid>https://dev.to/hariharan_sj_2003/the-hidden-power-of-useref-in-react-5d2m</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;When learning React Hooks, most developers immediately focus on hooks like useState and useEffect. While these Hooks are extremely important, another powerful Hook that often gets overlooked is useRef.&lt;/p&gt;

&lt;p&gt;At first glance, useRef may seem confusing because unlike useState, updating a useRef value does not re-render the component. But once you understand how it works, you’ll realize that useRef is one of the most useful Hooks for handling DOM interactions, storing mutable values, and improving performance in React applications.&lt;/p&gt;

&lt;p&gt;The useRef Hook allows developers to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Access DOM elements directly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Focus input fields&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store values between renders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Manage timers and intervals&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid unnecessary re-renders&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In modern React applications, useRef is commonly used behind the scenes for things like form handling, video controls, scroll tracking, and performance optimization.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll explore what useRef actually is, how it works i&lt;br&gt;
nternally, and where it is used in real-world React projects with practical examples and beginner-friendly explanations.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.What is useRef in React?
&lt;/h2&gt;

&lt;p&gt;useRef is a React Hook that lets you reference a value that’s not needed for rendering.&lt;/p&gt;

&lt;p&gt;In simple terms, useRef gives you a way to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Directly access DOM elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store mutable values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preserve values between renders&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike useState, updating a useRef value does not re-render the component.&lt;/p&gt;
&lt;h2&gt;
  
  
  3.Why Do We Need useRef?
&lt;/h2&gt;

&lt;p&gt;Sometimes in React, we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Focus an input field&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access a button directly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store timer IDs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remember previous values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Work with DOM elements&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where useRef becomes useful.&lt;/p&gt;

&lt;p&gt;Think of useRef as a special storage box that React remembers between renders.&lt;/p&gt;
&lt;h2&gt;
  
  
  4.Syntax of useRef
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ref&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="nx"&gt;initialValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;inputRef → Reference object&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;null → Initial value&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5.Understanding How useRef Works
&lt;/h2&gt;

&lt;p&gt;useRef returns an object like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;current:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;value&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myRef&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="mi"&gt;0&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="nx"&gt;myRef&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;current:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual value is stored inside:&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="nx"&gt;myRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6.Accessing DOM Elements with useRef
&lt;/h2&gt;

&lt;p&gt;One of the most common use cases.&lt;/p&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;focusInput&lt;/span&gt; &lt;span class="o"&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="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&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;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;focusInput&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Focus&lt;/span&gt; &lt;span class="nx"&gt;Input&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;How this works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;inputRef is connected to the input element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React stores the DOM element inside inputRef.current&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When button is clicked:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Input field gets focused&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7.useRef vs useState
&lt;/h2&gt;

&lt;p&gt;This is one of the most important concepts.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;useState&lt;/th&gt;
&lt;th&gt;useRef&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Causes re-render&lt;/td&gt;
&lt;td&gt;Does NOT cause re-render&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used for UI updates&lt;/td&gt;
&lt;td&gt;Used for storing mutable values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Updates visible data&lt;/td&gt;
&lt;td&gt;Stores values silently&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  8.Example Difference
&lt;/h2&gt;

&lt;p&gt;Using useState:&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing state updates the UI.&lt;/p&gt;

&lt;p&gt;Using useRef:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;countRef&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing:&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="nx"&gt;countRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will NOT re-render the component.&lt;/p&gt;

&lt;h2&gt;
  
  
  9.Storing Previous Values with useRef
&lt;/h2&gt;

&lt;p&gt;useRef is useful for remembering old values.&lt;/p&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="nx"&gt;useEffect&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="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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;previousCount&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="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="nx"&gt;previousCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;count&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;count&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Previous&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;previousCount&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Increase&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;count stores current value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;previousCount.current stores old value&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10.Real-World Use Cases of useRef
&lt;/h2&gt;

&lt;p&gt;You’ll commonly use useRef for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Focusing input fields&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessing DOM elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Managing timers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Storing previous values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preventing unnecessary re-renders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Video/audio controls&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scroll position tracking&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  11.Simple Analogy for Understanding useRef
&lt;/h2&gt;

&lt;p&gt;Think of useRef like a hidden notebook inside a component.&lt;/p&gt;

&lt;p&gt;React remembers the notebook between renders, but changing the notebook does not update the screen.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useState  → updates UI
useRef    → stores values silently
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  12.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;useRef is a powerful Hook that allows React developers to directly access DOM elements and store mutable values without causing unnecessary re-renders.&lt;/p&gt;

&lt;p&gt;While useState is used for updating the UI, useRef is mainly used for storing values behind the scenes and interacting with the DOM efficiently.&lt;/p&gt;

&lt;p&gt;Understanding when to use useRef instead of useState is an important step toward becoming a better React developer because it helps improve both performance and code organization.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Understanding React’s Core Hooks: useState &amp; useEffect</title>
      <dc:creator>Hariharan S J</dc:creator>
      <pubDate>Tue, 19 May 2026 11:30:43 +0000</pubDate>
      <link>https://dev.to/hariharan_sj_2003/understanding-reacts-core-hooks-usestate-useeffect-3p47</link>
      <guid>https://dev.to/hariharan_sj_2003/understanding-reacts-core-hooks-usestate-useeffect-3p47</guid>
      <description>&lt;h2&gt;
  
  
  1.Introduction
&lt;/h2&gt;

&lt;p&gt;React has become one of the most popular JavaScript libraries for building modern user interfaces. But what truly changed the way developers write React applications was the introduction of Hooks in React 16.8.&lt;/p&gt;

&lt;p&gt;Before Hooks, developers mainly relied on class components to manage state, lifecycle methods, and side effects. This often made applications harder to understand and maintain. React Hooks solved this problem by allowing developers to use state and other React features directly inside functional components.&lt;/p&gt;

&lt;p&gt;Hooks make React code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cleaner&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More readable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easier to reuse&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simpler to maintain&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today, almost every modern React application heavily depends on Hooks for handling state management, API calls, performance optimization, and reusable logic.&lt;/p&gt;

&lt;p&gt;From simple hooks like useState and useEffect to advanced hooks like useReducer and useMemo, each hook solves a specific problem and helps developers build better applications more efficiently.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll explore the most important React Hooks every developer should know, understand how they work, and learn where they are commonly used in real-world projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.What is useState in React?
&lt;/h2&gt;

&lt;p&gt;useState is one of the most important and commonly used Hooks in React. It allows functional components to store, manage, and update data that can change over time.&lt;/p&gt;

&lt;p&gt;In simple terms, useState helps React “remember” values and update the UI whenever those values change.&lt;/p&gt;

&lt;p&gt;For example, imagine a counter app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Count starts at 0&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you click a button, the value increases&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The UI updates automatically&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without useState, React would not know when to update the screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Do We Need useState?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Normally, variables in JavaScript do not persist between renders.&lt;/p&gt;

&lt;p&gt;For 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="kd"&gt;let&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;0&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;increase&lt;/span&gt;&lt;span class="p"&gt;()&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="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="nx"&gt;count&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;Even though count changes, React will not automatically update the UI because normal variables are not reactive.&lt;/p&gt;

&lt;p&gt;This is where useState comes in.&lt;/p&gt;

&lt;p&gt;useState tells React:&lt;/p&gt;

&lt;p&gt;“This value can change, and whenever it changes, update the UI.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of useState&lt;/strong&gt;&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&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="nx"&gt;initialValue&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 break this down:&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;count → Current state value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;setCount → Function used to update the state&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;0 → Initial value of the state&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count → current value
setCount → updates value
useState(0) → starting value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How useState Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When state changes, React automatically re-renders the component.&lt;/p&gt;

&lt;p&gt;Example:&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;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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&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;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;h1&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;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;
        Increase
      &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;How this works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initial value starts at 0&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;User clicks the button&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;setCount(count + 1) runs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React updates the state&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Component re-renders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UI updates automatically&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So if count becomes 1, React updates the screen instantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding State in Simple Words&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can think of state like a memory box inside a component.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter Component
┌───────────┐
│ count = 0 │
└───────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the button is clicked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter Component
┌───────────┐
│ count = 1 │
└───────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React notices the change and updates the UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updating State&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You update state using the setter function.&lt;/p&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This changes the value to 10.&lt;/p&gt;

&lt;p&gt;You can also update based on the previous value:&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called a functional update.&lt;/p&gt;

&lt;p&gt;This approach is useful when the new state depends on the old state.&lt;/p&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures React updates correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-World Use Cases of useState&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Counter apps&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Form handling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Toggle buttons&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dark/light theme&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search input values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shopping cart quantity&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modal open/close&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3.What is useEffect in React?
&lt;/h2&gt;

&lt;p&gt;useEffect is one of the most powerful Hooks in React. It is used to perform side effects inside functional components.&lt;/p&gt;

&lt;p&gt;In simple terms, useEffect allows React components to do something after rendering.&lt;/p&gt;

&lt;p&gt;This could include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fetching data from an API&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updating the document title&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setting timers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adding event listeners&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessing browser APIs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cleaning up resources&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before Hooks existed, these operations were handled using lifecycle methods in class components like componentDidMount and componentDidUpdate.&lt;/p&gt;

&lt;p&gt;But with useEffect, React made this process much simpler and cleaner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Side Effect?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A side effect is anything that affects something outside the component itself.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Calling an API&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changing the webpage title&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Starting a timer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Listening to keyboard or mouse events&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updating local storage&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These operations happen outside the normal rendering process.&lt;/p&gt;

&lt;p&gt;That’s why React provides useEffect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax of useEffect&lt;/strong&gt;&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="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;// side effect code&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 break this down:&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="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="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="s2"&gt;Component rendered&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;useEffect() → React Hook&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;First argument → Function containing side effect code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second argument → Dependency array&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How useEffect Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React first renders the component.&lt;/p&gt;

&lt;p&gt;After rendering is completed, React runs the code inside useEffect.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Component renders
2. UI appears on screen
3. useEffect runs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why useEffect is called a side-effect Hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Updating the Document Title&lt;/strong&gt;&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;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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Count: &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="s2"&gt;`&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;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="nx"&gt;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;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;What happens here?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Component renders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect runs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Browser tab title updates&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every time count changes, effect runs again&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Understanding the Dependency Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The dependency array controls when the effect should run.&lt;/p&gt;

&lt;p&gt;This is one of the most important concepts in useEffect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. No Dependency Array&lt;/strong&gt;&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="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="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="s2"&gt;Runs after every render&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Effect runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After first render&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After every re-render&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Empty Dependency Array&lt;/strong&gt;&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="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="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="s2"&gt;Runs only once&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Effect runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only once after initial render&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This behaves similar to:&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="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common use cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;API calls&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initial setup&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetching data&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Dependency Array with Values&lt;/strong&gt;&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="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="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="s2"&gt;Runs when count changes&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="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Effect runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Initial render&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever count changes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React watches the dependency value.&lt;/p&gt;

&lt;p&gt;If the value changes, React reruns the effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetching Data with useEffect&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most common use cases.&lt;/p&gt;

&lt;p&gt;Example:&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;useEffect&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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;Users&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUsers&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="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="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="s2"&gt;https://jsonplaceholder.typicode.com/users&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="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="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;setUsers&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="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="nx"&gt;users&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;user&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;user&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;user&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;p&gt;What happens here?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Component loads&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect runs once&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;API request happens&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State updates&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UI re-renders with data&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cleanup Function in useEffect&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes effects create resources that need cleanup.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Timers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event listeners&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Subscriptions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React provides a cleanup function for this.&lt;/p&gt;

&lt;p&gt;Syntax:&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="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;// setup code&lt;/span&gt;

  &lt;span class="k"&gt;return &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;// cleanup code&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;&lt;strong&gt;Real-World Use Cases of useEffect&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You’ll use useEffect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;API calls&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authentication checks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Timers and intervals&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event listeners&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local storage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updating document title&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-time data fetching&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Socket connections&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4.Final Takeaway
&lt;/h2&gt;

&lt;p&gt;React Hooks completely changed the way developers build React applications. Among all Hooks, useState and useEffect are the foundation of modern React development.&lt;/p&gt;

&lt;p&gt;useState helps components store and manage dynamic data, while useEffect allows components to interact with the outside world through side effects like API calls, timers, and event listeners.&lt;/p&gt;

&lt;p&gt;Together, these two Hooks make React applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;More dynamic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More interactive&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easier to maintain&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cleaner to write&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
