<?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>Forem: Buddhika Chathuranga</title>
    <description>The latest articles on Forem by Buddhika Chathuranga (@buddhikac96).</description>
    <link>https://forem.com/buddhikac96</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%2F358134%2F29c8286b-39e4-45d2-a328-06cf40784d73.jpeg</url>
      <title>Forem: Buddhika Chathuranga</title>
      <link>https://forem.com/buddhikac96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/buddhikac96"/>
    <language>en</language>
    <item>
      <title>Access Modifiers </title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Mon, 07 Jun 2021 19:25:24 +0000</pubDate>
      <link>https://forem.com/buddhikac96/access-modifiers-3647</link>
      <guid>https://forem.com/buddhikac96/access-modifiers-3647</guid>
      <description>&lt;p&gt;In the Java programming language, we can see two main types of modifiers which are access modifiers and non-access modifiers. &lt;/p&gt;

&lt;h3&gt;
  
  
  Access modifiers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;public&lt;/li&gt;
&lt;li&gt;protected&lt;/li&gt;
&lt;li&gt;default&lt;/li&gt;
&lt;li&gt;private&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Non-access modifiers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;static&lt;/li&gt;
&lt;li&gt;final&lt;/li&gt;
&lt;li&gt;abstract&lt;/li&gt;
&lt;li&gt;synchronized&lt;/li&gt;
&lt;li&gt;volatile&lt;/li&gt;
&lt;li&gt;transient&lt;/li&gt;
&lt;li&gt;native&lt;/li&gt;
&lt;li&gt;strictfp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I am going to talk about access modifiers.&lt;/p&gt;

&lt;p&gt;Access modifiers are responsible to define the scope, to where elements such as methods, variables of a code are visible.&lt;/p&gt;

&lt;h3&gt;
  
  
  private
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The private access modifier is accessible only from the class.&lt;/li&gt;
&lt;li&gt;Private access modifier is not applicable for classes and interfaces&lt;/li&gt;
&lt;li&gt;Applicable to methods, properties, and constructors
&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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp&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;A&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;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;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;A&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;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code has a syntax error. Because cannot access &lt;code&gt;x&lt;/code&gt; from class B, since it is modified as &lt;code&gt;private&lt;/code&gt; in class A. So &lt;code&gt;x&lt;/code&gt; is accessible only from class A.&lt;/p&gt;

&lt;h3&gt;
  
  
  default
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When the developer not defined access modifiers explicitly, that member has the default access modifier.&lt;/li&gt;
&lt;li&gt;default access modifier is visible to all classes in the same package. So-called package-private
&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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp&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;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;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;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;A&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;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the above example, can access &lt;code&gt;x&lt;/code&gt; from Class B. Because &lt;code&gt;x&lt;/code&gt; is modified as default and &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; both classes are in the same package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp&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;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;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp2&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;C&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;m&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;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;In the above code, even class &lt;code&gt;C&lt;/code&gt; has extended class &lt;code&gt;B&lt;/code&gt;, inside the class &lt;code&gt;C&lt;/code&gt; cannot access &lt;code&gt;x&lt;/code&gt;. Because &lt;code&gt;x&lt;/code&gt; is modified as default and class trying to access &lt;code&gt;x&lt;/code&gt; from a different package. &lt;/p&gt;

&lt;h3&gt;
  
  
  Protected
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;protected access modifier is visible to the same package&lt;/li&gt;
&lt;li&gt;protected access modifier is also visible to other packages, but only via a subclass&lt;/li&gt;
&lt;li&gt;applicable for constructor, method, and property
&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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp&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;A&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;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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp2&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;C&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;m&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;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;In the above example from the &lt;code&gt;C&lt;/code&gt; class of package &lt;code&gt;temp2&lt;/code&gt; can access &lt;code&gt;x&lt;/code&gt; which is defined in &lt;code&gt;A&lt;/code&gt; class in package &lt;code&gt;temp&lt;/code&gt; because x has the protected access modifier. Also, &lt;code&gt;x&lt;/code&gt; will be accessible to the Child classes of C.&lt;/p&gt;

