<?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: Jani Syed</title>
    <description>The latest articles on DEV Community by Jani Syed (@janisyed18).</description>
    <link>https://dev.to/janisyed18</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%2F1102910%2Fde11aa30-73bf-4b84-a463-0a110e501ca3.jpeg</url>
      <title>DEV Community: Jani Syed</title>
      <link>https://dev.to/janisyed18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/janisyed18"/>
    <language>en</language>
    <item>
      <title>Dependency Injection in Spring Boot: The Wizard Behind the Curtain</title>
      <dc:creator>Jani Syed</dc:creator>
      <pubDate>Mon, 16 Sep 2024 15:16:53 +0000</pubDate>
      <link>https://dev.to/janisyed18/dependency-injection-in-spring-boot-the-wizard-behind-the-curtain-49n8</link>
      <guid>https://dev.to/janisyed18/dependency-injection-in-spring-boot-the-wizard-behind-the-curtain-49n8</guid>
      <description>&lt;p&gt;Dependency Injection in Spring Boot: The Wizard Behind the Curtain&lt;/p&gt;

&lt;p&gt;Ever feel like Spring Boot is a magical butler that somehow just &lt;em&gt;knows&lt;/em&gt; what you need and hands it to you on a silver platter? That's basically &lt;strong&gt;Dependency Injection&lt;/strong&gt; (DI). You’ve likely used DI a hundred times without stopping to wonder: &lt;em&gt;How the heck does Spring know what to inject and when?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If that sounds like you, welcome aboard! We’re going to take a fun, behind-the-scenes tour of how Spring Boot's DI works its wizardry, starting with how it manages beans, &lt;code&gt;@Autowired&lt;/code&gt;, and bean lifecycles—from birth to destruction. By the end of this blog, you’ll be flexing your newfound DI knowledge like a pro.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What Is Dependency Injection? And Why Should You Care?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In layman's terms, Dependency Injection is like getting groceries delivered to your door instead of going out to buy them yourself. It’s about delegating the responsibility of "injecting" dependencies (beans) to Spring so that you don't have to manually create objects or worry about their lifecycle.&lt;/p&gt;

&lt;p&gt;Imagine you’re a chef running a busy kitchen (your application). You don’t have time to run out and pick up eggs, milk, and sugar every time you need them. Wouldn’t it be great if someone (say, Spring) just &lt;em&gt;magically&lt;/em&gt; delivered everything you needed exactly when you needed it?&lt;/p&gt;

&lt;p&gt;That’s exactly what Spring DI does: It finds all the ingredients (beans) you need and injects them into your code without you lifting a finger. Pretty neat, right?&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The Magic of Spring Container: Your Personal Butler&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Okay, so here’s where the magic happens. When you run your Spring Boot app using &lt;code&gt;SpringApplication.run()&lt;/code&gt;, Spring bootstraps the &lt;strong&gt;ApplicationContext&lt;/strong&gt;—think of it as your butler’s instruction manual. It knows exactly what to fetch and when.&lt;/p&gt;

&lt;p&gt;Let’s break it down step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Container Initialization:&lt;/strong&gt; When you hit &lt;code&gt;SpringApplication.run()&lt;/code&gt;, the Spring container (a.k.a. &lt;strong&gt;ApplicationContext&lt;/strong&gt;) springs into action. It’s like opening the doors to your virtual restaurant, where everything is ready to roll.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bean Creation:&lt;/strong&gt; The container scans your code for annotations like &lt;code&gt;@Component&lt;/code&gt;, &lt;code&gt;@Service&lt;/code&gt;, &lt;code&gt;@Repository&lt;/code&gt;, or &lt;code&gt;@Controller&lt;/code&gt;. Each of these becomes a &lt;strong&gt;bean&lt;/strong&gt;—an object managed by Spring. Think of them as the essential ingredients in your kitchen: flour, sugar, eggs, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;BeanFactory to the Rescue:&lt;/strong&gt; Spring Boot uses &lt;strong&gt;BeanFactory&lt;/strong&gt; to create and manage these beans. This factory knows exactly how and when to create your beans, making sure they’re available when needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Injection:&lt;/strong&gt; Once the beans are ready, Spring injects them wherever you’ve marked with &lt;code&gt;@Autowired&lt;/code&gt;. It’s like having a barista who doesn’t just make coffee, but also delivers it to the exact counter where it’s needed. You don’t even have to think about it—everything just &lt;em&gt;shows up&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;How Does @Autowired Work? Sherlock Holmes of Beans&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Ah, the good ol’ &lt;code&gt;@Autowired&lt;/code&gt; annotation. Ever wonder how Spring magically knows where to inject dependencies? It’s kind of like a detective that matches your needs with the right beans in its registry. &lt;/p&gt;

