<?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: Barış Ayten</title>
    <description>The latest articles on DEV Community by Barış Ayten (@baytendev).</description>
    <link>https://dev.to/baytendev</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%2F216645%2Fe8dd301e-df9e-4d13-bbd4-d28fd8abc699.png</url>
      <title>DEV Community: Barış Ayten</title>
      <link>https://dev.to/baytendev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baytendev"/>
    <language>en</language>
    <item>
      <title>Design Patterns | Fundamentals</title>
      <dc:creator>Barış Ayten</dc:creator>
      <pubDate>Sat, 13 May 2023 09:10:03 +0000</pubDate>
      <link>https://dev.to/baytendev/design-patterns-fundamentals-ma2</link>
      <guid>https://dev.to/baytendev/design-patterns-fundamentals-ma2</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oScpxMfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/mqjrwqm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oScpxMfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/mqjrwqm.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello Folks! 🖖🏼&lt;/p&gt;

&lt;p&gt;I've decided to embark on a new article series, and I'm going to tackle &lt;strong&gt;Design Patterns&lt;/strong&gt;. Combining the notes I've taken from the articles I've read and watched while learning Design Patterns, I've decided to create a perpetual resource on my blog.&lt;/p&gt;

&lt;p&gt;I'm planning to divide this article series into &lt;strong&gt;four parts&lt;/strong&gt;, where I'll delve into each category and pattern in detail, accompanied by examples. Throughout the examples, &lt;strong&gt;I'll be using Java as the programming language&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The article series will progress in the following order, as I anticipate:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Design Pattern Fundamentals&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creational Design Patterns&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Structural Design Patterns&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Behavioral Design Patterns&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll start with this article to provide &lt;strong&gt;a nice introduction and gain a general overview and understanding of Design Pattern concepts&lt;/strong&gt;. In the subsequent articles, we'll meticulously delve into each remaining part separately.&lt;/p&gt;

&lt;p&gt;Without further ado, let's dive into our first topic! 🤘🏼&lt;/p&gt;




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

&lt;p&gt;Design Patterns, in the realm of software development, hold a significant place as the cool-sounding name for a set of standardized software designs that developers employ to &lt;strong&gt;tackle recurring problems, create more flexible, sustainable, and reusable code in their projects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These designs, comprising numerous different patterns, are categorized into &lt;strong&gt;three distinct groups based on their usage and purposes, as outlined below&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iJhUnGaO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/mcj2eek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iJhUnGaO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/mcj2eek.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's now roughly delve into the realm of &lt;strong&gt;Creational Design Patterns&lt;/strong&gt;, which will be one of the pillars of our article series, and try to grasp what they are all about.🎉&lt;/p&gt;

&lt;h1&gt;
  
  
  Creational Design Patterns
&lt;/h1&gt;

&lt;p&gt;Creational Design Patterns encompass a category of design patterns that &lt;strong&gt;revolve around the process of creating new objects&lt;/strong&gt;. In this category, the patterns focus on &lt;strong&gt;how an object can be created in a flexible and reusable manner&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“In software engineering, creational design patterns are design patterns that deal with **object creation mechanisms, trying to create objects in a manner suitable to the situation.&lt;/em&gt;* The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.”* (1)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We've covered it quite extensively in the previous lines, but if we were to highlight them in bullet points, these design patterns address the following concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How objects are created.&lt;/li&gt;
&lt;li&gt;Managing the processes involved in creating large and complex objects.&lt;/li&gt;
&lt;li&gt;Defining the requirements during the creation of objects.&lt;/li&gt;
&lt;li&gt;Determining which objects to use in the object's creation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Within this category, we'll explore 6 different patterns. Now, let's take a quick peek at them.🧐&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cfCm1KwZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/2c8qm70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cfCm1KwZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/2c8qm70.png" alt="" width="797" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Singleton Pattern&lt;/strong&gt;: It is used to design a class with only one instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Factory Method Pattern&lt;/strong&gt;: It allows subclasses to use the interface of the parent class to create objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Abstract Factory Pattern&lt;/strong&gt;: It enables the creation of objects in a group that are related to each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Builder Pattern&lt;/strong&gt;: It allows the step-by-step construction of complex objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prototype Pattern&lt;/strong&gt;: It is used to create copies of objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As mentioned at the beginning of the article, we briefly covered each pattern. Now, let's take a look at our next category, &lt;strong&gt;Structural Design Patterns&lt;/strong&gt;. 🐱‍🏍&lt;/p&gt;

&lt;h1&gt;
  
  
  Structural Design Patterns
&lt;/h1&gt;