&lt;h3&gt;
  
  
  public
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;accessible to everywhere&lt;/li&gt;
&lt;li&gt;applicable to classes, interfaces, properties, methods, constructors
&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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp&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;A&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;temp&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;D&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;A&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;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&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;In the above code, &lt;code&gt;x&lt;/code&gt; variable of the class &lt;code&gt;A&lt;/code&gt; is accessible to everywhere of the code. So can access &lt;code&gt;x&lt;/code&gt; from class D in the package &lt;code&gt;temp2&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison
&lt;/h3&gt;

&lt;h4&gt;
  
  
  public
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;within class                          : yes&lt;/li&gt;
&lt;li&gt;within package                    : yes&lt;/li&gt;
&lt;li&gt;outside package by inherit : yes&lt;/li&gt;
&lt;li&gt;outside the package            : yes&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  protected
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;within class                        : yes&lt;/li&gt;
&lt;li&gt;within package                    : yes&lt;/li&gt;
&lt;li&gt;outside package by inherit : yes&lt;/li&gt;
&lt;li&gt;outside the package           : no&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  default
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;within class                          : yes&lt;/li&gt;
&lt;li&gt;within package                    : yes&lt;/li&gt;
&lt;li&gt;outside package by inherit : no&lt;/li&gt;
&lt;li&gt;outside the package           : no&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  private
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;within class                          : yes&lt;/li&gt;
&lt;li&gt;within package                    : no&lt;/li&gt;
&lt;li&gt;outside package by inherit : no&lt;/li&gt;
&lt;li&gt;outside the package           : no&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>ocpjp</category>
      <category>100daysofocpjp</category>
      <category>day1</category>
    </item>
    <item>
      <title>JDBC in simple</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Fri, 27 Nov 2020 17:31:27 +0000</pubDate>
      <link>https://forem.com/buddhikac96/jdbc-in-simple-c5m</link>
      <guid>https://forem.com/buddhikac96/jdbc-in-simple-c5m</guid>
      <description>&lt;p&gt;In this article, I will straightforwardly explain JDBC. Let's get into the topic directly. First, I will get a simple terminology to explain JDBC. &lt;/p&gt;

&lt;p&gt;Imagine we are living in a city called &lt;strong&gt;City A&lt;/strong&gt;. We are low on water. &lt;strong&gt;City B&lt;/strong&gt; has a well that is full of water. But our language (City A language) and their language (City B language) are different. So, first of all, we are going to need a language translator. Then we need a road to go to City B. Then we need a vehicle to go to City B. When we send our vehicle to City B, there should be a tank on the vehicle to bring water to City A. &lt;/p&gt;

&lt;p&gt;This terminology explains nothing but JDBC application architecture. Imagine there are a Database and a Java application. We need to get data from the database to our Java application. According to the above terminology, our Java application is nothing but the &lt;strong&gt;City A&lt;/strong&gt;, low on water. The database is nothing but the &lt;strong&gt;City B&lt;/strong&gt;, which is rich in water.&lt;/p&gt;

&lt;p&gt;Now we need some application that can convert our Java call into a Database understandable call. In the above terminology, this tool is nothing but the language translator. In JDBC terminology, it is the &lt;strong&gt;Driver&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When we get the connection, we need a vehicle to send our database request to the Java application database. This vehicle is nothing but &lt;strong&gt;Statement Object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When we send our vehicle to City B, there should be a tank to fill water. In JDBC terminology, this tank is nothing but &lt;strong&gt;Result Set&lt;/strong&gt;. When the vehicle brings back the tank with water from City B, we can carefully unload the water tank from the vehicle. When the statement object brings back the data we want in the result set, we can fetch that data from the Java application.&lt;/p&gt;