&lt;p&gt;Here’s how it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type Matching:&lt;/strong&gt; When Spring sees &lt;code&gt;@Autowired&lt;/code&gt;, it looks for a bean of the same type in the container. Imagine you ordered coffee beans (a &lt;code&gt;CoffeeService&lt;/code&gt; class), Spring looks in its bean repository and says, “Ah, I’ve got those! Let me inject them for you.”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Qualifiers:&lt;/strong&gt; But what if you have multiple beans of the same type? In that case, Spring might freak out and throw an exception like “NoUniqueBeanDefinitionException.” But don’t worry—you can calm Spring down by using &lt;code&gt;@Qualifier&lt;/code&gt; to specify which bean to inject:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Autowired&lt;/span&gt;
&lt;span class="nd"&gt;@Qualifier&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"espressoBean"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;CoffeeService&lt;/span&gt; &lt;span class="n"&gt;coffeeService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constructor Injection (the Best Way):&lt;/strong&gt; These days, constructor injection is the cool kid on the block. Not only does it make your beans immutable, but it also makes testing a breeze. Here’s how you do it:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoffeeShop&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;CoffeeService&lt;/span&gt; &lt;span class="n"&gt;coffeeService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;CoffeeShop&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CoffeeService&lt;/span&gt; &lt;span class="n"&gt;coffeeService&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;coffeeService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coffeeService&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;Spring goes on autopilot, injecting the beans into the constructor, and voilà—you’re good to go!&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The Lifecycle of a Spring Bean: From Birth to Retirement Party&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Beans in Spring Boot aren’t just objects. They have full-fledged lives, complete with an origin story, a fulfilling career, and an eventual retirement. Let’s follow the lifecycle of a bean:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Instantiation (Birth):&lt;/strong&gt; First, Spring creates an instance of the bean. This is like the bean’s birth. Spring goes, "Here you go, little guy!" and passes it into the container.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Injection:&lt;/strong&gt; After creating the bean, Spring populates it with dependencies (like ingredients in a cake recipe). This is where &lt;code&gt;@Autowired&lt;/code&gt; comes into play. Your bean gets everything it needs to work properly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Post-Initialization:&lt;/strong&gt; If you have methods annotated with &lt;code&gt;@PostConstruct&lt;/code&gt;, Spring calls those after it injects the dependencies. It’s like giving the bean a fresh coat of paint before it goes to work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ready for Action:&lt;/strong&gt; Now your bean is alive and kicking. It’s ready to take on the world!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pre-Destruction (Retirement):&lt;/strong&gt; When the application shuts down, Spring calls &lt;code&gt;@PreDestroy&lt;/code&gt; methods to give the bean a graceful exit. This is the bean's retirement party, where it cleans up its resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bean Destruction:&lt;/strong&gt; Finally, the bean is destroyed. Time to rest in peace.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s how you can track these lifecycle events in 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="nd"&gt;@Component&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;CoffeeBean&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@PostConstruct&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;onStart&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;"Bean is ready to brew some coffee!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@PreDestroy&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;onEnd&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;"Bean is retiring. Goodbye, world!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Bean Scopes: How Long Does the Magic Last?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Not all beans have the same life expectancy. Spring Boot allows you to define different &lt;strong&gt;scopes&lt;/strong&gt; for beans—basically how long they live. The two most common ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Singleton (the Default):&lt;/strong&gt; There’s only one instance of the bean, shared across the entire application. It’s like having one espresso machine for the whole coffee shop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prototype:&lt;/strong&gt; A new instance of the bean is created every time it’s needed. Imagine having a fresh espresso machine for every single order. It’s resource-heavy, but sometimes necessary.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Scope&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"prototype"&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;LatteMachine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This bean is made fresh for every use&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;SpringApplication.run(): The Grandmaster of DI&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Alright, let’s talk about what happens when you run your Spring Boot app using &lt;code&gt;SpringApplication.run()&lt;/code&gt;. This method is the grandmaster that kicks off the whole DI process.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Start the Application Context:&lt;/strong&gt; Spring fires up the &lt;strong&gt;ApplicationContext&lt;/strong&gt;, where all beans live.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scan for Beans:&lt;/strong&gt; Spring scans your code for beans and registers them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inject Dependencies:&lt;/strong&gt; Once the beans are ready, Spring starts injecting them wherever &lt;code&gt;@Autowired&lt;/code&gt; is used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Launch the Application:&lt;/strong&gt; Once everything is in place, the application goes live. Magic complete.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Real-Life Analogy: DI in a Coffee Shop&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Think of your Spring Boot application as a coffee shop. You’re the owner, and the beans are your ingredients: coffee, milk, sugar, etc. Instead of running around managing these ingredients yourself, you’ve got a barista (the Spring container) who fetches everything and delivers it exactly where it’s needed.&lt;/p&gt;

