<?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: Akam Chidi</title>
    <description>The latest articles on DEV Community by Akam Chidi (@akamzchidi).</description>
    <link>https://dev.to/akamzchidi</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%2F1465898%2F2cdb8342-775a-47d4-8ab8-43f467d5d86d.png</url>
      <title>DEV Community: Akam Chidi</title>
      <link>https://dev.to/akamzchidi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akamzchidi"/>
    <language>en</language>
    <item>
      <title>YAML Guide For Beginners: Learn YAML In 10 Minutes</title>
      <dc:creator>Akam Chidi</dc:creator>
      <pubDate>Tue, 15 Jul 2025 15:42:08 +0000</pubDate>
      <link>https://dev.to/akamzchidi/yaml-guide-for-beginners-learn-yaml-in-10-minutes-1627</link>
      <guid>https://dev.to/akamzchidi/yaml-guide-for-beginners-learn-yaml-in-10-minutes-1627</guid>
      <description>&lt;p&gt;As a Software Engineer, Cloud Engineer, SRE, or DevOps engineer, YAML is one language you must understand well. Most of the configuration files you will read or write will be in YAML. &lt;/p&gt;

&lt;p&gt;That is why it is important that you know how to not just read YAML as a language but also understand how to create files in YAML too. &lt;/p&gt;

&lt;p&gt;This article will take you through all you need to know about the YAML. You will also learn how to read and write YAML files, including their format and syntax.&lt;/p&gt;

&lt;p&gt;Before reading this post, you need to have a basic understanding of programming. Most importantly, you need to know about data and objects in programming. &lt;/p&gt;

&lt;h2&gt;
  
  
  What Is YAML
&lt;/h2&gt;

&lt;p&gt;YAML, also known as &lt;em&gt;“YAML A’int No Markup Language”&lt;/em&gt; is a human readable data serialization language. It is a data format that is used to store and exchange data. &lt;/p&gt;

&lt;p&gt;As the name implies, YAML is not like any other Markup language like HTML. It is more like a language, used to represent serialized data in a text format. &lt;/p&gt;

&lt;p&gt;To understand YAML better, you first need to understand the concept of serialization and deserialization of data. The next session will explain this in detail. &lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Serialization and Deserialization
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9c31noktexbc2khob627.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9c31noktexbc2khob627.png" alt=" " width="800" height="264"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serialization&lt;/strong&gt; is the process of converting data objects or complex data structures into small, manageable chunks of data, also known as a stream of bytes. &lt;/p&gt;

&lt;p&gt;In other words, serialization converts complex data objects to a form that is easily transmitted and read on different systems. The serialization process ensures that the state of the data is preserved during the transmission process. &lt;/p&gt;

&lt;p&gt;The stream of bytes can be transferred in a YAML file format, a database, or memory. This way, the data can be read by different systems or programming languages. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deserialization&lt;/strong&gt; on the other hand, is the process of converting a stream of bytes to a data object or complex data structure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"It is good to mention at this point that YAML is not a programming language and cannot be used to add commands. You only use it to store data."&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use YAML?
&lt;/h2&gt;

&lt;p&gt;As you may know, there are other serialization languages out there, like JSON and XML. YAML seems to be the most popular and preferred option. &lt;/p&gt;

&lt;p&gt;Here are some of the reasons why YAML is preferred over other serialization languages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Readable: YAML is simple to read and write. It is easier for non-programmers to read and understand YAML than JSON. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Popularity: YAML is very popular and is used in most programming languages. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy To Convert To Other Serialization Languages: It is easy to convert an input in YAML to JSON and XML. You write the input in YAML and get the output in JSON or XML.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Additional Tools: YAML has various tools available to enhance its functionality. Example of these are Passers and Lens IDE. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functionalities: The language has additional functionalities that other alternatives do not accommodate. For instance, comments. You can write comments in YAML which comes in handy for future reference. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use Cases Of YAML
&lt;/h2&gt;

&lt;p&gt;The main use case of YAML is to create configuration files for &lt;strong&gt;Kubernetes, Docker containers, Ansible, GitHub Actions, and many more.&lt;/strong&gt; Also, it is used for logs and cache in networking. &lt;/p&gt;

&lt;h2&gt;
  
  
  Create a YAML File
&lt;/h2&gt;

&lt;p&gt;To create a YAML file, add a &lt;em&gt;“.yaml”&lt;/em&gt; or &lt;em&gt;“.yml”&lt;/em&gt; extension to the file. This indicates that the file is written in YAML. &lt;/p&gt;

&lt;h2&gt;
  
  
  Data Types In YAML
&lt;/h2&gt;

&lt;p&gt;YAML has the same data types and data structures as most programming languages out there. It has &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;integers&lt;/li&gt;
&lt;li&gt;strings&lt;/li&gt;
&lt;li&gt;Booleans&lt;/li&gt;
&lt;li&gt;floats &lt;/li&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;li&gt;lists&lt;/li&gt;
&lt;li&gt;dictionaries.
&lt;/li&gt;
&lt;/ul&gt;

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

