<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Pratik Upreti's blog]]></title><description><![CDATA[Hello! I’m Pratik Upreti, Software Engineer with background in languages like C#, Java, JavaScript, Python, SQL, and MySQL, I bring a diverse skill set to the t]]></description><link>https://blog.pratikupreti.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 09:58:50 GMT</lastBuildDate><atom:link href="https://blog.pratikupreti.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding String Immutability in Java: A Comprehensive Guide]]></title><description><![CDATA[In Java, strings play a crucial role in most applications, ranging from simple text manipulation to complex data processing. One of the fundamental characteristics of strings in Java is their immutability. This blog delves into what immutability mean...]]></description><link>https://blog.pratikupreti.com/understanding-string-immutability-in-java-a-comprehensive-guide</link><guid isPermaLink="true">https://blog.pratikupreti.com/understanding-string-immutability-in-java-a-comprehensive-guide</guid><category><![CDATA[string]]></category><category><![CDATA[immutability]]></category><category><![CDATA[Java]]></category><category><![CDATA[java for beginners]]></category><dc:creator><![CDATA[Pratik Upreti]]></dc:creator><pubDate>Fri, 02 Aug 2024 19:27:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1722626263548/632facec-9953-42c5-80df-09f7d9feecf0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In Java, strings play a crucial role in most applications, ranging from simple text manipulation to complex data processing. One of the fundamental characteristics of strings in Java is their immutability. This blog delves into what immutability means, why strings are immutable, and how this impacts your programming.</p>
<h2 id="heading-what-is-string-immutability">What is String Immutability?</h2>
<p>Immutability means that once an object is created, it cannot be changed. For strings in Java, this implies that once a <code>String</code> object is instantiated, the value it holds cannot be altered. If you need to modify a string, a new <code>String</code> object is created with the modified value.</p>
<h2 id="heading-why-are-strings-immutable-in-java">Why are Strings Immutable in Java?</h2>
<h3 id="heading-1-security">1. <strong>Security</strong></h3>
<p>Strings are widely used as parameters for network connections, file paths, and user credentials. If strings were mutable, it would be possible for a malicious actor to alter the value of the string, leading to security vulnerabilities. Immutability ensures that the value of a string remains constant throughout its lifecycle, providing a layer of security.</p>
<h3 id="heading-2-string-pooling">2. <strong>String Pooling</strong></h3>
<p>Java optimizes memory usage by maintaining a pool of strings. When a new string is created, the JVM checks the pool to see if the string already exists. If it does, the reference to the existing string is returned. This process is known as string interning. Immutability allows string pooling to be effective because the same string value can be safely shared across multiple references without risk of modification.</p>
<h3 id="heading-3-thread-safety">3. <strong>Thread Safety</strong></h3>
<h3 id="heading-4-performance">4. <strong>Performance</strong></h3>
<p>While it may seem counterintuitive, the immutability of strings can lead to performance benefits. The JVM can make certain optimizations, knowing that a string’s value will not change. Additionally, operations like substring sharing and hashcode caching are facilitated by immutability.</p>
<h2 id="heading-how-does-string-immutability-work">How Does String Immutability Work?</h2>
<h3 id="heading-creating-a-string">Creating a String</h3>
<p>When you create a string in Java, it is stored in the string pool if it is a string literal.</p>
<pre><code class="lang-java">String str1 = <span class="hljs-string">"Hello"</span>;
String str2 = <span class="hljs-string">"Hello"</span>;
System.out.println(str1 == str2); <span class="hljs-comment">// Output: true</span>
</code></pre>
<p>In this example, both <code>str1</code> and <code>str2</code> refer to the same object in the string pool.</p>
<h3 id="heading-modifying-a-string">Modifying a String</h3>
<p>If you modify a string, a new object is created.</p>
<pre><code class="lang-java">String str1 = <span class="hljs-string">"Hello"</span>;
String str2 = str1.concat(<span class="hljs-string">" World"</span>);
System.out.println(str1); <span class="hljs-comment">// Output: Hello</span>
System.out.println(str2); <span class="hljs-comment">// Output: Hello World</span>
</code></pre>
<p>Here, <code>str1</code> remains unchanged, and <code>str2</code> is a new <code>String</code> object with the modified value.</p>
<h2 id="heading-benefits-of-string-immutability">Benefits of String Immutability</h2>
<h3 id="heading-1-consistency-and-predictability">1. <strong>Consistency and Predictability</strong></h3>
<p>With immutable strings, you can be confident that the value of a string will remain consistent throughout its lifecycle. This predictability makes it easier to reason about your code and reduces the likelihood of bugs.</p>
<h3 id="heading-2-reduced-memory-overhead">2. <strong>Reduced Memory Overhead</strong></h3>
<p>String pooling reduces memory overhead by reusing existing string objects. This is possible only because strings are immutable, ensuring that no reference to a pooled string will be unexpectedly altered.</p>
<h3 id="heading-3-improved-performance-in-collections">3. <strong>Improved Performance in Collections</strong></h3>
<p>Immutable objects are often used as keys in collections like <code>HashMap</code> and <code>HashSet</code>. Since the hashcode of an immutable object remains constant, lookups are more efficient, and the integrity of the collection is maintained.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Understanding string immutability is essential for effective Java programming. It enhances security, improves performance, simplifies thread safety, and optimizes memory usage through string pooling. By embracing the immutability of strings, you can write more robust and maintainable Java applications.</p>
<p>Happy coding!</p>
]]></content:encoded></item><item><title><![CDATA[Java Class Structure: Unlocking the Secrets of Static and Instance Components]]></title><description><![CDATA[In Java, understanding the various components of a class structure is fundamental to mastering the language. Here, we'll break down a sample class, ClassStructure, to explore static variables, instance variables, static blocks, instance blocks, const...]]></description><link>https://blog.pratikupreti.com/java-class-structure-unlocking-the-secrets-of-static-and-instance-components</link><guid isPermaLink="true">https://blog.pratikupreti.com/java-class-structure-unlocking-the-secrets-of-static-and-instance-components</guid><category><![CDATA[static vs instance]]></category><category><![CDATA[java static]]></category><category><![CDATA[instance variable]]></category><category><![CDATA[Java]]></category><category><![CDATA[instance]]></category><category><![CDATA[static method]]></category><category><![CDATA[instance variables]]></category><dc:creator><![CDATA[Pratik Upreti]]></dc:creator><pubDate>Thu, 01 Aug 2024 19:04:18 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1722539514122/42c2dac3-e7d4-45e6-8fad-862010a73ad8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In Java, understanding the various components of a class structure is fundamental to mastering the language. Here, we'll break down a sample class, ClassStructure, to explore static variables, instance variables, static blocks, instance blocks, constructors, and methods.</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * ClassStructure
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassStructure</span> </span>{
  <span class="hljs-comment">// static variable</span>
  <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> count;

  <span class="hljs-comment">// instance variable</span>
  <span class="hljs-keyword">int</span> id;

  <span class="hljs-comment">// static block</span>
  <span class="hljs-comment">// runs once when the class is loaded into memory for the first time</span>
  <span class="hljs-keyword">static</span> {
    count = <span class="hljs-number">0</span>;
  }

  <span class="hljs-comment">// constructor</span>
  ClassStructure() {
    <span class="hljs-comment">// increment the static counter and assign it to the instance variable</span>
    id = ++count;
  }

  <span class="hljs-comment">// static method</span>
  <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printCount</span><span class="hljs-params">()</span> </span>{
    System.out.println(<span class="hljs-string">"Total objects created: "</span> + count);
  }

  <span class="hljs-comment">// instance method</span>
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printId</span><span class="hljs-params">()</span> </span>{
    System.out.println(<span class="hljs-string">"Object ID: "</span> + id);
  }

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
    ClassStructure obj1 = <span class="hljs-keyword">new</span> ClassStructure();
    ClassStructure obj2 = <span class="hljs-keyword">new</span> ClassStructure();
    ClassStructure obj3 = <span class="hljs-keyword">new</span> ClassStructure();

    obj1.printId(); <span class="hljs-comment">// Output: Object ID: 1</span>
    obj2.printId(); <span class="hljs-comment">// Output: Object ID: 2</span>
    obj3.printId(); <span class="hljs-comment">// Output: Object ID: 3</span>

    ClassStructure.printCount(); <span class="hljs-comment">// Output: Total objects created: 3</span>
  }
}
</code></pre>
<h2 id="heading-explanation">Explanation:</h2>
<h3 id="heading-1-static-variable-count">1. Static Variable (<code>count</code>)</h3>
<p><strong>What it is:</strong></p>
<ul>
<li><p>A static variable belongs to the class, not to any specific object.</p>
</li>
<li><p>It’s shared among all instances of the class.</p>
</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> count;
</code></pre>
<ul>
<li>This means there is one <code>count</code> variable for the whole <code>ClassStructure</code> class.</li>
</ul>
<h3 id="heading-2-instance-variable-id">2. Instance Variable (<code>id</code>)</h3>
<p><strong>What it is:</strong></p>
<ul>
<li><p>An instance variable belongs to an instance of the class (an object).</p>
</li>
<li><p>Each object has its own copy of the instance variable.</p>
</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> id;
</code></pre>
<ul>
<li>Each <code>ClassStructure</code> object will have its own <code>id</code>.</li>
</ul>
<h3 id="heading-3-static-block">3. Static Block</h3>
<p><strong>What it is:</strong></p>
<ul>
<li><p>A static block is used to initialize static variables.</p>
</li>
<li><p>It runs once when the class is first loaded into memory.</p>
</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">static</span> {
  count = <span class="hljs-number">0</span>;
}
</code></pre>
<ul>
<li>This sets the <code>count</code> variable to <code>0</code> when the class is loaded.</li>
</ul>
<h3 id="heading-4-constructor">4. Constructor</h3>
<p><strong>What it is:</strong></p>
<ul>
<li><p>A constructor is a special method that runs when you create a new object.</p>
</li>
<li><p>It is used to initialize instance variables.</p>
</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java">ClassStructure() {
  id = ++count;
}
</code></pre>
<ul>
<li>Every time a new <code>ClassStructure</code> object is created, the <code>count</code> variable is incremented by 1, and the <code>id</code> of the object is set to the new value of <code>count</code>.</li>
</ul>
<h3 id="heading-5-static-method-printcount">5. Static Method (<code>printCount</code>)</h3>
<p><strong>What it is:</strong></p>
<ul>
<li><p>A static method belongs to the class, not to any specific object.</p>
</li>
<li><p>It can be called using the class name without creating an object.</p>
</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java">  <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printCount</span><span class="hljs-params">()</span> </span>{
  System.out.println(<span class="hljs-string">"Total objects created: "</span> + count);
}
</code></pre>
<ul>
<li>This static method prints the total number of objects created.</li>
</ul>
<h3 id="heading-6-instance-method-printid">6. Instance Method (<code>printId</code>)</h3>
<p><strong>What it is:</strong></p>
<ul>
<li><p>An instance method belongs to an object.</p>
</li>
<li><p>It can use instance variables.</p>
</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printId</span><span class="hljs-params">()</span> </span>{
  System.out.println(<span class="hljs-string">"Object ID: "</span> + id);
}
</code></pre>
<ul>
<li>This instance method prints the <code>id</code> of the object.</li>
</ul>
<h3 id="heading-7-main-method">7. Main Method</h3>
<p><strong>What it is:</strong></p>
<ul>
<li>The <code>main</code> method is the entry point of the program. It runs when you execute the program.</li>
</ul>
<p><strong>Example in our class:</strong></p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
  ClassStructure obj1 = <span class="hljs-keyword">new</span> ClassStructure();
  ClassStructure obj2 = <span class="hljs-keyword">new</span> ClassStructure();
  ClassStructure obj3 = <span class="hljs-keyword">new</span> ClassStructure();

  obj1.printId(); <span class="hljs-comment">// Output: Object ID: 1</span>
  obj2.printId(); <span class="hljs-comment">// Output: Object ID: 2</span>
  obj3.printId(); <span class="hljs-comment">// Output: Object ID: 3</span>

  ClassStructure.printCount(); <span class="hljs-comment">// Output: Total objects created: 3</span>
}
</code></pre>
<ul>
<li><p>This creates three <code>ClassStructure</code> objects (<code>obj1</code>, <code>obj2</code>, <code>obj3</code>).</p>
</li>
<li><p>It calls the <code>printId</code> method on each object to print their unique IDs.</p>
</li>
<li><p>It calls the <code>printCount</code> method to print the total number of objects created.</p>
</li>
</ul>
<h3 id="heading-summary">Summary:</h3>
<ul>
<li><p><strong>Static variables</strong> are shared among all instances of the class.</p>
</li>
<li><p><strong>Instance variables</strong> are unique to each object.</p>
</li>
<li><p><strong>Static blocks</strong> initialize static variables once when the class is loaded.</p>
</li>
<li><p><strong>Constructors</strong> initialize instance variables each time a new object is created.</p>
</li>
<li><p>The <strong>main method</strong> is the entry point of the program.</p>
</li>
<li><p><strong>Static methods</strong> belong to the class and can be called without creating an object.</p>
</li>
<li><p><strong>Instance methods</strong> belong to objects and can use instance variables.</p>
</li>
</ul>
<p>This example helps in understanding how each component works and interacts within a Java class. By incrementing the <code>id</code> in the constructor, each new object gets a unique <code>id</code>, demonstrating practical use of static and instance variables. Happy coding!</p>
]]></content:encoded></item><item><title><![CDATA[A Comprehensive Guide to Abstraction and Encapsulation in Java]]></title><description><![CDATA[Object-Oriented Programming (OOP) in Java leverages key principles like Abstraction and Encapsulation to create efficient and maintainable code. Understanding these concepts is fundamental for any Java developer. Let's explore them in detail with int...]]></description><link>https://blog.pratikupreti.com/a-comprehensive-guide-to-abstraction-and-encapsulation-in-java</link><guid isPermaLink="true">https://blog.pratikupreti.com/a-comprehensive-guide-to-abstraction-and-encapsulation-in-java</guid><dc:creator><![CDATA[Pratik Upreti]]></dc:creator><pubDate>Thu, 11 Jan 2024 01:15:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1704935714571/7fecaad4-92e2-44ff-8d29-ff3feacdeac7.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Object-Oriented Programming (OOP) in Java leverages key principles like Abstraction and Encapsulation to create efficient and maintainable code. Understanding these concepts is fundamental for any Java developer. Let's explore them in detail with intuitive examples.</p>
<h2 id="heading-abstraction-in-java"><strong>Abstraction in Java</strong></h2>
<p>Abstraction in Java is the process of hiding the complex implementation details and showing only the necessary functionalities to the user.</p>
<h3 id="heading-how-is-abstraction-achieved-in-java"><strong>How is Abstraction Achieved in Java?</strong></h3>
<p>Abstraction in Java is implemented using abstract classes and interfaces.</p>
<ul>
<li><p><strong>Abstract Classes</strong>: These are classes that cannot be instantiated. They can have abstract methods (methods without a body) and concrete methods (methods with an implementation).</p>
</li>
<li><p><strong>Interfaces</strong>: An interface in Java is a completely "abstract class" that is used to group related methods with empty bodies.</p>
</li>
</ul>
<h3 id="heading-example-of-abstraction"><strong>Example of Abstraction:</strong></h3>
<p>Consider a simple example of a shape. We know every shape has an area, but the formula to calculate the area is different for each shape. Here, we can create an abstract class <code>Shape</code> with an abstract method <code>calculateArea()</code>.</p>
<pre><code class="lang-java">javaCopy codeabstract <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Shape</span> </span>{
    <span class="hljs-comment">// Abstract method</span>
    <span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">double</span> <span class="hljs-title">calculateArea</span><span class="hljs-params">()</span></span>;

    <span class="hljs-comment">// Concrete method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Displaying the shape details."</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Circle</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Shape</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> radius;

    Circle(<span class="hljs-keyword">double</span> r) {
        radius = r;
    }

    <span class="hljs-comment">// Implementation of abstract method</span>
    <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">calculateArea</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-number">3.14</span> * radius * radius;
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rectangle</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Shape</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> length;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> width;

    Rectangle(<span class="hljs-keyword">double</span> l, <span class="hljs-keyword">double</span> w) {
        length = l;
        width = w;
    }

    <span class="hljs-comment">// Implementation of abstract method</span>
    <span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">calculateArea</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> length * width;
    }
}
</code></pre>
<p>In this example, <code>Shape</code> is an abstract class that provides a common abstraction (<code>calculateArea()</code>) for different shapes. The specific implementation of area calculation is provided in the subclasses <code>Circle</code> and <code>Rectangle</code>.</p>
<h2 id="heading-encapsulation-in-java"><strong>Encapsulation in Java</strong></h2>
<p>Encapsulation is about keeping the internal state of an object hidden from the outside world while exposing a controlled interface to interact with that state.</p>
<h3 id="heading-how-is-encapsulation-achieved-in-java"><strong>How is Encapsulation Achieved in Java?</strong></h3>
<p>Encapsulation in Java is achieved using access modifiers: private, protected, and public. By setting fields as private and providing public getter and setter methods, we control access to the data.</p>
<h3 id="heading-example-of-encapsulation"><strong>Example of Encapsulation:</strong></h3>
<p>Consider the example of a bank account:</p>
<pre><code class="lang-java">javaCopy codeclass BankAccount {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> balance;

    <span class="hljs-comment">// Constructor</span>
    BankAccount(<span class="hljs-keyword">double</span> initialBalance) {
        balance = initialBalance;
    }

    <span class="hljs-comment">// Getter method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">getBalance</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> balance;
    }

    <span class="hljs-comment">// Methods to modify the balance</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">deposit</span><span class="hljs-params">(<span class="hljs-keyword">double</span> amount)</span> </span>{
        <span class="hljs-keyword">if</span> (amount &gt; <span class="hljs-number">0</span>) {
            balance += amount;
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">withdraw</span><span class="hljs-params">(<span class="hljs-keyword">double</span> amount)</span> </span>{
        <span class="hljs-keyword">if</span> (amount &lt;= balance) {
            balance -= amount;
        }
    }
}
</code></pre>
<p>In this example, the <code>balance</code> field is encapsulated within the <code>BankAccount</code> class. External access to the <code>balance</code> field is controlled through methods like <code>deposit()</code> and <code>withdraw()</code>, ensuring the integrity of the balance's state.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>In Java, abstraction simplifies complex reality by providing relevant interfaces, while encapsulation protects the data integrity and hides the implementation details. Both principles are crucial in creating robust, reusable, and maintainable code.</p>
<p>Remember, abstraction in Java is like defining a template (like the <code>Shape</code> class), while encapsulation is like safeguarding the internal state of an object (like the <code>balance</code> in <code>BankAccount</code>) and exposing only what is necessary.</p>
]]></content:encoded></item><item><title><![CDATA[Mastering the static Keyword in Java: A Beginner's Guide to Expert Understanding]]></title><description><![CDATA[Introduction
Welcome to the world of Java programming! If you've been curious about what the static keyword does in Java, you're in the right place. This guide is tailored for beginners but packed with enough detail to elevate your understanding to a...]]></description><link>https://blog.pratikupreti.com/mastering-the-static-keyword-in-java-a-beginners-guide-to-expert-understanding</link><guid isPermaLink="true">https://blog.pratikupreti.com/mastering-the-static-keyword-in-java-a-beginners-guide-to-expert-understanding</guid><category><![CDATA[when to use static keyword in java]]></category><category><![CDATA[keyword in java]]></category><category><![CDATA[Java]]></category><category><![CDATA[static keyword]]></category><category><![CDATA[static in java]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Pratik Upreti]]></dc:creator><pubDate>Wed, 10 Jan 2024 23:52:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1704932187039/cff45622-cd04-407b-bf26-46e4874408f6.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>Welcome to the world of Java programming! If you've been curious about what the <code>static</code> keyword does in Java, you're in the right place. This guide is tailored for beginners but packed with enough detail to elevate your understanding to an expert level. We'll explore <code>static</code> in a fun, intuitive way, with plenty of examples to solidify your learning.</p>
<h2 id="heading-getting-to-know-static-in-java"><strong>Getting to Know</strong> <code>static</code> in Java</h2>
<p>Imagine you're building a robot. Every robot you build has a unique name, but they're all made in the same factory. In Java, each robot is an 'object,' the factory is a 'class,' and <code>static</code> is a special word we use to say something belongs to the factory itself, not just one robot.</p>
<h3 id="heading-what-does-static-mean"><strong>What Does</strong> <code>static</code> Mean?</h3>
<p>In Java, <code>static</code> means that a particular variable or method belongs to the class as a whole, rather than to any individual object (or robot, in our analogy). It's like a shared resource or tool that any object created from that class can use.</p>
<h2 id="heading-static-variables-sharing-common-features"><strong>Static Variables: Sharing Common Features</strong></h2>
<p>Let's dive into static variables with an example. Suppose we're coding a program that tracks how many robots we've made.</p>
<pre><code class="lang-java">    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RobotFactory</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> robotCount = <span class="hljs-number">0</span>; <span class="hljs-comment">// This is a static variable</span>

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">RobotFactory</span><span class="hljs-params">()</span> </span>{
        robotCount++; <span class="hljs-comment">// We increase the count for every new robot made</span>
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getRobotCount</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> robotCount; <span class="hljs-comment">// This method lets us see how many robots we've made</span>
    }
}
</code></pre>
<p>In this example, <code>robotCount</code> is a static variable. It's shared by all instances (or robots) created from the <code>RobotFactory</code> class. Every time a new robot is made, <code>robotCount</code> goes up, no matter which specific robot it is.</p>
<h3 id="heading-why-use-static-variables"><strong>Why Use Static Variables?</strong></h3>
<p>Static variables are like a shared scoreboard or a common piece of data that every object created from the class can see and use. They're perfect for when you have information that isn't specific to one instance but is common to all instances.</p>
<h2 id="heading-static-methods-tools-for-everyone"><strong>Static Methods: Tools for Everyone</strong></h2>
<p>Now, let's talk about static methods. These are like tools in our factory that any robot can use, regardless of their individual features.</p>
<p>Consider a utility method that converts inches to centimeters. This is a common action and doesn't depend on individual robot characteristics.</p>
<pre><code class="lang-java">    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MeasurementTool</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">double</span> <span class="hljs-title">inchesToCentimeters</span><span class="hljs-params">(<span class="hljs-keyword">double</span> inches)</span> </span>{
        <span class="hljs-keyword">return</span> inches * <span class="hljs-number">2.54</span>; <span class="hljs-comment">// Converts inches to centimeters</span>
    }
}
</code></pre>
<p>Here, <code>inchesToCentimeters</code> is a static method. You don't need a specific instance to use it. Any part of your program can call <code>MeasurementTool.inchesToCentimeters</code> and get the job done.</p>
<h3 id="heading-when-to-use-static-methods"><strong>When to Use Static Methods</strong></h3>
<p>Static methods are fantastic for actions that are general-purpose and not tied to the state of an object. They're like communal tools or utilities that any instance, or even outside classes, can use.</p>
<h2 id="heading-static-blocks-special-setup-routines"><strong>Static Blocks: Special Setup Routines</strong></h2>
<p>Imagine you need to set up some common data or configurations before making any robots. This is where static blocks come in.</p>
<pre><code class="lang-java">    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RobotFactory</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Map&lt;String, String&gt; configurations;

    <span class="hljs-keyword">static</span> {
        configurations = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
        configurations.put(<span class="hljs-string">"defaultColor"</span>, <span class="hljs-string">"Silver"</span>);
        <span class="hljs-comment">// Add more configurations</span>
    }
}
</code></pre>
<p>In this code snippet, the static block runs once when the class is first used. It's setting up common configurations for all robots.</p>
<h2 id="heading-conclusion-embracingstatic"><strong>Conclusion: Embracing</strong><code>static</code></h2>
<p>The <code>static</code> keyword is a powerful feature in Java, allowing shared resources and utilities to be efficiently managed at the class level. It's like having a set of tools and counters available to all instances created from a class, promoting efficiency and shared functionality.</p>
<h2 id="heading-next-steps"><strong>Next Steps</strong></h2>
<p>Experiment with <code>static</code> in your Java programs. Try creating static variables, methods, and blocks in a sample class. See how they behave differently from instance-level features. Remember, practice makes perfect in the world of programming!</p>
]]></content:encoded></item><item><title><![CDATA[Day 2: Exploring const vs let vs var]]></title><description><![CDATA[Recap : Day 1
In Day 1 of our JavaScript journey, you set up your development environment, cooked your first JavaScript "Hello World".Today, we're going to take a closer look at a fundamental aspect of JavaScript: variables. Variables are like contai...]]></description><link>https://blog.pratikupreti.com/day-2-exploring-const-vs-let-vs-var</link><guid isPermaLink="true">https://blog.pratikupreti.com/day-2-exploring-const-vs-let-vs-var</guid><category><![CDATA[cookingwithjs]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[var let const]]></category><category><![CDATA[day2]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[#beginners #learningtocode #100daysofcode]]></category><category><![CDATA[js]]></category><dc:creator><![CDATA[Pratik Upreti]]></dc:creator><pubDate>Wed, 13 Dec 2023 08:51:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1704932254931/b023810d-2cc5-40bd-b5d7-00a3dff8c222.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-recap-day-1">Recap : Day 1</h2>
<p>In Day 1 of our JavaScript journey, you set up your development environment, cooked your first JavaScript "Hello World".Today, we're going to take a closer look at a fundamental aspect of JavaScript: variables. Variables are like containers in your kitchen where you store data, just as you would store ingredients in jars. However, JavaScript offers three types of containers with distinct characteristics: <code>let</code>, <code>const</code>, and <code>var</code>. Let's dive deeper into these culinary-inspired JavaScript containers.</p>
<h2 id="heading-let-your-versatile-ingredient-jar"><code>let</code><strong>: Your Versatile Ingredient Jar</strong></h2>
<p>Imagine <code>let</code> as your versatile ingredient jar. It's like a container that you can easily open and change its content whenever you like. This makes it perfect for storing ingredients that might change during a recipe. For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> ingredient = <span class="hljs-string">"Tomato"</span>; <span class="hljs-comment">// The jar is labeled "ingredient," and it contains a tomato.</span>
ingredient = <span class="hljs-string">"Onion"</span>; <span class="hljs-comment">// Now it contains an onion.</span>
<span class="hljs-built_in">console</span>.log(ingredient); <span class="hljs-comment">// You can easily change the content inside.</span>
</code></pre>
<p>In this analogy, think of the <code>let</code> variable as a jar that you can open, empty, and fill with different ingredients as needed. It provides flexibility, allowing you to update its content based on the requirements of your code.</p>
<h2 id="heading-const-your-fixed-ingredient-jar"><code>const</code><strong>: Your Fixed-Ingredient Jar</strong></h2>
<p>In contrast to <code>let</code>, <code>const</code> is like a jar with a seal that cannot be opened once you've put an ingredient inside. It's perfect for storing constants or ingredients that should never change. Once sealed, you can't modify the content inside. For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> age = <span class="hljs-number">25</span>; <span class="hljs-comment">// The jar is sealed with the number 25.</span>
<span class="hljs-comment">// You can't change the content once it's sealed.</span>
<span class="hljs-built_in">console</span>.log(age); <span class="hljs-comment">// It will always be 25.</span>
</code></pre>
<p>The <code>const</code> variable is like sealing a jar with a specific ingredient, ensuring it remains unchanged throughout your cooking process. It's ideal for storing values that should remain constant and unaltered.</p>
<h2 id="heading-var-the-old-ingredient-jar"><code>var</code><strong>: The Old Ingredient Jar</strong></h2>
<p><code>var</code> used to be the primary type of jar for ingredients in JavaScript, but it's not as strict as <code>let</code> and <code>const</code>. It's like an old jar that you can use, but it might not follow all the modern kitchen rules. While it's less commonly used in modern JavaScript, it's essential to understand its characteristics.</p>
<pre><code class="lang-javascript">javascriptCopy codevar name = <span class="hljs-string">"Demo"</span>; <span class="hljs-comment">// The jar labeled "name" can store anything.</span>
name = <span class="hljs-string">"Alice"</span>; <span class="hljs-comment">// You can change it, but it's not as clear as `let` and `const`.</span>
<span class="hljs-built_in">console</span>.log(name);
</code></pre>
<p>In this analogy, the <code>var</code> variable is like an old jar that doesn't have as clear labeling or sealing as <code>let</code> and <code>const</code>. It's less predictable and more lenient, which can lead to unexpected behavior in some cases.</p>
<p>Understanding the differences between <code>let</code>, <code>const</code>, and <code>var</code> is crucial as you cook up your JavaScript recipes. Just like in the kitchen, choosing the right type of container (variable) can greatly affect the outcome of your code.</p>
<h2 id="heading-operators-the-spices-of-javascript"><strong>Operators: The Spices of JavaScript</strong></h2>
<p>Now that we've covered our ingredient jars let's talk about the spices—operators—that add flavor to your JavaScript recipes. Operators are symbols that perform operations on variables and values. Here are some common JavaScript operators:</p>
<h3 id="heading-arithmetic-operators"><strong>Arithmetic Operators</strong></h3>
<ul>
<li><p><strong>Addition (+)</strong>: Adds two values.</p>
</li>
<li><p><strong>Subtraction (-)</strong>: Subtracts the second value from the first.</p>
</li>
<li><p><strong>Multiplication (*)</strong>: Multiplies two values.</p>
</li>
<li><p><strong>Division (/)</strong>: Divides the first value by the second.</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-javascript">javascriptCopy codelet a = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">3</span>;
<span class="hljs-keyword">let</span> sum = a + b; <span class="hljs-comment">// sum is now 8</span>
<span class="hljs-keyword">let</span> product = a * b; <span class="hljs-comment">// product is now 15</span>
</code></pre>
<h3 id="heading-assignment-operators"><strong>Assignment Operators</strong></h3>
<ul>
<li><p><strong>Assignment (=)</strong>: Assigns a value to a variable.</p>
</li>
<li><p><strong>Addition Assignment (+=)</strong>: Adds a value to a variable and assigns the result.</p>
</li>
<li><p><strong>Subtraction Assignment (-=)</strong>: Subtracts a value from a variable and assigns the result.</p>
</li>
<li><p><strong>Multiplication Assignment (*=)</strong>: Multiplies a variable by a value and assigns the result.</p>
</li>
<li><p><strong>Division Assignment (/=)</strong>: Divides a variable by a value and assigns the result.</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-javascript">javascriptCopy codelet x = <span class="hljs-number">10</span>;
x += <span class="hljs-number">5</span>; <span class="hljs-comment">// x is now 15</span>
x -= <span class="hljs-number">3</span>; <span class="hljs-comment">// x is now 12</span>
</code></pre>
<h3 id="heading-comparison-operators"><strong>Comparison Operators</strong></h3>
<ul>
<li><p><strong>Equal (==)</strong>: Checks if two values are equal.</p>
</li>
<li><p><strong>Not Equal (!=)</strong>: Checks if two values are not equal.</p>
</li>
<li><p><strong>Strict Equal (===)</strong>: Checks if two values are equal without type conversion.</p>
</li>
<li><p><strong>Strict Not Equal (!==)</strong>: Checks if two values are not equal without type conversion.</p>
</li>
<li><p><strong>Greater Than (&gt;)</strong>: Checks if the first value is greater than the second.</p>
</li>
<li><p><strong>Less Than (&lt;)</strong>: Checks if the first value is less than the second.</p>
</li>
<li><p><strong>Greater Than or Equal (&gt;=)</strong>: Checks if the first value is greater than or equal to the second.</p>
</li>
<li><p><strong>Less Than or Equal (&lt;=)</strong>: Checks if the first value is less than or equal to the second.</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-javascript">javascriptCopy codelet pears = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> apples = <span class="hljs-number">3</span>;

<span class="hljs-keyword">let</span> isEqual = pears == apples; <span class="hljs-comment">// false</span>
<span class="hljs-keyword">let</span> isNotEqual = pears != apples; <span class="hljs-comment">// true</span>
<span class="hljs-keyword">let</span> isStrictEqual = pears === apples; <span class="hljs-comment">// false</span>
<span class="hljs-keyword">let</span> isGreaterThan = pears &gt; apples; <span class="hljs-comment">// true</span>
</code></pre>
<h3 id="heading-logical-operators"><strong>Logical Operators</strong></h3>
<ul>
<li><p><strong>Logical AND (&amp;&amp;)</strong>: Returns true if both conditions are true.</p>
</li>
<li><p><strong>Logical OR (||)</strong>: Returns true if at least one condition is true.</p>
</li>
<li><p><strong>Logical NOT (!)</strong>: Returns the opposite of the condition.</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-javascript">javascriptCopy codelet isSunny = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> isWarm = <span class="hljs-literal">false</span>;

<span class="hljs-keyword">let</span> isBeachWeather = isSunny &amp;&amp; isWarm; <span class="hljs-comment">// false</span>
<span class="hljs-keyword">let</span> isGoodWeather = isSunny || isWarm; <span class="hljs-comment">// true</span>
<span class="hljs-keyword">let</span> isNotSunny = !isSunny; <span class="hljs-comment">// false</span>
</code></pre>
<h3 id="heading-conditional-ternary-operator"><strong>Conditional (Ternary) Operator</strong></h3>
<ul>
<li><strong>Conditional (Ternary) Operator (?:)</strong>: A shorthand way of writing <code>if-else</code> statements.</li>
</ul>
<p>Example:</p>
<pre><code class="lang-javascript">javascriptCopy codelet age = <span class="hljs-number">20</span>;
<span class="hljs-keyword">let</span> canVote = age &gt;= <span class="hljs-number">18</span> ? <span class="hljs-string">"Yes"</span> : <span class="hljs-string">"No"</span>; <span class="hljs-comment">// "Yes"</span>
</code></pre>
<p>These operators are the spices that add flavor to your JavaScript recipes. Just like in cooking, using the right spices (operators) at the right time is crucial for achieving the desired outcome in your code.</p>
<h2 id="heading-stay-tuned-for-day-3-making-choices-in-javascriptexploring-if-statements-and-loops"><strong>Stay tuned for Day 3: "Making Choices in JavaScript:Exploring</strong> <code>if</code> <strong>Statements and Loops."</strong></h2>
<p>Stay tuned for more JavaScript adventures as we continue exploring the world of web development!we'll explore the art of decision-making in JavaScript. Just like a chef chooses recipes based on available ingredients, as a developer, you'll make choices in your code. We'll introduce you to control structures like <code>if</code> statements and loops, enabling you to create dynamic and interactive code. Get ready to level up your JavaScript skills! Until then, keep experimenting with your new knowledge and happy coding!</p>
]]></content:encoded></item><item><title><![CDATA[Day 1: Beginning Your Journey with JavaScript]]></title><description><![CDATA[Welcome to the World of JavaScript
Imagine you're a chef. Just like a chef uses various ingredients and recipes to create a dish, a programmer uses a programming language to build websites and applications. JavaScript is one of these essential 'ingre...]]></description><link>https://blog.pratikupreti.com/day-1-beginning-your-journey-with-javascript</link><guid isPermaLink="true">https://blog.pratikupreti.com/day-1-beginning-your-journey-with-javascript</guid><category><![CDATA[#cookwithjs]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[js]]></category><category><![CDATA[javascript for beginners]]></category><dc:creator><![CDATA[Pratik Upreti]]></dc:creator><pubDate>Fri, 01 Dec 2023 19:03:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701453978888/96e888a6-c770-4315-a534-c50943bc81c8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-welcome-to-the-world-of-javascript"><strong>Welcome to the World of JavaScript</strong></h3>
<p>Imagine you're a chef. Just like a chef uses various ingredients and recipes to create a dish, a programmer uses a programming language to build websites and applications. JavaScript is one of these essential 'ingredients' in web development.</p>
<p>It started as a simple way to make web pages interactive but has now grown into a language that can power both front-end and back-end development.</p>
<h3 id="heading-setting-up-your-kitchen-development-environment"><strong>Setting Up Your Kitchen (Development Environment)</strong></h3>
<p>Before you start cooking (coding), you need a kitchen (development environment). Here’s how to set it up:</p>
<ul>
<li><p><strong>Node.js:</strong> Think of it as your stove – it's where you'll 'cook' your JavaScript code. Download it from the <a target="_blank" href="https://nodejs.org/">Node.js official website</a>.</p>
</li>
<li><p><strong>Text Editor:</strong> This is like your chopping board. A place where you prepare your code. A popular choice is Visual Studio Code, which you can get from <a target="_blank" href="https://code.visualstudio.com/">VS Code official website</a>.</p>
</li>
</ul>
<h3 id="heading-cooking-your-first-dish-your-first-javascript-program"><strong>Cooking Your First Dish (Your First JavaScript Program)</strong></h3>
<p>Let’s cook up something simple. We’ll make the "Hello, World!" dish. In your text editor, create a new file named <code>hello.js</code> and add the following recipe (code):</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hello, World!'</span>);
</code></pre>
<p>Now, '<strong>cook</strong>' this dish. Open the terminal (like turning on your stove), go to the folder where your file is, and type <code>node hello.js</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701455487983/e1c4b5af-8f80-4264-acc3-bbbbbdf1a41f.png" alt class="image--center mx-auto" /></p>
<p>Voilà! You should see <code>Hello, World!</code> – your dish is served (your code is executed).</p>
<h3 id="heading-basic-ingredients-of-javascript"><strong>Basic Ingredients of JavaScript</strong></h3>
<ul>
<li><p><strong>Variables:</strong> These are like containers in your kitchen. You store data (ingredients) in them, just like you store salt in a container/jar.</p>
</li>
<li><p>In JavaScript, you can declare variables using <code>let</code>, <code>const</code>, or <code>var</code>.In the next blog we will see the main difference between them.</p>
</li>
<li><pre><code class="lang-javascript">    <span class="hljs-keyword">let</span> ingredient = <span class="hljs-string">"Tomato"</span>;
    <span class="hljs-built_in">console</span>.log(ingredient);

    <span class="hljs-keyword">var</span> name=<span class="hljs-string">"Demo"</span>;
    <span class="hljs-built_in">console</span>.log(name);

    <span class="hljs-keyword">const</span> age=<span class="hljs-number">25</span>;
    <span class="hljs-built_in">console</span>.log(age);