&lt;p&gt;The patterns within this category determine how the different components of software will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How came together,&lt;/li&gt;
&lt;li&gt;How be organized, and&lt;/li&gt;
&lt;li&gt;How interact with each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These design patterns are in line with the principles of &lt;strong&gt;object-oriented programming (OOP) and help optimize the interaction between the various components of the software&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Structural design patterns explain **how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient.&lt;/em&gt;** (2)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Structural Design Patterns allow for &lt;strong&gt;easier understanding and more convenient maintenance of the code and the resulting structure&lt;/strong&gt;. These design patterns, especially during the development of large and complex software, provide developers with &lt;strong&gt;opportunities to better organize and reuse their code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Within the &lt;strong&gt;Structural Pattern&lt;/strong&gt; category, there are 7 different design patterns. &lt;/p&gt;

&lt;p&gt;These include:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wk7cM_Q7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/h4yzy0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wk7cM_Q7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/h4yzy0i.png" alt="" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Adapter Pattern&lt;/strong&gt;: It is a structural design pattern that enables two classes with different interfaces to work together harmoniously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bridge Pattern&lt;/strong&gt;: It is a design pattern that separates the abstraction and implementation, allowing them to vary independently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite Pattern&lt;/strong&gt;: It is a design pattern that creates objects in a hierarchical structure, enabling operations to be performed on the entire structure instead of individually handling each object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decorator Pattern&lt;/strong&gt;: It is a structural design pattern that dynamically adds new features to an existing object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Facade Pattern:&lt;/strong&gt; It is a design pattern that simplifies a complex system by providing an interface for clients to interact with, making it easier for clients to use the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flyweight Pattern:&lt;/strong&gt; It is a design pattern that prevents the repeated creation of similar objects, thereby improving system performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Proxy Pattern:&lt;/strong&gt; It is a design pattern that acts as a substitute for an object and controls access to that object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Up until now, we have gained some understanding about two different categories and the 12 design patterns within them. At least now we're not completely clueless! 😁&lt;/p&gt;

&lt;p&gt;Now, let's take a look at our final guest of this article, &lt;strong&gt;Behavioral Design Patterns&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Behavioral Design Patterns
&lt;/h1&gt;

&lt;p&gt;Our friends in this design pattern category actually have similar goals. They assist in &lt;strong&gt;organizing interactions and responsibilities among objects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's dive into the various &lt;strong&gt;Behavioral Design Patterns&lt;/strong&gt; and explore their roles.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.&lt;/em&gt; (3)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These patterns help &lt;strong&gt;make object behaviors more flexible and cohesive&lt;/strong&gt;. They are used to &lt;strong&gt;facilitate easier maintenance and development of software&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here are some important &lt;strong&gt;Behavioral Design Patterns&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ij5SN1ij--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/bdhl2z7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ij5SN1ij--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.hizliresim.com/bdhl2z7.png" alt="" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Observer Pattern&lt;/strong&gt;: Establishes a relationship between objects, enabling automatic updates to dependent objects when one object changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Command Pattern:&lt;/strong&gt; Encapsulates a command-containing class, allowing separation of objects responsible for executing and parameterizing the command. This enables different ways of invoking and undoing the command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterator Pattern&lt;/strong&gt;: Provides an interface for accessing elements of a collection sequentially, ensuring the access method remains consistent even if the collection's structure changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Template Method Pattern:&lt;/strong&gt; Defines the steps of a process and allows subclasses to implement these steps as desired.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strategy Pattern:&lt;/strong&gt; Encapsulates a class containing an algorithm, enabling different objects to apply the algorithm. This allows the algorithm to be applied in different ways and changed at runtime.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chain of Responsibility Pattern:&lt;/strong&gt; Creates a chain of objects where each object is responsible for handling a specific request. The request is passed along the chain until it is handled.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State Pattern:&lt;/strong&gt; Used to manage an object's state and change its behavior based on the current state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interpreter Pattern:&lt;/strong&gt; Provides an object structure for interpreting a specific part of a language and interpreting the data accordingly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mediator Pattern:&lt;/strong&gt; Instead of direct communication between objects, it uses a mediator object to facilitate communication and maintain independence among objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visitor Pattern:&lt;/strong&gt; Provides an interface for accessing elements in an object structure and allows different objects to perform different operations on these elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These patterns &lt;strong&gt;contribute to creating more flexible and maintainable software systems by defining clear interactions and responsibilities among objects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So far, let's summarize it. We have three different categories of patterns at our disposal. These are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Creational&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Structural&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Behavioral&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And each of these categories focuses on a different mechanism.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creational Patterns &lt;strong&gt;deal with the creation of objects&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Structural Patterns address &lt;strong&gt;how objects will come together and be organized once they are created&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Behavioral Patterns allow us to &lt;strong&gt;make adjustments regarding interactions and responsibilities among objects.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thank you for taking the time to read through it. If you notice any missing or incorrect points, please feel free to let me know, and I'll be happy to make the necessary corrections. I hope that the notes I have gathered here will be of some use to you and contribute to your work, even if it's in a small way &lt;/p&gt;