&lt;p&gt;Now let's try to summarize this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components of the JDBC
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Driver - Translate Java calls into Database calls&lt;/li&gt;
&lt;li&gt;Connection - Create the connection between the java application and the Database&lt;/li&gt;
&lt;li&gt;Statement Object - Transport our database query to the database&lt;/li&gt;
&lt;li&gt;Result Set - Contains the data received from the database&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using these components, we can create a simple JDBC application. These are the basics steps to follow to create a JDBC application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to create a JDBC application
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Load and register the driver&lt;/li&gt;
&lt;li&gt;Establish the connection to the database&lt;/li&gt;
&lt;li&gt;Create the statement object&lt;/li&gt;
&lt;li&gt;Send and execute the query&lt;/li&gt;
&lt;li&gt;Receive and process data&lt;/li&gt;
&lt;li&gt;Close the connection&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following the above steps, we can develop a simple JDBC application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/complete-jdbc-programming-part-1/"&gt;https://www.udemy.com/course/complete-jdbc-programming-part-1/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>jdbc</category>
      <category>100daysofcode</category>
      <category>database</category>
    </item>
    <item>
      <title>Java finalize() is danger</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Fri, 27 Nov 2020 11:51:13 +0000</pubDate>
      <link>https://forem.com/buddhikac96/java-finalize-is-danger-2k1c</link>
      <guid>https://forem.com/buddhikac96/java-finalize-is-danger-2k1c</guid>
      <description>&lt;p&gt;In every Java object, there is a method called &lt;strong&gt;finalize()&lt;/strong&gt;. You can override this method in any of your classes. The purpose of this method was to implement clean-up codes. When the object is garbage collected by the garbage collector, the garbage collector will invoke this method.&lt;/p&gt;

&lt;p&gt;But no one can ensure whether the garbage collector will get a chance to invoke that method. Because we can't force to run the garbage collector even by calling &lt;strong&gt;System.GC()&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Refer to the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Demo&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;DBConnection&lt;/span&gt; &lt;span class="n"&gt;dbcon&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;Demo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DBConnection&lt;/span&gt; &lt;span class="n"&gt;con&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;dbcon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="nd"&gt;@Override&lt;/span&gt;
  &lt;span class="n"&gt;finalize&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;dbcon&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&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="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;1000000&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="nc"&gt;Demo&lt;/span&gt; &lt;span class="n"&gt;demo&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;Demo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
         &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DBConnection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"credentials"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
       &lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A million database connections will be opened when we run this code, but the connection closing logic is implemented inside the &lt;strong&gt;finalize&lt;/strong&gt; method. Since no one can ensure that the garbage collector will trigger before the application ends, a million database connections may be wasted. &lt;/p&gt;

&lt;p&gt;So it is not recommended to override the &lt;strong&gt;finalize&lt;/strong&gt; method. Especially when you want to implement a critical piece of code inside the finalize method.&lt;/p&gt;

&lt;p&gt;Because of this, Java has deprecated the &lt;strong&gt;finalize&lt;/strong&gt; method from version 9. But still, if you want to do this, there are few workarounds that you can take.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Register a shutdownhook
&lt;/h2&gt;

&lt;p&gt;You can use &lt;strong&gt;shutdownhook&lt;/strong&gt; to execute things like resource cleanups, closing log files, etc. Shutdownhook will be executed by the JVM before shutting down the JVM. You can register as many as &lt;strong&gt;shutdownhooks&lt;/strong&gt; you want and those will be executed concurrently, and you can't tell what order it will be executed. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Using PhamtomReference
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;A phantom reference is a kind of reference in Java, where the memory can be reclaimed. The phantom reference is one of the strengths or levels of 'non-strong' reference defined in the Java programming language; the others being weak and soft.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is the same definition from Wikipedia about the phantom reference in Java. I have no much clarity about phantom reference. Maybe I will explain this will in another article. &lt;br&gt;
Until that &lt;a href="https://www.baeldung.com/java-phantom-reference"&gt;https://www.baeldung.com/java-phantom-reference&lt;/a&gt; will help you ;).&lt;/p&gt;