</code></pre>
<p>  <strong>Data Types:</strong> Just like ingredients can be vegetables, fruits, or spices, data in JavaScript has types. The common ones are strings (text), numbers, and booleans (true/false).</p>
</li>
<li><pre><code class="lang-javascript">    <span class="hljs-keyword">let</span> age = <span class="hljs-number">10</span>; <span class="hljs-comment">// Number</span>
    <span class="hljs-keyword">let</span> name = <span class="hljs-string">"Mango"</span>; <span class="hljs-comment">// String</span>
    <span class="hljs-keyword">let</span> isRipe = <span class="hljs-literal">true</span>; <span class="hljs-comment">// Boolean (true/false)</span>
</code></pre>
<p>  <strong>Operators:</strong> These are like your culinary techniques. They help you perform operations on your variables and values. For example, adding (<code>+</code>), subtracting (<code>-</code>), etc.</p>
</li>
<li><pre><code class="lang-javascript">    <span class="hljs-keyword">let</span> sum = <span class="hljs-number">5</span> + <span class="hljs-number">3</span>; <span class="hljs-comment">// Adding 5 and 3</span>
    <span class="hljs-keyword">let</span> difference = <span class="hljs-number">10</span> - <span class="hljs-number">2</span>; <span class="hljs-comment">// Subtracting 2 from 10Wrapping Up Day 1 and A Peek into Day 2 Great job on your first day! You’ve set up your kitchen, cooked your first dish, and learned about some basic ingredients of JavaScript. Tomorrow, we’ll explore how to make decisions in your code, like choosing what dish to cook based on what ingredients you have – we’ll learn about control structures like if statements and loops. Stay tuned!</span>
</code></pre>
</li>
</ul>
<h3 id="heading-wrapping-up-day-1-and-a-peek-into-day-2"><strong>Wrapping Up Day 1 and A Peek into Day 2</strong></h3>
<p>Great job on your first day! You’ve set up your kitchen, cooked your first dish, and learned about some basic ingredients of JavaScript. Tomorrow, we’ll explore more on <strong>types of variables</strong> and their major differences, how to make <strong>decisions</strong> in your code, like choosing what dish to cook based on what ingredients you have – we’ll learn about control structures like <code>if</code> statements and loops. Stay tuned!</p>
]]></content:encoded></item></channel></rss>