&lt;p&gt;Thank you for taking the time to read this far. ♥&lt;/p&gt;

&lt;p&gt;Happy Coding 🖖🏼&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Design Patterns. &lt;a href="https://sourcemaking.com/design_patterns"&gt;https://sourcemaking.com/design_patterns&lt;/a&gt; (1),(2)&lt;/li&gt;
&lt;li&gt;Tasarım Desenleri, &lt;a href="https://yusufyilmazfr.gitbook.io/tasarim-desenleri-turkce-kaynak/tasarim-desenleri/tasarim-deseni-nedir"&gt;https://yusufyilmazfr.gitbook.io/tasarim-desenleri-turkce-kaynak/tasarim-desenleri/tasarim-deseni-nedir&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>designpatterns</category>
      <category>softwareengineering</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Method Overriding and Overloading in Java</title>
      <dc:creator>Barış Ayten</dc:creator>
      <pubDate>Wed, 05 Apr 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/baytendev/method-overriding-and-overloading-in-java-47cm</link>
      <guid>https://dev.to/baytendev/method-overriding-and-overloading-in-java-47cm</guid>
      <description>&lt;p&gt;Hello everyone 🤘🏼&lt;/p&gt;

&lt;p&gt;In this article, we'll be taking a look at the concepts of method &lt;strong&gt;overloading&lt;/strong&gt; and &lt;strong&gt;overriding&lt;/strong&gt; in Java, which I believe are often forgotten and confused by beginners (at least I was 😁).&lt;/p&gt;

&lt;p&gt;Before delving into these two concepts in detail, I'd like to briefly mention them. Method overriding is simply changing the functionality of a method without changing its signature, while method overloading is redefining a method with a different signature.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;These two definitions may not make much sense yet because I have only mentioned them briefly and in a simplistic manner. Although this section consists of only two sentences that may seem meaningless for now, by the end of the article, they will have the power to summarize the entire article. 🙂&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we dive into the topic fully, there is one tiny concept we need to learn and understand: &lt;strong&gt;The Method Signature&lt;/strong&gt;. Method signatures consist of the name of a method and its method parameters. As shown in the image below, the signature of the method named &lt;code&gt;justFun&lt;/code&gt; consists of its name "justFun" and its parameters &lt;code&gt;int num1&lt;/code&gt;,  &lt;code&gt;int num2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2UWA_en8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.hizliresim.com/9zxpi7n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2UWA_en8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.hizliresim.com/9zxpi7n.jpg" alt="" width="880" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I believe we have clarified this simple and short concept. Now, let's dive into the main concepts of this article. 🚀&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Method Overriding&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Method overriding, in simple terms, refers to methods that &lt;strong&gt;have the same signature but perform different tasks&lt;/strong&gt;. How does this situation arise?&lt;/p&gt;

&lt;p&gt;Let's assume that we have a class named &lt;code&gt;Animal&lt;/code&gt; with a method named &lt;code&gt;eat()&lt;/code&gt;. Now, I have created another class named &lt;code&gt;Dog&lt;/code&gt;, and I want it to inherit from the &lt;code&gt;Animal&lt;/code&gt; class. When we perform the inheritance process, we would like to use the &lt;code&gt;eat()&lt;/code&gt; method inherited from the super class in a way that is suitable for our subclass. Therefore, &lt;strong&gt;we change its functionality&lt;/strong&gt;. In other words, we override the &lt;code&gt;eat()&lt;/code&gt; method.🙂&lt;/p&gt;

&lt;p&gt;Let's give 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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;eat&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;"eating..."&lt;/span&gt;&lt;span class="o"&gt;);}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;  

&lt;span class="c1"&gt;//Overriding way&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;eat&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;"eating bread..."&lt;/span&gt;&lt;span class="o"&gt;);}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;eat&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;"eating fish..."&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;As we saw in the example, I did not touch the method signatures in any way. I did not change the method name or send a new parameter to that method... &lt;strong&gt;I just played with the internal dynamics of the method and added a completely different functionality.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, you may have a question like this in your mind🤔:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;em&gt;"What about the access modifiers of the methods we override? Do they have to be the same?"&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our answer to this question will be &lt;strong&gt;no&lt;/strong&gt;. They do not have to be the same, &lt;strong&gt;but&lt;br&gt;
the access modifier of the overriding method must be the same or less restrictive than the original method.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j_Bntf7Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.hizliresim.com/nzibmem.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j_Bntf7Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.hizliresim.com/nzibmem.jpg" alt="" width="880" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;According to the restriction diagram in the image, if a &lt;code&gt;protected&lt;/code&gt; method is to be overridden, &lt;strong&gt;its access modifier cannot be default or private&lt;/strong&gt;. It can either remain &lt;code&gt;protected&lt;/code&gt; or be made &lt;code&gt;public&lt;/code&gt;. In short, when overriding a method, &lt;strong&gt;the access modifier in the subclass must be the same as or less restrictive than that in the superclass.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another question that might come up is❓, &lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  "Do the return types of the methods we override have to be the same? Can't I specify a different return type?"
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our answer to this question &lt;strong&gt;will be No&lt;/strong&gt; 🙂. When overriding a method, &lt;strong&gt;the return type of the method must remain the same, along with the method signature&lt;/strong&gt;. This is primarily based on Java's principle of type safety.&lt;/p&gt;