&lt;p&gt;All you have to do is give the orders (set up your &lt;code&gt;@Autowired&lt;/code&gt; fields), and the barista handles the rest—perfectly brewing that dependency-filled cup of coffee for your customers (application).&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Wrapping It Up: DI Is Your Superpower&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At the end of the day, &lt;strong&gt;Dependency Injection&lt;/strong&gt; is what makes Spring Boot such a powerful framework. It simplifies your life, manages your beans, and ensures your code is easy to maintain and extend.&lt;/p&gt;

&lt;p&gt;Now that you’ve peeked behind the curtain, you’ve got a superpower that many developers take for granted. So go ahead—start using DI like the wizard you now are. And the next time you see &lt;code&gt;@Autowired&lt;/code&gt;, you’ll know exactly what’s going on under the hood.&lt;/p&gt;




&lt;p&gt;I hope this blog gave you a deeper understanding of Spring Boot DI and left you with a smile. Now go inject some beans and show your friends how it's done!&lt;/p&gt;




&lt;p&gt;How’s that for a blog that’s fun, informative, and easy to understand? Let me know if you'd like any more tweaks!&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>dependencyinjection</category>
      <category>learning</category>
    </item>
    <item>
      <title>Unveiling the Java Superhero: The Mysterious Object Class</title>
      <dc:creator>Jani Syed</dc:creator>
      <pubDate>Fri, 16 Feb 2024 20:14:25 +0000</pubDate>
      <link>https://dev.to/janisyed18/unveiling-the-java-superhero-the-mysterious-object-class-260k</link>
      <guid>https://dev.to/janisyed18/unveiling-the-java-superhero-the-mysterious-object-class-260k</guid>
      <description>&lt;p&gt;Ever wondered which class in Java holds the key to the kingdom of all classes? Is it the dazzling String, the wizard of calculations, Math, or the multitasking maestro Thread? Brace yourself because it's none other than the unsung hero - the Object class! Yes, you heard it right. It's the ultimate parent, the grand maestro that orchestrates the symphony of all Java classes. In this blog, let's dive deep into the concealed powers of the Object class and unravel its role in every Java program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meet the Object Class
&lt;/h2&gt;

&lt;p&gt;Nestled in the java.lang package, the Object class is the maestro, the top dog of the class hierarchy. Every class, whether home-brewed or snatched from the standard library, inherits from it – either directly or indirectly. So, what mystical powers does this class bestow upon its descendants?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Equality Dance&lt;/strong&gt;: The Object class gifts us the equals() method, enabling objects to tango and check if they are equal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hash Wizardry&lt;/strong&gt;: Need a unique signature for your object? The hashCode() method is at your service, cooking up a hash code that's as unique as a fingerprint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cloning Delight&lt;/strong&gt;: With the clone() method, objects can multiply like rabbits, creating clones of themselves.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;String Serenade&lt;/strong&gt;: The toString() method lets objects serenade us with their string representation. A charming act indeed!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Thread Tango&lt;/strong&gt;: For those in the concurrency ballroom, Object brings the dance moves with notify(), notifyAll(), and wait() methods.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Final Countdown&lt;/strong&gt;: Objects get a chance to do some last-minute actions before bidding adieu with the finalize() method.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These powers, bestowed by the Object class, are the foundations of Java's object-oriented prowess. However, each subclass can spice things up by overriding these methods to tailor them for specific needs. For instance, the String class takes the equals() method and transforms it into a content comparer rather than a memory address matcher.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Object Class in Action
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Grand Upcast
&lt;/h3&gt;

&lt;p&gt;Picture this: every class extending the Object class, creating a family tree that makes the British royals look like distant relatives. This inheritance means that every object is an instance of the Object class. So, we can play the grand upcast game:&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;Object&lt;/span&gt; &lt;span class="n"&gt;obj&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;"Alice"&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="s"&gt;"CS101"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This magical upcasting allows us to write code that can dance with any object, as long as it's a subclass of the Object class. For example:&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;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;printObject&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;"Alice"&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="s"&gt;"CS101"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// prints Student@12345678&lt;/span&gt;
&lt;span class="n"&gt;printObject&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;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bob"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// prints Employee@87654321&lt;/span&gt;
&lt;span class="n"&gt;printObject&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;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// prints Hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Grand Return
&lt;/h3&gt;