#Integer in YAML
password: 010

#String in YAML
password: Hello
password2: "Hello World"
password: 010

#Boolean in YAML
test: false
deploy: true

#Float in YAML
product_price: 37.89

#Null in YAML
user_name: null

#List in YAML
states:
    - new york
    - florida
    - texas
    - ohio

#Dictionary In YAML
product:
  product_name: mobile phone
  brand: samsung
  price: 230
  color: black
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, you can specify the data type in YAML by adding two exclamation marks, then the data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nomba: !!int 10
float: !!float 27.5
boolean: !!bool true
string: !!str jay 
null: !!null Null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rules For Writing In YAML
&lt;/h2&gt;

&lt;p&gt;YAML has a strict syntax format. These are some of the rules to bear in mind when writing in YAML: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Indentation is essential. Preferably two space indentations. &lt;/li&gt;
&lt;li&gt;Keys in dictionaries are case sensitive.&lt;/li&gt;
&lt;li&gt;Always maintain a space after the colon. &lt;/li&gt;
&lt;li&gt;Start your list with a dash “-“and no space.&lt;/li&gt;
&lt;li&gt;Include --- at the start of any YAML file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  YAML Writing Format and Syntax
&lt;/h2&gt;

&lt;p&gt;Understanding the format and syntax of YAML will help you write and read any file in YAML Language. &lt;/p&gt;

&lt;h3&gt;
  
  
  a. Key Value:
&lt;/h3&gt;

&lt;p&gt;This contains one key and one value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
password: 0000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  b. List:
&lt;/h3&gt;

&lt;p&gt;A List is used when an object has multiple values. It is similar to an array in some programming languages. &lt;/p&gt;

&lt;p&gt;You can create a list using the inline or block format. The inline format is more readable and is most commonly used. A list can contain a single data type or a mixed data type.&lt;br&gt;
&lt;/p&gt;

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

# Inline List:

user_detail:
  - username
  - email
  - password

#Block List: 

user_detail: [username, email, password]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;i. Mixed List:&lt;/strong&gt; contains a mixture of different data types&lt;br&gt;
single data list: similar data types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Mixed List: 

user_detail:
  - jay
  - 2020
  - active
  - 73.9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;ii. Nested List:&lt;/strong&gt; As the name implies, a nested list contains multiple lists with. One or more lists are nested in a parent list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Nested List in YAML: 

product:
  - product_name: phone
    brands:
      - apple
      - samsung
      - redmi
      - zero
  - colors: mobile
    os:
      - android
      - ios
      - windows mobile

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  c. Dictionary:
&lt;/h3&gt;

&lt;p&gt;is an object with one or multiple key-value pairs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Dictionary In YAML: 

user:
  name: obi
  age: 32
  city: manchester
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;i. Mixed Dictionary:&lt;/strong&gt; Is made of multiple data types. It is mostly a combination of key-value pairs, single values and lists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Mixed Dictionary:

user: 
  name: obi
  age: 32
  city: lagos
  products: 
    - phone
    - glasses
    - shoes
  user_login: 
    username: jay
    email: j@gmail.com
    password: 1009
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;ii. Nested Dictionary:&lt;/strong&gt; Is made up of more than one dictionaries. Here, two or more dictionaries is nested inside a parent dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Nested Dictionaries in YAML:

product:
  name: mobile
  description:
    brand: samsung
    os: android
  price:
    pro: 700
    promax: 950

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  d. Anchor and Alias
&lt;/h3&gt;

&lt;p&gt;This is similar to a function or method in most programming languages. It is used to create a piece of reusable “code” in YAML. &lt;/p&gt;

&lt;p&gt;You create an anchor to define the content of the object and use alias to refer to the anchor.  You write an anchor with &lt;strong&gt;“&amp;amp;”&lt;/strong&gt; and alias with &lt;strong&gt;“*”&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Anchor and Alias in YAML

user_details: &amp;amp;user1
  username: jay
  age: 32
  password: 0001
  email: j@gmail.com

  adduser: 
    &amp;lt;&amp;lt;: *user1
    add: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  e. Document
&lt;/h3&gt;

&lt;p&gt;This is used to create multiple documents in a single YAML file. Use &lt;strong&gt;“---”&lt;/strong&gt; to separate the documents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
# Document in YAML

username: jay
  age: 32
  password: 0001
  email: j@gmail.com

---

username: chidi
  age: 30
  password: 0002
  email: c@gmail.com

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  f. Comments in YAML
&lt;/h3&gt;

&lt;p&gt;It was mentioned earlier that YAML allows comments. Comments notify the compiler or interpreter to ignore a line or block of code. You write a comment by add “#” to the beginning of the line of code.&lt;/p&gt;

&lt;p&gt;YAML does not support multiple lines of comments. To include a comment in a block of code, adding “#” at the beginning of each line of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# ignore this line of code.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Validate YAML
&lt;/h2&gt;