&lt;p&gt;If a subclass could use a different return type while overriding methods of the superclass, it could lead to errors in places where the superclass is used. &lt;/p&gt;

&lt;p&gt;However, there is an exception to this rule where &lt;strong&gt;the return type of the overridden method can be a co-variant return type&lt;/strong&gt;. For example, suppose the return type of a superclass is &lt;code&gt;Animal&lt;/code&gt;, and the return type of the subclass is &lt;code&gt;Cat&lt;/code&gt;. While overriding the superclass method, the subclass can specify the return type as &lt;code&gt;Cat&lt;/code&gt;. This way, the object returned by the subclass will not be of type &lt;code&gt;Animal&lt;/code&gt;, but of a more specific subtype, which is &lt;code&gt;Cat&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another question that could arise is, &lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  "Can I override every method?"
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;It may be the most fundamental question to ask. 🙂&lt;/p&gt;

&lt;p&gt;Our answer to this question, unfortunately, will still be No. 😁&lt;/p&gt;

&lt;p&gt;If the method to be overridden is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;final&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;static&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;private&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;then we cannot override these methods. This is because &lt;strong&gt;the nature of the override operation is based on inheritance&lt;/strong&gt;. Methods with these three keywords cannot be inherited, so they cannot be overridden either. &lt;/p&gt;

&lt;p&gt;However, sometimes a subclass can redefine a static method of the superclass. Some of us might have seen this situation before. This is not an override but is called &lt;strong&gt;Method Hiding.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we summarize the process of overriding;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After the override process, the method signature remains the same (method name and parameters must stay the same), only the functionality of the method changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The return type of the overridden method should not change or it should be a co-variant return type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The access modifier of the overridden method should be the same or less restrictive than the original method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;private&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt; methods cannot be overridden.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have gained a general understanding and logic about overriding, let's take a look at the concept of overloading.&lt;/p&gt;
&lt;h1&gt;
  
  
  &lt;strong&gt;Method Overloading&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Method Overloading is &lt;strong&gt;the redefinition of a method with the same name but different parameters&lt;/strong&gt;. This means that the parameter section of the method signature is changed. This allows for the creation of different methods that perform the same task but with different parameter types, which can be called independently.&lt;/p&gt;

&lt;p&gt;Let's try to clarify the situation with some short code examples,&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;summation&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;num1&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;num2&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="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;summation&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;num1&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;num2&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;num3&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="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num3&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;Calculator&lt;/span&gt; &lt;span class="n"&gt;calc&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;Calculator&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;result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;summation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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;result2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;summation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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;"Summation of 2 and 3: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;result1&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;"Summation of 2, 3 and 4:  "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;result2&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;In the example above, we defined two different methods with the name &lt;code&gt;summation()&lt;/code&gt; inside the &lt;code&gt;Calculator&lt;/code&gt; class, where the first method takes &lt;strong&gt;two integer parameters&lt;/strong&gt;, and the second method takes &lt;strong&gt;three integer parameters&lt;/strong&gt;. In the main method, we call both methods with different parameters and print the result to the screen.&lt;/p&gt;

&lt;p&gt;Let's take a look at another example to reinforce our understanding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorld&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;greet&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;"Hello, world!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&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;HelloWorld&lt;/span&gt; &lt;span class="n"&gt;helloWorld&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;HelloWorld&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;helloWorld&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;helloWorld&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="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;In the example above, the &lt;code&gt;HelloWorld&lt;/code&gt; class defines two different methods named &lt;code&gt;greet()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The first method is a simple greeting method that takes &lt;strong&gt;no parameters&lt;/strong&gt; and prints the message &lt;em&gt;"Hello, world!"&lt;/em&gt;. The second method takes &lt;strong&gt;a parameter of type String&lt;/strong&gt; and uses it to create a customized greeting message. Both methods are called in the main method and their results are printed to the console.&lt;/p&gt;

&lt;p&gt;Okey, now that we have gone through the examples, I think we have some understanding about method overloading. Let's delve into some details about overloading using the same questions we used for understanding override.&lt;/p&gt;

&lt;p&gt;Then our first question is as follows🎇&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  "When overriding a method, we had to carefully choose the Access Modifier. That is, we had to either use the same Access Modifier as the previous method or a less restrictive one. Is this also true for Overloading?"
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our answer should be no 🙂 &lt;/p&gt;