&lt;p&gt;The Object class also sneaks into method signatures, posing as the default return type and parameter type. Take the getClass() method, for instance:&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;final&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method returns a Class object, a generic type representing a class's metadata. As a bonus, the Class object also extends the Object class. Similarly, the clone() method plays a mysterious game:&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;protected&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;CloneNotSupportedException&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It returns an Object, forcing us into the world of downcasting:&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="s"&gt;"Alice"&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="s"&gt;"CS101"&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="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="n"&gt;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// downcast the Object to Student&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A risky move that demands precision, as a ClassCastException might strike if the object isn't of the expected type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interface Intrigue
&lt;/h3&gt;

&lt;p&gt;The Object class doesn't stop there; it's the VIP guest at many interfaces and abstract classes in Java. The Serializable interface, for instance, extends the Object class, marking classes as serializable. Similarly, the Comparable interface adds a dash of order, allowing objects to define their natural sequence.&lt;/p&gt;

&lt;p&gt;For instance, the String class is a multitasker, implementing both Serializable and Comparable, inheriting the Object class's grandeur.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Grand Finale
&lt;/h2&gt;

&lt;p&gt;In conclusion, the Object class is not just a class; it's the masked superhero of Java. It shapes the destiny of every object, providing a common ground for behaviors and properties. Whether it's the grand upcast, the mysterious return types, or the interface intrigue, the Object class is the star of the show. So, next time you create a class in Java, remember, you're not just extending a class – you're inheriting the legacy of the ultimate Java superhero, the Object class!&lt;/p&gt;

</description>
      <category>objects</category>
      <category>java</category>
      <category>programming</category>
      <category>class</category>
    </item>
    <item>
      <title>Thread Safety in Java: Keeping Threads from Going Rogue!</title>
      <dc:creator>Jani Syed</dc:creator>
      <pubDate>Mon, 07 Aug 2023 19:16:00 +0000</pubDate>
      <link>https://dev.to/janisyed18/thread-safety-in-java-keeping-threads-from-going-rogue-1bah</link>
      <guid>https://dev.to/janisyed18/thread-safety-in-java-keeping-threads-from-going-rogue-1bah</guid>
      <description>&lt;p&gt;Hello there, fellow Java developers! Have you ever had a situation where your Java program was acting all crazy, throwing errors left and right, and you couldn't figure out why? Well, my friend, you might have encountered the infamous thread concurrency issue! But fear not, for in this blog, we'll unravel the mysteries of thread safety and show you how to tame those wild threads!&lt;/p&gt;

&lt;p&gt;Imagine threads as mischievous little gremlins running around your code. They are like tiny execution units that can work simultaneously, each doing its own thing. Now, if you have shared resources like variables or data structures that these threads can access, trouble might be brewing!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Race Condition:&lt;/strong&gt;&lt;br&gt;
Let's say Gremlin 1 and Gremlin 2 are both trying to update the same variable simultaneously. Gremlin 1 wants to change it to "A," while Gremlin 2 prefers "B." Oh boy, chaos ensues! One gremlin might override the other's changes, leading to data corruption - like trying to squeeze two friends into the same tiny car seat! This is what we call a "race condition."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deadlock:&lt;/strong&gt;&lt;br&gt;
Another conundrum is the dreaded deadlock! Imagine Gremlin 1 holding on to a resource and waiting for Gremlin 2 to release another resource, while Gremlin 2 is holding on to that resource and waiting for Gremlin 1 to let go. They are stuck in a never-ending stare-off, like two arm-wrestlers who can't decide the winner!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Inconsistency:&lt;/strong&gt;&lt;br&gt;
Gremlins can also be forgetful! If one gremlin modifies a resource while another is reading it, the data might end up inconsistent - like two people reading different chapters of the same book and discussing it without knowing the full story!&lt;/p&gt;

&lt;p&gt;So how do we keep those gremlins in check and make our code thread-safe? Here are some nifty techniques:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronization&lt;/strong&gt;: This is like giving each gremlin a ticket to access the shared resource one at a time. Only the gremlin with the ticket can make changes while others wait patiently in line. No more stepping on each other's toes!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt;: Make your resources immutable, meaning they can't be changed once created. It's like having a magical shield that protects the resource from greedy gremlins. They can look, but they can't touch!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Volatile Variables&lt;/strong&gt;: This is like hanging a big sign on the resource saying, "Hey, gremlins, this resource is for everyone to see and use!" Volatile variables ensure that changes made by one thread are immediately visible to other threads.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, you might be wondering, "But which Java methods, classes, and data types are already thread-safe?" Great question, dear developer!&lt;/p&gt;