</description>
      <category>java</category>
      <category>finalize</category>
      <category>jvm</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Java String Pool</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Wed, 25 Nov 2020 17:02:57 +0000</pubDate>
      <link>https://forem.com/buddhikac96/java-string-pool-2j56</link>
      <guid>https://forem.com/buddhikac96/java-string-pool-2j56</guid>
      <description>&lt;p&gt;We know in Java Strings are stored in the &lt;b&gt; heap &lt;/b&gt; memory area. Inside this heap memory, there is a specific memory area called &lt;strong&gt;String Pool&lt;/strong&gt;. When we create string primitives, that will be stored in the string pool memory area thanks to the java string's immutability.&lt;/p&gt;

&lt;p&gt;Refer to the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringPoolDemo&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;a&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;b&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="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both &lt;strong&gt;a&lt;/strong&gt;, and &lt;strong&gt;b&lt;/strong&gt; string primitives are the same. So without creating two string objects in a heap, JVM can create only one string and connect both reference variables to the same string object. When this happens, string objects will be created inside the special memory area called the heap's string pool.&lt;/p&gt;

&lt;p&gt;This string pool is a big &lt;strong&gt;hash map&lt;/strong&gt;. All the strings will be stored as elements of the hash map.&lt;/p&gt;

&lt;p&gt;But the story is slightly different when it comes to creating strings using the constructor. Because at that time, the string will behave as an object rather than a primitive. So string object will be created inside the heap. Not in the string pool.&lt;/p&gt;

&lt;p&gt;Also, we can add strings into the string pool memory area manually using the &lt;strong&gt;String.intern()&lt;/strong&gt; function.&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;StringPoolDemo&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s&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="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intern&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;When running this code &lt;strong&gt;s&lt;/strong&gt; will be stored inside the string pool memory.&lt;/p&gt;

&lt;p&gt;Let's write some code to test this out.&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;StringPoolDemo&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;a&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;b&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="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;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&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="c1"&gt;// true&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;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="c1"&gt;// true&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 both outputs, we get &lt;strong&gt;true&lt;/strong&gt; as the output. So we can say that both &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;b&lt;/strong&gt; reference to the same string object.&lt;/p&gt;

&lt;p&gt;You can use &lt;strong&gt;-XX:+PrintStringTableStatistics&lt;/strong&gt; JVM flag to see how many buckets are available in your JVM string pool and how many of them are used. &lt;/p&gt;

&lt;p&gt;If your application has a huge number of string primitives, you can gain considerable efficiency by increasing the string pool size using &lt;strong&gt;-XX: StringTableSize=&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Make sure to use a prime number as the value for best performance.&lt;/p&gt;

</description>
      <category>java</category>
      <category>string</category>
      <category>jvm</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Wanna become a great Java developer . . ?</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Fri, 23 Oct 2020 20:24:32 +0000</pubDate>
      <link>https://forem.com/buddhikac96/wanna-become-a-great-java-developer-5ep0</link>
      <guid>https://forem.com/buddhikac96/wanna-become-a-great-java-developer-5ep0</guid>
      <description>&lt;p&gt;If you want to become a great Java developer, then you must have a good understanding of the Java virtual machine, the JVM where your Java codes run. Read this article which explains the JIT compiler of Java virtual machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/runtimeerror/java-jit-compiler-c538e5e06a2"&gt;https://medium.com/runtimeerror/java-jit-compiler-c538e5e06a2&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Did you lose your internship due to COVID-19?</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Fri, 09 Oct 2020 06:08:33 +0000</pubDate>
      <link>https://forem.com/buddhikac96/did-you-lose-your-internship-due-to-covid-19-48h6</link>
      <guid>https://forem.com/buddhikac96/did-you-lose-your-internship-due-to-covid-19-48h6</guid>
      <description>&lt;p&gt;You may be one intern among thousands of interns, who lost their internship due to COVID-19. But you know, still, the opportunity is there. COVID-19 only needs social distance. But still, you can do your intern remotely. &lt;/p&gt;