&lt;p&gt;There are ways to validate the accuracy of YAML as you write. One way to do that is to use the YAML validator site. Paste your YAML code in the file and click on  &lt;a href="https://www.yamllint.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;YAML-Validator&lt;/strong&gt;&lt;/a&gt; to check the accuracy of your YAML write-up. &lt;/p&gt;

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

&lt;p&gt;So far, this article has covered the basics of YAML. That includes data types, use cases for YAML, writing format, and writing rules. At this point, you should be able to write and understand the format of any YAML file you read. &lt;/p&gt;

</description>
      <category>yaml</category>
      <category>devops</category>
      <category>deployment</category>
      <category>python</category>
    </item>
    <item>
      <title>Exception Handling In Java</title>
      <dc:creator>Akam Chidi</dc:creator>
      <pubDate>Fri, 11 Jul 2025 13:48:07 +0000</pubDate>
      <link>https://dev.to/akamzchidi/exception-handling-in-java-3app</link>
      <guid>https://dev.to/akamzchidi/exception-handling-in-java-3app</guid>
      <description>&lt;p&gt;When you run a Java code or program, it will either compile and execute or throw an error. When a code throws an, it’s a result of either an error or an exception.&lt;/p&gt;

&lt;p&gt;An error is more critical. It occurs outside the scope of the code but within the environment in which the application is running. The program is not expected to catch and handle an error.&lt;/p&gt;

&lt;p&gt;Some examples of errors are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OutOfMemoryError&lt;/li&gt;
&lt;li&gt;VirtualMachineError&lt;/li&gt;
&lt;li&gt;StackOverFlowError&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exceptions occur within the scope of the code. It is also known as execution error which means that it occurs during the execution of the code. The programmer is expected to catch and handle exceptions in a program.&lt;/p&gt;

&lt;p&gt;This post will focus more on exceptions and runtime errors specifically. You will learn all about exceptions and how to handle exception errors in your Java program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Execution Errors In Java
&lt;/h2&gt;

&lt;p&gt;There are three types of execution errors in Java. Here is a breakdown of each of them&lt;/p&gt;

&lt;h3&gt;
  
  
  . Compile-Time Error
&lt;/h3&gt;

&lt;p&gt;This type of error is known as compilation error or checked error. A Java program throws this code during the compilation stage. &lt;/p&gt;

&lt;p&gt;In other words, this error occurs when the Javac code is being compiled to bytecode. The compile-time error is usually caused by an error in the code syntax.&lt;/p&gt;