&lt;p&gt;Java has some built-in goodies for us:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronized Methods&lt;/strong&gt;: These methods are like the Swiss Army Knives of thread safety. They can only be used by one thread at a time, ensuring your resource stays out of trouble.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;java.util.concurrent Package&lt;/strong&gt;: It's like a treasure trove of thread-safe classes and data types. &lt;code&gt;ConcurrentHashMap&lt;/code&gt; is your superhero for managing key-value pairs, and &lt;code&gt;AtomicInteger&lt;/code&gt; is your trusty sidekick for atomic operations on integers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So why do we need thread safety, you ask? Well, imagine a world without it. Gremlins running wild, variables changing at random, deadlocks everywhere! Chaos, I tell you!&lt;/p&gt;

&lt;p&gt;Thread safety ensures that our multithreaded programs work smoothly, with gremlins cooperating instead of clashing. It's like a symphony of threads playing in perfect harmony!&lt;/p&gt;

&lt;p&gt;So, my fellow Java wizards, remember to use the power of synchronization, immutability, and volatile variables wisely. And don't forget about those thread-safe methods and classes in the Java toolkit. With these tools, you'll conquer the realm of thread safety and build reliable, concurrent applications!&lt;/p&gt;

&lt;p&gt;Now go forth and let your threads dance gracefully, for you are the master of thread safety! Happy coding!&lt;/p&gt;

</description>
      <category>java</category>
      <category>threads</category>
      <category>programming</category>
      <category>programmers</category>
    </item>
    <item>
      <title>Optional in Java: An Epic Fact Check</title>
      <dc:creator>Jani Syed</dc:creator>
      <pubDate>Wed, 12 Jul 2023 21:56:39 +0000</pubDate>
      <link>https://dev.to/janisyed18/optional-in-java-an-epic-fact-check-55ak</link>
      <guid>https://dev.to/janisyed18/optional-in-java-an-epic-fact-check-55ak</guid>
      <description>&lt;p&gt;Welcome, fellow Java adventurers! Today, we embark on an epic fact-checking journey into the fascinating realm of Optional in Java. Get ready for a thrilling ride filled with mind-blowing insights and discoveries about taming those pesky null values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #1:&lt;/strong&gt; Optional, the Mighty Shield against Null&lt;/p&gt;

&lt;p&gt;Prepare to be amazed! Optional is the mighty shield that guards your code against the dreadful NullPointerException. It's like having a superhero sidekick that saves the day by banishing null values from your kingdom.&lt;/p&gt;

&lt;p&gt;Imagine this: You're building a banking application, and you want to retrieve the account balance of a user. By utilizing Optional, you can gracefully handle scenarios where the balance might be unavailable. Instead of risking a NullPointerException, you can wield Optional to elegantly navigate the possibility of a missing value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #2:&lt;/strong&gt; The Mysterious Treasure Chest of Possibilities&lt;/p&gt;

&lt;p&gt;Unlock the secrets of Optional, the treasure chest of endless possibilities. It's your trusty companion for gracefully handling situations where values may be missing. Think of it as a magical key that opens doors to null-free bliss.&lt;/p&gt;

&lt;p&gt;For example, imagine you're developing a music streaming service, and you want to retrieve the artist name of a song. Instead of dealing with null checks and potential crashes, you can use Optional to encapsulate the artist name. This way, you can smoothly handle cases where the song doesn't have an associated artist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #3:&lt;/strong&gt; Enhanced Code Clarity and Intention with Optional&lt;/p&gt;

&lt;p&gt;Optional brings a new level of clarity and intention to your code. By harnessing the power of Optional, you announce to the world, "I shall gracefully handle the absence of values!" It's like wielding a magic wand that banishes null in an instant.&lt;/p&gt;

&lt;p&gt;Imagine a situation where you're constructing a playlist for a music streaming service. You aim to retrieve the duration of the playlist. With Optional, you can express your code's purpose with precision. Instead of returning null, you provide an Optional object, signifying that the duration may or may not be available.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #4:&lt;/strong&gt; Optional's Dazzling Dance of Functional Delights&lt;/p&gt;

&lt;p&gt;Prepare to be mesmerized by Optional's elegant dance with functional programming. With a flick of your coding wand, you can chain powerful methods like map(), filter(), and flatMap(). It's a captivating performance that brings your code to life!&lt;/p&gt;