&lt;p&gt;The logic here is different from overriding. Access modifier selection was important for overriding because inheritance played a leading role. However, in overloading, &lt;strong&gt;we have no constraints and we are completely free to choose any modifier we want.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;As in the example below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sum&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;a&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;b&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;c&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sum&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;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Nothing to sum"&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;And for our another question,&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  "The return types of the methods we override must be the same or a co-variant type. How does this work in Overloading?"
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;Actually, we have already demonstrated this situation in the example we provided earlier. The return type is not important in Overloading. It can be the same or different.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  "Can I overload every method?"
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer to this question should be yes 🙂 There is no problem with overloading a method that is &lt;code&gt;private&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt;, or &lt;code&gt;final&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's reinforce this situation with the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&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;doSomething&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;x&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;"doSomething with int: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&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;doSomething&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&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;"doSomething with double: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&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;final&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&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;x&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;"doSomething with String: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&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;If we summarize the overloading process in short points;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After the overloading process, &lt;strong&gt;the method signature changes (method name remains the same, method parameters change).&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The return type of the overloaded method can &lt;strong&gt;remain the same or be different.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The access modifier of the overloaded method can remain the same or be different.&lt;br&gt;
&lt;code&gt;private&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt; methods can also be overloaded, there is no restriction.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we've made it this far understanding, I believe we have an idea about the concepts of Overloading and Overriding. This has been a compilation of what I've learned and researched about Override and Overload. Thank you for taking the time to read this far.&lt;/p&gt;

&lt;p&gt;Happy Coding! 🤞🏼&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>programming</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Mutable and Immutable in Java</title>
      <dc:creator>Barış Ayten</dc:creator>
      <pubDate>Mon, 03 Apr 2023 11:25:26 +0000</pubDate>
      <link>https://dev.to/baytendev/mutable-and-immutable-in-java-4f4m</link>
      <guid>https://dev.to/baytendev/mutable-and-immutable-in-java-4f4m</guid>
      <description>&lt;p&gt;Hello Folks 🖖🏼,&lt;/p&gt;

&lt;p&gt;The topic of this article will be about the concept of Mutablity and Immutablity in Java. I will try to answer questions such as &lt;strong&gt;"What does Mutable &amp;amp; Immutable mean?"&lt;/strong&gt;, "&lt;strong&gt;What is the logic behind this functionality?&lt;/strong&gt;", "&lt;strong&gt;How are Mutable or Immutable classes created?&lt;/strong&gt;", "&lt;strong&gt;What are the advantages and disadvantages?&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;Let's dive right in and explore these fascinating concepts together. 🚀&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. Mutable Classes&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Mutable objects are objects that &lt;strong&gt;can be modified after they are initialized&lt;/strong&gt;. In some cases, these types of classes can be preferred over immutable classes due to their flexibility and mutable fields.&lt;/p&gt;

&lt;p&gt;Examples of these classes include &lt;strong&gt;StringBuffer&lt;/strong&gt;, &lt;strong&gt;StringBuilder&lt;/strong&gt;, and the &lt;strong&gt;Date&lt;/strong&gt; class found in the Java util package. To further clarify what I mean, let me provide a short code 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="c1"&gt;// Initialize variable called myString&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;myString&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;StringBuilder&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;//and manipulate it&lt;/span&gt;
&lt;span class="n"&gt;myString&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" Word"&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;myString&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In fact, those who are not familiar with the topic may be a little surprised. They might say, '&lt;strong&gt;&lt;em&gt;I can already do this in any variable I want, can't I?&lt;/em&gt;&lt;/strong&gt;' Our friends will be able to grasp the underlying logic more clearly when we delve into the details of the topic and the immutable part 🙂&lt;/p&gt;

&lt;p&gt;When a change is made in mutable classes, the change is made on the same field &lt;strong&gt;without creating a new space in memory&lt;/strong&gt; (the situation is the opposite in immutables 🙂). In fact, the logic behind mutable classes is that they have a &lt;strong&gt;set()&lt;/strong&gt; method that we call a &lt;strong&gt;setter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;being changeable&lt;/strong&gt; is not always a good thing for us, as it can cause some side effects... &lt;/p&gt;

&lt;p&gt;For example, using mutable classes in threaded operations can sometimes cause problems for us. &lt;strong&gt;When two execution threads try to access and change a mutable value at the same time, a situation may arise where one overrides the other&lt;/strong&gt;. Therefore, we should be careful when using mutable classes.&lt;/p&gt;

&lt;p&gt;We mentioned that there are mutable classes that already come with Java. So how would we create our own if we wanted to?&lt;/p&gt;