&lt;p&gt;There are many programs which are providing software engineering internships. Google Summer of Code is the most popular event among all of that. So do you know, the Google Summer of Code 2021 is coming? Don't be late. Read this article to learn how to get selected into Google summer of code 2021. &lt;/p&gt;

&lt;p&gt;P.S. &lt;br&gt;
If you are fed up with the non-paying internships, Google's summer of code is not among them. They provide amazing stipends. Anyway, experience is the best thing you should collect from the event.&lt;/p&gt;

&lt;p&gt;Don't be late. If you start ASAP, the opportunity is bigger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/runtimeerror/path-to-gsoc-2021-e6a81e62308d"&gt;https://medium.com/runtimeerror/path-to-gsoc-2021-e6a81e62308d&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Do you know how the operating system manages to run multiple programs concurrently? </title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Fri, 09 Oct 2020 05:42:29 +0000</pubDate>
      <link>https://forem.com/buddhikac96/do-you-know-how-the-operating-system-manages-to-run-multiple-programs-concurrently-2gg4</link>
      <guid>https://forem.com/buddhikac96/do-you-know-how-the-operating-system-manages-to-run-multiple-programs-concurrently-2gg4</guid>
      <description>&lt;p&gt;Modern operating systems can run multiple programs at one time. Those operating systems are using an operation call context switching to manage to run multiple programs at once. &lt;/p&gt;

&lt;p&gt;Would you like to learn how context switching happens in the operating system? Read this article. I have explained in a very simple manner how context switching happens in a modern operating system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/runtimeerror/context-switching-in-depth-1d6d4e51ab32"&gt;https://medium.com/runtimeerror/context-switching-in-depth-1d6d4e51ab32&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>What is the compiler</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Fri, 09 Oct 2020 05:35:30 +0000</pubDate>
      <link>https://forem.com/buddhikac96/what-is-the-compiler-11op</link>
      <guid>https://forem.com/buddhikac96/what-is-the-compiler-11op</guid>
      <description>&lt;p&gt;The compiler is a nothing but a source code translator. There are multiple types of compilers according to how they translate source code to another source code. &lt;/p&gt;

&lt;p&gt;When a compiler translates high-level source code into low-level source code we call it a traditional compiler. &lt;/p&gt;

&lt;p&gt;When the compiler translates low-level source code, we call it decompiler.&lt;/p&gt;

&lt;p&gt;When the compiler translates high-level language into another high-level language we call it a transcompiler or transpiler.&lt;/p&gt;

&lt;p&gt;Would you like to learn more about compilers and their architecture? Read this article. I have briefly explained how compilers work in this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/runtimeerror/what-is-the-compiler-62c595a828c9"&gt;https://medium.com/runtimeerror/what-is-the-compiler-62c595a828c9&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Erlang Run Time System Book</title>
      <dc:creator>Buddhika Chathuranga</dc:creator>
      <pubDate>Sun, 17 May 2020 03:12:55 +0000</pubDate>
      <link>https://forem.com/buddhikac96/erlang-run-time-system-book-546p</link>
      <guid>https://forem.com/buddhikac96/erlang-run-time-system-book-546p</guid>
      <description>&lt;p&gt;I just finished reading The Erlang Run-Time System Book. The most awesome book that I got to read 😍. Hats off to the contributors 🎩. If you wish to get started with Erlang or Elixir, highly recommended to read. And please be kind to contribute. If you are a beginner, there are some typos that you can help with 💪.&lt;/p&gt;

&lt;p&gt;Find the book here : &lt;a href="https://blog.stenmans.org/theBeamBook/"&gt;https://blog.stenmans.org/theBeamBook/&lt;/a&gt; &lt;/p&gt;

</description>
      <category>erlang</category>
      <category>elixir</category>
      <category>beam</category>
      <category>erts</category>
    </item>
  </channel>
</rss>