&lt;p&gt;Let's say you're developing a weather application, and you want to retrieve the temperature of a specific location. With Optional's functional powers, you can effortlessly apply transformations and filters to handle various scenarios. You can map the temperature to a different unit, filter out temperatures below a certain threshold, or even flatMap to fetch additional weather details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #5:&lt;/strong&gt; Optional's Surprising Tricks and Treats&lt;/p&gt;

&lt;p&gt;Get ready for Optional's bag of surprises! It's packed with enchanting methods like orElseGet() and orElseThrow(). With orElseGet(), you can summon alternative values when your Optional is empty, like summoning a helpful genie from a magic lamp. And with orElseThrow(), you can unleash the wrath of exceptions upon an audacious empty Optional. Beware, nulls!&lt;/p&gt;

&lt;p&gt;Suppose you're developing a social media application, and you want to fetch the user's profile picture. With Optional, you can gracefully handle cases where the user hasn't set a profile picture yet. You can use orElseGet() to provide a default image, like a stylish avatar or a picture of a friendly cat. And if, by some unexpected turn of events, the Optional is empty, you can unleash the fury of orElseThrow() and raise a custom exception like "NoProfilePictureException"!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #6:&lt;/strong&gt; Unleashing the Decision-Making Potential of Optional&lt;/p&gt;

&lt;p&gt;Embark on a journey of decision-making prowess with Optional as your trusted companion. Explore the capabilities of methods like ifPresent() and ifPresentOrElse(). It's akin to having a knowledgeable mentor guiding you through the labyrinth of nulls. Embrace the myriad choices and let your code venture unfold!&lt;/p&gt;

&lt;p&gt;Imagine you're crafting a social media platform, and you wish to exhibit the number of new notifications for a user. Leveraging Optional, you can gracefully determine the appropriate course of action based on the availability of unread messages. Utilize ifPresent() to display the count if it exists, or employ ifPresentOrElse() to present a friendly message such as "No unread messages" when appropriate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fact Check #7:&lt;/strong&gt; Optional's Immutable Charm&lt;/p&gt;

&lt;p&gt;Behold the immutability of Optional! Once an Optional object is born, its value remains steadfast and unchanging. It's like an ancient artifact safeguarding your code's integrity, ensuring that nulls dare not meddle.&lt;/p&gt;

&lt;p&gt;Fellow adventurers, gear up for the epic quest with Optional! Embrace the unexpected, conquer the null beasts, and let the power of Optional guide you to greatness.&lt;/p&gt;

&lt;p&gt;Remember, wield Optional's power responsibly. Use it wisely and with purpose, sparingly invoking its might to enhance the clarity and resilience of your code.&lt;/p&gt;

&lt;p&gt;I hope this epic fact check has ignited your curiosity and set you on a thrilling path of null-free coding. To witness the magic of Optional in action, here's a simple Java code snippet showcasing some of its fascinating methods:&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;java.math.BigDecimal&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.ArrayList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.List&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Optional&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;ShoppingCart&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ShoppingCart&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;items&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&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="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getTotalPrice&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ZERO&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalPrice&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;ShoppingCart&lt;/span&gt; &lt;span class="n"&gt;cart&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;ShoppingCart&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Add items to the cart&lt;/span&gt;
        &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addItem&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;Item&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"iphone 14 pro"&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;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"9999"&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
        &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addItem&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;Item&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Airpods pro 2nd"&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;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"249.99"&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
        &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addItem&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;Item&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AirTag"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Simulating a missing price&lt;/span&gt;

        &lt;span class="c1"&gt;// Calculate the total price&lt;/span&gt;
        &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getTotalPrice&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalPrice&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&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;"Total price: $"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="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;"Unable to calculate the total price. Invalid or missing item 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;Feel the power of Optional as you explore its methods and witness the null-taming magic in action!&lt;/p&gt;

&lt;p&gt;If you have any questions or epic tales of triumph over nulls, share them in the comments below. Happy coding, and may your Java adventures be forever free from the clutches of null! 🗡️🔥&lt;/p&gt;

</description>
      <category>java</category>
      <category>optional</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>5 Interesting Things About Strings in Java</title>
      <dc:creator>Jani Syed</dc:creator>
      <pubDate>Fri, 07 Jul 2023 17:16:29 +0000</pubDate>
      <link>https://dev.to/janisyed18/5-interesting-things-about-strings-in-java-3i6f</link>
      <guid>https://dev.to/janisyed18/5-interesting-things-about-strings-in-java-3i6f</guid>
      <description>&lt;p&gt;In Java programming, strings play a crucial role by adding style and charm to text. From storing names to creating formatted output, strings are essential for every Java developer. In this blog post, get ready to rock and roll as we explore five fun and fascinating facts about strings in Java. Get ready for some string-tastic knowledge!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Immutability:&lt;/strong&gt; "The Unceasingly Bestowing Melodies" of Strings;&lt;br&gt;