&lt;p&gt;I'll try to explain this with a short code.&lt;br&gt;
&lt;/p&gt;

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

   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mutableField1&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;String&lt;/span&gt; &lt;span class="n"&gt;mutableField2&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;MutableClass&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;fld1&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;fld2&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;mutableField1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fld1&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;mutableField2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fld2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getMutableField1&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="n"&gt;mutableField1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setMutableField1&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;mutableField1&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;mutableField1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutableField1&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;String&lt;/span&gt; &lt;span class="nf"&gt;getMutableField2&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="n"&gt;mutableField2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setMutableField2&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;mutableField2&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;mutableField2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutableField2&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;In this example, we defined the class fields as mutable. Additionally, we created two setter methods, &lt;strong&gt;setMutableField1()&lt;/strong&gt; and &lt;strong&gt;setMutableField2()&lt;/strong&gt;, for each field to enable us to change their values. This way, &lt;strong&gt;the fields of this class can be modified as desired after the initial use&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🌟&lt;strong&gt;In summary&lt;/strong&gt;,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mutable objects are ones that &lt;strong&gt;can be changed without creating a new object in memory&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;They have a &lt;strong&gt;setter method&lt;/strong&gt; in their class.&lt;/li&gt;
&lt;li&gt;They may cause disadvantages in threaded operations due to &lt;strong&gt;race conditions&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2.Immutable&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In fact, in this concept, the opposite of the situations explained above is present. &lt;strong&gt;Once an immutable object is initialized, we cannot change its value&lt;/strong&gt;. Examples of these objects include &lt;strong&gt;primitive types&lt;/strong&gt;, &lt;strong&gt;Wrapper Classes&lt;/strong&gt;, &lt;strong&gt;Legacy classes&lt;/strong&gt; &lt;a href="https://www.javatpoint.com/legacy-class-in-java"&gt;(what!?)&lt;/a&gt;, and the &lt;strong&gt;String&lt;/strong&gt; class. As an example, readers can refer to my blog post "&lt;a href="https://bayten.dev/posts/string-stringbuilder-and-stringbuffer-in-java/"&gt;String, StringBuilder, and StringBuffer in Java&lt;/a&gt;" where I discussed the implications of being mutable or immutable for these three &lt;br&gt;
classes.&lt;/p&gt;

&lt;p&gt;In immutable classes, unlike mutable classes, only &lt;strong&gt;Getter methods are present&lt;/strong&gt;. When a change is desired in these classes, &lt;strong&gt;a new area is created in memory and the manipulated version of the variable is stored in this new area&lt;/strong&gt;. This creates &lt;strong&gt;unnecessary memory usage&lt;/strong&gt; as a new area is constantly created for each manipulation, making immutable classes slower in terms of performance compared to mutable classes. &lt;/p&gt;

&lt;p&gt;Another difference from mutable classes is that &lt;strong&gt;they are safer in threaded processes since they are immutable and cannot be changed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, what should we do if we want to create an immutable class? Let's take a look:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can define the class as &lt;strong&gt;final&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;We can define the variables that will be used in the class as final and assign their initial values through the &lt;strong&gt;constructor method.&lt;/strong&gt; Therefore, since these fields are final and have initial values, they will give an error if they are attempted to be changed.&lt;/li&gt;
&lt;li&gt;We do not use the &lt;strong&gt;Setter&lt;/strong&gt; method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's now show all that has been explained in the code. In fact, let's try to make the "&lt;strong&gt;MutableClass&lt;/strong&gt;" that we created above immutable now.&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImmutableClass&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;immutableField1&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;String&lt;/span&gt; &lt;span class="n"&gt;immutableField2&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;ImmutableClass&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;fld1&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;fld2&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;immutableField1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fld1&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;immutableField2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fld2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getImmutableField1&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="n"&gt;immutableField1&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;String&lt;/span&gt; &lt;span class="nf"&gt;getImmutableField2&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="n"&gt;immutableField2&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;In the code I wrote above, we declared the fields of the class as &lt;strong&gt;final&lt;/strong&gt; and assigned &lt;strong&gt;their initial values in the constructor&lt;/strong&gt;, making it immutable. Additionally, since we do not have any &lt;strong&gt;setter methods&lt;/strong&gt;, the fields of this class cannot be accessed and assigned any value.&lt;/p&gt;

&lt;p&gt;To summarize the whole topic and make a comparative analysis, we can create a table as follows, from start to finish.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mutable&lt;/th&gt;
&lt;th&gt;Immutable&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;We can change the value of mutable objects &lt;strong&gt;after initialization&lt;/strong&gt;.&lt;/td&gt;
&lt;td&gt;Once an immutable object is initiated; &lt;strong&gt;We can not change its values&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The state can be changed.&lt;/td&gt;
&lt;td&gt;The state can not be changed.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;In mutable objects, &lt;strong&gt;no new objects are formed&lt;/strong&gt;.&lt;/td&gt;
&lt;td&gt;In immutable objects, &lt;strong&gt;a new object is formed when the value of the object is altered.&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It provides &lt;strong&gt;methods to change the object.&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;It does &lt;strong&gt;not provide any method to change the object value.&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It supports &lt;strong&gt;get() and set() methods&lt;/strong&gt; to dela with the object.&lt;/td&gt;
&lt;td&gt;It only supports &lt;strong&gt;get()&lt;/strong&gt; method to pass the value of the object.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutable classes are &lt;strong&gt;may or may not be thread-safe.&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Immutable classes &lt;strong&gt;are thread-safe.&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The essentials for creating a mutable class are methods for &lt;strong&gt;modifying fields, getters and setters.&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;The essentials for creating an immutable class are &lt;strong&gt;final class, private fields, final mutable objects&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;In conclusion&lt;/strong&gt;🌟, &lt;/p&gt;