&lt;p&gt;For instance, when the programmer fails to add a semicolon at the end of a statement or fails to initialize a variable before printing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public class JavaExceptionCodes {
    public static void main (String []args){
        int y = 10      //no semicolon
        System.out.println(y);
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Run-Time Error
&lt;/h3&gt;

&lt;p&gt;The run-time error is also known as the exception error or unchecked error. It occurs as the program is being executed. The code will compile but will throw an error during runtime.&lt;/p&gt;

&lt;p&gt;The runtime error can be further classified into built-in exceptions or user-defined exceptions. Examples of built-in exceptions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ArrayIndexOutOfBoundsException&lt;/li&gt;
&lt;li&gt;ArithmeticException&lt;/li&gt;
&lt;li&gt;FileNotFoundException&lt;/li&gt;
&lt;li&gt;NullPointerException&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance if a user inputs an int instead of a string or inserts values bigger than the size of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class JavaExceptionCodes {
    public static void main (String []args) {
        int nombas[] = {1, 2, 3, 4, 5};
        System.out.println(nombas[6]); 

    }
}
//ArrayIndexOutOfBoundsException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Logical Error
&lt;/h3&gt;

&lt;p&gt;Also known as semantic error, it occurs if there is an error in a program’s logical output. For instance, 3 + 3 shows an output of 7, or when you log into your account and land on another profile.&lt;/p&gt;

&lt;p&gt;The compile-time error is the easiest to catch because your IDE will always point out this error to the programmer. Logical error most times is the hardest error to see because the code will throw no compile time or runtime error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concept of Exception Handling
&lt;/h2&gt;

&lt;p&gt;Exception handling is used to catch errors in a program and maintain the normal flow of the execution of a program. Usually, once a program throws an error, the code will stop executing.&lt;/p&gt;

&lt;p&gt;Catching and handling errors beforehand will enable the program to skip the block of code with errors and continue executing the rest of the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exception Handling Methods:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Try – Catch – Finally – Throw – Throws&lt;/strong&gt; are the list of methods for exception handling in Java. Let’s break each of them in detail with examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  . Try
&lt;/h3&gt;

&lt;p&gt;Try is used to enclose a line or blocks of code that may throw an exception. There is also a nested try which is a try with an inner try block (s). They serve the same purpose.&lt;/p&gt;

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

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

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Catch
&lt;/h3&gt;

&lt;p&gt;The catch is a block of code used to catch errors thrown in the try block.&lt;/p&gt;

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

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

}
catch (Exception e){

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;multiple catch blocks&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class JavaExceptionCodes {
    public static void main (String []args) {
        int x = 10;
        int y = 0;
        int z ;
        int nombas [] = {5, 6, 7, 8, 9};
        try {
            System.out.println(z = x / y);
            System.out.println(nombas[6]);
        }
        catch (ArithmeticException e){
            System.out.println("Can't divide by 0. Choose another number" + e);
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Array Out Of Bound" + e);
        }
        System.out.println("End");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Finally
&lt;/h3&gt;

&lt;p&gt;Finally, is an optional block of code, that is usually placed after the try and catch code blocks. It is used to execute important codes. The finally block is always executed whether the program throws an error or not.&lt;/p&gt;

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

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

}
catch (Exception e){

}
finally{

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Throw
&lt;/h3&gt;

&lt;p&gt;Throw is a keyword used to throw an exception inside a method or block of code.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public class JavaExceptionCodes {
    public static void main (String []args) {

           try { throw new ArithmeticException("Try Run");
           }
           catch (ArithmeticException e){
               System.out.println("Catch Exception Error");
           }

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Throws
&lt;/h3&gt;

&lt;p&gt;Unlike the throw keyword, the throws keyword is not used to throw an exception. However, it specifies that there may be an exception in the method. The Throws is used in the method signature and not inside the method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices For Handling Exceptions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A try block can have several statements (lines of code). It is best to create multiple catch blocks to catch the error in each statement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always specify the type of exception if you are working with multiple catch blocks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If there are multiple statements in a try block and you don’t know the statement with the error, create a final general exception at the end of other exceptions to catch unknown errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always place the general exception catch block at the end of the other custom exception catch blocks. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>exceptionhandling</category>
      <category>springboot</category>
      <category>programming</category>
    </item>
    <item>
      <title>Authentication Vs Authorization In Backend Systems</title>
      <dc:creator>Akam Chidi</dc:creator>
      <pubDate>Thu, 30 Jan 2025 11:36:08 +0000</pubDate>
      <link>https://dev.to/akamzchidi/authentication-vs-authorization-in-backend-systems-2n0d</link>
      <guid>https://dev.to/akamzchidi/authentication-vs-authorization-in-backend-systems-2n0d</guid>
      <description>&lt;p&gt;The backend system of any application handles sensitive data. That includes operations to create, modify, store, retrieve, and delete data when needed. &lt;/p&gt;

&lt;p&gt;It is important to know who can access and modify these data, so security checks and systems are put in place to limit who can access them.  &lt;/p&gt;

&lt;p&gt;Authentication and authorization are part of the security systems that are put in place to secure sensitive data in backend systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Authentication In Backend Systems
&lt;/h3&gt;

&lt;p&gt;Authentication involves checking the identity of a user or users before providing access to the system or app. It is used to verify user credentials before granting access to an entire system or specific functionality. &lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Authorization In Backend Systems
&lt;/h3&gt;

&lt;p&gt;Authorization checks if a user(s) has the authority(ies) to access a resource in an application. In other words, authorization confirms if a user is authorized or has the authorization to access a specific functionality in an app or not. &lt;/p&gt;

&lt;h2&gt;
  
  
  Importance Of Authentication and Authorization In A Backend System
&lt;/h2&gt;

&lt;p&gt;As mentioned, authentication and authorization are both security systems that help secure sensitive data in any backend system. Each of them has a distinct role in securing the APIs and data in the backend system of any application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference Between Authentication and Authorization
&lt;/h2&gt;

&lt;p&gt;Let’s explore the differences between authentication and authorization and the role each plays in backend system security. &lt;/p&gt;

&lt;p&gt;Let’s explore the differences between authentication and authorization and the role each plays in backend system security. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Authentication&lt;/th&gt;
&lt;th&gt;Authorization&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Use Case&lt;/td&gt;
&lt;td&gt;Checks User ID or log in details.  Verifies log in details and provide access.&lt;/td&gt;
&lt;td&gt;Checks for User authorization to access a resource or functionality.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When Invoked&lt;/td&gt;
&lt;td&gt;Authentication is done before  authorization.&lt;/td&gt;
&lt;td&gt;Authorization is done after authentication.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requirement&lt;/td&gt;
&lt;td&gt;Requires User ID or Login Details.&lt;/td&gt;
&lt;td&gt;Requires User Role or Privileges.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Error Response&lt;/td&gt;
&lt;td&gt;Returns a 401 Error Response if  authentication fails.&lt;/td&gt;
&lt;td&gt;Returns a 403 Error Response if  authorization fails.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Summary&lt;/td&gt;
&lt;td&gt;Authentication verifies a User's  Identity.&lt;/td&gt;
&lt;td&gt;Authorization checkmates what a User is allowed to do after the User's identity has been verified.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Real Case Scenario Of Concepts
&lt;/h2&gt;

&lt;p&gt;If you’re still finding it hard to differentiate between these concepts, here is a real case scenario to help you understand better. &lt;/p&gt;

&lt;p&gt;For instance, a bank application. A customer and bank employee can access the bank application. Both are considered as users. The customer or bank employee trying to access the bank app has to input a valid email address and password into the bank application. &lt;/p&gt;

&lt;p&gt;The function of the authentication system is to ensure that the username and password entered by either the customer or the employee are valid. After verifying these details, both the customer and employee can access the bank app. &lt;/p&gt;

&lt;p&gt;Authorization on the other hand will be used to determine the roles and authorities of the user to checkmate the functionality the user can access on the app. In this instance, the bank employee will have access to the database of customers, which the customer can’t access. &lt;/p&gt;

&lt;h3&gt;
  
  
  In Summary
&lt;/h3&gt;

&lt;p&gt;So far, this post has explained authentication and authorization in detail and the roles both concepts play in securing the backend system of an application. This is an introductory post. Subsequent posts will handle security architecture and more complex details. &lt;/p&gt;

</description>
      <category>backend</category>
      <category>security</category>
      <category>authorization</category>
      <category>apisecurity</category>
    </item>
    <item>
      <title>Exception Handling In Java</title>
      <dc:creator>Akam Chidi</dc:creator>
      <pubDate>Tue, 23 Jul 2024 17:56:16 +0000</pubDate>
      <link>https://dev.to/akamzchidi/exception-handling-in-java-f32</link>
      <guid>https://dev.to/akamzchidi/exception-handling-in-java-f32</guid>
      <description>&lt;p&gt;When you run a Java code or program, it will either compile and execute or throw an error. When a code throws an, it’s a result of either an error or an exception.&lt;/p&gt;

&lt;p&gt;An error is more critical. It occurs outside the scope of the code but within the environment in which the application is running. The program is not expected to catch and handle an error.&lt;/p&gt;

&lt;p&gt;Some examples of errors are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OutOfMemoryError&lt;/li&gt;
&lt;li&gt;VirtualMachineError&lt;/li&gt;
&lt;li&gt;StackOverFlowError&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exceptions occur within the scope of the code. It is also known as execution error which means that it occurs during the execution of the code. The programmer is expected to catch and handle exceptions in a program.&lt;/p&gt;

&lt;p&gt;This post will focus more on exceptions and runtime errors specifically. You will learn all about exceptions and how to handle exception errors in your Java program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Execution Errors In Java
&lt;/h2&gt;

&lt;p&gt;There are three types of execution errors in Java. Here is a breakdown of each of them&lt;/p&gt;

&lt;h3&gt;
  
  
  . Compile-Time Error
&lt;/h3&gt;

&lt;p&gt;This type of error is known as compilation error or checked error. A Java program throws this code during the compilation stage. &lt;/p&gt;

&lt;p&gt;In other words, this error occurs when the Javac code is being compiled to bytecode. The compile-time error is usually caused by an error in the code syntax.&lt;/p&gt;

&lt;p&gt;For instance, when the programmer fails to add a semicolon at the end of a statement or fails to initialize a variable before printing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public class JavaExceptionCodes {
    public static void main (String []args){
        int y = 10      //no semicolon
        System.out.println(y);
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Run-Time Error
&lt;/h3&gt;

&lt;p&gt;The run-time error is also known as the exception error or unchecked error. It occurs as the program is being executed. The code will compile but will throw an error during runtime.&lt;/p&gt;

&lt;p&gt;The runtime error can be further classified into built-in exceptions or user-defined exceptions. Examples of built-in exceptions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ArrayIndexOutOfBoundsException&lt;/li&gt;
&lt;li&gt;ArithmeticException&lt;/li&gt;
&lt;li&gt;FileNotFoundException&lt;/li&gt;
&lt;li&gt;NullPointerException&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance if a user inputs an int instead of a string or inserts values bigger than the size of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class JavaExceptionCodes {
    public static void main (String []args) {
        int nombas[] = {1, 2, 3, 4, 5};
        System.out.println(nombas[6]); 

    }
}
//ArrayIndexOutOfBoundsException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Logical Error
&lt;/h3&gt;

&lt;p&gt;Also known as semantic error, it occurs if there is an error in a program’s logical output. For instance, 3 + 3 shows an output of 7, or when you log into your account and land on another profile.&lt;/p&gt;

&lt;p&gt;The compile-time error is the easiest to catch because your IDE will always point out this error to the programmer. Logical error most times is the hardest error to see because the code will throw no compile time or runtime error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concept of Exception Handling
&lt;/h2&gt;

&lt;p&gt;Exception handling is used to catch errors in a program and maintain the normal flow of the execution of a program. Usually, once a program throws an error, the code will stop executing.&lt;/p&gt;

&lt;p&gt;Catching and handling errors beforehand will enable the program to skip the block of code with errors and continue executing the rest of the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exception Handling Methods:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Try – Catch – Finally – Throw – Throws&lt;/strong&gt; are the list of methods for exception handling in Java. Let’s break each of them in detail with examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  . Try
&lt;/h3&gt;

&lt;p&gt;Try is used to enclose a line or blocks of code that may throw an exception. There is also a nested try which is a try with an inner try block (s). They serve the same purpose.&lt;/p&gt;

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

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

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Catch
&lt;/h3&gt;

&lt;p&gt;The catch is a block of code used to catch errors thrown in the try block.&lt;/p&gt;

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

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

}
catch (Exception e){

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;multiple catch blocks&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class JavaExceptionCodes {
    public static void main (String []args) {
        int x = 10;
        int y = 0;
        int z ;
        int nombas [] = {5, 6, 7, 8, 9};
        try {
            System.out.println(z = x / y);
            System.out.println(nombas[6]);
        }
        catch (ArithmeticException e){
            System.out.println("Can't divide by 0. Choose another number" + e);
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println("Array Out Of Bound" + e);
        }
        System.out.println("End");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Finally
&lt;/h3&gt;

&lt;p&gt;Finally, is an optional block of code, that is usually placed after the try and catch code blocks. It is used to execute important codes. The finally block is always executed whether the program throws an error or not.&lt;/p&gt;

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

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

}
catch (Exception e){

}
finally{

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Throw
&lt;/h3&gt;

&lt;p&gt;Throw is a keyword used to throw an exception inside a method or block of code.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public class JavaExceptionCodes {
    public static void main (String []args) {

           try { throw new ArithmeticException("Try Run");
           }
           catch (ArithmeticException e){
               System.out.println("Catch Exception Error");
           }

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  . Throws
&lt;/h3&gt;

&lt;p&gt;Unlike the throw keyword, the throws keyword is not used to throw an exception. However, it specifies that there may be an exception in the method. The Throws is used in the method signature and not inside the method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices For Handling Exceptions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A try block can have several statements (lines of code). It is best to create multiple catch blocks to catch the error in each statement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always specify the type of exception if you are working with multiple catch blocks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If there are multiple statements in a try block and you don’t know the statement with the error, create a final general exception at the end of other exceptions to catch unknown errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always place the general exception catch block at the end of the other custom exception catch blocks. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>exceptionhandling</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Memory Management and Garbage Collection In Java</title>
      <dc:creator>Akam Chidi</dc:creator>
      <pubDate>Tue, 28 May 2024 13:58:06 +0000</pubDate>
      <link>https://dev.to/akamzchidi/memory-management-and-garbage-collection-in-java-3odf</link>
      <guid>https://dev.to/akamzchidi/memory-management-and-garbage-collection-in-java-3odf</guid>
      <description>&lt;p&gt;As a Java developer, memory management and garbage collection is one topic you need to understand well. If you are having issues understanding these concepts, no need to worry.&lt;/p&gt;

&lt;p&gt;This post will explain the entire concepts in details while giving examples. At the end of this post, you should understand the following concepts well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory Management in Java&lt;/li&gt;
&lt;li&gt;Garbage Collection in Java&lt;/li&gt;
&lt;li&gt;Types of Memory in Java&lt;/li&gt;
&lt;li&gt;Garbage Collection Process&lt;/li&gt;
&lt;li&gt;Types of Garbage Collectors in Java&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction To Concepts
&lt;/h2&gt;

&lt;p&gt;There are some basic concepts you need to know that will help you understand this topic better.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Memory In Java:
&lt;/h3&gt;

&lt;p&gt;This refers to the allocated space where objects, variables and data are stored in the process of executing a java application. In other words, every variable, object or other data types the developer inputs in the process of creating a java program is stored in memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Memory management:
&lt;/h3&gt;

&lt;p&gt;Memory Management is the process of allocation and deallocation of memory space for objects during the exaction of an application. This process takes place in the Java Virtual Machine JVM.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Garbage Collection:
&lt;/h3&gt;

&lt;p&gt;This, on the other hand is the process of cleaning up the memory space. How it works is that the garbage collector identifies and removes objects that are no longer referenced in the heap memory.&lt;/p&gt;

&lt;p&gt;In some programming languages like C and C++, it's the work of the programmer to manually handle the memory management process. The programmer is expected to manually allocate and deallocate memory space while creating the application.&lt;/p&gt;

&lt;p&gt;Thankfully, Java handles the process automatically via garbage collection. The garbage collection process is better than manual because it helps to prevent the issue of &lt;strong&gt;dangling reference.&lt;/strong&gt; You will understand how the garbage collection process works later in this post.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Dangling Reference:
&lt;/h3&gt;

&lt;p&gt;This error occurs when a reference or pointer still points to a memory location in the heap memory that has been reallocated or removed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types Of Memory In Java
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, memory in Java is the space where data is stored during the process of executing a program. There are three types of memory spaces in Java. They include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Stack Memory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Heap Memory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MetaSpace Memory&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s explain each of the memory types in details now.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Stack Memory:
&lt;/h3&gt;

&lt;p&gt;The stack memory is the section of the memory where method invocation takes place. Method invocation happens when a new method is created, a scope is created in the stack memory to store the data in the method. That includes, local variables and parameters.&lt;/p&gt;

&lt;p&gt;The stack memory is always smaller than the heap memory. Also, during the garbage collection process, the stack memory gets cleaned up first. The process happens automatically when a method is done executing.&lt;/p&gt;

&lt;p&gt;The types of data that is stored in the Stack memory includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Primitive Values/ Temporary Variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory block for methods (Scope).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reference of an object. Note that the object itself is stored in the heap memory while the reference is stored in the stack memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread. Each thread has its own stack memory while all threads share a common heap memory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, the stack memory throws the ‘stackoverflow’ error when it is full.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Heap Memory
&lt;/h3&gt;

&lt;p&gt;The heap memory is the section of the memory where objects are stored. It is larger than the stack memory. The heap memory is divided into two main sessions, the young generation and the old generation.&lt;/p&gt;

&lt;p&gt;The young generation is further divided into Eden Space and Survivor Space. The Survivor space is further divided into S0 and S1 spaces. You will understand the types of the heap memory and their functions later in this post.&lt;/p&gt;

&lt;h3&gt;
  
  
  . MetaSpace Memory
&lt;/h3&gt;

&lt;p&gt;The metaspace was previously known as the Permanent Generation before the Java 7 version, and was a part of the heap memory. The Permanent Generation was not expandable and throws a heap error once the memory is full.&lt;/p&gt;

&lt;p&gt;Now, it has evolved to metaspace memory, which is a stand-alone memory space from the heap memory. The metaspace stores class variables, constants, annotations, and class metadata. &lt;/p&gt;

&lt;p&gt;In other words, information about the class from which objects are created including constants (static and final) are stored in the metaspace memory.&lt;/p&gt;

&lt;p&gt;The JVM loads the class if it needs it and removes if from the metaspace when it has no need for it. One good thing about the metaspace is that it is expandable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Picture To Explain Data Storage In Java Memory.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] a) {
        int seat = 4; //primitive data
        String str1 = "30"; //string literal
        String str2 = new String ("30") //String Object
        Car newCar = new Car(); //object

    }

    public void method2(){
      String str3 = "30"; //string literal
      Car newCar2 = new Car(); //object

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz3k1xatw2os8d9kyp38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz3k1xatw2os8d9kyp38.png" alt=" " width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How Memory Management Works In Java
&lt;/h2&gt;

&lt;p&gt;The memory management process frees up space in memory for incoming data. Here is how the process work; when a method is done executing, the JVM cleans up the stack memory automatically. The values and variables in the method are forgotten.&lt;/p&gt;

&lt;p&gt;The JVM does not clean up the heap memory automatically when a method is done executing because the object on the heap may be needed by another method which is still in the stack memory.&lt;/p&gt;

&lt;p&gt;An object becomes eligible for garbage collection when it no longer has a reference in the stack memory.&lt;/p&gt;

&lt;p&gt;Java Virtual Machine JVM initiates the garbage collection process automatically. The developer can suggest to the JVM to start the garbage collection process by executing &lt;strong&gt;‘System.gc( )’&lt;/strong&gt; code, but it doesn’t guarantee that it will start the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Garbage Collection Works In Java
&lt;/h2&gt;

&lt;p&gt;The space that a method occupies in the stack memory is known as a scope. The closing bracket in a method marks the end of a scope and the cleanup process starts. All variables and references in the method are removed.&lt;/p&gt;

&lt;p&gt;The JVM deletes the scope starting from the latest scope. It applies the last in, first out cleanup process. After the variables and object references in the stack memory is removed, the garbage collection process starts in the heap memory. The garbage collector deletes the unreferenced objects in the heap memory.&lt;/p&gt;

&lt;p&gt;The first garbage collection process is called the mark and sweep algorithm process. The garbage collector checks for and marks dead objects (objects without reference) and separates them from live objects (objects with reference). Objects with no references are moved to the Eden space.&lt;/p&gt;

&lt;p&gt;The surviving objects is swept to either the S0 or S1 which is known as the survivor space. The survivor space has a threshold as new objects are created and the garbage collection process continues.&lt;/p&gt;

&lt;p&gt;Once the survivor space gets to its threshold, it is promoted to old generation space in the heap memory.&lt;/p&gt;

&lt;p&gt;The garbage collection process runs more frequently in the new generation than in the old generation. This is because objects moved to the old generation are used frequently and may have several references pointing to them indirectly.&lt;/p&gt;

&lt;p&gt;Note that the garbage collection process that takes place in the young generation is known as the minor garbage collection while that of the old generation is known as major garbage collection.&lt;/p&gt;

&lt;p&gt;Knowledge of when to promote an object from young to old generation is vital in the garbage collection process.&lt;/p&gt;

&lt;p&gt;The garbage collector uses different collection strategies or algorithms for each generation. For instance, it can use the mark - sweep strategy for the&lt;/p&gt;

&lt;h2&gt;
  
  
  Types Of Garbage Collection Process In Java
&lt;/h2&gt;

&lt;p&gt;There are three main collection process the garbage collector applies to clean up memory space. They include the following:&lt;/p&gt;

&lt;h3&gt;
  
  
  . Normal Mark Sweep:
&lt;/h3&gt;

&lt;p&gt;The process here involves two processes. The mark and the sweep process. First, go through the heap memory to identify and mark live objects from dead objects. After that, the garbage collector initiates the sweep process.&lt;/p&gt;

&lt;p&gt;That is sweeping or removing dead objects to the next memory space. It starts from the Eden space and the remaining objects are moved to the Survivor space.&lt;/p&gt;

&lt;p&gt;After the removal of the dead objects remaining objects remains or maintains initial spaces. This method is not ideal especially if the incoming objects are bigger and can’t fit in the between the space.&lt;br&gt;
Note: that the Garbage Collection Cycle is the amount of time it takes the garbage collector to complete a full garbage collection round.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Mark Sweep With Compact:
&lt;/h3&gt;

&lt;p&gt;After the garbage collector complete the collection cycle, it compacts the remaining objects to free up space for new objects. This way, there is enough space for incoming objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Mark Sweep Copy:
&lt;/h3&gt;

&lt;p&gt;Here, the garbage collector marks objects from the ‘from space’ region and moves them to the ‘to space’ region. After that, it compacts the remaining objects to free space for new objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Garbage Collection Algorithms
&lt;/h2&gt;

&lt;p&gt;There are five garbage collection algorithms in java. Here is a breakdown of each collection algorithm, features and where it applies:&lt;/p&gt;

&lt;h3&gt;
  
  
  . Serial Garbage Collector:
&lt;/h3&gt;

&lt;p&gt;This garbage collector algorithm is good for a single core machine. It uses the mark- sweep for young generation and mark sweep compact for the old generation. &lt;/p&gt;

&lt;p&gt;The serial garbage collector runs on a single thread. This means that other threads in the program will pause (Stop-The-World) during the garbage collection process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suitable for programs with small data sets where the pause time does not matter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow because it runs on a single thread.&lt;/li&gt;
&lt;li&gt;More expensive.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  . Parallel Garbage Collector:
&lt;/h3&gt;

&lt;p&gt;This collector works as the default garbage collector in java. The Parallel collector uses the mark sweep copy for young generation and mark sweep compact for old generation.&lt;/p&gt;

&lt;p&gt;It uses multiple threads to perform garbage collection. The program gets pause time during the garbage collection process but for a shorter amount of time. It runs well on a multi core machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster than the serial collector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slower than other garbage collector algorithms below.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  . Concurrent Mark Sweep Garbage Collector:
&lt;/h3&gt;

&lt;p&gt;This is an upgrade from the previous parallel collector. It uses the mark sweep copy for young generation and concurrent mark sweep for the old generation and uses multiple threads to perform garbage collection.&lt;/p&gt;

&lt;p&gt;This collection method does not require the program to stop. The garbage collection process runs concurrently with execution of the program. Mostly, it uses the concurrent collection method, but will revert back to stop-the-world if there is need for it.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Garbage First (G1) Garbage Collector:
&lt;/h3&gt;

&lt;p&gt;This was first introduced in Java 7 and uses multiple threads to perform garbage collection. It divides the heap memory into small regions. It applies the parallel, concurrent, and incrementally compacting collection methods.&lt;/p&gt;

&lt;p&gt;The G1 collector has the shortest pause time because of the division of the heap memory to small regions (memory segments). It keeps track of live and dead objects in each memory segment. The segment with the greatest number of objects is collected first. Stop-the-world (pause time) happens but for a shorter time period. &lt;/p&gt;

&lt;p&gt;This collector algorithm is best for machines with large memory space and high performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Z Garbage Collector:
&lt;/h3&gt;

&lt;p&gt;This was first introduced in Java 11. Also, uses multiple threads to perform garbage collection. Here, the garbage collection method is completed while the program is running concurrently with less than 10 milliseconds of pause time.&lt;/p&gt;

&lt;p&gt;The garbage collection process starts with marking live objects with reference coloring and not with maps as with other collectors. The reference states are stored in bytes of the reference.&lt;/p&gt;

&lt;p&gt;The Z garbage collector works well on 64 bits machines as the references does not have enough bits to execute on a 32 bit system. Also, it applies fragmented memory as a result of the garbage collection process. These entire process runs concurrently with the program hence the 10 milliseconds pause time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So far, this post has all you need to know about memory management in java. Finally, if you are wondering which garbage collection algorithm is best for your program. The answer is, it depends. It depends on the program in question, and the system. For example, the serial collector method is best for a small program with minor data sets.&lt;/p&gt;

</description>
      <category>java</category>
      <category>memorymanagement</category>
      <category>javamemory</category>
      <category>garbagecollection</category>
    </item>
  </channel>
</rss>