In the programming language Java, strings possess a truly distinctive characteristic: their immutability. Once a string is created, it remains unalterable forever, comparable to having an adored melody that gets entrenched in your thoughts and refuses to leave! For example:&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;favoriteSong&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Never gonna give you up"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;favoriteSong&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;favoriteSong&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", never gonna let you down"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, even though we're appending a new part to the &lt;code&gt;favoriteSong&lt;/code&gt; string, the original string remains the same. Instead, a new string object is created with the modified contents. So, just like Rick Astley, Java strings never give you up or let you down!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The String Pool:&lt;/strong&gt; Where Strings Party and Share Memories&lt;br&gt;
Imagine a poolside party where strings mingle and share memories. That's the string pool in Java! When you create a string, it jumps into the string pool for a fun-filled splash. Let's dive into an example:&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;firstGuest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Java"&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;secondGuest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code snippet, both &lt;code&gt;firstGuest&lt;/code&gt; and &lt;code&gt;secondGuest&lt;/code&gt; refer to the same string object in the string pool. Java's smart enough to say, "Hey, we've got a Java here already!" and doesn't create a new string object. Instead, it saves memory by reusing the existing one. It's like a party where everyone gets their own drink, but they share the memories!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance Boost:&lt;/strong&gt; Strings That Dance to the Beat&lt;br&gt;
Strings aren't just groovy, they can also boost your program's performance! Check out these two examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;a) Constant Groove:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;GREETING&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&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="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;TARGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;By using strings as constants, you save memory and CPU cycles. It's like having your favorite song on repeat without any extra effort. These constant strings are created only once and shared across your code, making your program sing with efficiency.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;b) Caching Moves:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cachedResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateExpensiveComputation&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Strings can be your secret cache for expensive computations. Once you've done a costly operation, store the result in a string. Simply pull it from the cache the next time you need it. It's similar to having a dancing move that dazzles the audience without breaking a sweat. Your program performs faster, and you become the star!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Regular Expressions:&lt;/strong&gt; The Rockstar Soloists of String Manipulation&lt;br&gt;
Regular expressions are like the rockstar soloists of string manipulation. They let you find patterns, replace text, and extract substrings like a guitar shredding a mind-blowing solo. For example:&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hey Java, you rock!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;containsRock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;matches&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;".*rock.*"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code snippet, the &lt;code&gt;matches()&lt;/code&gt; method uses a regular expression to check if the string &lt;code&gt;text&lt;/code&gt; contains the word "rock". Regular expressions let you bring out the rockstar in your string manipulations and create powerful performances!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strings and Java APIs:&lt;/strong&gt; A Collaborative Symphony&lt;br&gt;
Strings are the maestros of collaboration in the Java world, working seamlessly with various Java APIs. Here are a few examples of their symphony:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;a) I/O API:&lt;/strong&gt;
Strings are the lyricists of file reading and writing. For instance:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;BufferedReader&lt;/span&gt; &lt;span class="n"&gt;reader&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;BufferedReader&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;FileReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MyLyrics.txt"&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;read&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;b) Networking API:&lt;/strong&gt; 
Strings connect us to the world. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;serverAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"www.example.com"&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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Socket&lt;/span&gt; &lt;span class="n"&gt;socket&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;Socket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serverAddress&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;c) XML API:&lt;/strong&gt;
Strings harmonize perfectly with XML processing. For instance:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;xmlData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;book&amp;gt;&amp;lt;title&amp;gt;Java Rocks!&amp;lt;/title&amp;gt;&amp;lt;/book&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;DocumentBuilderFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DocumentBuilderFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;DocumentBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newDocumentBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&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;InputSource&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;StringReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xmlData&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strings and Java APIs create a collaborative symphony, allowing you to work with various resources and domains.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
Strings in Java bring joy, performance, and collaboration to your programming symphony. From their immutability to the grooves of the string pool, strings are full of surprises. They can optimize your program's performance, rock your string manipulations with regular expressions, and collaborate with various Java APIs like true symphony performers.&lt;/p&gt;

&lt;p&gt;So, embrace the magic of strings and let them be the rockstars of your Java programs. Get ready to jam and code on with the power of strings in Java!&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this engaging and fun blog post on the fascinating things about strings in Java. Feel free to share your favorite string moments or ask any burning questions in the comments. Keep coding, keep rocking, and keep stringing those Java programs!&lt;/p&gt;