&lt;p&gt;the use of Mutable and Immutable classes in Java is important for software developers to &lt;strong&gt;improve code quality, minimize errors, and optimize performance&lt;/strong&gt;. While the use of Mutable classes can be &lt;strong&gt;advantageous in terms of flexibility and customization, their misuse can lead to program errors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The use of Immutable classes is &lt;strong&gt;ideal for writing safe and shareable code, but in some cases, they may be disadvantaged in terms of performance.&lt;/strong&gt; By choosing the right class types according to the needs of the application, programmers can improve code quality and create more secure, scalable, and performant applications.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;If you have taken the time to read this far, thank you very much. If you notice any mistakes, please feel free to contact me via email.&lt;/p&gt;

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

&lt;p&gt;⭐For the original article -&amp;gt; &lt;a href="https://bayten.dev/posts/mutable-and-immutable-in-java/"&gt;https://bayten.dev/posts/mutable-and-immutable-in-java/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>String, StringBuilder, and StringBuffer in Java</title>
      <dc:creator>Barış Ayten</dc:creator>
      <pubDate>Mon, 03 Apr 2023 11:21:35 +0000</pubDate>
      <link>https://dev.to/baytendev/string-stringbuilder-and-stringbuffer-in-java-3cj6</link>
      <guid>https://dev.to/baytendev/string-stringbuilder-and-stringbuffer-in-java-3cj6</guid>
      <description>&lt;p&gt;Hello!🖖🏼&lt;/p&gt;

&lt;p&gt;This article will be about the String class that we frequently use in Java and its alternatives, StringBuilder and StringBuffer. There may be points that I missed, skipped, or inadvertently conveyed incorrectly. If you give me feedback via email in such a situation, I would be very happy.&lt;/p&gt;

&lt;p&gt;Now, let’s get started🚀&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1.String&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;First, let’s look at the String class. As we all know, the String class is one of the complex data types in Java that represents character arrays. This class stores an array of characters and provides multiple functions for manipulating these characters. When we want to use it, we define it as shown below and assign a value.&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;str&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Merhaba"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far, there is no problem here. However, as we know, in Java, the &lt;strong&gt;String&lt;/strong&gt; class is "&lt;strong&gt;immutable&lt;/strong&gt;" meaning it cannot be changed. But of course, some of you might say, "&lt;em&gt;Oh no, I have changed and assigned many times and performed the operations I wanted!&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;This is where situations arise that prompted me to write this article.&lt;/p&gt;

&lt;p&gt;In fact, when we manipulate a String variable, the value that the variable points to does not change 🙂. In such an operation, a new space is opened in the Heap, and the manipulated value is placed there, and our reference points to this new address.&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;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Merhaba"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Dünya"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we define as shown above and then perform the "&lt;strong&gt;concat&lt;/strong&gt;" operation, as mentioned, a new space is created in memory, and our "&lt;strong&gt;str&lt;/strong&gt;" variable now references the address where the "&lt;em&gt;MerhabaDünya&lt;/em&gt;" variable is located.&lt;/p&gt;

&lt;p&gt;So, a new space is being constantly opened in memory every time the variable is manipulated. And of course, this is not something we would want because it causes both performance and time loss. 🙂&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yeBk2zuv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.hizliresim.com/fqus7se.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yeBk2zuv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.hizliresim.com/fqus7se.jpg" alt="a" width="880" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
At this point, the &lt;strong&gt;StringBuilder&lt;/strong&gt; class comes to our help.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;2.StringBuilder&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The role of &lt;strong&gt;StringBuilder&lt;/strong&gt; is also the same as &lt;strong&gt;String&lt;/strong&gt;. It represents character strings. However, the difference from String is that it is a "&lt;strong&gt;mutable&lt;/strong&gt;" or changeable class. Therefore, it is a better choice than the &lt;strong&gt;String&lt;/strong&gt; class for efficient text manipulation.&lt;/p&gt;

&lt;p&gt;For example, if we want to convert all the words in a text file to uppercase, the &lt;strong&gt;StringBuilder&lt;/strong&gt; class can easily do this without recreating the entire text in memory (String was creating a new space in memory for this new value).&lt;/p&gt;

&lt;p&gt;Or a very simple example like the one below.&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;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sentence&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is a sentence."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Added word."&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;sentence&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="c1"&gt;//This is a sentence.Added word.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, there is an important drawback to this class that has not yet been mentioned, which is its lack of a thread-safe structure. If a process involving threads is to be used, the &lt;strong&gt;StringBuilder&lt;/strong&gt; class is not suitable in this scenario. This limitation can be explained with the following code snippet:&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;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t1&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
            &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;});&lt;/span&gt;

    &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;t2&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
            &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;});&lt;/span&gt;

    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&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;sb&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, two distinct threads, &lt;strong&gt;t1&lt;/strong&gt; and &lt;strong&gt;t2&lt;/strong&gt;, access the &lt;strong&gt;StringBuilder&lt;/strong&gt; object concurrently and append the characters "A" and "B" 10,000 times each in a sequential manner. However, due to the lack of synchronization in the &lt;strong&gt;StringBuilder&lt;/strong&gt; object, the resulting output is indeterminate and can yield varying results on each execution. In essence, a data race condition is introduced.&lt;/p&gt;

&lt;p&gt;To address this issue, we can use the &lt;strong&gt;StringBuffer&lt;/strong&gt; class instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3.StringBuffer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I won't repeat the same information with StringBuffer here, if you have read this far, you can already guess that the only difference of this class is its "thread-safe" or synchronized structure. It has the same methods and logic as StringBuilder.&lt;/p&gt;

&lt;p&gt;However, this great feature also brings some drawbacks. Its synchronized structure causes it to work slower than StringBuilder.&lt;/p&gt;

&lt;p&gt;If we try to explain this with a small code snippet,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringBufferVsStringBuilder&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="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;startTime&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endTime&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;duration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// StringBuffer performance test&lt;/span&gt;
            &lt;span class="nc"&gt;StringBuffer&lt;/span&gt; &lt;span class="n"&gt;buffer&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;StringBuffer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;startTime&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;nanoTime&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&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="n"&gt;endTime&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;nanoTime&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endTime&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;startTime&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;"Time taken by StringBuffer: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" nanoseconds"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;//Time taken by StringBuffer: 19494000 nanoseconds&lt;/span&gt;

            &lt;span class="c1"&gt;// StringBuilder performance test&lt;/span&gt;
            &lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;startTime&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;nanoTime&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&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;append&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="n"&gt;endTime&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;nanoTime&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endTime&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;startTime&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;"Time taken by StringBuilder: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" nanoseconds"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;//Time taken by StringBuilder: 17145000 nanoseconds&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;This code calculates the elapsed time for &lt;strong&gt;StringBuilder&lt;/strong&gt; and &lt;strong&gt;StringBuffer&lt;/strong&gt; to append the word "&lt;em&gt;hello&lt;/em&gt;" one million times. When we run this code, we can easily observe that &lt;strong&gt;StringBuffer&lt;/strong&gt; works slower due to its synchronized structure. We can make this difference more prominent by increasing the value of N.&lt;/p&gt;

&lt;p&gt;As a summary, I am adding the features of both classes in the table below,&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Features&lt;/th&gt;
&lt;th&gt;StringBuilder&lt;/th&gt;
&lt;th&gt;StringBuffer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Thread-Safe&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Faster&lt;/td&gt;
&lt;td&gt;Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Memory Usage&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Less&lt;/td&gt;
&lt;td&gt;More&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Mutable&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;Finally,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is no harm in using the &lt;strong&gt;String&lt;/strong&gt; class, but we should keep in mind that it is immutable, and every time we try to modify it, &lt;strong&gt;a new memory space is created&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StringBuilder&lt;/strong&gt;, on the other hand, performs all operations without the need for creating new memory spaces, but it may provide incorrect results in thread-containing applications since it does not have a synchronized structure.&lt;/li&gt;
&lt;li&gt;We can solve the problem of &lt;strong&gt;StringBuilder&lt;/strong&gt; by using &lt;strong&gt;StringBuffer&lt;/strong&gt;, which operates in a synchronized structure, but this class may cause some performance loss compared to &lt;strong&gt;StringBuilder&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have taken the time to read this far, thank you very much. If you notice any mistakes, please feel free to contact me via email.&lt;/p&gt;

&lt;p&gt;⭐For the original article -&amp;gt; &lt;a href="https://bayten.dev/posts/string-stringbuilder-and-stringbuffer-in-java/"&gt;https://bayten.dev/posts/string-stringbuilder-and-stringbuffer-in-java/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy coding! 🎉&lt;br&gt;
 &lt;br&gt;
 &lt;/p&gt;

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

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html"&gt;https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StringBuffer.html"&gt;https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StringBuffer.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StringBuilder.html"&gt;https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StringBuilder.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/string-vs-stringbuffer-vs-stringbuilder"&gt;https://www.digitalocean.com/community/tutorials/string-vs-stringbuffer-vs-stringbuilder&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>interviewquestions</category>
    </item>
  </channel>
</rss>