</description>
      <category>strings</category>
      <category>java</category>
      <category>core</category>
    </item>
    <item>
      <title>Diving Into Spring Security 3.1: Embrace The Change, Developers!</title>
      <dc:creator>Jani Syed</dc:creator>
      <pubDate>Sat, 17 Jun 2023 00:08:44 +0000</pubDate>
      <link>https://dev.to/janisyed18/diving-into-spring-security-31-embrace-the-change-developers-3lpm</link>
      <guid>https://dev.to/janisyed18/diving-into-spring-security-31-embrace-the-change-developers-3lpm</guid>
      <description>&lt;p&gt;Hello there, adventurous coders!&lt;/p&gt;

&lt;p&gt;If you've been wrestling with Spring Security lately, you're in for a treat. Say "hello" to Spring Security 3.1 – the shiny new version ready to help you secure your applications like a pro. What’s the fuss all about? Well, allow me to guide you through this maze of new features, improvements, and yes, deprecations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The OAuth 2.0 Upgrade&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let's start by talking about our dear old friend OAuth 2.0, that incredibly popular authorization protocol that, let's be honest, we all sometimes love to hate. Spring Security 3.1 now provides a more streamlined support for OAuth 2.0. Client registration, password grant, and refresh token grant – all included! Building OAuth 2.0-based applications is now as smooth as a well-oiled bicycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  The JWT Lift
&lt;/h2&gt;

&lt;p&gt;Next up is JWT, or JSON Web Tokens. If you've ever wished for a fortune cookie that carries secure claims instead of vague prophecies, JWT is your genie in a bottle. Spring Security 3.1 provides a more effective support for decoding JWT tokens and extracting those sweet, sweet claims. Your JWT-based applications are about to hit a new level of ease and efficiency.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Great Deprecation Party
&lt;/h3&gt;

&lt;p&gt;Spring Security 3.1 isn’t just about welcoming new features; it’s also about saying 'Adieu!' to some old ones. Let's take a moment to bid farewell to CSRF protection, HTTP Basic authentication, and form login.&lt;/p&gt;

&lt;h2&gt;
  
  
  - CSRF Protection
&lt;/h2&gt;

&lt;p&gt;Once a shield against Cross-Site Request Forgery attacks, CSRF protection takes a bow as we welcome the more secure SameSite cookie attribute. Attackers, beware!&lt;/p&gt;

&lt;h2&gt;
  
  
  - HTTP Basic Authentication and Form Login
&lt;/h2&gt;

&lt;p&gt;HTTP Basic authentication and form login, both authentication methods we have relied on for so long, have decided to retire in favor of the newer and more secure OAuth 2.0 protocol. Talk about passing on the baton!&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional Interfaces: The New Kid On The Block
&lt;/h2&gt;

&lt;p&gt;With Java 8 came the power of functional interfaces, enabling us to write more concise and expressive code. Spring Security 3.1 harnesses this power for features like authentication and authorization. For instance, AuthenticationProvider, a functional interface, allows you to create custom authentication providers. Similarly, with the AuthorizationDecisionManager interface, you can write custom decision managers for resource access control. It's like giving your coding abilities a turbo boost!&lt;/p&gt;

&lt;p&gt;` &lt;br&gt;
/*&lt;br&gt;
*SecurityConfig Class Bean Configuration Example&lt;br&gt;
*/&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

    http.csrf(csrf -&amp;gt; csrf.disable())
            .authorizeRequests().
            requestMatchers("/test").authenticated().requestMatchers("/auth/login").permitAll()
            .anyRequest()
            .authenticated()
            .and().exceptionHandling(ex -&amp;gt; ex.authenticationEntryPoint(point))
            .sessionManagement(session -&amp;gt; session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
    http.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);
    return http.build();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;code&gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Improved Performance
&lt;/h2&gt;

&lt;p&gt;Finally, if the earlier features were the icing on the cake, then the improved performance is the cherry on top. This version introduces a series of optimizations, including security data caching and enhanced thread pool management. Some benchmarks have even reported a whopping 2x performance improvement!&lt;/p&gt;

&lt;p&gt;In the world of Spring Security, this is akin to turning your bicycle into a race car!&lt;/p&gt;

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

&lt;p&gt;All in all, Spring Security 3.1 is like a superhero upgrade, packed with new features, improvements, and some farewells too. If you're using Spring Security, consider upgrading to 3.1 - your future self will thank you. And even if you hit a few bumps on the road, remember, every great developer was once where you are. Keep learning, keep improving, and most importantly, keep coding!&lt;/p&gt;

&lt;p&gt;That's it for now. Until next time, happy secure coding!&lt;/p&gt;

</description>
      <category>springsecurity</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
