<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[aneesh.co.uk]]></title><description><![CDATA[Aneesh is a software engineer within financial services based in London.]]></description><link>https://aneesh.co.uk</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 30 Mar 2026 20:49:36 GMT</lastBuildDate><item><title><![CDATA[Risk neutral pricing a multi-period binomial model]]></title><description><![CDATA[Calculate the risk neutral price and delta hedge required for a multi-period binomial model]]></description><link>https://aneesh.co.uk/risk-neutral-pricing-a-multi-period-binomial-model</link><guid isPermaLink="false">https://aneesh.co.uk/risk-neutral-pricing-a-multi-period-binomial-model</guid><pubDate>Wed, 24 Apr 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Risk neutral pricing a multi-period binomial model&lt;/h4&gt;
&lt;p&gt;
In this video, we review pages 8 to 15 from Steven Shreve&apos;s Stochastic Calculus for Finance part I where we derive the risk-neutral price and delta hedge required for a multi-period binomial model. We revisit the delta hedging formula and risk-neutral pricing formula within a multi-period model to demonstrate how they can influence the fair price of the option at time 0.
&lt;p&gt;&lt;a href=&quot;&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAUBAgME/8QAFgEBAQEAAAAAAAAAAAAAAAAAAgAD/9oADAMBAAIQAxAAAAFjgxiVTgFl/8QAGxAAAgIDAQAAAAAAAAAAAAAAAAECEQMhIjH/2gAIAQEAAQUCy6l4KoopDXf/xAAXEQADAQAAAAAAAAAAAAAAAAAAARJB/9oACAEDAQE/AVGoo//EABcRAAMBAAAAAAAAAAAAAAAAAAABERL/2gAIAQIBAT8BrMs//8QAHxAAAQIGAwAAAAAAAAAAAAAAAAIyARAREiExUXGR/9oACAEBAAY/AnKzwOX6UulqArs//8QAGxABAAIDAQEAAAAAAAAAAAAAAQARMUFxgfH/2gAIAQEAAT8hNDwrEXLZDCrVtiDkHs+RC2+5/9oADAMBAAIAAwAAABA4D//EABkRAAIDAQAAAAAAAAAAAAAAAAABESExcf/aAAgBAwEBPxBIaPpSoP/EABgRAAIDAAAAAAAAAAAAAAAAAAARASFR/9oACAECAQE/EMpGWz//xAAbEAACAwADAAAAAAAAAAAAAAABEQAhQTHw8f/aAAgBAQABPxAqNZAQBBCACwdKMi6Ns3sX1nCOeNj1oiUU/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/f57e118b73900095a8555c5a37838462/f93b5/135_stoch3.jpg&quot;
        srcset=&quot;/static/f57e118b73900095a8555c5a37838462/8ddbc/135_stoch3.jpg 75w,
/static/f57e118b73900095a8555c5a37838462/75985/135_stoch3.jpg 150w,
/static/f57e118b73900095a8555c5a37838462/f93b5/135_stoch3.jpg 300w,
/static/f57e118b73900095a8555c5a37838462/20e5d/135_stoch3.jpg 450w,
/static/f57e118b73900095a8555c5a37838462/b4294/135_stoch3.jpg 600w,
/static/f57e118b73900095a8555c5a37838462/002df/135_stoch3.jpg 2546w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Risk-neutral pricing and the delta-hedging ratio]]></title><description><![CDATA[Calculate the risk neutral price and delta hedge required on a one period european option]]></description><link>https://aneesh.co.uk/risk-neutral-pricing-and-the-delta-hedging-ratio</link><guid isPermaLink="false">https://aneesh.co.uk/risk-neutral-pricing-and-the-delta-hedging-ratio</guid><pubDate>Mon, 01 Apr 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Risk-neutral pricing and the delta-hedging ratio&lt;/h4&gt;
&lt;p&gt;
In this video, we will walk through pages 4 to 8 of Steven Shreve&apos;s Stochastic calculus for finance part 1 where we develop the risk-neutral pricing formula and delta hedging ratio for a one-period binomial model.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/W2ch4pOKMmo&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAEFAwT/xAAXAQADAQAAAAAAAAAAAAAAAAAAAQID/9oADAMBAAIQAxAAAAGjjQRSOIef/8QAGRABAAIDAAAAAAAAAAAAAAAAAQARAyEx/9oACAEBAAEFAspuFBKId//EABgRAAIDAAAAAAAAAAAAAAAAAAABERJB/9oACAEDAQE/AVTUSf/EABYRAQEBAAAAAAAAAAAAAAAAAAARAf/aAAgBAgEBPwG6j//EAB0QAAEDBQEAAAAAAAAAAAAAAAACETIBEBIhMXH/2gAIAQEABj8CkrZJQ2T25QV6f//EAB0QAQABBAMBAAAAAAAAAAAAAAEAETFBUSFx4fH/2gAIAQEAAT8hQrpaDOfeUuwyxBuD3PlQb78z/9oADAMBAAIAAwAAABD47//EABgRAAIDAAAAAAAAAAAAAAAAAAABEUFx/9oACAEDAQE/EEhZoj//xAAXEQADAQAAAAAAAAAAAAAAAAAAAVER/9oACAECAQE/EJM3T//EAB0QAQACAgIDAAAAAAAAAAAAAAEAESExQWFx4fH/2gAIAQEAAT8QBngKwUEENtes+sSOw0tl8yvxmqDPiYZXs23U/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/1d848383dac35d1c5de09257d66daed9/f93b5/134_stoch2.jpg&quot;
        srcset=&quot;/static/1d848383dac35d1c5de09257d66daed9/8ddbc/134_stoch2.jpg 75w,
/static/1d848383dac35d1c5de09257d66daed9/75985/134_stoch2.jpg 150w,
/static/1d848383dac35d1c5de09257d66daed9/f93b5/134_stoch2.jpg 300w,
/static/1d848383dac35d1c5de09257d66daed9/20e5d/134_stoch2.jpg 450w,
/static/1d848383dac35d1c5de09257d66daed9/b4294/134_stoch2.jpg 600w,
/static/1d848383dac35d1c5de09257d66daed9/1688a/134_stoch2.jpg 2194w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to merge data with Panda DataFrames]]></title><description><![CDATA[Take a look at inner, outer, left and right joins when using the merge function with pandas]]></description><link>https://aneesh.co.uk/how-to-merge-data-with-panda-dataframes</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-merge-data-with-panda-dataframes</guid><pubDate>Fri, 29 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Merging data with Panda DataFrames - Python Tutorial&lt;/h4&gt;
&lt;p&gt;
Explore some of the ways you can merge DataFrames with the pandas library. This involves handling indexed columns, specifying columns to merge upon and how to merge with an inner, left, right and outer join.
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Python&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Python&lt;/a&gt;
File: Pandas 4.ipynb&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/qH8DYzoEqLs&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQT/xAAWAQEBAQAAAAAAAAAAAAAAAAACAAH/2gAMAwEAAhADEAAAAZw43W6MR//EABkQAQEBAQEBAAAAAAAAAAAAAAIBAxMAEf/aAAgBAQABBQLISjUHpqYNOjkLV9Ffn//EABgRAAIDAAAAAAAAAAAAAAAAAAABAhEh/9oACAEDAQE/AXLCz//EABYRAQEBAAAAAAAAAAAAAAAAAAASIf/aAAgBAgEBPwHEv//EAB0QAAICAQUAAAAAAAAAAAAAAAERAAIhEDEyQYL/2gAIAQEABj8C4vzBlPpQiu0xYiMlnT//xAAcEAEAAgIDAQAAAAAAAAAAAAABABExUSFBYaH/2gAIAQEAAT8hAtb9qZtKBsKNxgl9QrjtDLBGC3mHy+T/2gAMAwEAAgADAAAAEPQv/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAEhMf/aAAgBAwEBPxDAOof/xAAYEQEBAAMAAAAAAAAAAAAAAAABABEhMf/aAAgBAgEBPxAFogY7f//EABsQAQACAwEBAAAAAAAAAAAAAAEAESExUXGB/9oACAEBAAE/ELL0XuHLIUMgCX0QgUqrZMQUU6SBPZ8EHBdQAA+hn//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/8934e5d87edc73590f497ef4a11a0527/f93b5/129_panda4.jpg&quot;
        srcset=&quot;/static/8934e5d87edc73590f497ef4a11a0527/8ddbc/129_panda4.jpg 75w,
/static/8934e5d87edc73590f497ef4a11a0527/75985/129_panda4.jpg 150w,
/static/8934e5d87edc73590f497ef4a11a0527/f93b5/129_panda4.jpg 300w,
/static/8934e5d87edc73590f497ef4a11a0527/20e5d/129_panda4.jpg 450w,
/static/8934e5d87edc73590f497ef4a11a0527/b4294/129_panda4.jpg 600w,
/static/8934e5d87edc73590f497ef4a11a0527/6327e/129_panda4.jpg 3803w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The one period binomial model]]></title><description><![CDATA[An introduction to the one-period binomial model and replicating a European option with it]]></description><link>https://aneesh.co.uk/the-one-period-binomial-model</link><guid isPermaLink="false">https://aneesh.co.uk/the-one-period-binomial-model</guid><pubDate>Sat, 23 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;The 1-period binomial model&lt;/h4&gt;
&lt;p&gt;
Walkthrough the first 4 pages of Steven Shreve&apos;s Stochastic calculus for finance I, where we introduce the one-period binomial model and how a replicating portfolio can be created for a European call option in the stock and money market.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/AnA4TEUjPtc&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQCAwX/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAL/2gAMAwEAAhADEAAAAdGp8mImOP/EABsQAAIBBQAAAAAAAAAAAAAAAAABAgMRISIx/9oACAEBAAEFAqi2FhFkR7//xAAYEQACAwAAAAAAAAAAAAAAAAAAARESQf/aAAgBAwEBPwFV1En/xAAWEQEBAQAAAAAAAAAAAAAAAAAAEQH/2gAIAQIBAT8Buo//xAAcEAABAwUAAAAAAAAAAAAAAAAAAhEyECExUXL/2gAIAQEABj8Ckq+iSxnemBXR/8QAHBAAAgICAwAAAAAAAAAAAAAAAAERQTFxIWGR/9oACAEBAAE/IXKWlQ5KKcK2NJ5SOh4JG9Z//9oADAMBAAIAAwAAABC8z//EABgRAAIDAAAAAAAAAAAAAAAAAAABEUFx/9oACAEDAQE/EEjZoj//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFR/9oACAECAQE/EMpXf//EAB4QAQEAAgEFAQAAAAAAAAAAAAERACExQVFhceHw/9oACAEBAAE/EAwI6jUBgwO2gi8fridWMW2+8gUTuXH4LBhu05eM/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/0cc347d4bffb99cf8c4618071a3967ea/f93b5/130_binomial1.jpg&quot;
        srcset=&quot;/static/0cc347d4bffb99cf8c4618071a3967ea/8ddbc/130_binomial1.jpg 75w,
/static/0cc347d4bffb99cf8c4618071a3967ea/75985/130_binomial1.jpg 150w,
/static/0cc347d4bffb99cf8c4618071a3967ea/f93b5/130_binomial1.jpg 300w,
/static/0cc347d4bffb99cf8c4618071a3967ea/20e5d/130_binomial1.jpg 450w,
/static/0cc347d4bffb99cf8c4618071a3967ea/b4294/130_binomial1.jpg 600w,
/static/0cc347d4bffb99cf8c4618071a3967ea/0862b/130_binomial1.jpg 1828w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Combining dataframes with Pandas]]></title><description><![CDATA[A quick demonstration on how you can concatenate DataFrames along both axis using a shared index]]></description><link>https://aneesh.co.uk/combining-dataframes-with-pandas</link><guid isPermaLink="false">https://aneesh.co.uk/combining-dataframes-with-pandas</guid><pubDate>Fri, 22 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Combining DataFrames with Pandas&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Walkthrough how you can use the concat method with the pandas library to combine DataFrames both on the x axis and y axis. &lt;/p&gt;
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Python&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Python&lt;/a&gt;
File: Pandas_3.ipynb&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/WG4o79N9f-c&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABnDjdrRhj/8QAGRABAQEBAQEAAAAAAAAAAAAAAgEDABMR/9oACAEBAAEFAshKNQfTWQaejnFq9Ffn/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAECESH/2gAIAQMBAT8BcsLP/8QAFREBAQAAAAAAAAAAAAAAAAAAABL/2gAIAQIBAT8BS//EAB0QAAICAQUAAAAAAAAAAAAAAAERAAIhEDEyQYL/2gAIAQEABj8C4vzBlPpQiu0xYiMlnT//xAAcEAEAAgIDAQAAAAAAAAAAAAABABExYSFBUaH/2gAIAQEAAT8hCtb9qZQcgGwo9jhLtA9HaZcIwW8wmXyf/9oADAMBAAIAAwAAABBwL//EABYRAQEBAAAAAAAAAAAAAAAAACEAAf/aAAgBAwEBPxDAWov/xAAXEQEBAQEAAAAAAAAAAAAAAAABEQAx/9oACAECAQE/EArMCd3/xAAbEAEAAgMBAQAAAAAAAAAAAAABABExUXEhgf/aAAgBAQABPxCw5FqC9WQYrQAvRBByqV7iAynAoE6Pgg0LBgD6Gf/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/61e79cb4ab3f5fc83d3da2da160981c2/f93b5/128_panda3.jpg&quot;
        srcset=&quot;/static/61e79cb4ab3f5fc83d3da2da160981c2/8ddbc/128_panda3.jpg 75w,
/static/61e79cb4ab3f5fc83d3da2da160981c2/75985/128_panda3.jpg 150w,
/static/61e79cb4ab3f5fc83d3da2da160981c2/f93b5/128_panda3.jpg 300w,
/static/61e79cb4ab3f5fc83d3da2da160981c2/20e5d/128_panda3.jpg 450w,
/static/61e79cb4ab3f5fc83d3da2da160981c2/b4294/128_panda3.jpg 600w,
/static/61e79cb4ab3f5fc83d3da2da160981c2/34df8/128_panda3.jpg 3819w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Statistical Methods with Python Pandas]]></title><description><![CDATA[Review some of the basic statistical methods that are available with the pandas library]]></description><link>https://aneesh.co.uk/statistical-methods-with-python-pandas</link><guid isPermaLink="false">https://aneesh.co.uk/statistical-methods-with-python-pandas</guid><pubDate>Fri, 15 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Statistical methods with Pandas - Python Tutorial&lt;/h4&gt;
&lt;p&gt;
A walkthrough of some of the most common statistical methods you can perform out of the box with the Pandas library. 
&lt;p&gt;This includes:
Sorting data, filtering data, finding correlations amongst fields, replacing values and sampling data. &lt;/p&gt;
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Python&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Python&lt;/a&gt;
File: Pandas_2.ipynb&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/mYkZ8G4_ghw&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABUBAQEAAAAAAAAAAAAAAAAAAAIA/9oADAMBAAIQAxAAAAGUcjyBxD//xAAaEAEBAQADAQAAAAAAAAAAAAACAQMAERMS/9oACAEBAAEFAsxKNAfTWfGnqzC1eRXr/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAECESH/2gAIAQMBAT8BcsLP/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAEREv/aAAgBAgEBPwGLZf/EAB0QAQACAQUBAAAAAAAAAAAAAAEAEQIQITEyQYH/2gAIAQEABj8C638hvV+VEx4lGSRVt0//xAAaEAEBAQADAQAAAAAAAAAAAAABEQAxQVHh/9oACAEBAAE/ISrN9rdM8oEImFEo9xEd4OslwVxPjf/aAAwDAQACAAMAAAAQkC//xAAWEQEBAQAAAAAAAAAAAAAAAAAhAAH/2gAIAQMBAT8QwFqL/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARITH/2gAIAQIBAT8QCayGOz//xAAcEAEAAgMAAwAAAAAAAAAAAAABABEhMVFhcbH/2gAIAQEAAT8QeKxd2vlnIUQwhF3Mrg5Ss6gAJ1QCA+VDQ4LABY+w/Z//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/c3ec9d05ccefa03b30bdea2ee53b65d7/f93b5/127_panda2.jpg&quot;
        srcset=&quot;/static/c3ec9d05ccefa03b30bdea2ee53b65d7/8ddbc/127_panda2.jpg 75w,
/static/c3ec9d05ccefa03b30bdea2ee53b65d7/75985/127_panda2.jpg 150w,
/static/c3ec9d05ccefa03b30bdea2ee53b65d7/f93b5/127_panda2.jpg 300w,
/static/c3ec9d05ccefa03b30bdea2ee53b65d7/20e5d/127_panda2.jpg 450w,
/static/c3ec9d05ccefa03b30bdea2ee53b65d7/b4294/127_panda2.jpg 600w,
/static/c3ec9d05ccefa03b30bdea2ee53b65d7/b1958/127_panda2.jpg 2575w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Series and DataFrames with Python Pandas]]></title><description><![CDATA[An introduction to panda series, DataFrames and lambda functions]]></description><link>https://aneesh.co.uk/series-and-dataframes-with-python-pandas</link><guid isPermaLink="false">https://aneesh.co.uk/series-and-dataframes-with-python-pandas</guid><pubDate>Sat, 24 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Python Pandas 1&lt;/h4&gt;
&lt;p&gt;
This video serves as an introduction to Python Series and DataFrame structures from the pandas library.
We will walk through the key built-in methods that enable you to create the structures, select, filter and transform the data. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/HEbZ-N_tCPo&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAABlDkdtGE//8QAGRAAAwEBAQAAAAAAAAAAAAAAAQIDEgAR/9oACAEBAAEFApquaKu6jFNsORjwY+f/xAAWEQEBAQAAAAAAAAAAAAAAAAAAEQH/2gAIAQMBAT8Buq//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEv/aAAgBAgEBPwFL/8QAHBAAAgICAwAAAAAAAAAAAAAAAAECERAxIUGB/9oACAEBAAY/AtJ+Cp1fVDUdHEmN4//EABsQAQACAwEBAAAAAAAAAAAAAAEAESExUUGh/9oACAEBAAE/IUZN6ti62ykIqCiUdipAZ8Ymxt1bBbfJ/9oADAMBAAIAAwAAABDnD//EABcRAAMBAAAAAAAAAAAAAAAAAAABESH/2gAIAQMBAT8Qc45B6w//xAAYEQEBAAMAAAAAAAAAAAAAAAABABEhMf/aAAgBAgEBPxABcRp2/8QAHBABAAICAwEAAAAAAAAAAAAAAQARIUExUXGx/9oACAEBAAE/EE6rVUw9WQroEAjvMrkxSs8QVTbFG4IjpFtesKAH0P2f/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/734e44591cee1cef58446a9773015ba9/f93b5/126_panda1.jpg&quot;
        srcset=&quot;/static/734e44591cee1cef58446a9773015ba9/8ddbc/126_panda1.jpg 75w,
/static/734e44591cee1cef58446a9773015ba9/75985/126_panda1.jpg 150w,
/static/734e44591cee1cef58446a9773015ba9/f93b5/126_panda1.jpg 300w,
/static/734e44591cee1cef58446a9773015ba9/20e5d/126_panda1.jpg 450w,
/static/734e44591cee1cef58446a9773015ba9/b4294/126_panda1.jpg 600w,
/static/734e44591cee1cef58446a9773015ba9/a5160/126_panda1.jpg 3859w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How the Electricity Market of Great Britain functions]]></title><description><![CDATA[An overview of the lifecycle of electricity generation and consumption in Great Britain, and the markets and mechanisms that enable them to function.]]></description><link>https://aneesh.co.uk/how-the-electricity-market-of-great-britain-functions</link><guid isPermaLink="false">https://aneesh.co.uk/how-the-electricity-market-of-great-britain-functions</guid><pubDate>Sat, 17 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Topics&lt;/strong&gt;&lt;br&gt;
&amp;#8226; The lifecycle of electricity, from generation to consumption.&lt;br&gt;
&amp;#8226; The various markets that enable market participants to trade electricity.&lt;br&gt;
&amp;#8226; How the National Grid ESO balance the supply and demand of electricity.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction&lt;/h4&gt;
&lt;p&gt;
This blog is going to help you develop an understanding of the mechanisms that enable the electricity market of Great Britain to operate. We will first understand the challenges imposed from the properties of electricity that translate to how it is created, distributed and (not) stored. Following this, we will explore the various physical and financial markets that electricity are traded in. Lastly, we will look at the governance and management of the electricity system that sustain a reliable system.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Generating Electricity&lt;/h4&gt;
&lt;p&gt;
The lifecycle of electricity can be categorised into 3 main steps: generation, transmission, and consumption. 
&lt;/p&gt;
&lt;p&gt;
Generation is achieved by dedicated power generators in one of many ways. The most common generators are turbine-driven, they convert mechanical energy into electrical energy by forcing electrons through a circuit. Steam turbines work by burning fuel or biomass to produce pressurised steam. Nuclear power reactors use nuclear fuel rods to produce pressurised steam. Both of these reactors convert the steam into mechanical energy for electricity generation.
&lt;/p&gt;
&lt;p&gt;
A similar type of generator are combustion gas turbines. These generators instead use the combustion of gaseous or liquid fuels to produce pressurised gasses to spin turbines.
&lt;/p&gt;
&lt;p&gt;
And lastly there are alternative, but less commonly used generators such as hydroelectric turbines that use the force of moving water to spin turbines, wind turbines and also solar photovoltaic systems that convert photons from sunlight into electricity.
&lt;/p&gt;
&lt;p&gt;
But what IS electricity?
&lt;/p&gt;
&lt;p&gt;
You can find many definitions of electricity, but at it&apos;s very core, it is the &lt;u&gt;flow&lt;/u&gt; of electrical power. Electricity represents a flow of electrons, and is often referred to as an energy carrier as it can be converted into other forms of energy such as mechanical energy or heat. This property of electricity, being a flow of electrons, bring us to the main challenge it poses: it cannot be stored at a wholesale level. There is a potential for it to be converted and stored, such as in a battery, however for the wholesale movement of electricity, this would not be feasible or cost effective. As a result, the supply and demand of electricity must be kept in a constant balance, and we will later see how this is achieved at a granular level.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Transmission Networks in GB&lt;/h4&gt;
&lt;p&gt;
The electricity created by a generator is subsequently transmitted around the country via transmission networks. Transmission networks come in 2 forms: large transmission towers which you may see line motorways, and smaller distribution networks that transmit electricity at a local level to individual homes and businesses. 
&lt;/p&gt;
&lt;p&gt;
Transmission networks are long distance, high voltage lines transporting electricity from generators to distribution networks. These are considered the &apos;motorways&apos; of the electricity system. We introduce the term &apos;voltage&apos; to quantify the pressure which pushes electricity through the wires. Voltage in a transmission network tends to be from 275 to 400 kilovolts. 
&lt;/p&gt;
&lt;p&gt;
Distribution networks transmit electricity at a lower voltage from transmission lines to homes and businesses for consumption. This voltage tends to be around 132 kilovolts. There are 14 &apos;grid supply point groups&apos;, each are managed by a distribution network operator (DNO). The distribution network is responsible for reducing the voltage of the electricity so it can be safely distributed for consumption within homes and businesses.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 165.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAhCAIAAABWXBxEAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAFb0lEQVQ4y5WUCVBTVxSGLyGKQdEO4xREQbYWFIkCYQlSlOJCamcEV7S4AEJbDMMiARpJZKloRGRJTIiELRBkCQGSAGELtlhtqgVBqKDIZgtiFQWFGrI1L0FNLbHTM2/uvGW+c879338uUCgUMrlMoQq5XK74PwHmMZkSk0slspvdDbd7r4l6GptErOmXUx/OCMEDo3cTaceGR4Z+Hx+OYGzClSFj2bb+RMvfeu9BfclkWmG5VJFQ4PPNWc/nE69ZvIy4SvuzjZuxFFRpedF/bgSM/zl6kujL5/O77rcryyrJiPwNOELY4Eh/7+DN/qGuOYlEK9z2M/dopHdi7uGsooT4yk0p9e64YqeoHPR3Za44lmN0oaOgjaNqXroAPDT6QNDCI1C/Cjrjhi9zS6pDJ/Hdk+vdknjo1GaPcDqS31CtFVbrkVdIDzzjTLnuRxEezm49kNm8lyoMyLjmgyV5Pxl7rm3zkNr3+nqT0k4dItjEsVyS+egzta7JdWiiwDOEYU9lXPiAbEAimfu1o4NSlOwXhoxioAgC99N16KgKNJlsejEO+XBk4kOw2l73+x+EE/fFljlezDRPv2RalbisDQcuB699NDaugmVaTaJMnEQOPRqP9sVu+DHZ7DHN/CnDauDyJ11ZViIaZnigXxs/b89GIS8E7+33tVct0WEiZ+UgefUweeUzmrEgCeTn7ZFCSstV10KVlSubz2rgc26lrhohgeE0+GSGTkOcbmEeoFENhgYfLlj87WBANuosDR1LBX+k6w6chZFOLLLeBmcUwooLgLCVpfwqlc5pg6Gs7exz9G9NyxNcokO9dD31F3vqpZIQ1WzAyP18elryb9nfwKqXM7OzfQ+Gnk7ONLYLkYfMbfzXhREtGrkrKq8aVlXsfjwx/F7zQDOT8oM691+zs96BnwEHsCtgRTPf/Id2a1aBXjr+mET2ttACMBRSlbjV9RzgDrZYGuJ9jL6P/AjnbXLA0OjOLZHmhANNUrlOT0yP940pb/hcLnAFLk5Lk6ydYpBovJvHXtgSUgQOgiUStUbvYJkUer7b0h27PLo+gsdEFYQiAgztbP183YMImIBEjP/+jUG4PSNPnszvXC7XqAwdY4r6tLo8UzrPpLrRrCLAiA5WMfelRVE6SEQBPupG7IFzu3O8tjUdPNzBqdGorBJhTiwpSWcVOV1pAeUoi2xgzQIfc622p5yuDY+vPRV/PiDD3JQDdNgAUJca9Pf0/FMwKZSDHFLibZ2yxrPYHiO08RLa7aQcL9kfSN5DRui1GJs1Obg02yILYXqCigqgUVjRNzqK5TQgqExQydLH0A3WcpbZ1xlsuLKbEowl7igF8EoY/KoOjAsAaYn+7c5OCJaqpOscGFxMosIZldbcNvvaZkRKAXDKB0ZstNeRYOx6go1J01LjFnvHnz5F0nT1sk7FSDQH49nU1EEa44vUC2tSs2C5VfCCIv1Dp9227uPnglqqTn6oTvpGRM4Sg0vL4bE7fR69eP6+SaSzMy+npsqqmBhCLOr4l5tPooLiLCqyF5XnIMpzFm2hGFukW5hcNl9TaLcrd694Wqzxn1WmLRWU7Ij39MVvxeA9PHBupPOWN6ptrtfaV9FWWzEtV11HmbZuXEE2i8yOUcjeVJaoRvJ2t8gr2tWf5BeUeWRLONr1xLq0BB1KIjw6WCcqzNg5GWWZjnRO9qAzr8y9Es+3rfZqz/3u7TgPh1Bb5/D1qEA7ZklReU1x1qUQHpfNrWGKRL+8mpx58Xjy5dNpdZtyhRyo1eK11HhhXbYGo7PpmQmZcTEpkRKxdMFzUw5ZcX745uGunjvx52KEwlaVz8Ti12LVL5RCpwQUUvW0quNtor8BPN6Tw+ZKt+UAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Grid Supply Points in Great Britain&quot;
        title=&quot;Grid Supply Points in Great Britain&quot;
        src=&quot;/static/cbeb96dbc532420af92446d643076ce7/5a46d/125_gsp.png&quot;
        srcset=&quot;/static/cbeb96dbc532420af92446d643076ce7/1db73/125_gsp.png 75w,
/static/cbeb96dbc532420af92446d643076ce7/8a4e8/125_gsp.png 150w,
/static/cbeb96dbc532420af92446d643076ce7/5a46d/125_gsp.png 300w,
/static/cbeb96dbc532420af92446d643076ce7/5f1d2/125_gsp.png 302w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Electricity Consumption&lt;/h4&gt;
&lt;p&gt;
The consumption of electricity takes place within homes and businesses, where electricity is used for many reasons, such as keeping your lights on, charging your mobile phone, and watching YouTube videos about Java.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The supply and demand of electricity&lt;/h4&gt;
&lt;p&gt;
We measure the generation of power using watts. Watts capture the rate at which electricity is transferred at a single point of time, and represents the amount of energy required for an item to function. Given the large scale of power generation, we refer to 1000 watts as a kilowatt, 1000 kilowatts as a megawatt, 1000 megawatts as a gigawatt, and 1000 gigawatts as a terawatt.
&lt;/p&gt;
&lt;p&gt;
To put a watt into perspective, an average toaster consumes 1100 watts.
&lt;/p&gt;
&lt;p&gt;
From a supply perspective, consider a wind power generator, which generates on average 3 megawatts of electricity. This fairs in comparison to one rooftop solar system that you find on a residential home, which generates just 4 kilowatts of electricity. 
&lt;/p&gt;
&lt;p&gt;
When we talk about the &lt;strong&gt;consumption&lt;/strong&gt; of electricity, however, we use watt hours. Energy is measured in watt hours to capture how much electricity is produced or demanded in a given hour. You can think of this as a car travelling at 30 miles per hour for 1 hour. The speed (watt) would be 30, however the total distance travelled will be 30 miles (watt-hours).
For some more perspective: Great Britain consumed 321 terawatt hours of electricity in 2022.
&lt;/p&gt;
&lt;p&gt;
The supply and demand of electricity in Great Britain is based upon watt hours. The parties who are demanding electricity are energy suppliers. These tend to be the privately-owned corporations that you pay bills to, such as British Gas, EDF, E. ON, and Scottish Power. Energy suppliers enter into contracts with power generators, using a forecast for how much energy their customer base will require as the basis for the contract. We will see later on in this blog how the suppliers are better able to meet the finer granularity of demand they face from their customers on an hour-by-hour basis.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Electricity governance and regulation&lt;/h4&gt;
&lt;p&gt;
With hundreds of participants in the electricity market entering into contracts for the purchase and delivery of electricity, there exists a single entity responsible for ensuring the balance of all these contracts in unison. That is the National Grid Electricity System Operator (ESO). 
&lt;/p&gt;
&lt;p&gt;
When generators and suppliers of electricity enter into a contract, they will inform the National Grid ESO of their intentions. With a consolidated view of electricity movement across national generators and suppliers, the National Grid can realise where an imbalance lies, whether that is &apos;long&apos; from too much supply of power, or &apos;short&apos;, due to an inefficient supply. &lt;br&gt;
The National Grid are then able to operate reserve generators to increase or reduce their contribution to the network to maintain a balance. 
&lt;/p&gt;
&lt;p&gt;
All participants within Great Britain&apos;s energy market have obtained a license from OFGEM. 
OFGEM are Great Britain&apos;s independent gas and electricity regulator. They exist to protect consumers of electricity by enabling market competition, enforcing market rules and promoting innovation.
OFGEM are responsible for imposing price caps on electricity suppliers, and imposing fines on generators. You can find an interesting read on one of the larger fines imposed by OFGEM in 2020: &lt;a href=&apos;https://www.ofgem.gov.uk/publications/ofgem-requires-intergen-pay-ps37m-over-energy-market-abuse&apos; target=&apos;_blank&apos;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Determining the domestic cost of electricity&lt;/h4&gt;
&lt;p&gt;
In this section, we are going to look at how the price you pay for electricity is determined. This price comes down to an understanding of Great Britain&apos;s &lt;u&gt;generation stack&lt;/u&gt;.
&lt;/p&gt;
&lt;p&gt;
The supply of electricity in Great Britain is made up from a generation stack of different types of generators, all contributing electricity to the National Grid network and at different prices. Electricity from wind turbines will typically be generated at a cheaper rate than nuclear, carbon or gas power stations.
&lt;/p&gt;
&lt;p&gt;
A &apos;merit order&apos; is defined within the stack to determine which electricity source is consumed first, this will always be the cheapest.
&lt;/p&gt;
&lt;p&gt;
When we consider the total energy that is demanded at a given time, the merit order is used to determine which generators will supply the electricity for that given time. As demand for electricity rises, the number of generators required to meet that demand will increase, and subsequently the more expensive generators down the merit order will be used. The relationship between the merit order and your electricity bill is that the price you pay for electricity at a given time is determined by the most expensive generator used to meet the demand at that time. This is why off-peak electricity, which is generated when demand is low, will be cheaper than when demand is higher and more expensive generators are contributing to the supply of power. This is known as the &apos;pay-as-you-clear&apos; model. 
&lt;/p&gt;
&lt;p&gt;
The merit order also has a role in determining the development of new power generators. When considering the capital expenditure and return a generator can produce, developers will forecast how often the generator will be contribute to the network and generate income.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Electricity Markets&lt;/h4&gt;
&lt;p&gt;
So far, we have touched the surface of electricity generation, transportation and distributed to your homes. We can understand how the price of electricity can be derived at different times, and the high level challenges the National Grid ESO tackle to maintain a balance of supply and demand in Great Britain. We are now going to take a look at who the different players are within the electricity market, and why they are motivated to buy or sell electricity.
&lt;/p&gt;
&lt;p&gt;
We will begin by dividing the electricity ecosystem into four segments: the capacity market, the wholesale market, ancillary services, and the non-physical market.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Wholesale Market&lt;/h4&gt;
&lt;p&gt;
The wholesale market is where the majority of electricity is bought and sold. Agreements for electricity occur both bilaterally, known as an over-the-counter deal, and through exchanges. Agreements can vary in time frames from delivery of electricity months in advance, down to delivery in the very same day. 
&lt;/p&gt;
&lt;p&gt;
Within a bilateral agreement, the suppliers negotiate a price in MWh for the delivery of electricity at a specific time period with the generators. When a bilateral contract is agreed, the National Grid ESO must be informed so they can manage the supply and demand balance of electricity. The bilateral agreement tend to be long-standing contracts that enable the energy supplier to guarantee the delivery for months at a time.
The wholesale market also operates through a power exchange. A power exchange is similar to financial exchanges where suppliers and consumers meet to determine a purchase price. Power exchanges can be used for long-dated contracts as well as short-dated contracts as we will soon discuss.
&lt;/p&gt;
&lt;p&gt;
When buying and selling electricity, both parties will reference specific time periods for delivery. One common time frame is for delivery at every hour within a given day, known as &apos;baseload&apos;. An energy supplier will use a baseload contract to purchase the least amount of electricity they expect to need in a given day. The baseload therefore ensures the supplier are able to meet the minimum electricity demand they forecast from their customer base throughout a given day.
&lt;/p&gt;
&lt;p&gt;
The wholesale contracts can be traded in large periods ahead of time to secure the delivery and price of electricity. As you may expect, the demand for electricity in a normal household will not be equal at all times. The typical &apos;peak hours&apos; are from 7am until 7pm. As a result of the higher demand during these hours, the energy suppliers must increase their orders of electricity during these periods, which can be achieved through a &apos;peakload&apos; contract. The process of matching the specific demand of electricity for all hours of the day is known as &apos;adding shape&apos;.
&lt;/p&gt;
&lt;p&gt;
In the image below, I have used a red line to illustrate how the forecasted demand of electricity can change within a given day. We can then see how a supplier can meet that demand with individual baseload and peakload contracts. The outstanding gap between the red line and the blocks can be met with more granular agreements in the market.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAACU0lEQVQ4y31TbVPaQBDm//+JzvSLLR1F0Y61M+pIQgiRCG1S3vGlBTSKBAnkckkuTzcEYtTanXmyd7t7z+1m93IgiaIIG+0HIl1n7StgLfN5akfWTpLLEoYiwuV48UwSa8aSffzxA/iKCk8qQ3S6by7+D2FCJsa3CHf3U1LRasP5aeBJCPDjU0TcR/b8ijAthxCEIiGMREJomHDLCsJWK7lQrWJhWXA4h1tvILq7e1F6mmFWNiWDsmDVGqbWA4KyCjAfQtPhOUvMqRL36gaimVwUUewmy5zjOLDo1sfJBNOp/fwPmQd+dAD2eA3fVMHzHxDUJQQPl2BWH652AmEaSWwYppnmhsMhDMOASej1B7gaL5MEZ3Pw4kd42id4+g5YbRss1mqebAR5C8Hx97Sy6L2SB+sMw1YXXD2Cq2/DaxyCdxXwThm8XQLTyGZ+Q9DQ4asaIs9LpiDO8LkpAgFZL/Ue0GnBlxWwpoxA24J/nkeQga99RnhRAHdnCKlCKApADYpedzmWk/INpB0VkjyA3BhCVpqQKx2UlDakNeQKQe1Cqo9R6TmQftkYT9jLOdyM0r5qoXD+iGLVQlG5xW7lHkf6BOXmDGeGjZJp4/D8AXtK4i/KI2yfDdNm5l5P+640wpfTPyiUhthZY48Ofa2McaAQSBekjW9E6xHyFN8fvUPYupmj1rFR79m46CaotaekZ9DJHqPRf0p9cVzsv7e9t13OPqGs9HtttJsmfl8P0DR/rPS/ZDWHrzMUpIXIgPac3uxisQSjYXddhuXSXcVn4zbn/wIZ+WbRLDb+dQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Demand of electricity in a day&quot;
        title=&quot;Demand of electricity in a day&quot;
        src=&quot;/static/537a4acb8bf4c59e751162357012f0ee/5a46d/125_demand.png&quot;
        srcset=&quot;/static/537a4acb8bf4c59e751162357012f0ee/1db73/125_demand.png 75w,
/static/537a4acb8bf4c59e751162357012f0ee/8a4e8/125_demand.png 150w,
/static/537a4acb8bf4c59e751162357012f0ee/5a46d/125_demand.png 300w,
/static/537a4acb8bf4c59e751162357012f0ee/fc2a6/125_demand.png 450w,
/static/537a4acb8bf4c59e751162357012f0ee/85ff8/125_demand.png 467w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
As the time for electricity to be delivered to consumers approaches, energy suppliers can more accurately forecast their expected levels of electricity required. This is shown as the dashed black lines between the peakload and forecasted demand. Electricity suppliers can work to a granularity of hourly and half-hourly contracts to meet this outstanding demand. The finer requests for electricity supply can be obtained through day-ahead and within-day exchanges, for suppliers in Great Britain, these are the EPEX Spot and Nordpool exchanges.
&lt;/p&gt;
&lt;p&gt;
EPEX and Nordpool offer two types of markets for short-term electricity delivery which can help market participants to . These are the day-ahead (DA) and within-day (WD) markets. As described, the day ahead exchange will deliver electricity for the next day, and within-day is for same-day delivery. 
Epex and Nordpool work as an auction exchange, where generators and suppliers publish their acceptable price limits to buy and sell electricity. The market will then meet an equilibrium to arrive at a price for electricity at each given time-interval. 
&lt;/p&gt;
&lt;p&gt;
The wholesale market operates across huge timelines that enable both a guarantee for the delivery of electricity in the future, but also the granularity of electricity delivery at an hour-by-hour basis. The agreement for electricity delivery can take place at any time up to &apos;gate closure&apos;, which is 1 hour before the electricity is set to be delivered. Upon gate closure, generators will inform the National Grid ESO of their intentions to generate electricity.&lt;br&gt; 
Upon gate closure, the National Grid ESO can begin ensuring the supply and demand of electricity is balanced. Balancing is achieved through the use of a balancing mechanism, where generators and industrial users are paid to change their intended demand and supply. This can be a little detailed, so I will write about this in it&apos;s own section at the end of this blog.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Capacity Market&lt;/h4&gt;
&lt;p&gt;
The capacity market can be thought of as an insurance policy for the delivery of electricity to avoid a potential blackout. The capacity market is a standby market of generators that are ready to produce electricity in periods of high stress on the existing system. High stress can be created where the supply of electricity is low, due to factors such as low wind, and the demand for electricity is high. The capacity market therefore benefits electrical generators that can easily start up and turn off on demand. Contracts within this market can range from 1 to 15 years. With long-term contracts, generators expect to exist with longevity in mind, and play an important role in the long-term generation stack that Great Britain uses.
&lt;/p&gt;
&lt;p&gt;
Generators that contribute to the capacity market are paid for the availability they provide the National Grid ESO to turn on at peak times of electricity consumption. In the event of an unexpected sudden surge in electricity requirement, known as a capacity market event, the National Grid ESO will issue a 4-hour warning to these generators to turn on. This is known as a capacity market notice. 
&lt;/p&gt;
&lt;p&gt; 
The capacity market was first auctioned in 2014 and works in two auctions: T-4 and T-1. The auctions work on a yearly basis, where the 4 year auction can be for a 15 year contract that begins in 4 years time. The T-1 auction market is used by the National Grid to ensure there is sufficient capacity to support the following years winter.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Ancillary Services&lt;/h4&gt;
&lt;p&gt;
Ancillary services capture ways that generators can earn money through services that support the stability and efficiency of the power grid. Ancillary services can be split into 6 responsibilities: frequency response, reactive power, voltage management, inertia services, reserve services and restoration.
&lt;/p&gt;
&lt;p&gt;
The frequency response service exists to ensure transmission towers run at a target frequency of 50 hertz. This frequency of electrical transmission is important to maintain as it can otherwise create blackouts or damage the equipment. The frequency response service will manage frequency on a second-by-second basis to actively manage frequency by ramping power up and down. 
&lt;/p&gt;
&lt;p&gt;
Reactive power services exist to ensure voltage levels within the system remain within their target range. Reactive power exist as a by-product of power from AC circuits, you might think of this as the head of a beer when it is poured. It is measured in MVars (megavolt-ampere reactive) and is actively absorbed to reduce voltage or generated to increase voltage on the grid. This balance of reactive power ensures the grid is efficient in delivering power, and not overloaded which can create network volatility. 
&lt;/p&gt;
&lt;p&gt;
Voltage management covers the movement of electricity at a specific voltage through the network. It is closely related to reactive power as both of these relate to the rate which power is moved. Each transmission network is designed to manage a level of voltage, such as 400 kilovolts (for national networks) or 275 kilovolts (for distribution networks). Voltage management is achieved through the use of an electrical transformer to optimise the voltage at which electricity is transported. If the voltage is too low, there may be lost value from the transmission networks in how much electricity is being transported, however on the other end, if voltage is too high, this can result in electrical equipment consuming power in its transmission, and even creating an unstable supply of electricity. There is roughly a 5% buffer for which voltage must be managed at in kilovolts. 
&lt;/p&gt;
&lt;p&gt;
Inertia services exist to support the reliable frequency of power generation to the grid. In traditional turbine-driven power generators, inertia acts as a softener to frequency changes in electricity as turbines continue to spin even when they are not generating power. This means a sudden change in frequency from a generator will not cause a hiccup in the electricity grid. You can think of inertia as similar to the suspension in a car, which absorbs the shock of a change in the road surface. &lt;br&gt;
The design of wind turbines has reduced the inertia created when the generator stops producing electricity, this means the National Grid must purchase inertia from other generators to maintain the stable level of frequency on the grid.
&lt;/p&gt;
&lt;p&gt;
Reserve services ensure there is sufficient power in reserve in case of a change in generation or denied services.
&lt;/p&gt;
&lt;p&gt;
Lastly, restoration services, formerly known as black start, are responsible for supporting the grid in the event of a complete shutdown. Restoration allows a power station to restart without an external electricity flow to re-energise the grid.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The non-physical market&lt;/h4&gt;
&lt;p&gt;
So far, we have discussed the physical market for electricity where the counterparts involved are engaging in contracts with the intention to either produce or distribute and consume electricity. Non-physical trade differs as it driven by the means of financial speculation or risk management. The scenarios to be discussed are highly simplified for the purpose of the blog.
&lt;/p&gt;
&lt;p&gt;
The non-physical market use a variety of financial derivatives to trade electricity, however the most commonly used derivative is a forward contract.&lt;br&gt;
The forward contract represents an agreement between two parties to buy or sell a quantity of electricity in the future at a price agreed today. The forward contract thus reduces the price risk for the buyer of the electricity who can fix it ahead of time, and the supply risk for the generator who can guarantee a buyer. 
&lt;/p&gt;
&lt;p&gt;
Non-physical trading can enable an agent to realise a profit without actually generating or consuming electricity: an agent could enter into a forward contract agreement to purchase a given amount of electricity at a given forward price, K, 1 year from today. As the date for the purchase of electricity approaches, the agent could sell their forward agreement in the market, thus realising a potential profit depending on how the current price of electricity fairs in comparison to the forward price K. 
&lt;/p&gt;
&lt;p&gt;
In our scenario, the agent is &apos;impersonating&apos; a supplier of electricity as they purchase the forward contract from a generator, but when they resell the forward agreement to a supplier, they are &apos;impersonating&apos; a generator of electricity by selling the contract to a supplier. I put the word &apos;impersonating&apos; in quotes as the agent are not really impersonating anyone, but are more fairly just taking a position in the market that the generator or supplier would be taking.
&lt;br&gt;
An agent can also enter into agreements with suppliers to sell electricity at a given price, thus impersonating a generator. This position would be sold in the market thus granting the contract to generate electricity to an actual generator.
&lt;/p&gt;
&lt;p&gt;
The non-physical market exposes opportunity based on the disparity between a forward price of electricity and the actual price of electricity when the time for delivery approaches. This disparity does not present an arbitrage opportunity. The agents who enter into such deals are speculating upon the price of electricity in the future. If the current price of electricity is £10 MWh, and the forward price for 1 year is £12 MWh, the agent will benefit if the price of electricity rises to £14 MWh as they can receive the power at £12 MWh and sell it in the market at £14 MWh. If the price however falls to £9 MWh, then they are obligated to purchase the electricity at £12 MWh and to sell it at £9 MWh, or effectively sell the forward contract at a loss in the market. 
&lt;/p&gt;
&lt;p&gt;
Within the scope of the non-physical market are also asset-backed traders. These are agents that may enter into financial contracts to hedge or speculate upon the price of electricity, however they have the infrastructural means to generate electricity and deliver against the contract, rather than selling their position in the market as the forward date approaches.
&lt;/p&gt;
&lt;p&gt;
Agents are incentivised to realise greater profits from their generation by engaging in the speculative market on potential price, and makes up 90% of the electricity trades that are made in advance of delivery.
&lt;/p&gt;
&lt;p&gt;
For non-physical trading, I have only mentioned the forward contract, which is utilised in bilateral agreements. The futures market, which is effectively a forward contract that is traded through an exchange contributes heavily to the electricity market. 
&lt;/p&gt;
&lt;p&gt;
Futures derivatives represent similar contracts, however are more standardised and traded through an exchange. The common exchanges used for trading electricity are ICE for long-dated agreements, and EPEX Spot or Nordpool for day-ahead and within-day trading. Within ICE, the two main prices for Great Britain power are the UBL, the UK Base Electricity Future, and UPL, the UK Peak electricity future. The futures agreements can be traded as far as 5 years into the future, and tend to be for delivery at entire months at a time at the very minimum, this is the standardised nature of a future.
&lt;/p&gt;
&lt;p&gt;
The standardisation of contracts for electricity are largely based on time periods within the day. I have already mentioned baseload and peakload within this blog which are used for long-dated futures with ICE. Nordpool and EPEX Spot are exchanges that are used for short-term electricity delivery and therefore use a much finer granularity of time intervals for delivery. To better understand how a day can be broken down for exchange trading, we can take a look at the EFA day. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The EFA Day&lt;/h4&gt;
&lt;p&gt;
EFA stands for Electricity Forward Agreement. It is used to capture the time-granularity at which electricity can be traded within a day.
&lt;/p&gt;
&lt;p&gt;
The EFA day consists of 6 4-hour time blocks. The time blocks start at 23:00 from the day before, and run until 23:00 of the current day. There are 48 half-hour blocks, running from midnight to midnight. When making an agreement using the settlement blocks, they will therefore apply from previous day.&lt;br&gt;
Each EFA block can be split in half, to give us 1a, 1b, 2a, 2b etc. And EFA blocks can be grouped to provide a descriptive product. These products include:&lt;br&gt;
&amp;#8226; Baseload - Blocks 1 to 6.&lt;br&gt;
&amp;#8226; Off-Peak - Blocks 1, 2 and 6 during weekdays, and all weekend.&lt;br&gt;
&amp;#8226; Peak - Blocks 3, 4, and 5 during weekdays.&lt;br&gt;
&amp;#8226; Overnight - Blocks 1 and 2.&lt;br&gt;
&amp;#8226; Daytime - Blocks 3, 4, 5, 6.&lt;br&gt;
These blocks are typically traded at at larger time-scales before delivery, whereas smaller blocks of delivery are traded closer to the time of delivery as forecasts for demand are better known from the purchasers of power. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 36%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABYlAAAWJQFJUiTwAAABZ0lEQVQoz1WQaUsCURiF5++IoriLYozCqDjW6Ogszbil4659CNOIFoqgzBKR/u/pvi8a9OHhnnvOuaukPDyhc/hBfnWH3O0azm6P+vsGZzdL2NtvWAJ5ucbF6xta+wPU5xcxX6EpdOPjk7Wx2cL+2qFw/whJH09gL65hTGdoTKYwZ3PW+mgM46g1b8AZQV1tMGCfclpP2povUBuOIDmmifNyGbZhoFGtQtcucHnUlJm6zrRdB3XhuZYF17Y4b9o2d6x6HY7wDdGTOt0umq0Wrno9tDsd1p64Aelev8++47roC01d8khTbzgaca/veUyr3YaUyWSQSCQQj8f/SCaTDPlEqVRCpVL5l9N4yoloNIpYLAapLJ4ryzIvqtVqUBQFuVwO5JPO5/OcEZqmoVAoIJvNsk9QT1VVGOKbisUiJDrJ5/MhnU6jKv6FTjvNw+EwAoEA/H4/EwwGkUqlePNTFgqFmEgkwvkvTtARcGPncNoAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;The EFA Blocks&quot;
        title=&quot;The EFA Blocks&quot;
        src=&quot;/static/c823c8befa4209ad3b2551b8634a406a/5a46d/125_efa.png&quot;
        srcset=&quot;/static/c823c8befa4209ad3b2551b8634a406a/1db73/125_efa.png 75w,
/static/c823c8befa4209ad3b2551b8634a406a/8a4e8/125_efa.png 150w,
/static/c823c8befa4209ad3b2551b8634a406a/5a46d/125_efa.png 300w,
/static/c823c8befa4209ad3b2551b8634a406a/fc2a6/125_efa.png 450w,
/static/c823c8befa4209ad3b2551b8634a406a/0a47e/125_efa.png 600w,
/static/c823c8befa4209ad3b2551b8634a406a/2acc9/125_efa.png 2378w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;
&lt;br&gt;
&lt;small style=&quot;float: right;&quot; &gt;Picture: EFA Blocks from YouTube Video by &lt;a href=&apos;https://www.youtube.com/watch?v=ycu24MfufJk&amp;t=63s&apos;&gt;Modo Energy&lt;/small&gt;&lt;/a&gt;&lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Day-ahead and Within-Day Power Trading&lt;/h4&gt;
&lt;p&gt;
The day-ahead and within-day markets provide an opportunity for finer granularity trading at different time periods close to the delivery. These markets use spot markets for price discovery in the here and now. The exchange markets enable energy consumers to finely match their required demand at each hour to the electricity they have incoming from different suppliers as the time for delivery approaches.
&lt;/p&gt;
&lt;p&gt;
As mentioned earlier in this blog, electricity suppliers will often enter into long-dated contracts to fulfil their baseload and peakload requirements using their forecasts. The energy suppliers can then use the Day-ahead (DA) and Within-day (WD) markets to match the outstanding demand at all other times of delivery.
&lt;/p&gt;
&lt;p&gt;
The power exchanges Epex Spot and Nordpool both operate as auction exchanges; they build an order book of the volumes their registered participants need to buy and sell and the prices they are willing to accept for each day. Using the order book, the exchange can calculate the market price at each time interval.
&lt;/p&gt;
&lt;p&gt;
The auctions run everyday, accepting all the bid and offers 1 day prior to the day-ahead auction and calculating the price. The within-day auctions run all the way up to gate closure. Gate closure is 1 hour before the time for delivery of electricity, it is where all participants in the market must submit their intentions for electricity demand and supply to enable the National Grid to ensure the system is balanced and that electricity can be reliably delivered.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Balancing Mechanism&lt;/h4&gt;
&lt;p&gt;
The balancing mechanism is used by the National Grid to ensure electricity supply and demand are balanced at the time for delivery. The motions for balancing take place after gate closure when generators and suppliers publish details of their intended generation and consumption for the settlement period. From a high level, there are two functions of the balancing mechanism that ensure supply meets demand. The first function comes from designated Balancing Mechanism Units that increase or decrease their intended demand or supply, and the second function is from Net Imbalancing Units that have excess electricity to sell to the grid when required.
&lt;/p&gt;
&lt;p&gt;
The submission from market participants to the National Grid of their intentions is known as a physical notification. The notifications can be updated up until gate closure, when they then become &apos;final notifications&apos;. 
The National Grid use final notifications to calculate the imbalance that is present between supply and demand. They then use the balancing mechanism to equate the two.
&lt;/p&gt;
&lt;p&gt;
The Balancing Mechanism operates through registered participants. Participants register themselves with Elexon, the organisation who manage the Balancing and Settlement code.
Participants are known as Balancing Mechanism Units (BM Units) and they contribute to the Balancing Mechanism by submitting bid and offer prices for each half-hourly period up to gate closure for what they are willing to pay to reduce or increase their contribution to supply or demand. A bid price is submitted if a generator want to reduce their generation, or if a supplier want to increase their demand. This is known as a flex up. An offer price is made for the opposite, where a generator increases supply or a supplier decreases demand, known as a flex down. The National Grid ESO will decide which bid and offer to accept for balancing using factors such as cost efficiency and volumes from the bid and offers. 
&lt;/p&gt;
&lt;p&gt;
These bid and offer prices can also be negative, where the participants are paid to reduce/increase against their original plans. Once the National Grid have accepted a price, they issue a Balancing Offer Acceptance (known as a BOA).
&lt;/p&gt;
&lt;p&gt;
There is a further mechanism beyond the BMU&apos;s to ensure the balancing mechanism works, and this is through net imbalance volumes. Once the BMU participants are selected, and the National Grid see a balance between supply and demand, there is still a dependency for all participants to contribute as expected. If there is a shortfall, for example a generator not contributing as expected, or a financial participant has agreed to buy and sell unequal amounts of electricity, Elexon are responsible for meeting the shortfall. Participants that fail to deliver are considered &apos;out of position&apos; and must therefore pay Elexon what is known as a &apos;system price&apos; or &apos;cash out&apos; price for failing to meet what is due. The system price is considered a penalty as the price paid will be greater than the market rate for electricity, whether it is for a difference in supply or demand. 
&lt;/p&gt;
&lt;p&gt;
When an imbalance occurs, Elexon will turn to a new participant for the generation of electricity, and these are generators who have excess demand available that they had not assigned to the grid. Generators will receive payment at the system price for the excess electricity that is required, thus possibly realising a greater profit for their electricity than they had at a market price. This unique situation has lead to the term &apos;NIV Chasing&apos;, where generators can deliberately over-generate on the possibility of receiving exposure to the system price. 
&lt;/p&gt;
&lt;p&gt;
With the facility from BMU&apos;s and generators who contribute to net imbalancing, the National Grid are able to manage the supply and demand of electricity across the grid at all times of the day to minimise the potential for unused electricity while ensuring the expected demand of electricity is met.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
This blog has taken a walk through the electricity markets of Great Britain at a very high level to capture the mechanisms in-place for the electricity lifecycle, physical and financial participation and balancing mechanisms. This simplified view of generators and suppliers enable us to understand where our electricity comes from, the factors that contribute to the price we pay for it and the complexity of the system that enables it to remain stable and efficient.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An introduction to Numpy]]></title><description><![CDATA[A look over the main methods and mathematical operations available with numpy]]></description><link>https://aneesh.co.uk/an-introduction-to-numpy</link><guid isPermaLink="false">https://aneesh.co.uk/an-introduction-to-numpy</guid><pubDate>Sat, 27 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Numpy&lt;/h4&gt;
&lt;p&gt;
Follow along in this Python tutorial where I introduce the basic methods and 
operations that you can perform with numpy. 
We will take a look at how you can create an array in numpy, reshaping it into different matrix sizes and selecting specific rows and columns from that matrix. 
We also dive into the different ways you can generate and obtain random numbers with numpy. 
And lastly we will conclude with some basic mathematical operations you can perform with numpy.
&lt;p&gt;This video will serve as a perfect first stop with getting yourself familiar and
confident with the numpy package from Python. &lt;/p&gt;
&lt;p&gt;Link to numpy documentation: &lt;a href=&quot;https://numpy.org/doc/&quot;&gt;https://numpy.org/doc/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:00 - Introduction
00:28 - Creating arrays
03:00 - Generating random numbers
05:55 - Reshaping data
06:48 - Indexing numbers
10:45 - Array Operations
12:28 - Mathematical Operations&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/Hq1SDrs1VQs&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEAv/EABUBAQEAAAAAAAAAAAAAAAAAAAEA/9oADAMBAAIQAxAAAAFU8rGDYn//xAAaEAACAwEBAAAAAAAAAAAAAAABAgADEhMh/9oACAEBAAEFAqwmLFXraMWbYRCYD5//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARES/9oACAEDAQE/AdSt/8QAFhEBAQEAAAAAAAAAAAAAAAAAABIx/9oACAECAQE/AdS//8QAHBABAAIBBQAAAAAAAAAAAAAAAQAxAhARIUGB/9oACAEBAAY/AqHyGzcQqcZMXvT/xAAaEAEBAQEAAwAAAAAAAAAAAAABEQBRMXGR/9oACAEBAAE/IVCs6vLEAUQEmFGo7p4D05QjfCufj5v/2gAMAwEAAgADAAAAEFDv/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAEhEf/aAAgBAwEBPxB41LB1D//EABgRAAMBAQAAAAAAAAAAAAAAAAABESEx/9oACAECAQE/EEqgsdP/xAAdEAEAAwACAwEAAAAAAAAAAAABABEhMUFRcZGx/9oACAEBAAE/EMDfay29ZmQEqBZ9x5eFK3iFCm+KJe+nsNV5gRq9h/Z//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/25d52ce3a375b9a7907213527b5dcf94/f93b5/124_numpy.jpg&quot;
        srcset=&quot;/static/25d52ce3a375b9a7907213527b5dcf94/8ddbc/124_numpy.jpg 75w,
/static/25d52ce3a375b9a7907213527b5dcf94/75985/124_numpy.jpg 150w,
/static/25d52ce3a375b9a7907213527b5dcf94/f93b5/124_numpy.jpg 300w,
/static/25d52ce3a375b9a7907213527b5dcf94/20e5d/124_numpy.jpg 450w,
/static/25d52ce3a375b9a7907213527b5dcf94/b4294/124_numpy.jpg 600w,
/static/25d52ce3a375b9a7907213527b5dcf94/4a95b/124_numpy.jpg 2598w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[CORBA 101]]></title><description><![CDATA[A look into the CORBA architecture and how it works.]]></description><link>https://aneesh.co.uk/corba-101</link><guid isPermaLink="false">https://aneesh.co.uk/corba-101</guid><pubDate>Sun, 26 Nov 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Distributed system communication&lt;/h4&gt;
&lt;p&gt;
CORBA - the common object request broker architecture (yes, a mouthful), is a mechanism used within computer networks to allow different machines to identify, manage and invoke operations upon each others Objects remotely.
What makes CORBA so useful, is that it has an almost complete language-agnostic design, meaning
systems within the network don&apos;t need to be written in the same language to interoperate. This is what makes CORBA unique and relevant to engineers, even 30 years after it&apos;s inception.
&lt;/p&gt;
&lt;p&gt;
With it&apos;s interoperable design, CORBA is a concept I routinely face in my job. What I had thought would be a simple 5-minute google session turned into a multi-day activity, as the CORBA architecture and workflow isn&apos;t as straight-forward as it&apos;s value proposition. 
&lt;/p&gt;
&lt;p&gt;
In this blog, we are going to look into the components that make CORBA work while demystifying the many terms used around CORBA networks. At the end, you should be better positioned to navigate a conversation on CORBA (as I have  struggled to do), and to understand why designing interactions within a CORBA network is extremely important. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;What CORBA is and what it isn&apos;t&lt;/h4&gt;
&lt;p&gt;
Before we dive into CORBA, I will offer a few comparisons of CORBA to help us understand why it still has relevance in engineering, and how it differs from other protocols.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CORBA vs Remote Method Invocation (RMI)&lt;/strong&gt;&lt;br&gt;
RMI provides a mechanism for which a server and client can communicate with each other, they pass information back and forth and where remote Objects can be created, referenced and invoked upon by the client. &lt;br&gt;
RMI differs from CORBA as it is only used for Java applications, whereas CORBA is language agnostic. RMI uses Java interfaces to define contracts for the type of Objects that can be invoked upon, whereas CORBA uses the interface definition language (IDL), which I will discuss later.&lt;br&gt;
RMI passes Object by value and reference, whereas CORBA uses reference only, via the naming service (also, more on this later). &lt;br&gt;
And lastly, RMI uses the JVM to identify and locate Objects, whereas CORBA uses a Local Object Adaptor (LOA) to do the same.
&lt;/p&gt;
&lt;p&gt;
One way CORBA differs from other translation layers, such as RPC, is that it supports remote Object references. This means you can pass Object references as parameters within your service call. For example, if you had an Object that represented a file, you could pass that Object as a reference to another remote call to then add the file to a database, thus allowing Objects to be passed as parameters. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CORBA vs SOAP&lt;/strong&gt;&lt;br&gt;
SOAP, the simple Object access protocol, is an xml-based protocol used for distributed members of an application to communicate with each other. SOAP is powerful as it enables language agnostic communication and supports a variety of protocols including HTTP, SMTP and TCP. 
The main difference between CORBA and SOAP is that CORBA is stateful, whereas SOAP is stateless. With CORBA, you can capture the state of an application from within the instance of the Object. The state of the Object is modified by calling functions upon the Object. SOAP, however, uses a stateless protocol.
&lt;/p&gt;
&lt;p&gt;
SOAP also differs from it&apos;s bespoke procedures. Instead of translating requests and responses on both the client and server, the client and server would instead agree to a common interface codified in a language called extended markup language, or XML. This makes SOAP a little more accessible to use and maintain over CORBA. We will later see how translations of requests work in CORBA. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CORBA vs REST&lt;/strong&gt;&lt;br&gt;
REST is similar to SOAP in how it resolved a communication challenge amongst different systems built with different programming languages. REST provides greater scalability and flexibility in it&apos;s design through a dynamic exchange. As a result, REST differs from CORBA in a similar way that SOAP does, through it&apos;s stateless design and protocols for communication. Nevertheless, I would say that both REST and SOAP serve different purposes to CORBA in the active management and control of remote Objects. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The CORBA Architecture&lt;/h4&gt;
&lt;p&gt;
Thank you for reading this far, we&apos;ve opened up quite a few ends from that introduction, but now we can start to look at the real detail of CORBA..
&lt;/p&gt;
&lt;p&gt;
To explain how CORBA works, I will introduce the individual components one by one, and describe how each one interlinks through an interaction between a client and server.&lt;br&gt;
Below is an image of the CORBA architecture to capture the individual elements for a request.
(Image by Vinoski via: https://www.dre.vanderbilt.edu/~schmidt/PDF/vinoski.pdf)
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b8bf5cd994c908de459ff7d0d6581ce3/0c96c/123_corba.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAECBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAdtWhlB//8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECERASIf/aAAgBAQABBQLbrmJ2mVj/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAXEAADAQAAAAAAAAAAAAAAAAAAEEEx/9oACAEBAAY/AqVY/wD/xAAYEAADAQEAAAAAAAAAAAAAAAAAASERcf/aAAgBAQABPyF4xjXIIWQWCHEZDiT6JYf/2gAMAwEAAgADAAAAEIMP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAHBAAAgICAwAAAAAAAAAAAAAAAREAITFRQWFx/9oACAEBAAE/EBomWUQ7NzcAqk8rcxQ5VwLGFtCFQBDybUACAA8Cn//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Image of CORBA Network&quot;
        title=&quot;Image of CORBA Network&quot;
        src=&quot;/static/b8bf5cd994c908de459ff7d0d6581ce3/f93b5/123_corba.jpg&quot;
        srcset=&quot;/static/b8bf5cd994c908de459ff7d0d6581ce3/8ddbc/123_corba.jpg 75w,
/static/b8bf5cd994c908de459ff7d0d6581ce3/75985/123_corba.jpg 150w,
/static/b8bf5cd994c908de459ff7d0d6581ce3/f93b5/123_corba.jpg 300w,
/static/b8bf5cd994c908de459ff7d0d6581ce3/20e5d/123_corba.jpg 450w,
/static/b8bf5cd994c908de459ff7d0d6581ce3/b4294/123_corba.jpg 600w,
/static/b8bf5cd994c908de459ff7d0d6581ce3/0c96c/123_corba.jpg 692w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Client&lt;/strong&gt;&lt;br&gt;
A client is responsible for initiating the workflow with CORBA as they invoke operations upon an Object and process their response. The client is made aware of the structure of the Object through it&apos;s interface, and is able to experience the behaviour of the Object through invoking it&apos;s methods. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Interface Definition Language (IDL)&lt;/strong&gt;&lt;br&gt;
The IDL is independent of any programming language; it enables systems of all programming languages to access Objects via the advertised interface. The logical processing of the CORBA Object is hidden from the client, however it&apos;s structure and methods are made clear through the IDL.
&lt;/p&gt;
&lt;p&gt;
The IDL provides the ability for CORBA clients to invoke actions upon an Object. The IDL is defined for each CORBA Object and is provided to the client for invocation. Any client that wants to invoke upon the Object will use the interface and transmit (by marshalling) the parameters for that request.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Object&lt;/strong&gt;&lt;br&gt;
An Object represents an implementation of an IDL. Each Object can be identified by a reference which uniquely names the instance across servers (known as an Object reference).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Naming Service&lt;/strong&gt;&lt;br&gt;
The naming service enables CORBA clients to obtain an Object through it&apos;s associated name. 
The naming service will both create bindings between Objects and their reference names to make them available to a client.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Internet Inter-ORB Protocol (IIOP)&lt;/strong&gt;&lt;br&gt;
The CORBA standard uses the IIOP for client-server communication. The IIOP enables a client and server from almost any vendor, computer, OS, language and network to communicate with each other through an Object Request Broker (ORB).
&lt;p&gt;As a result, the client can communicate with the server through it&apos;s address (server and port) known as the Interoperable Object Reference (IOR). &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Object Request Broker&lt;/strong&gt;&lt;br&gt;
The ORB, also referred to as an item bus, enables clients to make application calls to the server. It acts as an agent to transmit operation invocations in a distributed environment. The ORB handles communication, marshalling and unmarshalling of parameters with each request to enable CORBA servers to correctly receive request details.
&lt;/p&gt;
&lt;p&gt;
The delivery of requests is performed by the ORB Core using IIOP over TCP. 
&lt;/p&gt;
&lt;p&gt;
The ORB also manages the interface repository (IFR), a standardised database of IDL interface definitions. From the client&apos;s perspective, the ORB provides interface definitions which enables it to construct invocations against Objects.
&lt;/p&gt;
&lt;p&gt;
The ORB Interface is an abstraction for the client and server to initialise and shutdown the ORB. It can also be used to convert Object references to strings and to create argument lists for requests made through the dynamic invocation interface (DII). 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Portable Object Adaptor (POA)&lt;/strong&gt;&lt;br&gt;
The POA is responsible for assisting the ORB in delivering client requests to Objects. Firstly, it will carry out the activation and deactivation of Object instances when transmitting a request to the server. This is performed for memory conservation. The POA will deactivate and activate as according to a policy that is set by the developer for flexibility in memory requirements. 
&lt;/p&gt;
&lt;p&gt;
The POA is also responsible for generating Object references as used by the client. This is then interpreted by the POA when received in a client&apos;s invocation request. The POA will also work with the skeleton to invoke operations on the server side.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Stubs and Skeletons&lt;/strong&gt;
A stub and skeleton can be thought of as a local proxy for the client and server of the CORBA Object. They provide a mechanism for invoking an Object reference.
&lt;/p&gt;
&lt;p&gt;
The stub and the skeleton are effectively files that are produced by the IDL Compiler. Both files contain code which is used for operating invocations by the methods specified in the IDL. 
&lt;p&gt;On the client&apos;s side, the stub will contain code that is generated during the client&apos;s build process that is used when sending the request. This compilation of code enables the request to be interpreted once received by the server. &lt;/p&gt;
&lt;p&gt;On the server side, the skeleton acts as a pointer for the client&apos;s request to the appropriate code in the Object implementation to satisfy the request. The skeleton is therefore connected to both the ORB and the server. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Stubs and Skeletons are used by clients and servers respectfully with the ORB. A stub is used to send requests via the ORB which is then interpreted by the skeleton from the server regardless of the client&apos;s programming language or vendor. The interface defines the operations and types the object supports and therefore the requests it handles. 
The stub and skeleton facilitates the communication between the client and server through the ORB. The stub will marshal application parameters into a common data-level representation. The Skeleton will demarshal the data back to the application-level parameter. 
&lt;/p&gt;
&lt;p&gt;
The stub will forward the call to the ORB to be transported to the server.  
At the server side, the call is routed with an Object adaptor. The Object adaptor passes the call to the skeleton.
&lt;/p&gt;
&lt;p&gt;
Both the stub and the skeleton have dynamic elements. The dynamic invocation interface (DII) for the stub and dynamic skeleton interface (DSI) allow the ORB to deliver requests to the servant that has no compile time knowledge of the IDl interface it is implementing.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Putting it all together&lt;/h4&gt;
&lt;p&gt;
Now that we have touched on each of the components, we can begin to appreciate the many steps taken by our client, ORB, and server when we submit a request upon a remote Object with CORBA:
&lt;/p&gt;
&lt;p&gt;
1. The CORBA Server is responsible for registering Object-name bindings in the naming service.  
2. The CORBA Client queries the naming service for an Object reference it can create a request upon.
3. Each Object will contain an IDL that the client can use to understand structure and methods for invocation. The Client submits a request.
3. The IDL Compiler converts the client&apos;s request into a stub that is sent via the ORB to the server.
4. The ORB Interface will ensure the ORB is initialised for transport.
5. The ORB Core will deliver the request to the server via IIOP over TCP
6. On the server side, the POA will ensure the Object is activated, and submit the request to the Skeleton to be invoked.
7. The Skeleton demarshals the request for the server to interpret and invoke.
8. On the CORBA server, a response can be returned for the client to receive. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
This blog has taken a look at how CORBA distinguishes itself as one of the most popular middleware options to distributed applications. We have then reviewed the many components that enable it to work. This high level view has demonstrated the complexity of the system and how a single request from a client is dependent on multiple transformations, references and transmissions before the server can invoke and respond with a result. 
&lt;/p&gt;
&lt;p&gt;
CORBA&apos;s interoperability make it a dynamic solution that can be implemented it a diverse array of systems; however with this comes a heavy technical understanding that is required to extract an optimised solution. I hope you have been able to follow the coverage of this blog well, however I have merely scratched the surface of CORBA where other components such as servants and dynamic invocations can be a good starting point for further research and detail.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Commodity Options: the vanilla and the exotic]]></title><description><![CDATA[A look at vanilla and exotic options in the context of commodity derivatives.]]></description><link>https://aneesh.co.uk/commodity-options-the-vanilla-and-the-exotic</link><guid isPermaLink="false">https://aneesh.co.uk/commodity-options-the-vanilla-and-the-exotic</guid><pubDate>Tue, 22 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand what an options contract is and how it can be used.&lt;br&gt;
&amp;#8226; Explore exotic options and the various benefits they offer.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction&lt;/h4&gt;
&lt;p&gt;
My previous blog took at a look at forwards and swap derivatives in general with an application to commodity derivatives. This blog will build upon the last blog to look at the different types of option derivatives available within the context of commodities. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;An option contract&lt;/h4&gt;
&lt;p&gt;
If we take a look back at the forward contract, upon agreement, the buyer will have an obligation to purchase the exact amount of the commodity upon maturity at the agreed strike price from the seller. While the contract is not often seen to maturity, there remains an outstanding obligation for the contract to be exercised. An options derivative differs as there is no such obligation. A common definition you will see for an option is:&lt;br&gt;
&lt;i&gt;An option provides the right, but not the obligation, to buy or sell a asset at an agreed upon price and quantity at maturity.&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
Let&apos;s take a look at what this means for both sides of the deal.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;How option contracts work&lt;/h4&gt;
&lt;p&gt;
An option contract works as such: &lt;br&gt;
1. The contract is purchased by the buyer from the seller for a fee. &lt;br&gt;
2. The contract will specify which asset can be bought/sold, the strike price and volume. &lt;br&gt;
3. The contract will have a maturity date, and depending on the type of contract it is, the buyer can then exercise the contract.
&lt;/p&gt;
&lt;p&gt;
The options contract has a few variables to the buyer, so let&apos;s take a look at these:&lt;br&gt;
1. If the contract is to purchase a security, it is known as a &apos;call&apos; option. If it is to sell a security, it is known as a &apos;put&apos; option.&lt;br&gt;
2. A &apos;European&apos; option will only allow the buyer of the option to exercise it at maturity, whereas an &apos;American&apos; option will allow the buyer to exercise at anytime until maturity. Lastly, a &apos;Bermudan&apos; option will allow the holder to exercise at a set of pre-agreed dates (such as the 28th of each month). &lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
An option&apos;s value will continuously dip and rise in intrinsic value to the holder as the market price of the underlying security changes. For example with a call option, if the price of the security increases beyond the strike price, the option holder can exercise it (if it is American) to make a profit. Exercising the option to buy at a the lower market price will create a profit as they can (in-theory) sell at the higher value in the market. For a put option, the option holder will be able to make money if the value of the security drops, as they are able to then sell the security for a price that is higher than the market price. For both of these scenarios, the option holder are considered &apos;in the money&apos;, where they can profit from exercising their right on the option.
&lt;/p&gt;
&lt;p&gt;
One small point to remember is the fee (aka premium) paid by the buyer to the seller of the option to have the right to buy or sell the asset. As a result of the fee, the option buyer will only be in the money once the strike price has surpassed (for calls) or fallen below (for puts) the market price combined with the option premium. The two graphs below illustrate how a bought call and put option can move in and out of the money as the market price changes.&lt;br&gt;
For the call option, the premium is £10, and the strike is £100, therefore break even is at £110 for the market price. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9f83f542d8b5f58bcee5331cbc3968fe/a878e/122_graphs.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 36%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABYlAAAWJQFJUiTwAAAA90lEQVQoz1VR27KDMAj0///zdPSl9ai5ErJlSVqnzqwg4LLA0nuHGjqAWivWbcO6briuAD7Mf6AT+/6Pv8cDz9cLqneOdpGmCDGjSkMuFecZcBhiKtagIWWxnJqtXkM/pjxrMkrlfyPemhE6Uc7WSR0fNejqZASbskET8w2tNUur2/1IuAKbj7qFzDkXJ2PBDcUZq8eHwuJkRHVSwXFlj6v5xaZzQr78YxYTqoLL1hAiVVUbSVyhaptTDPXRAO5vTvdVyF1QMo/CXe1nNiTbT3WkSSgylItNEJL8TMScEzalwjE/4SuYS/aY227E8j0KrYhO/wYv/gYKcyXMBu93CAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Graphs of call and put PnL&quot;
        title=&quot;Graphs of call and put PnL&quot;
        src=&quot;/static/9f83f542d8b5f58bcee5331cbc3968fe/5a46d/122_graphs.png&quot;
        srcset=&quot;/static/9f83f542d8b5f58bcee5331cbc3968fe/1db73/122_graphs.png 75w,
/static/9f83f542d8b5f58bcee5331cbc3968fe/8a4e8/122_graphs.png 150w,
/static/9f83f542d8b5f58bcee5331cbc3968fe/5a46d/122_graphs.png 300w,
/static/9f83f542d8b5f58bcee5331cbc3968fe/fc2a6/122_graphs.png 450w,
/static/9f83f542d8b5f58bcee5331cbc3968fe/0a47e/122_graphs.png 600w,
/static/9f83f542d8b5f58bcee5331cbc3968fe/a878e/122_graphs.png 2048w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why Options are useful&lt;/h4&gt;
&lt;p&gt;
The optionality of option contracts make them useful in multiple ways for hedging risk and taking speculative positions on the market. 
&lt;/p&gt;
&lt;p&gt;
The first way an option can be used is to take exposure in an underlying security. If you expect the price of a commodity to rise, you can purchase it in the market with the expectation to sell at a higher value in the future. This exposes you to the complete price movement of the commodity, both upwards &lt;u&gt;and&lt;/u&gt; downwards. A future can also be used to obtain exposure. On the other hand, purchasing a call option will provide similar exposure to the commodities price movement, but with a limited downside equal to the premium paid for the option. Therefore, the investor can take a more precautionary position on the commodity while obtaining exposure to it&apos;s expected rise in price.
&lt;/p&gt;
&lt;p&gt;
An option can also be used as an asset itself. The premium paid for the option can be reflective of the current market position, expiry and volatility of the commodity. A trader can then sell their right to exercise the option in the market for a greater premium.
&lt;/p&gt;
&lt;p&gt;
Options can be used to hedge the risk of a portfolio should there be adverse movements in the underlying securities price. For example, if a portfolio consisted of copper, they would be completely exposed to a price rise (and fall) of copper. By buying a put option, they will be paying a premium, but guaranteeing a price that they can sell copper should it fall in price. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Exotic Options&lt;/h4&gt;
&lt;p&gt;
Exotic options capture a slightly different, and more complex, way an option can be altered to see a different purpose for the investor and seller.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Binary Option&lt;/h5&gt;
&lt;p&gt;
A binary option, also known as a digital option works where the buyer of the option pays a premium to the seller and receives a fixed pay out if the price of the underlying security reaches a specific value, known as a barrier. 
&lt;/p&gt;
&lt;p&gt;
The buyer receives a fixed return if the option is exercised, regardless of the market price of the underlying, but provided it has reached the barrier price. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Barrier Option&lt;/h5&gt;
&lt;p&gt;
&lt;i&gt;Takes deep breathe...&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
A barrier option itself is pretty complicated, made more confusing as we have just mentioned a &apos;barrier price&apos; in the binary option. This naming is purely coincidental. Now let&apos;s take a look at the barrier option.
&lt;/p&gt;
&lt;p&gt;
Unlike an ordinary option, a barrier option will have a condition attached to it where the option contract is either granted or cancelled when the underlying security reaches a specific price point, known as a trigger.
&lt;/p&gt;
&lt;p&gt;
This means we start with the investor either already owning an option, with the possibility that it will be cancelled when the trigger is reached, or the investor not already owning the option, and the option being granted upon the trigger.&lt;br&gt;
When an option is cancelled, it is known as a &apos;knock out&apos;, and when it is granted, it is known as a &apos;knock in&apos;.
&lt;/p&gt;
&lt;p&gt;
The placement of the trigger price can be in one of two places: either in the money or out of the money, depending on the option type. A trigger that is out of the money is known as a standard barrier, and one that is in the money is known as a reverse barrier.
&lt;/p&gt;
&lt;p&gt;
So we have 4 different types of barrier options: a standard knock in, a reverse knock in, a standard knock out and a reverse knock out.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;A call barrier example&lt;/h5&gt;
&lt;p&gt;
Let&apos;s take a &lt;u&gt;call&lt;/u&gt; option with the below values:&lt;br&gt;
Strike: £120&lt;br&gt;
Market Price: £100
&lt;/p&gt;
&lt;p&gt;
This means the owner of the call can buy the asset at a price of £120, presumably exercised when the market price is above that.
&lt;/p&gt;
&lt;p&gt;
A standard knock in barrier would place a trigger out of the money, and the option would be granted. Therefore the trigger could be £90. This is known as a &apos;down and in&apos; barrier, as the price goes down and the option is granted. 
&lt;/p&gt;
&lt;p&gt;
Similarly, a reverse knock in would have a trigger in the money, so £125 for example. This is an up and in. 
&lt;/p&gt;
&lt;p&gt;
We can also have a standard knock out where the trigger is out of the money, and the option is cancelled, so the trigger would be £95 and this would be down and out. A reverse knock out would see the option cancelled with a trigger of £125 for example.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;A put barrier example&lt;/h5&gt;
&lt;p&gt;
The strike values will differ slightly for a put option, which grants the right to sell the option. Let&apos;s use the below values:&lt;br&gt;
Strike: £90&lt;br&gt;
Market Price: £100
&lt;/p&gt;
&lt;p&gt;
This contract will mean the owner of the put can sell the underlying security for £90, presumably when the underlying value of the security has fallen below that.
&lt;/p&gt;
&lt;p&gt;
A standard knock in will see the trigger placed out of the money for the option to be granted. So this value would be £105 thus creating an &apos;up and in&apos; trigger. This is unlike the call option&apos;s standard knock in which is down and in.
&lt;/p&gt;
&lt;p&gt;
A reverse knock in would have an in the money trigger (£85) and would therefore be down and in. A standard knock out would have the value at £105 which is an up and out, with a reverse knock out with a trigger of £85 would be down and out.
&lt;/p&gt;
&lt;p&gt;
The additional condition that comes with a barrier option means they are less likely to be exercised as more events (barrier price being reached for knock-in and not being reached for knock out) are required to occur before the option can be exercised. As a result, the barrier option tend to be cheaper than a vanilla option. The customisability of barrier options also mean they are traded over-the-counter and not through an exchange.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Spread Option&lt;/h5&gt;
&lt;p&gt;
A spread option will use two underlying assets and pay off the difference between the two to an agreed strike price. A call option will pay off if the spread is greater than the strike, and a put will pay off if the spread is less than the strike.&lt;br&gt;
Therefore we can use the below formulas to calculate the return on a spread call and put:&lt;br&gt;
Spread call return = max (p1 - p2 - strike, 0).&lt;br&gt;
Spread put return = max (strike - p1 + p2, 0).&lt;br&gt;
Where p1 and p2 are the prices of the 2 assets in the option.
&lt;/p&gt;
&lt;p&gt;
Spread options are popular derivatives within commodities. They provide protection against 2 underlying prices such as the cost of an input (crude oil), and the price of it&apos;s output (jet fuel). This can be particularly useful for a refiner who would like to protect the spread they earn from converting the crude oil to jet fuel. &lt;br&gt;
Spread options can offer more variety in their application, where underlying securities can differ by location (thus protecting geographic price ranges) or futures (for protecting against delivery date spreads).
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Average rate option&lt;/h5&gt;
&lt;p&gt;
The last exotic option we will look at are average rate options.
Also known as an &apos;avro&apos;, the average rate option is the most popular exotic option for commodities.
Unlike a vanilla option, an average rate option will use the underlying &lt;u&gt;average&lt;/u&gt; price of a security over a time period to determine the market price.
&lt;/p&gt;
&lt;p&gt;
Avros are specifically popular with commodities due to the nature of the contracts that are constructed for physical commodities. Commercial contracts for physical commodities will use average rates. The average rate serves both the buyer and seller from avoiding the highly volatile prices of the commodity, be it the high for the buyer, or the low for the seller.
&lt;/p&gt;
&lt;p&gt;
The relationship that exists between physical and financial commodities derivates is extended towards the payoff on the derivatives that are also used in commercial contracts. Therefore the average rate option is helpful in avoiding a mismatch between the cashflows of the derivative and the physical contract.
&lt;/p&gt;
&lt;p&gt;
The lower price volatility of an average rate option will also reduce the premium required to buy it. Less volatility of price will reduce the opportunity for the contract to be exercised. This is known as a reduced &apos;implied volatility&apos; of the contract.
&lt;/p&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The option contract differentiates itself from other financial derivatives through the optionality of it to be exercised. Further flexibility is provided in it&apos;s ability to be used for buying or selling a security, and for the variations of exotic options that exist. This can result in controlled payouts, average price monitoring and using the spread between two securities.
&lt;/p&gt;
&lt;p&gt;
A major opportunity with option contracts lie in the limited downside they require upfront from the investor in the premium, but the potentially unlimited upside they can provide in exposure to the security. This factor makes option contracts popular across asset classes and has thus lead to the creation of exotic types and strategies to incorporate them to a portfolio.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Commodity Derivatives: Futures, Forwards and Swaps]]></title><description><![CDATA[Understand how the financial derivatives of a future, forward and swap works.]]></description><link>https://aneesh.co.uk/commodity-derivatives-futures-forwards-and-swaps</link><guid isPermaLink="false">https://aneesh.co.uk/commodity-derivatives-futures-forwards-and-swaps</guid><pubDate>Sat, 12 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; A futures contract allows a trader to guarantee the price and quantity of a commodity for delivery at a date in the future.&lt;br&gt;
&amp;#8226; A forward differs to a future by where it is booked and provides greater flexibility in it&apos;s terms.&lt;br&gt;
&amp;#8226; A swap enables a counterparty to exchange cash flows between different price indexes or a fixed rate.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;An intro to financial derivatives&lt;/h4&gt;
&lt;p&gt;
My last &lt;a href=&quot;https://aneesh.co.uk/how-financial-institutions-use-commodities&quot; target=&quot;_blank&quot;&gt;blog&lt;/a&gt; was the 2nd in my series on commodities where we took a look at the different ways a financial institution can influence commodity markets, and how a commodity can become traded. This blog will extend upon traded commodities to look at the main financial derivatives that can be created and the benefits they present. 
&lt;/p&gt;
&lt;p&gt;
This blog will take a look at forwards, futures and swaps; my next blog will look at final piece of the puzzle, option contracts, which are a little more complicated. I will generally use commodities as examples in these explanations, however note that financial derivatives are not limited to just this asset class, so where you see &apos;commodity&apos;, you can replace it with &apos;asset&apos;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The future contract&lt;/h4&gt;
&lt;p&gt;
A future is a contractual obligation between two counterparties to purchase or sell a given commodity, at a specific volume and price (known as the futures price) for delivery at a specified date in the future. The buyer of a future contract will benefit as they can guarantee the price of a commodity to be delivered at a date in the future. The seller in the future contract benefits from the guaranteed sale of a specific volume of commodity at a price in the future. Both participants benefit by removing the price volatility for the asset in the sale at the future date.
&lt;/p&gt;
&lt;p&gt;
One example may be an airline, who might like to use a future contract to fix the price of jet fuel for the future. By fixing the price, the airline can better estimate the costs of flights and set prices of tickets. The supplier of jet fuel has a guaranteed sale, and the airline have a guaranteed price that is not impacted by the volatility of market price for jet fuel in the time-frame of the contract.
&lt;/p&gt;
&lt;p&gt;
While a future has many practical benefits, they are not often seen to expiry as a financial derivative. Roughly 95% of futures contracts are settled before expiry, and the delivery of the asset is never made. The two counterparties in the contract will settle a difference in payment between the market rate and the futures price, known as &apos;marking to market&apos;. For example, if a futures contract is made to buy one ounce of gold at £300 per ounce, and at expiry, the market value of gold is £310 an ounce, the seller of the contract will pay the buyer £10, as they are in theory buying gold at £310 in the market, and selling it (at the futures price) of £300. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Over-the-counter vs exchange trading&lt;/h4&gt;
&lt;p&gt;
A future and a forward contract differ in two main ways: where the trade is made and the terms of the contract. A futures contract is placed through an exchange that uses a central clearing house (CCP). The CCP acts as the buyer to the seller and the seller to the buyer, thus guaranteeing the terms of the trade to both counterparties, even in the event of a default from one of the counterparties. Participants in the contract will pay a small deposit, known as margin, to the clearing house that enables them to guarantee the terms. With the CCP, the market of participants are protected from potential knock-on effects of a single counterparty defaulting, and thus causing other counterparties unable to fulfil their onward financial obligations.
&lt;/p&gt;
&lt;p&gt;
A further difference between a future and forward lies in the terms of the trade, and what can be customised in the transaction. Exchanges operate in a uniform way that specify the terms of the contract for individual commodities. For example, this can control the trading units used for a commodity, along with how a price is quoted (currency), how the trade is settled, which months delivery can take place and margin requirements.
&lt;/p&gt;
&lt;p&gt;
Within commodities, the standardisation of trading can be hugely beneficial. This is because commodities can exist in many different forms to introduce other variables on the commodity such as commodity make-up, refiners, approval of content and delivery volumes.
&lt;/p&gt;
&lt;p&gt;
With a forward contract, the terms of the agreement are made between the two counterparties upon the same factors the exchange standardises. This flexibility allows each party to hedge risk more specifically, or to obtain exposure that otherwise wouldn&apos;t ordinarily be available in the exchange.
&lt;/p&gt;
&lt;p&gt;
The margin requirement of the exchange had previously been an outstanding difference between the forward and future, however due to evolving regulatory requirements, the margin requirements between OTC trading are now relatively similar to that of an exchange. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Swaps&lt;/h4&gt;
&lt;p&gt;
A swap contract creates an exchange of cash flows between two parties. The swap is typically made for long periods of time (more than 1 year) and the underpinning cashflows between the two will be based upon different price indexes.
&lt;/p&gt;
&lt;p&gt;
A typical swap will involve the exchange of a fixed rate with that of a variable rate, thus allowing one party to guarantee outgoing cash flows and providing the other party with exposure to a floating rate. The exchange of cash flows will take place at the agreed upon tenor until expiry. 
&lt;/p&gt;
&lt;p&gt;
The notional of the trade will determine the payout that is made between the counterparties. For example, bank A and bank B may enter into an interest rate swap. Bank A pays bank B 5% interest and bank B pays the national bank rate. The notional for the swap may be £100 with tenor of 3 months, and expiration in 1 year.&lt;br&gt;
So if after 3 months the bank rate is 4%, bank B will pay bank A 1% (5% - 4%) of £100. If, after 6 months, the bank rate is 7%, bank A will pay bank B 2% (7% - 5%) of £100. These exchanges of cash flows will take place until expiration. 
&lt;/p&gt;
&lt;p&gt;
Within commodities, the use of a swap can vary by nature of the commodity. For example, gold has a floating lease rate (the interest rate on a gold-backed loan), and can therefore be swapped with a fixed lease rate. You can also have swaps on the actual floating price of the commodity itself.
&lt;/p&gt;
&lt;p&gt;
Swaps are typically used for hedging risk; if a bank is exposed to the underlying movement of a price index, they can fix their exposure by entering into a swap for a fixed rate. On the contrary, another bank can obtain exposure to the floating rate, possibly for speculative purposes, while limiting their downward exposure against the fixed rate.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
This blog has taken a brief look at the fundamental financial derivatives of futures, forwards and swaps, all of which are used by financial institutions to manage their exposure and to speculate on the underlying price movements of commodities. &lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
My next blog will complete our view of financial derivatives as I look into the options contract, and the various ways it can be used with commodities.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How financial institutions use commodities]]></title><description><![CDATA[Understand how different financial institutions use commodities and why not all commodities are traded.]]></description><link>https://aneesh.co.uk/how-financial-institutions-use-commodities</link><guid isPermaLink="false">https://aneesh.co.uk/how-financial-institutions-use-commodities</guid><pubDate>Sat, 05 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the various roles financial institutions play with commodities.&lt;br&gt;
&amp;#8226; Understand why some commodities are traded and others are not.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction&lt;/h4&gt;
&lt;p&gt;
In my last &lt;a href=&quot;https://aneesh.co.uk/an-intro-to-commodity-derivatives&quot; target=&quot;_blank&quot;&gt;blog&lt;/a&gt;, I took a high-level view of the various commodity subdivisions and how they are used in the real world. I then touched upon the importance of price discovery within commodity markets and the role of the price reporting agencies. This blog will extend to help us understand the various financial institutions that participate in the commodities market, as well as the requirements necessary for a commodity to become traded.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The role of financial institutions with commodities&lt;/h4&gt;
&lt;p&gt;
Amongst investment banks, there are many other types of financial institutions that play a role within the commodities markets, including: commodity trading houses, hedge funds, and real money accounts. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Investment Banks&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Investment banks typically act across a variety of services with commodities. A &apos;full service&apos; investment bank will exist to deal with financial derivatives, but also to act as a counterparty in the physical trade of the commodity. As a result, an investment bank can play a pivotal role in the delivery of commodities with their cash-rich position and ability to hedge risk at scale. Some examples of how they can be influence the physical commodity market include:&lt;br&gt;
&amp;#8226; Owning inventory for refiner participants to reduce their operational capital requirement.&lt;br&gt;
&amp;#8226; Purchasing storage facilities or pipelines to transport and store commodities between market participants.&lt;br&gt;
&amp;#8226; Provide financial support to supply chain participants.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Commodity Trading Houses (CTH)&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
A CTH originally existed as a middle-man between consumers and producers of commodities, earning a profit from buying and selling commodities at wholesale. CTH have since evolved to own parts of the supply chains. They therefore solve a similar problem that investment banks do with financing participants and facilitating supply chains to remain operational.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Hedge Funds&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Hedge funds operate to serve a smaller pool of investors when compared to investment banks. They tend to use actively managed strategies with the goal of generating a profit in all market conditions. As a result, fees tend to be higher to the investor. 
&lt;/p&gt;
&lt;p&gt;
The smaller size and specific goal-driven business of a hedge fund will more often operate with the financial derivatives side of commodities, and will employ event-driven strategies to maximise returns on market-specific events that impact commodity prices.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Real Money Accounts&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Some financial institutions do not have a similar capacity to borrow money to finance larger operations like an investment bank. Such accounts include those for insurance and pension funds as well as retail and private banks. They will therefore use commodity derivatives as an investment vehicle for purposes such as hedging risk and seeking diversification in their portfolios to match the goals (risk profile and return) of the fund. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why not all commodities can be traded&lt;/h4&gt;
&lt;p&gt;
Now that we understand the different types of institutions that can trade commodities and how their functions within the market can differ, let&apos;s take a look at the properties of a commodity that enable it to be traded on an exchange.  
&lt;/p&gt;
&lt;p&gt;
When we think about a commodity that is traded, it will typically sit in an exchange where buyers and sellers can efficiently receive a price for their desired product. Sometimes, where markets are insufficiently liquid, an investment bank can also step in and act as a market-maker: to be a buyer to the seller and seller to the buyer. Either way, a price for the commodity is transparent and accepted across all market participants of the exchange.
&lt;/p&gt;
&lt;p&gt;
The challenge that commodities introduce to us align with their timelines for delivery and the abundance of grades and varieties that they can exist within. A single commodity (such as gold) can differ by shape, grade, source, transportation and percentage makeup. As a result, a &apos;spot price&apos; might not exist for each individual commodity subtype, and instead is derived from a reference point price for the commodity instead. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Defining a reference point...&lt;/h5&gt;
&lt;p&gt;
In this section, I am going to talk a little bit about the prices of commodities and the markets they trade in. Before I do that, let&apos;s go over a few definitions.&lt;br&gt;
A &lt;strong&gt;spot price&lt;/strong&gt; is the price of an asset, derivative, currency or any product at the current market rate, and for &apos;immediate&apos; delivery. You can think of a spot price as the price you pay in your supermarket for any item on the shelf. You are paying £1 for a bag of bananas as they are immediately sold and owned by you after checkout. &lt;br&gt;
A &lt;strong&gt;spot market&lt;/strong&gt;, also known as a cash market or physical market, is where assets, derivatives and currencies are traded for immediate delivery. In the spot market, delivery is not going to be instantaneous, but is often made 2 business days later (T+2).&lt;br&gt;
A &lt;strong&gt;spot transaction&lt;/strong&gt; involves a trader buying or selling a product in the spot market for the immediate delivery (T+2). 
&lt;/p&gt;
&lt;p&gt;
The unique nature of commodities and their supply chain for extraction, refinement, transportation and delivery, means the spot market for commodities will not often reflect the spot delivery of 2 days time. Instead, the spot price will often reference a point on the futures curve of the commodity, with delivery reflective of that date. 
&lt;/p&gt;
&lt;p&gt;
The emergence of a commodity as a traded asset in an exchange is achieved through price discovery in the market. If market participants are able to receive transparency for what price the commodity is traded, they will then be able to confidently use it for their intended purposes.
&lt;/p&gt;
&lt;p&gt;
A further factor to a commodity becoming traded exists with it&apos;s requirement to be frequently traded. This may begin to sound like a chicken-and-egg scenario where higher trade frequency of a commodity can come from it&apos;s global need (through necessity), but is also facilitated by transparency of it&apos;s fair price.
As commodities are traded more frequently, the demand for price reporting agencies to provide market-wide price transparency increases, and thus a commodity can establish itself upon an exchange.
&lt;/p&gt;
&lt;p&gt;
As previously mentioned, the heterogeneity of commodities do not place an expectation upon PRAs to provide all price definitions of a commodity. They instead provide specific prices for a commodity that act as a reference point to derive further prices from. For example, a price may be given for a grade of 80% gold, where a 70% grade will be traded at a price that is derived below that. 
&lt;/p&gt;
&lt;p&gt;
Following the establishment of a commodity into an exchange, we can see market participants benefit with improved transparency and liquidity of the commodity for trading. This can also lead to standardisation for it&apos;s trading in futures and commercial contracts. Lastly, the price confidence across market participants for the commodity will lead to its inclusion with different financial derivatives. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
Financial institutions can play varying roles in the commodities market. While commodities can behave like other asset classes to a bank, by providing a source for speculation and to hedge risk, a bank can also play a vital role in the operational existence of a commodity. The unique physical presence of the commodity asset class mean they present opportunities for specialist financial institutions to play a role in it&apos;s creation.
&lt;/p&gt;
&lt;p&gt;
Price reporting agencies will provide price transparency into spot markets for commodities once it&apos;s frequency of trading has been established. The PRAs provide price transparency for benchmark grades and variations of a commodity where subsequent prices can be extrapolated.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An intro to commodity derivatives]]></title><description><![CDATA[A look into the various commodity segments, key players in the market and my career change]]></description><link>https://aneesh.co.uk/an-intro-to-commodity-derivatives</link><guid isPermaLink="false">https://aneesh.co.uk/an-intro-to-commodity-derivatives</guid><pubDate>Wed, 12 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; A small change in my job role and company.&lt;br&gt;
&amp;#8226; Commodity derivatives encapsulates both a financial and physical market.&lt;br&gt;
&amp;#8226; Price discovery in commodity derivatives plays a major role for the multiple participants in it&apos;s supply chain.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;A new job and learning chapter..&lt;/h4&gt;
&lt;p&gt;
It&apos;s been one year since I last wrote a blog. Since then, I started creating tutorials on the MEAN stack followed by short videos around backend fundamentals. This diversion away from more &apos;coding&apos; focussed content has come about from a change in job. 
&lt;/p&gt;
&lt;p&gt;
In March 2023, I joined the Macquarie group in an opportunity that explores technical infrastructure alongside Java development. This new role will see me work upon the applications used across the bank&apos;s commodity desks and through the trade lifecycle. 
&lt;/p&gt;
&lt;p&gt;
So with that said, let&apos;s take a look at the bare bones of Commodity derivates, before I write deeper upon these topics in the coming months.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Commodity markets&lt;/h4&gt;
&lt;p&gt;
Commodity markets, just like other asset classes, are classified into smaller segments that focus on specific products. What&apos;s unique here, is that commodities represent real, tangible products that are used in the world around us. There may be some overlap between markets, however the main ones are as follows:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Energy Markets&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Energy markets include the trade of crude oil, refined products, power, natural gas, natural gas liquids and coal.
&lt;/p&gt;
&lt;p&gt;
Crude oil fossil fuels are found between sedimentary rock under the Earth&apos;s surface. They are a combination of hydrocarbons (a chemical compound of hydrogen and carbon) formed over millions of years from the remains of animals and plants. Over the years, the remains are covered in layers of sand and rock to create the raw product under heat and pressure. 
&lt;/p&gt;
&lt;p&gt;
Crude oil can be transformed into many different refined products after it is extracted. These &apos;petroleum&apos; products include gasoline, diesel, heating oil, jet fuels, waxes and lubricants.
&lt;/p&gt;
&lt;p&gt;
Power refers to electricity as a tradeable commodity. Electricity is often measured in kilowatt hours, and is interchangeable to how it is generated. For example, electricity can be produced from coal or natural gas, and both of these are measured the same way, through kilowatt hours.
&lt;/p&gt;
&lt;p&gt;
Coal is also a hydrocarbon fossil fuel, found in sedimentary rock. It can be burned to produce fuel or for generating electricity. 
&lt;/p&gt;
&lt;p&gt;
Natural gas is a gaseous hydrocarbon,  mostly consisting of methane. While it exists in abundance under the Earth&apos;s surface, it&apos;s gaseous form means it must be transported via pipelines, or converted to a liquid for transportation. This conversion can add to the costs associated with it. Natural gas, once refined, can be used for electrical power generation, residential heating, and industrial production.  
&lt;/p&gt;
&lt;p&gt;
Natural gas liquids are components of natural gas that have been separated from the gaseous state. Liquids can be produced in the form of Ethane (used to create plastics), Propane (used for heating), Butane (used for synthetic rubber) and Isobutane (used for aerosols).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Industrial Metals&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Industrial metals include copper and aluminium. 
&lt;/p&gt;
&lt;p&gt;
Copper is a malleable metal that also acts as an effective conductor of heat and electricity. It is most commonly used for wiring, lighting, motors, cables and electric generators.
&lt;/p&gt;
&lt;p&gt;
Aluminium is one of the most widespread metals on Earth. It is soft and malleable making it useful for a variety of products including foils, window frames, beer kegs and electrical conductors.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Precious Metals&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Precious metals include gold and silver. Gold is a durable metal that will not rust or erode with time. Gold is malleable and conductive, making it useful for carrying electrical currents in your smart-phone, but is more commonly used for ornamental objects such as medals and fine jewellery. 
&lt;p&gt;
&lt;p&gt;
Silver is not quite as dense as gold, meaning it will be less durable, but is more malleable. Silver is historically used for photography due to it&apos;s ability to reflect natural light. In the modern day, it is used for mirrors, tableware, batteries and alloys amongst many other ornamental objects. 
&lt;/p&gt;
&lt;p&gt;
The ownership of these precious metals extend beyond their use to create products, and are also held for hedging against inflation. We will see more on this in a future blog.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;u&gt;Agriculture&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Agriculture include &apos;soft&apos; commodities that are grown, such as grains and coffee, as well as livestock. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Grains are widely consumed and include wheat, corn, soybeans, rice and oats. Livestock includes animals that are grown for meat production, accessory production (such as wool or worm casting) or human consumption (such as dairy products, honey or leather).  
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;u&gt;Speciality Commodities&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The last commodity subdivision captures unique products and derivatives such as forest production, carbon emissions, weather and freight. 
&lt;/p&gt;
&lt;p&gt;
Forest production includes pulp and recovered paper. Trees can be used to create wood pulp, that is then used to create paper.  
&lt;/p&gt;
&lt;p&gt;
Carbon emissions are government-granted allotments of carbon dioxide outputs that are granted to companies. The carbon credits enable the companies to emit a specific number of emissions, therefore those below their threshold (such as Tesla) are able to sell their credits to enable other companies to manufacture more. 
&lt;/p&gt;
&lt;p&gt;
Weather as a derivative is interesting...This can be used to mitigate the risk of unfavourable weather in specific markets. Weather derivatives can be used to value good and bad weather in specific regions. Entities can then trade these to hedge the risk from unfavourable weather to their business. 
&lt;/p&gt;
&lt;p&gt;
Lastly, freight derivatives are derived from the current and future value of freight rates. Freight is used to transport commodities and can be used by supply chains to hedge to price volatility risk associated with transport.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;What makes commodities unique&lt;/h4&gt;
&lt;p&gt;
The commodities market differentiates itself from other asset classes as they operate in both a physical and financial market. There are financial derivatives for commodities such as options, futures and swaps, however the underlying products represent physical products that must be grown/extracted, transported and stored along an agreed timeline. 
&lt;/p&gt;
&lt;p&gt;
The physical market consists of 3 main participants: producers, refiners and consumers.
Producers are responsible for sourcing the raw commodity, refiners will transform the commodity into a consumable product (such as transforming oil into gasoline), and the consumers make use of the product. These products, similar to stocks, come in different grades by quality. The supply chain is extended further to include participants for purpose-built storage facilities, and transportation to account for the transfer of ownership for the commodity.
&lt;/p&gt;
&lt;p&gt;
Unlike financial derivatives, commodities cannot be created and placed in a deal book in an instance. There is a timeline for it&apos;s creation, production and delivery. This timeline exposes multiple participants to a price risk. For example, the farmer who grows grain will be exposed to future demand and subsequent price volatility. The refiner will be exposed to the cost of delivery and storage to their facility, and the consumer will be exposed to all of these compounded fees.
&lt;/p&gt;
&lt;p&gt;
As a result of the vast number of participants and timeline for product delivery, price discovery plays a major role to all members of the commodities supply-chain.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;A common challenge in Commodities&lt;/h4&gt;
&lt;p&gt;
As with all asset classes, commodities presents their own unique challenges for market participants and financial institutions. One challenge I will repeatedly refer to within my blogs are price discovery. 
&lt;/p&gt;
&lt;p&gt;
Price discovery is a term that will enable all market participants to understand what a &apos;fair price&apos; is for the product they are buying. Each participant in the supply chain will be exposed to unique price factors that can influence the value of the commodity as it is extracted, refined and transported. Some factors can include operational costs such as credit protection and transportation and storage fees. As well as the availability of the commodity in the region it is sourced, and also at a grade quality that is expected.
&lt;/p&gt;
&lt;p&gt; 
Commodity markets are vast with different qualities of each product, meaning markets are not always liquid enough to provide natural price discovery via an exchange.
&lt;/p&gt;
&lt;p&gt;
A solution to this challenge exists in the form of a price reporting agency (PRA). This is an independent research provider that provides pricing indexes for commodities, thus bringing price transparency to physical commodity markets. The price indexes are available from the agency through a subscription and can be used to understand prices across entire commodity segments. 
&lt;/p&gt;
&lt;p&gt;
Popular PRA&apos;s include IOSCO and Platts, who are responsible for deriving commodity index markets. These indexes can then be used for commercial contracts and wholesale commodity deals.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
This blog has taken a high level view to the complexities of commodity derivatives; their vast array of products, participants and markets introduces many opportunities for financial services to speculate and hedge risk for their clients and stakeholders. 
&lt;/p&gt;
&lt;p&gt;
What makes commodities so valuable to learn about is their representation in real life. The energy sources and raw material that are traded are all around us. This extends to infrastructure, devices, transportation, food and homes; all of which are products of a commodity that has been grown, extracted, refined, transported and stored, all with the help of financial institutions. 
&lt;/p&gt;
&lt;p&gt;
I look forward to writing my next blog, where I look at the various roles financial institutions play for commodity market participants, and the difference between traded and non-traded commodities.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Authentication with Mongo DB Part 5]]></title><description><![CDATA[Add a logout and auto-login functionality using the local storage from the browser.]]></description><link>https://aneesh.co.uk/authentication-with-mongo-db-part-5</link><guid isPermaLink="false">https://aneesh.co.uk/authentication-with-mongo-db-part-5</guid><pubDate>Fri, 27 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Auto-authentication&lt;/h4&gt;
&lt;p&gt;
This video completes our mongodb authentication functionality by creating logout functionality. We will also
explore the local storage capability of our browser that will allow us to persist our web token to enable
automatic authentication when a user leaves the web application. 
&lt;p&gt;Link to learn more about local storage:
&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage&quot;&gt;https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Join along with this tutorial with the source code below:
There you will find a before and after folder that contains the code at the stand and end of the video.
Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-5&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-5&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/7WzkHXGyTBU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABUBAQEAAAAAAAAAAAAAAAAAAAEA/9oADAMBAAIQAxAAAAGcsTsDif/EABkQAAMBAQEAAAAAAAAAAAAAAAECAwAREv/aAAgBAQABBQICbrSctUeKLRkysTgx5//EABcRAQADAAAAAAAAAAAAAAAAAAABESH/2gAIAQMBAT8ByFv/xAAWEQEBAQAAAAAAAAAAAAAAAAAAEjH/2gAIAQIBAT8B1L//xAAdEAACAQQDAAAAAAAAAAAAAAABAgAQERIxIVFh/9oACAEBAAY/Ahm3PrQYnZ7hC6l1NoWO6f/EABoQAQEBAQADAAAAAAAAAAAAAAERADEhUfH/2gAIAQEAAT8hZeLnDAkaIy4Y0Sj3k2s+NYlfXfI3/9oADAMBAAIAAwAAABBID//EABcRAQEBAQAAAAAAAAAAAAAAACEAAaH/2gAIAQMBAT8QIztqL//EABcRAAMBAAAAAAAAAAAAAAAAAAABITH/2gAIAQIBAT8QQUaf/8QAHBABAAMBAAMBAAAAAAAAAAAAAQARITFBYXGx/9oACAEBAAE/EL7ndFrzlZyWEmL2DKlMHKVvIhA1kDkbsDqF8gBo/Q/s/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/a9da7041f90d5e58f64402c8e6717721/f93b5/118_auth5.jpg&quot;
        srcset=&quot;/static/a9da7041f90d5e58f64402c8e6717721/8ddbc/118_auth5.jpg 75w,
/static/a9da7041f90d5e58f64402c8e6717721/75985/118_auth5.jpg 150w,
/static/a9da7041f90d5e58f64402c8e6717721/f93b5/118_auth5.jpg 300w,
/static/a9da7041f90d5e58f64402c8e6717721/20e5d/118_auth5.jpg 450w,
/static/a9da7041f90d5e58f64402c8e6717721/b4294/118_auth5.jpg 600w,
/static/a9da7041f90d5e58f64402c8e6717721/d40e0/118_auth5.jpg 3818w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Authentication with Mongo DB Part 4]]></title><description><![CDATA[Make UI enhancements to help users understand their options when authenticated and not.]]></description><link>https://aneesh.co.uk/authentication-with-mongo-db-part-4</link><guid isPermaLink="false">https://aneesh.co.uk/authentication-with-mongo-db-part-4</guid><pubDate>Fri, 20 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;UI Authentication enhancements&lt;/h4&gt;
&lt;p&gt;
This video will enhance the user interface and experience with authentication. Visual components will 
only become available if a user is authenticated to help the user understand what they can and cannot do. 
We will also create a route guard to prevent any unwanted navigation from users through the URL. 
&lt;p&gt;Join along with this tutorial with the source code below:
There you will find a before and after folder that contains the code at the stand and end of the video.
Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-4&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-4&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/0z3EtI_jngA&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAEDAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABUefQUaLn/8QAGRAAAwEBAQAAAAAAAAAAAAAAAQIDABES/9oACAEBAAEFAgIutEjqjzQOyZCd07//xAAXEQADAQAAAAAAAAAAAAAAAAAAARFR/9oACAEDAQE/AasKf//EABgRAAIDAAAAAAAAAAAAAAAAAAABAhEh/9oACAECAQE/AVHSj//EAB8QAAIBAgcAAAAAAAAAAAAAAAABAhAhERIiMUFRYf/aAAgBAQAGPwJOcr+yI5XdvsaWxpeA3zT/xAAbEAEAAwEAAwAAAAAAAAAAAAABABEhMVFx0f/aAAgBAQABPyFlGPRMzmXuiCzUeZaNhyKH6dYVHyf/2gAMAwEAAgADAAAAEATP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAhEf/aAAgBAwEBPxBI5HWX/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAERMWH/2gAIAQIBAT8QksxbP//EABsQAQADAQADAAAAAAAAAAAAAAEAESExQWFx/9oACAEBAAE/EEMK7NV5ys5BOMEPYZH15itjSoLIHIyA7gL5DbD9DP/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/6847b6d2ef8211061afd796203b72634/f93b5/117_auth4.jpg&quot;
        srcset=&quot;/static/6847b6d2ef8211061afd796203b72634/8ddbc/117_auth4.jpg 75w,
/static/6847b6d2ef8211061afd796203b72634/75985/117_auth4.jpg 150w,
/static/6847b6d2ef8211061afd796203b72634/f93b5/117_auth4.jpg 300w,
/static/6847b6d2ef8211061afd796203b72634/20e5d/117_auth4.jpg 450w,
/static/6847b6d2ef8211061afd796203b72634/b4294/117_auth4.jpg 600w,
/static/6847b6d2ef8211061afd796203b72634/4a95b/117_auth4.jpg 2598w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Authentication with Mongo DB Part 3]]></title><description><![CDATA[Attach a generated web token to a request and use it to verify user requests.]]></description><link>https://aneesh.co.uk/authentication-with-mongo-db-part-3</link><guid isPermaLink="false">https://aneesh.co.uk/authentication-with-mongo-db-part-3</guid><pubDate>Mon, 09 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Token Verification&lt;/h4&gt;
&lt;p&gt;
This video will look at how we can automatically enrich http requests with a web token once it has been generated in TypeScript. 
The enriched HTTP request can then be used for to verify user requests with Express JS before they are completed.
&lt;p&gt;Join along with this tutorial with the source code below:
There you will find a before and after folder that contains the code at the stand and end of the video.
Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-3&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-3&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/ZEBRsSfobeM&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwX/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABWrPcZHLn/8QAGRAAAwEBAQAAAAAAAAAAAAAAAQIDABES/9oACAEBAAEFAgJOtEjqjxQOy5CcGPP/xAAXEQADAQAAAAAAAAAAAAAAAAAAARFR/9oACAEDAQE/AasKf//EABYRAQEBAAAAAAAAAAAAAAAAAAASIf/aAAgBAgEBPwGdS//EAB8QAAIBAgcAAAAAAAAAAAAAAAABAhIhEBEiMUFRYf/aAAgBAQAGPwKNcr+yFS7t9jUdjS8hvnD/xAAbEAEAAwEAAwAAAAAAAAAAAAABABExIUFR8f/aAAgBAQABPyFuTjMIYPMjuiGiUe4jdjIq9teZ8if/2gAMAwEAAgADAAAAENQP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAhEf/aAAgBAwEBPxBI5HWX/8QAGBEAAwEBAAAAAAAAAAAAAAAAAAExESH/2gAIAQIBAT8QWqYuKf/EABwQAQACAwADAAAAAAAAAAAAAAEAESExQWFxsf/aAAgBAQABPxBojem13HNQpVhHkMSmDlKzqPmoWQNTKk8oF1AAsfYfs//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/5ff4987aab7c9d2a3eb15bec5947d662/f93b5/116_auth3.jpg&quot;
        srcset=&quot;/static/5ff4987aab7c9d2a3eb15bec5947d662/8ddbc/116_auth3.jpg 75w,
/static/5ff4987aab7c9d2a3eb15bec5947d662/75985/116_auth3.jpg 150w,
/static/5ff4987aab7c9d2a3eb15bec5947d662/f93b5/116_auth3.jpg 300w,
/static/5ff4987aab7c9d2a3eb15bec5947d662/20e5d/116_auth3.jpg 450w,
/static/5ff4987aab7c9d2a3eb15bec5947d662/b4294/116_auth3.jpg 600w,
/static/5ff4987aab7c9d2a3eb15bec5947d662/c296a/116_auth3.jpg 3813w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Authentication with Mongo DB Part 2]]></title><description><![CDATA[Create web tokens with the JWT package when authenticating users with MongoDB and Angular.]]></description><link>https://aneesh.co.uk/authentication-with-mongo-db-part-2</link><guid isPermaLink="false">https://aneesh.co.uk/authentication-with-mongo-db-part-2</guid><pubDate>Sat, 31 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Logging in and creating web tokens&lt;/h4&gt;
&lt;p&gt;
Learn how you can create web tokens with the jwt package in express js. This video will enhance our MEAN application to 
verify the details of a user when they submit the login form, and return with either a generated web token from the jwt package, or with an exception for an incorrect username or password. 
This video is part 2 of a series of videos to create authentication with Angular and MongoDB.
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-2&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/46NmmjsFi44&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQX/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/2gAMAwEAAhADEAAAAYpz6GjGp//EABgQAQEBAQEAAAAAAAAAAAAAAAIBAwAR/9oACAEBAAEFAjM2NM8u1MDjR4q9FfP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQMBAT8BxX//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAgEBPwFH/8QAHhAAAQQBBQAAAAAAAAAAAAAAAQACERIhEDFBUXH/2gAIAQEABj8Cbd2fUKnM9ogbKWmETzp//8QAGxABAAMAAwEAAAAAAAAAAAAAAQARMSFBUaH/2gAIAQEAAT8hesGMgryiO6Ihaj2aIRlra7gNfJ//2gAMAwEAAgADAAAAEO/P/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAEhMf/aAAgBAwEBPxCVg6h//8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARMSH/2gAIAQIBAT8QC3Yc7P/EABsQAQEBAQEAAwAAAAAAAAAAAAERADFhIXGx/9oACAEBAAE/EHURDHHnJ4YYNUj0GTQLpHrmdpjKBzN2H8oFxwh+w/u//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/e0d970dbf1eeaa93d19d5ec17c1cb169/f93b5/115_auth2.jpg&quot;
        srcset=&quot;/static/e0d970dbf1eeaa93d19d5ec17c1cb169/8ddbc/115_auth2.jpg 75w,
/static/e0d970dbf1eeaa93d19d5ec17c1cb169/75985/115_auth2.jpg 150w,
/static/e0d970dbf1eeaa93d19d5ec17c1cb169/f93b5/115_auth2.jpg 300w,
/static/e0d970dbf1eeaa93d19d5ec17c1cb169/20e5d/115_auth2.jpg 450w,
/static/e0d970dbf1eeaa93d19d5ec17c1cb169/b4294/115_auth2.jpg 600w,
/static/e0d970dbf1eeaa93d19d5ec17c1cb169/c2dd6/115_auth2.jpg 3891w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Authentication with Mongo DB Part 1]]></title><description><![CDATA[Learn how to authenticate users with MongoDB. This part will focus on signing up new users to the database.]]></description><link>https://aneesh.co.uk/authentication-with-mongo-db-part-1</link><guid isPermaLink="false">https://aneesh.co.uk/authentication-with-mongo-db-part-1</guid><pubDate>Mon, 05 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Authentication with MongoDB Part 1&lt;/h4&gt;
&lt;p&gt;
This video will look at how we can sign up users to our application and to store their details with MongoDB.
Our application will allow users to sign up with a new sign up form built within Angular. We will then connect the form
with an express endpoint to MongoDB. We will also verify accounts are unique and apply a hash to the password to help protect user information.
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-1&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/mongodb-auth-1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/Jew_t1mXJxw&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABgnBRdGGf/8QAGBABAQEBAQAAAAAAAAAAAAAAAgEAAxH/2gAIAQEAAQUCMDD5891MDjRwV0V8/8QAFhEBAQEAAAAAAAAAAAAAAAAAABFh/9oACAEDAQE/Abiv/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEx/9oACAECAQE/AdR//8QAHhAAAgEDBQAAAAAAAAAAAAAAAAECERJBECExUXH/2gAIAQEABj8CjdLf0Vss9jS4KxdBvOn/xAAbEAEAAwEAAwAAAAAAAAAAAAABABExIUFRof/aAAgBAQABPyF6yYyWoWkavyIWo9zRCItevMBr5P/aAAwDAQACAAMAAAAQ68//xAAXEQADAQAAAAAAAAAAAAAAAAAAASER/9oACAEDAQE/EMJB1D//xAAYEQACAwAAAAAAAAAAAAAAAAAAASExQf/aAAgBAgEBPxBYFGz/xAAdEAEBAAIBBQAAAAAAAAAAAAABEQAhMUFhcYGx/9oACAEBAAE/EDxxGnPU7GXwOWh1MmQLpHlxjtMZScYlYTaBcOEPkP3P/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/12066829f913e4ebbd185d03894352ef/f93b5/114_auth1.jpg&quot;
        srcset=&quot;/static/12066829f913e4ebbd185d03894352ef/8ddbc/114_auth1.jpg 75w,
/static/12066829f913e4ebbd185d03894352ef/75985/114_auth1.jpg 150w,
/static/12066829f913e4ebbd185d03894352ef/f93b5/114_auth1.jpg 300w,
/static/12066829f913e4ebbd185d03894352ef/20e5d/114_auth1.jpg 450w,
/static/12066829f913e4ebbd185d03894352ef/b4294/114_auth1.jpg 600w,
/static/12066829f913e4ebbd185d03894352ef/c2dd6/114_auth1.jpg 3891w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Enhancements with Java 14]]></title><description><![CDATA[The syntax changes introduced with Java 14]]></description><link>https://aneesh.co.uk/enhancements-with-java-14</link><guid isPermaLink="false">https://aneesh.co.uk/enhancements-with-java-14</guid><pubDate>Sat, 29 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java 14 Enhancements&lt;/h4&gt;
&lt;p&gt;
This video will look at the two key features introduced with Java 14 for the developer. These include
the improved syntax for switch statements, and the detailed feedback provided with a null pointer exception. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/0X_CMJj1tMo&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAwAE/8QAFgEBAQEAAAAAAAAAAAAAAAAAAgAB/9oADAMBAAIQAxAAAAHKYPsNqkf/xAAaEAACAwEBAAAAAAAAAAAAAAACAwABEhMx/9oACAEBAAEFAlB1jF0LGjg9WMXeoPn/xAAWEQEBAQAAAAAAAAAAAAAAAAAAERL/2gAIAQMBAT8BrT//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEjH/2gAIAQIBAT8B1D//xAAaEAEAAgMBAAAAAAAAAAAAAAABACECEBEx/9oACAEBAAY/AlyYHaYh5KeRW3X/xAAdEAEAAQMFAAAAAAAAAAAAAAABABEhcTFBUYHR/9oACAEBAAE/IRuali9J4nEBWo5hrvBiB+xVnbmf/9oADAMBAAIAAwAAABCj3//EABgRAQADAQAAAAAAAAAAAAAAAAEAESEx/9oACAEDAQE/EGhyIvJ//8QAFxEBAQEBAAAAAAAAAAAAAAAAAQARMf/aAAgBAgEBPxAFZCzt/8QAGxABAQADAQEBAAAAAAAAAAAAAREAITFRobH/2gAIAQEAAT8QM4CWz85gvJGo3w3WT7pGLzA3dO9FxswwPUI4BCgvRX7n/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/0d2706f7c2412205eeca8ecf97bf79c5/f93b5/113_java14.jpg&quot;
        srcset=&quot;/static/0d2706f7c2412205eeca8ecf97bf79c5/8ddbc/113_java14.jpg 75w,
/static/0d2706f7c2412205eeca8ecf97bf79c5/75985/113_java14.jpg 150w,
/static/0d2706f7c2412205eeca8ecf97bf79c5/f93b5/113_java14.jpg 300w,
/static/0d2706f7c2412205eeca8ecf97bf79c5/20e5d/113_java14.jpg 450w,
/static/0d2706f7c2412205eeca8ecf97bf79c5/b4294/113_java14.jpg 600w,
/static/0d2706f7c2412205eeca8ecf97bf79c5/3fff8/113_java14.jpg 3836w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to verify static method calls]]></title><description><![CDATA[Subtitle]]></description><link>https://aneesh.co.uk/how-to-verify-static-method-calls</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-verify-static-method-calls</guid><pubDate>Sat, 22 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;How to verify static method calls&lt;/h4&gt;
&lt;p&gt;
Learn how you can verify that a static method has been called with PowerMock!
This video will look at why Mockito is unable to verify static method calls. We will then 
review the PowerMock dependencies required, and write a method that verifies if a static method is called
and the number of times it is invoked.
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/VerifyStatic&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/VerifyStatic&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/w12cuVpqf7w&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAABAACA//EABYBAQEBAAAAAAAAAAAAAAAAAAIAAf/aAAwDAQACEAMQAAABLkqNuMqR/8QAGRABAQEAAwAAAAAAAAAAAAAAAgEDABIx/9oACAEBAAEFAszNZpmC9T0cVMztXD5//8QAFhEBAQEAAAAAAAAAAAAAAAAAABES/9oACAEDAQE/Aa0//8QAFhEBAQEAAAAAAAAAAAAAAAAAABIx/9oACAECAQE/AdQ//8QAGhABAAMBAQEAAAAAAAAAAAAAAQARIQIQUf/aAAgBAQAGPwKm7Ic7f2IbMai9a+f/xAAbEAEAAwADAQAAAAAAAAAAAAABABEhMUFRcf/aAAgBAQABPyEsLDnKlASk05AV0JpK3jLBIYLKdR+z/9oADAMBAAIAAwAAABCr3//EABgRAQADAQAAAAAAAAAAAAAAAAEAESEx/9oACAEDAQE/EGhyIvJ//8QAFxEBAQEBAAAAAAAAAAAAAAAAAREAMf/aAAgBAgEBPxAqmFO7/8QAGxABAQACAwEAAAAAAAAAAAAAAREAITFBUaH/2gAIAQEAAT8Qtuu2CdcFw30oDV80YvWKKF47ygeANC4bJRGob4wIKj0V+5//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/f93b5/112_verifystatic.jpg&quot;
        srcset=&quot;/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/8ddbc/112_verifystatic.jpg 75w,
/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/75985/112_verifystatic.jpg 150w,
/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/f93b5/112_verifystatic.jpg 300w,
/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/20e5d/112_verifystatic.jpg 450w,
/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/b4294/112_verifystatic.jpg 600w,
/static/7f98cc66ded1d7f83c5dccf6ee51aa3f/be0d0/112_verifystatic.jpg 3923w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to use @RepeatedTest]]></title><description><![CDATA[Learn how to use the Repeated Test annotation ]]></description><link>https://aneesh.co.uk/how-to-use-repeatedtest</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-use-repeatedtest</guid><pubDate>Sat, 15 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;How to use @RepeatedTest&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;The RepeatedTest annotation enables you to run the same test multiple times. This video will look at why you may want to use the annotation, and the various ways you can customise it to provide detailed feedback in the console and within the IDE.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/HvP9ibiMpO8&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABYBAQEBAAAAAAAAAAAAAAAAAAIABP/aAAwDAQACEAMQAAABU0yzJSQ7/8QAGhABAAIDAQAAAAAAAAAAAAAAAQACAxARIf/aAAgBAQABBQLlmctoPckKgf/EABgRAQEAAwAAAAAAAAAAAAAAAAEAAxET/9oACAEDAQE/AXGBu6X/xAAaEQACAgMAAAAAAAAAAAAAAAAAAgEDBBJh/9oACAECAQE/AVtlm1Jx+n//xAAZEAABBQAAAAAAAAAAAAAAAAAQAAIhQVH/2gAIAQEABj8CuBqcf//EAB0QAQACAgIDAAAAAAAAAAAAAAEAESExQVFxgbH/2gAIAQEAAT8hsIWdskp7PLBvXtAKjmaBwzEPs//aAAwDAQACAAMAAAAQfP8A/8QAGREAAgMBAAAAAAAAAAAAAAAAAAERIUGR/9oACAEDAQE/EJib6Vw//8QAGhEBAAMAAwAAAAAAAAAAAAAAAQARcWGx4f/aAAgBAgEBPxA+ArONgLfT2f/EABsQAQEAAgMBAAAAAAAAAAAAAAERACExQZHB/9oACAEBAAE/EKRWgNTnEhdofM2qQMI5xuAWNe5uhaNBlyMKF6Wf/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/806f8f39b69cd443fc35fde8f546ac7a/f93b5/111_repeatedTest.jpg&quot;
        srcset=&quot;/static/806f8f39b69cd443fc35fde8f546ac7a/8ddbc/111_repeatedTest.jpg 75w,
/static/806f8f39b69cd443fc35fde8f546ac7a/75985/111_repeatedTest.jpg 150w,
/static/806f8f39b69cd443fc35fde8f546ac7a/f93b5/111_repeatedTest.jpg 300w,
/static/806f8f39b69cd443fc35fde8f546ac7a/20e5d/111_repeatedTest.jpg 450w,
/static/806f8f39b69cd443fc35fde8f546ac7a/b4294/111_repeatedTest.jpg 600w,
/static/806f8f39b69cd443fc35fde8f546ac7a/eea4a/111_repeatedTest.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Create a full-stack MEAN application]]></title><description><![CDATA[A complete end-to-end video on how you can create a full-stack application]]></description><link>https://aneesh.co.uk/create-a-full-stack-mean-application</link><guid isPermaLink="false">https://aneesh.co.uk/create-a-full-stack-mean-application</guid><pubDate>Sat, 01 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Create a MEAN Application&lt;/h4&gt;
&lt;p&gt;
Build a full-stack mean application from scratch, step-by-step with this video!
We will create a web application that can be used for storing, updating and deleting diary entries.
This application will use Angular (and bootstrap) for the front-end, node and express JS for the backend server, and mongo db for data persistence!
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/MyDiary&quot;&gt;https://github.com/4neesh/MyDiary&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:41 - Setting up Angular
03:22 - Adding Bootstrap 3
04:27 - Creating a HTML Table
05:50 - Angular Shared Services
07:55 - Populating data from an array
09:02 - Adding action buttons to each entry
09:47 - Deleting diary entries
11:14 - Angular Subjects
13:39 - Angular Reactive Forms for adding entries
20:07 - Editing a Diary Entry
26:12 - Splitting the Application
29:11 - Creating a node server
33:12 - Getting started with Express
37:03 - Using next()
37:56 - Registering URI&apos;s
38:44 - Migrating Data from Angular to Express
40:41 - Angular HTTP requests
48:15 - Making POST requests
52:22 - Making DELETE requests
56:22 - Making PUT requests
01:00:00 - Setting up MongoDB
01:02:30 - Using Mongoose
01:06:10 - Connection our app to MongoDB
01:08:11 - Saving to MongoDB
01:08:56 - MongoDB Shell
01:10:32 - Fetching data with MongoDB
01:13:33 - Deleting from MongoDB
01:15:43 - Editing data with MongoDB&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/ZictJujePhs&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgT/xAAWAQEBAQAAAAAAAAAAAAAAAAACAAT/2gAMAwEAAhADEAAAAVzqVnVRQr//xAAYEAADAQEAAAAAAAAAAAAAAAABAhEAEP/aAAgBAQABBQKaHjItfBQB/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAEDERP/2gAIAQMBAT8BcdI0P//EABsRAAIBBQAAAAAAAAAAAAAAAAABAgMEERJh/9oACAECAQE/AYVt3gdv0//EABgQAAIDAAAAAAAAAAAAAAAAAAAQMVHh/9oACAEBAAY/AtVkP//EAB0QAAICAQUAAAAAAAAAAAAAAAABESExQVGBobH/2gAIAQEAAT8hbTa7ChhY3kTnHItMoFpLRlQ9P//aAAwDAQACAAMAAAAQSP8A/8QAGhEAAgIDAAAAAAAAAAAAAAAAAAERIUGR8P/aAAgBAwEBPxBUzfaK4P/EABoRAAICAwAAAAAAAAAAAAAAAAABEeExYbH/2gAIAQIBAT8QglWdWIbniz//xAAbEAEBAQADAQEAAAAAAAAAAAABEQAhMVFBYf/aAAgBAQABPxC3X9uCqkesNyqQMI71qfIUX3F3xEQe9GFC/Fv/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/709b26a6b9d60a67050b46835fd98b12/f93b5/110_mean6.jpg&quot;
        srcset=&quot;/static/709b26a6b9d60a67050b46835fd98b12/8ddbc/110_mean6.jpg 75w,
/static/709b26a6b9d60a67050b46835fd98b12/75985/110_mean6.jpg 150w,
/static/709b26a6b9d60a67050b46835fd98b12/f93b5/110_mean6.jpg 300w,
/static/709b26a6b9d60a67050b46835fd98b12/20e5d/110_mean6.jpg 450w,
/static/709b26a6b9d60a67050b46835fd98b12/b4294/110_mean6.jpg 600w,
/static/709b26a6b9d60a67050b46835fd98b12/eea4a/110_mean6.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a MEAN application: Part 5]]></title><description><![CDATA[Part 5 of the MEAN application series: set up and connect to mongodb to persist data.]]></description><link>https://aneesh.co.uk/creating-a-mean-application-part-5</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-mean-application-part-5</guid><pubDate>Sat, 24 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;MEAN Application: Part 5&lt;/h4&gt;
&lt;p&gt;
Part 5 of this MEAN Application series will complete our application with a MongoDB database. We are going to create an account with MongoDB and a cluster to store our data within. We will also use the mongoose package to define a schema for our data, before connecting to MongoDB and persisting the data. We will complete the full-stack functionality by enabling POST, PUT and DELETE functionality with the database and application together.
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/MyDiary&quot;&gt;https://github.com/4neesh/MyDiary&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:00 - Introduction
00:26 - Setting up MongoDB
02:54 - Using Mongoose
06:36 - Connection our app to MongoDB
08:38 - Saving to MongoDB
09:22 - MongoDB Shell
10:58 - Fetching data with MongoDB
13:58 - Deleting from MongoDB
16:10 - Editing data with MongoDB&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/ttF2oG0EA2U&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQT/xAAWAQEBAQAAAAAAAAAAAAAAAAACAAT/2gAMAwEAAhADEAAAAU3plnSkhX//xAAZEAEBAAMBAAAAAAAAAAAAAAACAQAQERL/2gAIAQEAAQUC8rOLSB68hkn/xAAYEQACAwAAAAAAAAAAAAAAAAAAAQMRE//aAAgBAwEBPwFx0jQ//8QAGxEAAgEFAAAAAAAAAAAAAAAAAAECAwQREmH/2gAIAQIBAT8BhW3eB2/T/8QAGBAAAgMAAAAAAAAAAAAAAAAAEDEAUaH/2gAIAQEABj8CWi4j/8QAHBAAAgMAAwEAAAAAAAAAAAAAAAERITFBgZGx/9oACAEBAAE/IXJoJFX6JznYtNQLSXDKh9P/2gAMAwEAAgADAAAAEAT/AP/EABoRAAICAwAAAAAAAAAAAAAAAAABESFBofD/2gAIAQMBAT8QVI+0Vwf/xAAaEQEAAgMBAAAAAAAAAAAAAAABAOERIWGx/9oACAECAQE/EEEBvlwFz4uf/8QAGhABAQEBAQEBAAAAAAAAAAAAAREAITFBUf/aAAgBAQABPxCxe8rWC4Afa5uqkDCPdan0KL+4paIiD7owoX4t/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/678c473529111579212986bbc12d8da7/f93b5/109_mean5.jpg&quot;
        srcset=&quot;/static/678c473529111579212986bbc12d8da7/8ddbc/109_mean5.jpg 75w,
/static/678c473529111579212986bbc12d8da7/75985/109_mean5.jpg 150w,
/static/678c473529111579212986bbc12d8da7/f93b5/109_mean5.jpg 300w,
/static/678c473529111579212986bbc12d8da7/20e5d/109_mean5.jpg 450w,
/static/678c473529111579212986bbc12d8da7/b4294/109_mean5.jpg 600w,
/static/678c473529111579212986bbc12d8da7/eea4a/109_mean5.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a MEAN application: Part 4]]></title><description><![CDATA[Part 4 of the MEAN application series: expand express js to make PUT and DELETE requests.]]></description><link>https://aneesh.co.uk/creating-a-mean-application-part-4</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-mean-application-part-4</guid><pubDate>Sat, 17 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;MEAN Application: Part 4&lt;/h4&gt;
&lt;p&gt;
Part 4 of this MEAN Application series will build-out our Express middleware to provide complete backend functionality to our application. We currently use the backend service to obtain the diary entries for our app. This video will expand the functionality so we can perform PUT, POST and DELETE requests to our backend server so we can update, edit and remove entries. We will enhance the data service in the Angular single-page application to make HTTP requests to the Node backend service where data is stored.
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/MyDiary&quot;&gt;https://github.com/4neesh/MyDiary&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:00 - Introduction
00:14 - Making POST requests
04:26 - Making DELETE requests
08:24 - Making PUT requests&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/cy1L95FFPyA&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIDAQT/xAAWAQEBAQAAAAAAAAAAAAAAAAACAAT/2gAMAwEAAhADEAAAAU3plmSkh3//xAAZEAEBAQADAAAAAAAAAAAAAAACAQAQESL/2gAIAQEAAQUCpW9cIHt6GSf/xAAYEQACAwAAAAAAAAAAAAAAAAAAAgERE//aAAgBAwEBPwFkqDQ//8QAGhEAAgIDAAAAAAAAAAAAAAAAAAECAwQRYf/aAAgBAgEBPwGFrm9Dx+n/xAAYEAACAwAAAAAAAAAAAAAAAAAQMQBRof/aAAgBAQAGPwJaLiP/xAAdEAACAgIDAQAAAAAAAAAAAAAAAREhMUFRgZGx/9oACAEBAAE/IeSEbz6JzjsWmUC0lplQ+n//2gAMAwEAAgADAAAAEAz/AP/EABkRAAIDAQAAAAAAAAAAAAAAAAABESFBsf/aAAgBAwEBPxBMj6Vw/8QAGhEBAAIDAQAAAAAAAAAAAAAAAQDhESFhsf/aAAgBAgEBPxAyA3y4C58XP//EABwQAQACAwADAAAAAAAAAAAAAAEAESExQVGx8P/aAAgBAQABPxAa3l39ZXgEO6e5lWQNFNy6ryFi+YQexEQdymFC+Kf/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/e3170a8b2d2d6a4ae81440c4387a8d2c/f93b5/108_mean4.jpg&quot;
        srcset=&quot;/static/e3170a8b2d2d6a4ae81440c4387a8d2c/8ddbc/108_mean4.jpg 75w,
/static/e3170a8b2d2d6a4ae81440c4387a8d2c/75985/108_mean4.jpg 150w,
/static/e3170a8b2d2d6a4ae81440c4387a8d2c/f93b5/108_mean4.jpg 300w,
/static/e3170a8b2d2d6a4ae81440c4387a8d2c/20e5d/108_mean4.jpg 450w,
/static/e3170a8b2d2d6a4ae81440c4387a8d2c/b4294/108_mean4.jpg 600w,
/static/e3170a8b2d2d6a4ae81440c4387a8d2c/eea4a/108_mean4.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a MEAN application: Part 3]]></title><description><![CDATA[Part 3 of the MEAN application series: add express js to our node server.]]></description><link>https://aneesh.co.uk/creating-a-mean-application-part-3</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-mean-application-part-3</guid><pubDate>Sat, 03 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;MEAN Application: Part 3&lt;/h4&gt;
&lt;p&gt;
Part 3 of our MEAN application series will incorporate Express.js into our application.
Express will allow us to add valuable middleware into our backend service to register different
endpoints for our backend service to provide unique data responses to.
We will begin to connect our Angular static webpages to the backend by registering URI&apos;s with express
and by formatting and returning data. In Angular, we will use the http package to make back-end calls to the node server to retrieve the data. 
We also dive into CORS policy and what it means to our application.
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/MyDiary&quot;&gt;https://github.com/4neesh/MyDiary&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:00 - Introduction
00:22 - Getting started with Express
04:08 - Using next()
05:03 - Registering URI&apos;s
05:50 - Migrating Data from Angular to Express
07:48 - Angular HTTP requests&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/Qog0zUFLc-k&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgT/xAAWAQEBAQAAAAAAAAAAAAAAAAACAAT/2gAMAwEAAhADEAAAAVzqVnVRQr//xAAYEAEBAQEBAAAAAAAAAAAAAAABAgAREP/aAAgBAQABBQJHcryont4kD//EABgRAAIDAAAAAAAAAAAAAAAAAAABAxET/9oACAEDAQE/AXHSND//xAAbEQACAQUAAAAAAAAAAAAAAAAAAQIDBBESYf/aAAgBAgEBPwGFbd4Hb9P/xAAYEAACAwAAAAAAAAAAAAAAAAAQMQBRof/aAAgBAQAGPwJaLiP/xAAbEAACAwEBAQAAAAAAAAAAAAAAAREhMYFBsf/aAAgBAQABPyGXsJFQ10TnOi01AtJeMqH0/9oADAMBAAIAAwAAABDE/wD/xAAaEQACAgMAAAAAAAAAAAAAAAAAAREhQaHw/9oACAEDAQE/EFTPtFcH/8QAGhEBAAIDAQAAAAAAAAAAAAAAAQDhESFhsf/aAAgBAgEBPxBDIb5cBc+Ln//EABoQAQEBAQEBAQAAAAAAAAAAAAERIQAxQVH/2gAIAQEAAT8QGlfN1vEQD2qztVIGEe9anoUX94p6IiD70YUL8Xf/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/d329bfa7e7b96a38842e43a731cb3edc/f93b5/107_mean3.jpg&quot;
        srcset=&quot;/static/d329bfa7e7b96a38842e43a731cb3edc/8ddbc/107_mean3.jpg 75w,
/static/d329bfa7e7b96a38842e43a731cb3edc/75985/107_mean3.jpg 150w,
/static/d329bfa7e7b96a38842e43a731cb3edc/f93b5/107_mean3.jpg 300w,
/static/d329bfa7e7b96a38842e43a731cb3edc/20e5d/107_mean3.jpg 450w,
/static/d329bfa7e7b96a38842e43a731cb3edc/b4294/107_mean3.jpg 600w,
/static/d329bfa7e7b96a38842e43a731cb3edc/eea4a/107_mean3.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a MEAN application: Part 2]]></title><description><![CDATA[Part 2 of the MEAN application series: build a node server to later provide a data service.]]></description><link>https://aneesh.co.uk/creating-a-mean-application-part-2</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-mean-application-part-2</guid><pubDate>Sun, 28 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;MEAN Application: Part 2&lt;/h4&gt;
&lt;p&gt;
This part of the MEAN series will create the backend server using Node.js.
We will start by testing node and then creating a server that will respond to all requests
using the methods that come out of the box with the http modules from node. 
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/MyDiary&quot;&gt;https://github.com/4neesh/MyDiary&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:00 - Introduction
00:15 - Application overview
03:13 - Creating a node server&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/CbWZt_c142g&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABYBAQEBAAAAAAAAAAAAAAAAAAIABP/aAAwDAQACEAMQAAABm2mWdKSFf//EABgQAQEBAQEAAAAAAAAAAAAAAAECABEQ/9oACAEBAAEFAkdyvKie3iQP/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAEDERP/2gAIAQMBAT8BcdI0P//EABsRAAIBBQAAAAAAAAAAAAAAAAABAgMEERJh/9oACAECAQE/AYVt3gdv0//EABgQAAIDAAAAAAAAAAAAAAAAABAxAFGh/9oACAEBAAY/AlouI//EABwQAAICAgMAAAAAAAAAAAAAAAABESExgUGRsf/aAAgBAQABPyGXn2EhRG5E5xsWmUC0lwyoen//2gAMAwEAAgADAAAAEIT/AP/EABoRAAICAwAAAAAAAAAAAAAAAAARASFBkfD/2gAIAQMBAT8QiY+0Vwf/xAAaEQEAAgMBAAAAAAAAAAAAAAABAOERIWGx/9oACAECAQE/EEEhvlwFz4uf/8QAGxABAQADAQEBAAAAAAAAAAAAAREAITFBUYH/2gAIAQEAAT8QG1479MUBBel+ubVIGEdy1PYUX7hT0REHuRhQviz/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/ac1b7e765a1310b122c453b3269f0fb2/f93b5/106_mean2.jpg&quot;
        srcset=&quot;/static/ac1b7e765a1310b122c453b3269f0fb2/8ddbc/106_mean2.jpg 75w,
/static/ac1b7e765a1310b122c453b3269f0fb2/75985/106_mean2.jpg 150w,
/static/ac1b7e765a1310b122c453b3269f0fb2/f93b5/106_mean2.jpg 300w,
/static/ac1b7e765a1310b122c453b3269f0fb2/20e5d/106_mean2.jpg 450w,
/static/ac1b7e765a1310b122c453b3269f0fb2/b4294/106_mean2.jpg 600w,
/static/ac1b7e765a1310b122c453b3269f0fb2/eea4a/106_mean2.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a MEAN application: Part 1]]></title><description><![CDATA[Part 1 of the MEAN application series: build a basic Angular app with update, insert and delete functions.]]></description><link>https://aneesh.co.uk/creating-a-mean-application-part-1</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-mean-application-part-1</guid><pubDate>Sat, 20 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;MEAN Application: Part 1&lt;/h4&gt;
&lt;p&gt;
In this 5-part series we will look at how to create a MEAN application. 
A MEAN application uses Mongo DB, Express JS, Angular and Node JS to create a full-stack application.
This first part of the series will create the bare-bones of our angular application.
We will create a personal diary that displays our posts in a table. We will be able to add, delete and update posts within the application and to store the data in an array.
&lt;p&gt;Link to source code:
&lt;a href=&quot;https://github.com/4neesh/MyDiary&quot;&gt;https://github.com/4neesh/MyDiary&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;🕒 Timestamps 🕒&lt;/p&gt;
&lt;p&gt;00:00 - Introduction
00:41 - Setting up Angular
03:22 - Adding Bootstrap 3
04:27 - Creating a HTML Table
05:50 - Angular Shared Services
07:55 - Populating data from an array
09:02 - Adding action buttons to each entry
09:47 - Deleting diary entries
11:14 - Angular Subjects
13:39 - Angular Reactive Forms for adding entries
20:07 - Editing a Diary Entry&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/YHGm05BMt3U&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABYBAQEBAAAAAAAAAAAAAAAAAAIABP/aAAwDAQACEAMQAAABQ1SzpSQr/8QAGBABAQEBAQAAAAAAAAAAAAAAAQIAERD/2gAIAQEAAQUCR3K8qJ7eJA//xAAYEQACAwAAAAAAAAAAAAAAAAAAAQMRE//aAAgBAwEBPwFx0jQ//8QAGxEAAgEFAAAAAAAAAAAAAAAAAAECAwQREmH/2gAIAQIBAT8BhW3eB2/T/8QAGBAAAgMAAAAAAAAAAAAAAAAAEDEAUaH/2gAIAQEABj8CWi4j/8QAHBAAAgMAAwEAAAAAAAAAAAAAAAERITFBcYGx/9oACAEBAAE/IZewkVfcic56LTUC0lwyofT/2gAMAwEAAgADAAAAEET/AP/EABoRAAICAwAAAAAAAAAAAAAAAAABESFBofD/2gAIAQMBAT8QVM+0Vwf/xAAaEQEAAgMBAAAAAAAAAAAAAAABAOERIWGx/9oACAECAQE/EEMhvlwFz4uf/8QAGxABAAMBAAMAAAAAAAAAAAAAAQARITFBUWH/2gAIAQEAAT8QGl+utgCQHdMmqyBop2XVehYvuFXsREHsphQvwp//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/fd2f11c5c87f0bd1112e69e95a450f28/f93b5/105_mean1.jpg&quot;
        srcset=&quot;/static/fd2f11c5c87f0bd1112e69e95a450f28/8ddbc/105_mean1.jpg 75w,
/static/fd2f11c5c87f0bd1112e69e95a450f28/75985/105_mean1.jpg 150w,
/static/fd2f11c5c87f0bd1112e69e95a450f28/f93b5/105_mean1.jpg 300w,
/static/fd2f11c5c87f0bd1112e69e95a450f28/20e5d/105_mean1.jpg 450w,
/static/fd2f11c5c87f0bd1112e69e95a450f28/b4294/105_mean1.jpg 600w,
/static/fd2f11c5c87f0bd1112e69e95a450f28/eea4a/105_mean1.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[From Zero to Hero: Mockito & JUnit Course]]></title><description><![CDATA[A compilation video of my JUnit and Mockito tutorials]]></description><link>https://aneesh.co.uk/from-zero-to-hero-mockito-junit-course</link><guid isPermaLink="false">https://aneesh.co.uk/from-zero-to-hero-mockito-junit-course</guid><pubDate>Sat, 13 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;From Zero to Hero &lt;/h4&gt;
&lt;p&gt;
This video is a compilation of individual JUnit and Mockito tutorials published on this channel.
&lt;p&gt;Follow the course to learn all about core JUnit and Mockito skills, use the below chapters to navigate to a specific part of the course if required:&lt;/p&gt;
&lt;p&gt;Link to source code: &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/4neesh/YouTube-Channel&quot;&gt;https://github.com/4neesh/YouTube-Channel&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/0ZtU3X9n6tI&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAMC/8QAFQEBAQAAAAAAAAAAAAAAAAAAAQP/2gAMAwEAAhADEAAAAcrTiki//8QAGRAAAgMBAAAAAAAAAAAAAAAAAAECESED/9oACAEBAAEFAqkymIeHTWoqv//EABgRAAIDAAAAAAAAAAAAAAAAAAABAhET/9oACAEDAQE/AXGjQ//EABoRAAICAwAAAAAAAAAAAAAAAAABAgQREkH/2gAIAQIBAT8BjPd4HXT6f//EABkQAAIDAQAAAAAAAAAAAAAAABARAAEhQf/aAAgBAQAGPwLuTWLR/8QAGhAAAwEBAQEAAAAAAAAAAAAAAREhADFRof/aAAgBAQABPyGkBj0iJk1HpeJtUP3AxxcMDxYOB93/2gAMAwEAAgADAAAAECQf/8QAGBEBAQADAAAAAAAAAAAAAAAAAQARQZH/2gAIAQMBAT8QE5V7DNX/xAAaEQACAgMAAAAAAAAAAAAAAAAAARFxYbHh/9oACAECAQE/EHpRKKrIwnTp/8QAHRABAQACAQUAAAAAAAAAAAAAAREAITFBUWGRof/aAAgBAQABPxDYbsOI7Yc4EPWEVdQWvuAcpIp23gDd6V1hIQPCz//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/994ffc7a998055a4d2ea38702a20ddb3/f93b5/104_JunitAll.jpg&quot;
        srcset=&quot;/static/994ffc7a998055a4d2ea38702a20ddb3/8ddbc/104_JunitAll.jpg 75w,
/static/994ffc7a998055a4d2ea38702a20ddb3/75985/104_JunitAll.jpg 150w,
/static/994ffc7a998055a4d2ea38702a20ddb3/f93b5/104_JunitAll.jpg 300w,
/static/994ffc7a998055a4d2ea38702a20ddb3/20e5d/104_JunitAll.jpg 450w,
/static/994ffc7a998055a4d2ea38702a20ddb3/b4294/104_JunitAll.jpg 600w,
/static/994ffc7a998055a4d2ea38702a20ddb3/eea4a/104_JunitAll.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Syntax changes from Java 12]]></title><description><![CDATA[A combined video of all main syntax changes introduced with Java 12]]></description><link>https://aneesh.co.uk/syntax-changes-from-java-12</link><guid isPermaLink="false">https://aneesh.co.uk/syntax-changes-from-java-12</guid><pubDate>Sat, 06 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java 12 Syntax Changes&lt;/h4&gt;
&lt;p&gt;
This video covers the main syntax changes that are introduced to Java 12. 
We will look at how the String class, Files class and Collectors class have all changed
to offer new functionality and easier ways to approach unique problems. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/cKbXARepUP4&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIEAf/EABYBAQEBAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAABTaZcFyIp/8QAGxAAAQQDAAAAAAAAAAAAAAAAAQACAxEQITH/2gAIAQEAAQUColU7AG5im8//xAAYEQEBAAMAAAAAAAAAAAAAAAABAAMRE//aAAgBAwEBPwFxgbul/8QAGxEAAgEFAAAAAAAAAAAAAAAAAAIBAwQREkH/2gAIAQIBAT8BSrLtqTbZ6f/EABkQAAIDAQAAAAAAAAAAAAAAAAAQAhEh4f/aAAgBAQAGPwLpqkUv/8QAGhABAAIDAQAAAAAAAAAAAAAAAQARITFhQf/aAAgBAQABPyEuAKm47Cuwbvb2AVHsay2EiTFZ/9oADAMBAAIAAwAAABC03//EABgRAAIDAAAAAAAAAAAAAAAAAAABESFB/9oACAEDAQE/EJmbK4f/xAAaEQACAgMAAAAAAAAAAAAAAAABEQBxYaHh/9oACAECAQE/EE0AqxcYY07P/8QAGxABAQEAAgMAAAAAAAAAAAAAAREAITFBcZH/2gAIAQEAAT8QSQtCFma1F0WpuoRGC7c3ALHH3BEhlDziQA97/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/e87bf86b2f86fc83d7f4ddc3f73771b6/f93b5/103_java12_all.jpg&quot;
        srcset=&quot;/static/e87bf86b2f86fc83d7f4ddc3f73771b6/8ddbc/103_java12_all.jpg 75w,
/static/e87bf86b2f86fc83d7f4ddc3f73771b6/75985/103_java12_all.jpg 150w,
/static/e87bf86b2f86fc83d7f4ddc3f73771b6/f93b5/103_java12_all.jpg 300w,
/static/e87bf86b2f86fc83d7f4ddc3f73771b6/20e5d/103_java12_all.jpg 450w,
/static/e87bf86b2f86fc83d7f4ddc3f73771b6/b4294/103_java12_all.jpg 600w,
/static/e87bf86b2f86fc83d7f4ddc3f73771b6/eea4a/103_java12_all.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to duplicate and merge a stream with Teeing]]></title><description><![CDATA[Understand, with an example, how to use the new Teeing collector method with Java 12.]]></description><link>https://aneesh.co.uk/how-to-duplicate-and-merge-a-stream-with-teeing</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-duplicate-and-merge-a-stream-with-teeing</guid><pubDate>Sat, 30 Jul 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Duplicate, collect and merge: How to use Teeing&lt;/h4&gt;
&lt;p&gt;
Take a look at the new method &apos;Teeing&apos; introduced to the Collectors class with Java 12. 
Teeing allows us to duplicate a stream, to collect the streams separately, and to merge them anyway we choose to in a single function. 
We will walk through a demonstration where we use teeing to extract the maximum and minimum values of a collection and to merge them into a map. 
Teeing is a useful method for specific use cases where we find ourselves streaming a collection multiple times and combining the resultant collection with each other. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/7GmIHdGptps&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAQC/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQID/9oADAMBAAIQAxAAAAHCqXB2iU//xAAcEAABAwUAAAAAAAAAAAAAAAABAAIDEBESITH/2gAIAQEAAQUCsVi6gG5im8//xAAYEQEBAAMAAAAAAAAAAAAAAAABAAMRE//aAAgBAwEBPwFxgbul/8QAGxEAAgEFAAAAAAAAAAAAAAAAAAIBAwQREkH/2gAIAQIBAT8BSrLtqTbZ6f/EABkQAAEFAAAAAAAAAAAAAAAAABAAAQIR0f/aAAgBAQAGPwJ9MlQ//8QAGxABAQEAAgMAAAAAAAAAAAAAAREAITFBYYH/2gAIAQEAAT8hHQVneQ7p9w29vvASPOalcJknFd//2gAMAwEAAgADAAAAELTf/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAERIUH/2gAIAQMBAT8QmRsrh//EABoRAAICAwAAAAAAAAAAAAAAAAERAHFhoeH/2gAIAQIBAT8QXQCrFxhjTs//xAAcEAEAAgMAAwAAAAAAAAAAAAABABEhMUFhcZH/2gAIAQEAAT8QBm7gBR8wYvTbGoRGhbY3ALTH2BEhlh2EgB7n/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/010863ef1d60de15ed5f173fe5ee41ed/f93b5/102_teeing.jpg&quot;
        srcset=&quot;/static/010863ef1d60de15ed5f173fe5ee41ed/8ddbc/102_teeing.jpg 75w,
/static/010863ef1d60de15ed5f173fe5ee41ed/75985/102_teeing.jpg 150w,
/static/010863ef1d60de15ed5f173fe5ee41ed/f93b5/102_teeing.jpg 300w,
/static/010863ef1d60de15ed5f173fe5ee41ed/20e5d/102_teeing.jpg 450w,
/static/010863ef1d60de15ed5f173fe5ee41ed/b4294/102_teeing.jpg 600w,
/static/010863ef1d60de15ed5f173fe5ee41ed/eea4a/102_teeing.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Files.mismatch with Java 12]]></title><description><![CDATA[Explore and understand the Files.mismatch method, introduced with Java 12]]></description><link>https://aneesh.co.uk/filesmismatch-with-java-12</link><guid isPermaLink="false">https://aneesh.co.uk/filesmismatch-with-java-12</guid><pubDate>Sat, 23 Jul 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;A new way to compare Files with Java 12&lt;/h4&gt;
&lt;p&gt;
Take a look at the new mismatch method introduced to the Files class with Java 12. 
Files mismatch will help us to determine the exact index position where the content of two files
will differ. This video will use an example of two files to see how the method can be used to 
compare the content of two files, and to see what is returned with identical and different files.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/g_t1Rhi-22U&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAQC/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQID/9oADAMBAAIQAxAAAAHCqXB2iU//xAAbEAACAQUAAAAAAAAAAAAAAAABEQIAAxAhMf/aAAgBAQABBQJFqWAN3jUef//EABgRAQEAAwAAAAAAAAAAAAAAAAEAAxET/9oACAEDAQE/AXGBu6X/xAAbEQACAQUAAAAAAAAAAAAAAAAAAgEDBBESQf/aAAgBAgEBPwFKsu2pNtnp/8QAGRAAAQUAAAAAAAAAAAAAAAAAEAACESGx/9oACAEBAAY/Aq0uUD//xAAbEAEAAwEAAwAAAAAAAAAAAAABABEhMUFhgf/aAAgBAQABPyEVazsBds+wbvr7gFR5jWWxIkys/9oADAMBAAIAAwAAABD03//EABgRAAIDAAAAAAAAAAAAAAAAAAABESFB/9oACAEDAQE/EJmbK4f/xAAaEQACAgMAAAAAAAAAAAAAAAABEQBxYaHh/9oACAECAQE/EE0AqxcYY07P/8QAHBABAAICAwEAAAAAAAAAAAAAAQARMWEhQXGR/9oACAEBAAE/EDm1dAKjuLrsraYhEaFljcAtOPsCJDLDuEgB7P/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/58299c13b5de8f9ce9a010693724f7c1/f93b5/101_java12mismatch.jpg&quot;
        srcset=&quot;/static/58299c13b5de8f9ce9a010693724f7c1/8ddbc/101_java12mismatch.jpg 75w,
/static/58299c13b5de8f9ce9a010693724f7c1/75985/101_java12mismatch.jpg 150w,
/static/58299c13b5de8f9ce9a010693724f7c1/f93b5/101_java12mismatch.jpg 300w,
/static/58299c13b5de8f9ce9a010693724f7c1/20e5d/101_java12mismatch.jpg 450w,
/static/58299c13b5de8f9ce9a010693724f7c1/b4294/101_java12mismatch.jpg 600w,
/static/58299c13b5de8f9ce9a010693724f7c1/eea4a/101_java12mismatch.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[String Method updates with Java 12]]></title><description><![CDATA[Explore the 2 new methods added to the String class with Java 12]]></description><link>https://aneesh.co.uk/string-method-updates-with-java-12</link><guid isPermaLink="false">https://aneesh.co.uk/string-method-updates-with-java-12</guid><pubDate>Sat, 16 Jul 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java 12 String enhancements&lt;/h4&gt;
&lt;p&gt;
Take a look at the 2 new methods introduced to the String class with Java 12. 
String.indent() will allow you to add and remove empty characters to the start of each line of a String.
String.transform() will allow you to pass in a functional interface to accept a String and return a String for a convenient way to update a String. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/6jNKXdOjCzo&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAQC/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQID/9oADAMBAAIQAxAAAAHCqXB2iU//xAAaEAACAgMAAAAAAAAAAAAAAAAAAQIRAyEx/9oACAEBAAEFAqbdSEJbzMjz/8QAGBEBAQADAAAAAAAAAAAAAAAAAQADERP/2gAIAQMBAT8BcYG7pf/EABoRAAICAwAAAAAAAAAAAAAAAAECABEDBBL/2gAIAQIBAT8BTKztyIdezdz/xAAZEAABBQAAAAAAAAAAAAAAAAAQAAIRYXH/2gAIAQEABj8Cyy5QP//EABoQAQEBAAMBAAAAAAAAAAAAAAERACExQWH/2gAIAQEAAT8hqSr2z2aT7vUqYCR7mpXCZJxXf//aAAwDAQACAAMAAAAQ9N//xAAXEQADAQAAAAAAAAAAAAAAAAAAAREh/9oACAEDAQE/ELI2Kch//8QAGhEAAgIDAAAAAAAAAAAAAAAAABFhcQFB4f/aAAgBAgEBPxBZMKosabI6f//EAB0QAQADAAEFAAAAAAAAAAAAAAEAESExQVFxkfD/2gAIAQEAAT8QD9oqhUfiVouzdV3iKFAoLyxuAWme4ESGWHWEgB5n/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/11aa39203f421562f5507f8ee0139f3e/f93b5/100_java12String.jpg&quot;
        srcset=&quot;/static/11aa39203f421562f5507f8ee0139f3e/8ddbc/100_java12String.jpg 75w,
/static/11aa39203f421562f5507f8ee0139f3e/75985/100_java12String.jpg 150w,
/static/11aa39203f421562f5507f8ee0139f3e/f93b5/100_java12String.jpg 300w,
/static/11aa39203f421562f5507f8ee0139f3e/20e5d/100_java12String.jpg 450w,
/static/11aa39203f421562f5507f8ee0139f3e/b4294/100_java12String.jpg 600w,
/static/11aa39203f421562f5507f8ee0139f3e/eea4a/100_java12String.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Basics of Angular Routing]]></title><description><![CDATA[Create an angular app that is configured with basic and nested routing to load different components]]></description><link>https://aneesh.co.uk/basics-of-angular-routing</link><guid isPermaLink="false">https://aneesh.co.uk/basics-of-angular-routing</guid><pubDate>Wed, 15 Jun 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Angular Routing&lt;/h4&gt;
&lt;p&gt;
Take a look at how you can configure routes with the Angular Routing Module. 
We will create multiple components and link them to individual routes. We will then use 
the RouterLink to create buttons that take us to those routes, before completing the tutorial 
with a demonstration of how to create nested routes. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/vJJ9Cyx4giY&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCA//EABYBAQEBAAAAAAAAAAAAAAAAAAIABP/aAAwDAQACEAMQAAAByslzo5Hf/8QAGRABAQEAAwAAAAAAAAAAAAAAAREAAgMh/9oACAEBAAEFAoujjQHs9TiT/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAIBEyH/2gAIAQMBAT8BZIXSw//EABoRAAICAwAAAAAAAAAAAAAAAAEDAAIEEUH/2gAIAQIBAT8BW0stqHHB7P/EABkQAAEFAAAAAAAAAAAAAAAAABAAAREhQf/aAAgBAQAGPwLVchz/AP/EABsQAAICAwEAAAAAAAAAAAAAAAERACExQVGh/9oACAEBAAE/IWtiM2Il0DrhNqw+xwAswaDhQOg9n//aAAwDAQACAAMAAAAQxB//xAAXEQEBAQEAAAAAAAAAAAAAAAABACFB/9oACAEDAQE/EBNLDOX/xAAZEQACAwEAAAAAAAAAAAAAAAABEQBx4bH/2gAIAQIBAT8QSICrYQfGz//EABwQAQACAwADAAAAAAAAAAAAAAEAESExQVFhof/aAAgBAQABPxBdDgNTEOD+HB2EVe0F2+zFNtbYA3vK8QkIHpT/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/4ec1bb6cd99d197afe612e6ec7b95833/f93b5/099_angularRouting.jpg&quot;
        srcset=&quot;/static/4ec1bb6cd99d197afe612e6ec7b95833/8ddbc/099_angularRouting.jpg 75w,
/static/4ec1bb6cd99d197afe612e6ec7b95833/75985/099_angularRouting.jpg 150w,
/static/4ec1bb6cd99d197afe612e6ec7b95833/f93b5/099_angularRouting.jpg 300w,
/static/4ec1bb6cd99d197afe612e6ec7b95833/20e5d/099_angularRouting.jpg 450w,
/static/4ec1bb6cd99d197afe612e6ec7b95833/b4294/099_angularRouting.jpg 600w,
/static/4ec1bb6cd99d197afe612e6ec7b95833/eea4a/099_angularRouting.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Introduction to Repos]]></title><description><![CDATA[What is the Repo market and why it is so important to financial systems.]]></description><link>https://aneesh.co.uk/introduction-to-repos</link><guid isPermaLink="false">https://aneesh.co.uk/introduction-to-repos</guid><pubDate>Mon, 13 Jun 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; What a repo is and how it works.&lt;br&gt;
&amp;#8226; The benefit of a repo and its importance to financial markets.&lt;br&gt;
&amp;#8226; How central banks are able to control bank reserves through policy and the repo market.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;About this blog&lt;/h4&gt;
&lt;p&gt;
I originally expected this blog to be fairly short; while the Repo market is worth trillions of dollars in a global market, it consists of a fairly simple exchange (as we will later see). The elevated importance of Repos as the &apos;pipelines of the global financial system&apos; will take this blog through a more interesting turn to understanding monetary policy and as a means for central banks to influence the behaviour of the repo market participants.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;What is a Repo?&lt;/h4&gt;
&lt;p&gt;
The term &apos;Repo&apos; is short for repurchase agreement. A repurchase agreement is commonly referred to as a &apos;pawn shop&apos; for banks, let&apos;s look at why...
&lt;/p&gt;
&lt;p&gt;
A repurchase agreement is used by banks to trade debt for cash. With this in mind, this means banks &lt;u&gt;need&lt;/u&gt; cash and are happy to trade the high-grade securities they own for it. In a single line, a Repo is a &apos;short term collateralised loan&apos;, where emphasis on &apos;collateralised&apos; should be taken. The debt traded in a Repo tends to be of high credit quality, such as a government bond. Repurchase agreements are typically traded on an exchange; let&apos;s look at an example: &lt;br&gt;
Bank A holds a government bond, Bond A, but needs to raise some short term capital for it&apos;s own requirements. One way Bank A can do this is by selling Bond A on the market, but what if Bank A would also like to keep Bond A in it&apos;s portfolio for the longer term? Well, Bank A can sell Bond A and then repurchase it from the buyer after an agreed period.
&lt;/p&gt;
&lt;p&gt;
On the other hand, Bank B has excess capital sitting in its reserves, and would like to make a return on that capital rather than have it sit in its books earning nothing. Let&apos;s take a look at how Bank A and Bank B can create a mutually beneficial agreement:&lt;br&gt;
Bank A sells Bond A to Bank B. Bank B transfers £100 cash to Bank A and receives Bond A. Bank A only needs the £100 cash overnight, so they agree for Bank B to sell Bond A back to Bank A the next day. Bank B will sell this bond for a pre-agreed price of £101 to realise a £1 profit. Bank A is satisfied that they have ultimately obtained their £100 capital for a cost of £1. 
&lt;/p&gt;
&lt;p&gt;
What we have is a repurchase agreement; a short term &lt;i&gt;collateralised&lt;/i&gt; loan between two banks. The loan is collateralised because Bank B is effectively lending Bank A the money they require, and in-turn receive Bond A as collateral. If Bank A is unable to repurchase Bond A (and thus default on the repo), Bank B won&apos;t face a loss as they can simply sell Bond A in the market. 
&lt;/p&gt;
&lt;p&gt;
The percentage difference between the price Bond A is sold for and the price Bond A is repurchased for is known as the &lt;strong&gt;repo rate&lt;/strong&gt;. In our case, Bond A is sold for £100 and bought back for £101, creating a repo rate of 1%. As a repo is effectively a return on a low-risk, short term collateralised loan, a repo rate can be considered a proxy for an overnight risk-free loan.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Reverse Repo&lt;/h4&gt;
&lt;p&gt;
The reverse repo is used with the intention of purchasing a security and reselling it at a higher price. A central bank may use reverse repos to inject capital into the market. They offer reverse repo&apos;s to eligible counterparties that can purchase bonds from them with an agreement to be repurchased at a higher price.
The transfer of bonds from the government with cash from the banks will allow the government to increase their capital stores while draining the capital reserves of the banks. We will later see when reverse repos are used in action.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Repo Haircuts&lt;/h4&gt;
&lt;p&gt;
The securities traded within a Repo tend to be of high credit quality and low volatility. Why?&lt;br&gt;
The reliability of a repo trade is that the underlying security does not change value (to any significant degree) for the life of the trade. If the security holds it&apos;s value, then the participant purchasing the bond can be confident that in the event of default from the counterparty, they will be able to sell the security to not realise any losses. 
&lt;/p&gt;
&lt;p&gt;
The party purchasing the debt in the repo will want to protect themselves from any volatility of the underlying security price, and therefore may request a higher value security is transferred during the trade. As a result, rather than having a £100 bond transferred from Bank A to Bank B, we may have a £108 Bond transferred. This way, if the bond falls in value to £101, Bank B will still be able to realise the same expected profit of £1 from the repo.&lt;br&gt;
The additional value of the security in the trade is known as the &lt;strong&gt;haircut&lt;/strong&gt;. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;An interesting story on repo haircuts..&lt;/strong&gt;&lt;br&gt;
&lt;i&gt;In 2008, Lehman Brothers had found a unique accounting loophole regarding repos that allowed them to hide liabilities. During the life of a repo, the debt is not actually exchanged, but is only used as collateral. Where a haircut on a repo was large enough, Lehman Brothers were able to project the incoming cash &lt;u&gt;as a sale&lt;/u&gt; of an asset that can be used to hide liabilities. As we know, all Repos are meant to be repurchased, therefore the 1st leg of the repo transaction (the 1st exchange) should not be treated as a sale, as the security is going to be repurchased during the 2nd leg. This is known as the 105 rule, read about it &lt;a href=&quot;https://blogs.harvard.edu/michaellaw/2014/01/29/lehman-brothers-repo-105-recap/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/i&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Repos and monetary policy&lt;/h4&gt;
&lt;p&gt;
So far we understand that repos are used by banks to control their cash balances, and that they typically hold high-grade securities as collateral to a short term loan. The instruments traded are typically government debt as the debt will hold it&apos;s value in turbulent markets and therefore requires less of a haircut on the trade.
&lt;/p&gt;
&lt;p&gt;
The role government debt plays in the financial market enables governments to influence the behaviours of banks. For example, a government can issue new debt into the market to enable banks to directly achieve a given return on their excess cash holdings. Issuing new debt can therefore drain cash reserves from the financial markets as it flows from the banks towards the central banks.
&lt;/p&gt;
&lt;p&gt;
Similarly, governments are able to inject capital into the financial market by buying back their debt from banks.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Reference Rates with Repos&lt;/h4&gt;
&lt;p&gt;
While reading about the Repo market, I came across a recent market event that illustrated how the monetary policy of a central bank can shape the repo market. In September 2019, the secured overnight financing rate, also known as SOFR, had a huge spike creating headline news... 
&lt;/p&gt;
&lt;p&gt;
SOFR measures the cost of overnight borrowing of dollar-denominated repo transactions. SOFR is one of many overnight reference rates, others include SONIA for Sterling interbank money market trades, and EONIA for the Euro. These reference rates are calculated using real trade data for derivatives and aim to replace reference rates from LIBOR, which are based upon estimates. You can read more about LIBOR in my blog &lt;a href=&quot;https://aneesh.co.uk/the-liborois-spread&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Repo Rate Hike of 2019&lt;/h4&gt;
&lt;p&gt;
In September 2019 the SOFR rate spiked from 2.2% to 5.25%; the graph below illustrates the significance of the spike to historical trends:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 82.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAARABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAECAwX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHvTmLoAFgf/8QAGBAAAwEBAAAAAAAAAAAAAAAAAAERECD/2gAIAQEAAQUCxFZKLj//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/AR//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAZEAABBQAAAAAAAAAAAAAAAAAAECAhIjH/2gAIAQEABj8CXC0t/8QAHBAAAgEFAQAAAAAAAAAAAAAAAAEREDFBUXFh/9oACAEBAAE/IYezpYYlD1kLnGBYUKxseeU//9oADAMBAAIAAwAAABAAzzz/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/EB//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/EB//xAAhEAACAgECBwAAAAAAAAAAAAABEQAxIRCRQVFhcYGx0f/aAAgBAQABPxAMdVSnBEC5d3jt2QthmXg9YIPH2BOxDh8ofTUf/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/152115559970fbad12e9e20d97a8cff7/f93b5/098_sofr.jpg&quot;
        srcset=&quot;/static/152115559970fbad12e9e20d97a8cff7/8ddbc/098_sofr.jpg 75w,
/static/152115559970fbad12e9e20d97a8cff7/75985/098_sofr.jpg 150w,
/static/152115559970fbad12e9e20d97a8cff7/f93b5/098_sofr.jpg 300w,
/static/152115559970fbad12e9e20d97a8cff7/20e5d/098_sofr.jpg 450w,
/static/152115559970fbad12e9e20d97a8cff7/b4294/098_sofr.jpg 600w,
/static/152115559970fbad12e9e20d97a8cff7/a3695/098_sofr.jpg 624w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;small&gt;Source: Data available on FRBNY public website.&lt;/small&gt;&lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Why the rate hiked&lt;/h5&gt;
&lt;p&gt;
As we understand, the repo market is used to help banks obtain cash for their high-graded securities, typically overnight. 
The rate hike occurred due to a sudden rise in the demand for cash, and this is largely considered to be due to the timely occurrence of two events...
&lt;/p&gt;
&lt;p&gt;
The first event were the Q3 taxes that were due for banks in mid September 2019. 
The tax deadline meant cash from the banks had been planned to flow to the US central bank, the Federal Reserve.
As banks needed more cash on hand to pay the taxes, the demand rose. 
&lt;/p&gt;
&lt;p&gt;
The second event were the issuance of new investment grade securities by the treasury that also coincided around mid-September to the value of roughly $115 billion. 
The trading volumes of the new securities meant broker-dealers relied upon the repo market for the cash to fund the volumes. 
These two events meant cash within the financial markets were in large demand. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Bank reserve frameworks&lt;/h5&gt;
&lt;p&gt;
Bank reserves refer to the amount of cash banks hold that is readily available for use.
Governments use a minimum level of reserves target that they would like the banks to hold to reflect the market conditions and prospects.
Prior to 2008, the Federal Reserve used a &apos;scarce reserves&apos; framework where banks held only enough reserves needed for day-to-day activities. 
Banks were able to borrow from the repo market when cash was required and lend when they had too much. Following the financial crisis, the cash reserves required by banks had grown to a high degree.
&lt;/p&gt;
&lt;p&gt;
From 2008 to 2014, Quantitative Easing had been used as a strategy to introduce a new cash supply within the system to support higher cash reserves.
The Federal Reserve implemented Quantitative Easing by purchasing treasury securities from the banks to provide capital in the system.
&lt;/p&gt;
&lt;p&gt;
Since 2014, the banks have applied an &apos;ample reserves&apos; framework on their balance sheet. 
The Federal Reserve introduced a new interest rate &apos;interest on excess reserves&apos; (IOER) where banks were paid a separate interest rate for holding excess amount of reserves in their books. IOER encouraged banks to hold further reserves, but also placed a new rate for the Federal Reserve to control that would influence the amounts of cash reserves the banks held. Increasing IOER would encourage more excess reserves, while the opposite would have the opposite effect.
&lt;/p&gt;
&lt;p&gt;
A further instrument of the ample reserves framework were the overnight reverse repos (ON RRP). 
A reverse repo will see the Treasury sell a security to an eligible counterparty and repurchase the security at a higher price, implying an interest rate. 
These transactions were performed overnight for a short-term interest rate gain for the counterparty. The benefit to the counterparty were to protect
them from the lower-bounds of the interest on reserves balances rate (IORB). IORB is paid to banks for the reserves they hold, and if that rate fell very low, 
the ON RRP can be used as a floor to ensure at least the ON RRP % return was possible for the cash reserves. As the ON RRP rate fell, the amount of capital banks
were willing to hold increased as they could earn a higher rate than through an ON RRP. 
&lt;/p&gt;
&lt;p&gt;
As the reserves in banks fell with the ample reserves framework, the confidence banks had as to what &apos;ample reserves&apos; would be required became less and less obvious. As a result, banks would hold more cash than they felt required to further drive the demand for cash.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;How the Hike impacted banks&lt;/h5&gt;
&lt;p&gt;
The immediate impact of the SOFR rates hike first meant banks were not as able to obtain cash from the repo market as required. A larger repo rate meant
a larger cost to the bank for obtaining short-term cash. There would also be banks that benefited from the hike to realise greater profits in their cash reserves if held.
&lt;/p&gt;
&lt;p&gt;
A secondary impact of the SOFR rates hike were the much larger number of parties that used SOFR as a reference rate, specifically to borrow at. This meant
their fees for borrowing may have increased in a short term to reflect the sudden SOFR hike. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;How the Federal Reserved intervened&lt;/h5&gt;
&lt;p&gt;
The Federal Reserve were able to intervene to the SOFR hike to provide means for reducing the demand for cash. 
This was achieved by growing their balance sheet (increasing the assets they held) and then 
offering $75 billion in cash to borrowers on the repo market. The array of collateral that was accepted by the New York Fed increased to include Treasury Securities, agency debt securities and agency mortgage-backed securities. This made it easier for participants to access the cash injection.
&lt;br&gt;
Further intervention involved lowering the IEOER rate to 20 basis points below the top of its target range to also discourage excess reserves held by banks.
From October 11th 2019, the Fed also injected reserves into the financial system by purchasing Treasury Bills to the value of roughly $60 billion per month
through the second quarter of 2020. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The Repo Market is used to help banks obtain short term cash using highly rated securities as collateral. The Repo Market supports banks to operate
with minimal cash reserves they think they require as cash is so easily available and at a low price.
&lt;/p&gt;
&lt;p&gt;
The Repo market uses many reference rates to determine what rate will be returned for exchanging cash overnight, however repos can also be reversed where
the party purchasing the security benefit from the resale at a higher price. 
&lt;/p&gt;
&lt;p&gt;
The Repo market is one of global magnitude, where reference rates impact both day-to-day operations of a bank and the many parties that use the rate for borrowing and lending. The 2019 SOFR rate hike is one example that illustrates the degree of control the central bank may have in controlling the cash reserves 
banks hold, and to influence their motivations and behaviour to use the repo market. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The LIBOR/OIS Spread]]></title><description><![CDATA[A look into the OIS Libor spread and why it is important]]></description><link>https://aneesh.co.uk/the-liborois-spread</link><guid isPermaLink="false">https://aneesh.co.uk/the-liborois-spread</guid><pubDate>Sun, 29 May 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; An overview of the interbank markets and why they exist.&lt;br&gt;
&amp;#8226; Understand the use and differences between LIBOR and OIS.&lt;br&gt;
&amp;#8226; Understand how LIBOR-OIS spread can act as an indicator to interbank credit confidence.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Interbank markets&lt;/h4&gt;
&lt;p&gt;
The interbank market differs from the &apos;open market&apos; as it only enables specific banks to trade with each other. The interbank market often refers to the interbank foreign exchange (FX) market where currency and currency derivatives are traded. There also exists an interbank lending market where short term loans are also traded amongst banks to enable them to better manage their capital reserves and liquidity requirements. This blog will focus on the interbank lending market. 
&lt;/p&gt;
&lt;p&gt;
The interbank lending market enables banks to manage their liquidity risk to meet stringent requirements. Amongst many, one reason these requirements are in place are to ensure the banks are able to withstand any potential &apos;bank runs&apos; by customers. On the other hand, banks with excess levels of capital can lend their reserves to earn an interest income.
&lt;/p&gt;
&lt;p&gt;
Why would banks want to lend to each other?&lt;br&gt;
Imagine if Bank A has £10 million pounds. Bank A will be able to make a nice profit by lending £8 million of that loan to commercial purposes at an interest rate of 2%. But Bank A has not yet found a purpose for the other £2 million on it&apos;s books. So rather than letting it sit in the bank earning nothing, they can then lend to other banks in the &lt;i&gt;interbank lending market&lt;/i&gt; for a set period of time until they find a more lucrative use for the capital. 
&lt;/p&gt; 
&lt;p&gt;
Bank B may be a customer to Bank A as they may require extra short-term capital for their own books (perhaps to meet liquidity requirements or to satisfy a new loan opportunity). They are able to borrow capital on the interbank market at an agreed interest rate. Bank A and Bank B will agree a timeframe for which the money is lent; Bank A is happy as they find a use for their extra £2 million and Bank B is happy because they are able to satisfy their immediate capital requirements. 
&lt;/p&gt;
&lt;p&gt;
The banks that are participating in the interbank market tend to be of high-credit quality. The interbank market is unique as loans are made without collateral and at a very low interest rate to reflect the credit-quality of the participants. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;LIBOR&lt;/h4&gt;
&lt;p&gt;
LIBOR - The London Interbank Offered Rate is a variable interest rate that is used by banks for short-term, unsecured interbank lending, just as Bank A and Bank B would engage in.
&lt;/p&gt;
&lt;p&gt;
LIBOR is a globally accepted benchmark and reflects the cost of lending between banks. LIBOR is calculated on the rate that a consortium of banks lend 5 currencies (USD, EUR, GBP, JPY and CHF) at 7 different maturities (overnight, 1 week and 1, 2, 3, 6, and 12 months) within the interbank market. Despite there being 35 different LIBOR combinations of currency and maturity, the 3-month USD LIBOR rate is most commonly quoted as the &apos;current LIBOR rate&apos;.
&lt;/p&gt;
&lt;p&gt;
The LIBOR rate is posted on a daily basis. It is calculated by the British Banking Association (BBA) who ask major global banks what they would expect to pay for a short term loan in the interbank market. The BBA then remove the tail-end values and calculate the average. This is known as a &apos;trimmed average&apos;. The daily LIBOR rate is published by the ICE (Intercontinental Exchange) Benchmark Administration. 
&lt;/p&gt;
&lt;p&gt;
The rate that each bank offers for short term lending will change each day to reflect credit risk on the loan. Credit risk is the risk that the original loan amount will not be repaid. As the length of the loan increases, so does the credit risk, as there is a longer period of time where the loanee may default and be unable to repay the loan in full. LIBOR is known as a &lt;strong&gt;forward-looking&lt;/strong&gt; interest rate as it is a determined rate that is set at the beginning of the loan, and is paid at the loan&apos;s maturity. 
&lt;/p&gt;
&lt;p&gt;
So why would an everyday person care about LIBOR?
&lt;/p&gt;
&lt;p&gt;
As a bank customer, you may also be impacted by LIBOR. LIBOR is often used to determine the interest rate on various products such as credit cards, loans and mortgages. As LIBOR impacts the rate at which a bank can obtain a loan at, it will impact subsequent loans the bank makes to its customers to therefore ensure they remain profitable.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Overnight Index Swap&lt;/h4&gt;
&lt;p&gt;
The overnight index swap is a fixed-floating interest rate swap. The swap uses an overnight index as the floating rate. The overnight index is calculated by compounding an overnight unsecured lending rate to a new maturity. The overnight unsecured index rate is reflective of a single central bank. Indexes include:&lt;br&gt;
&amp;#8226; SONIA: Sterling Overnight Index Average&lt;br&gt;
&amp;#8226; EONIA: Euro Overnight Index Average&lt;br&gt;
&amp;#8226; SARON: Swiss Average Rate Overnight Index&lt;br&gt;
&amp;#8226; Mutan Rate: Japanese Overnight Index Rate&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The unsecured overnight interest rate is calculated each day using the volume-weighted average of all unsecured loans for that date of a certain value. For example SONIA uses the interest rate applied to all overnight unsecured loans made up to 6pm that day that are greater than 25 million pounds.  
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;LIBOR vs OIS&lt;/h4&gt;
&lt;p&gt;
The key difference between LIBOR and OIS lies in the credit risk they represent. As capital is actually exchanged with a loan using LIBOR, there is a real presence of credit risk that is accounted for in the rate. As the maturity of the loan becomes greater, the credit risk on the loan increases as the opportunity for the counterparty to default increases. &lt;br&gt;
OIS differs as credit risk is &lt;i&gt;virtually&lt;/i&gt; eliminated. The overnight rate for a loan accounts for almost no credit risk due to the immediate repayment made on the loan. There is a very small opportunity for the loanee to default on the repayment. The OIS is considered to be risk-free as principal is not exchanged for the swap, but rather the difference in the agreed fixed rate and the floating index rate at maturity. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The LIBOR OIS Spread&lt;/h4&gt;
&lt;p&gt;
The LIBOR-OIS (also known as LOIS) spread compares both the floating rate of LIBOR for a given maturity, and the compounded OIS rate to the same maturity. 
&lt;/p&gt;
&lt;p&gt;
As a result, the LIBOR-OIS spread represents the rate of interest that is required within the interbank market to compensate for the additional credit risk that comes with LIBOR due to the longer maturity on loans and capital transferred. As a result, we can use the LOIS to gather an understanding for how banks see credit risk in the interbank market. 
&lt;/p&gt;
&lt;p&gt;
You can find the historical LOIS spread &lt;a href=&quot;https://www.icmagroup.org/market-practice-and-regulatory-policy/repo-and-collateral-markets/market-data/libor-ois-spreads-lois/&quot;&gt;here&lt;/a&gt;. Notice in March 2020 there had been a huge spike in the spread. A possible cause for this may be the outbreak of Covid-19 and the global lockdown that subsequently followed. A similar spike occurred in 2008 prior to the financial crisis. LOIS can be considered a valuable indicator to the perception of credit risk within the interbank market. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;LIBOR scandal&lt;/h4&gt;
&lt;p&gt;
Earlier in the blog, we mentioned how LIBOR was calculated, and how it had been a competitive rate set by specific banks that were averaged out by the British Bankers Association. Unfortunately, this &apos;competitive&apos; rate submitted by the banks were not always done so with the purest of intentions.
&lt;/p&gt;
&lt;p&gt;
One assumption made within an investment bank is the presence of a &apos;Chinese wall&apos;. This is a theoretical wall that prevents communication between different divisions of the bank that may have an influence on each other. For example, between the parts of the bank that submit their LIBOR rates to the BBA, and the traders whose derivatives have exposures on LIBOR. The implementation of the Chinese wall may not have been carried out so well with certain banks. 
&lt;/p&gt;
&lt;p&gt;
If the LIBOR submitters for a bank are aware that a specifically higher or lower LIBOR can benefit the net position of their traders, they may be motivated to post artificially high or low LIBOR rates to the BBA. It is also believed that up to 20 banks colluded to submitting LIBOR rates that were not strictly competitive to benefit their positions. 
&lt;/p&gt;
&lt;p&gt;
A further drive behind collusion in submitting LIBOR rates that were not competitive were to give a false premise for the banks position to borrow and lend in the market. If a bank considered themselves riskier to lend to, they may reduce the LIBOR rate they they think they can lend at. As a result the LIBOR rate for the day may be reduced, thus enabling the bank that submitted the lower rate to borrow more cheaply. 
&lt;/p&gt;
&lt;p&gt;
In the aftermath of the scandal, believed to have started since 2003, LIBOR is currently being phased out, expected to be completed by June 30 2023. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The interbank lending market enables banks to efficiently allocate and obtain capital depending on whether they hold excess amounts or require it for short-term unsecured borrowing. LIBOR is a variable interest rate that determines the rate which banks can lend to each other in 35 different currency/maturity combinations. 
&lt;br&gt;
OIS is a rate used by banks to borrow overnight, and therefore reflects a reduced presence of credit risk in the pricing.
&lt;br&gt;
The LIBOR-OIS spread is a useful indicator to summarise the confidence banks have in lending to each other as it reflects the credit risk they perceive for longer term interbank loans. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Populate multiple Excel columns in Java]]></title><description><![CDATA[Use the apache poi library to create and populate an Excel workbook]]></description><link>https://aneesh.co.uk/populate-multiple-excel-columns-in-java</link><guid isPermaLink="false">https://aneesh.co.uk/populate-multiple-excel-columns-in-java</guid><pubDate>Tue, 17 May 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Populate multiple Excel Columns with Java&lt;/h4&gt;
&lt;p&gt;
Use a multi-dimensional Java array pattern to efficiently populate multiple columns in an Excel worksheet with Java. 
We will first look at how you can manually create cells and populate them with data in a for-loop, then improve our design by populating a multi-dimensional array and filling the day in accordingly. 
We will conclude the video by looking at an efficient way we can add headers to our worksheet to make the Excel file more organised. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/1GmBExsgl_E&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMBAv/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAHjbTiki//EABkQAQADAQEAAAAAAAAAAAAAAAEAAhEDIf/aAAgBAQABBQLGYkI1J09Spn//xAAZEQABBQAAAAAAAAAAAAAAAAAAAQIREyH/2gAIAQMBAT8BVkaWH//EABsRAAIBBQAAAAAAAAAAAAAAAAABAgMEERJB/9oACAECAQE/AY1N3gdun0//xAAZEAADAAMAAAAAAAAAAAAAAAAAEBECIUH/2gAIAQEABj8COxZaIv/EABsQAAIDAQEBAAAAAAAAAAAAAAERACExQVGh/9oACAEBAAE/IWEhmtsTqTouE2rD9hZbqAgsWQOg+z//2gAMAwEAAgADAAAAECAf/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARQaH/2gAIAQMBAT8Qo2ewZk//xAAbEQEAAQUBAAAAAAAAAAAAAAABABFhcZGx4f/aAAgBAgEBPxBxIaxeIV49n//EAB0QAQACAgIDAAAAAAAAAAAAAAEAESExQWFRocH/2gAIAQEAAT8QQBV1xmZDzSD5CKvaC7e5VhrrHcKwPmYhIQOlP//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/532be90f47350b9e50f0f1673a94794f/f93b5/096_Excel2.jpg&quot;
        srcset=&quot;/static/532be90f47350b9e50f0f1673a94794f/8ddbc/096_Excel2.jpg 75w,
/static/532be90f47350b9e50f0f1673a94794f/75985/096_Excel2.jpg 150w,
/static/532be90f47350b9e50f0f1673a94794f/f93b5/096_Excel2.jpg 300w,
/static/532be90f47350b9e50f0f1673a94794f/20e5d/096_Excel2.jpg 450w,
/static/532be90f47350b9e50f0f1673a94794f/b4294/096_Excel2.jpg 600w,
/static/532be90f47350b9e50f0f1673a94794f/eea4a/096_Excel2.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Create an Excel workbook in Java]]></title><description><![CDATA[Use the apache poi library to create and populate an Excel workbook]]></description><link>https://aneesh.co.uk/create-an-excel-workbook-in-java</link><guid isPermaLink="false">https://aneesh.co.uk/create-an-excel-workbook-in-java</guid><pubDate>Tue, 10 May 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Creating an Excel workbook&lt;/h4&gt;
&lt;p&gt;
Use the apache poi and apache poi ooxml libraries to create an Excel workbook in Java.
We will add the packages into the maven project and use some of the classes such as XSSFWorkbook, XSSFSheet, Row and Cell 
to populate the Excel workbook with data!
&lt;p&gt;&lt;a href=&quot;https://youtu.be/TEBhTU4PHzY&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAECBP/EABYBAQEBAAAAAAAAAAAAAAAAAAACA//aAAwDAQACEAMQAAABhuclmIV//8QAGxAAAQQDAAAAAAAAAAAAAAAAAAECAyEQETH/2gAIAQEAAQUCsvD00TKN5//EABYRAQEBAAAAAAAAAAAAAAAAAAARE//aAAgBAwEBPwGRo//EABoRAAICAwAAAAAAAAAAAAAAAAABAhEEEkH/2gAIAQIBAT8BUtnQ8e+n/8QAGRAAAgMBAAAAAAAAAAAAAAAAABACEUFx/9oACAEBAAY/AtNUulL/xAAcEAEAAwACAwAAAAAAAAAAAAABABEhMUFhcYH/2gAIAQEAAT8h36+IV7HyDzVpCCMhMtiRJlZ//9oADAMBAAIAAwAAABCI3//EABkRAQACAwAAAAAAAAAAAAAAAAEAITFBYf/aAAgBAwEBPxDIq12V1P/EABsRAAICAwEAAAAAAAAAAAAAAAERAGEhocHh/9oACAECAQE/ECDUZqgexpjT2f/EABwQAQEBAAMAAwAAAAAAAAAAAAERADFBUSFhof/aAAgBAQABPxCM16GC3xwtauVkDUfgTBefwMdNSUftw5otQ7x4EX3f/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/04db3357614f34d3910782989f1f1452/f93b5/095_excel1.jpg&quot;
        srcset=&quot;/static/04db3357614f34d3910782989f1f1452/8ddbc/095_excel1.jpg 75w,
/static/04db3357614f34d3910782989f1f1452/75985/095_excel1.jpg 150w,
/static/04db3357614f34d3910782989f1f1452/f93b5/095_excel1.jpg 300w,
/static/04db3357614f34d3910782989f1f1452/20e5d/095_excel1.jpg 450w,
/static/04db3357614f34d3910782989f1f1452/16c42/095_excel1.jpg 476w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An Introduction to Yield Curves]]></title><description><![CDATA[Understand what a yield curve is, and how they are used for understanding interest rate expectations.]]></description><link>https://aneesh.co.uk/an-introduction-to-yield-curves</link><guid isPermaLink="false">https://aneesh.co.uk/an-introduction-to-yield-curves</guid><pubDate>Thu, 28 Apr 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; How yield curves are used.&lt;br&gt;
&amp;#8226; Review the properties of a bond.&lt;br&gt;
&amp;#8226; Understand how and why the yield curve can change shape.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Yield Curves - An introduction&lt;/h4&gt;
&lt;p&gt;
This blog is split into multiple sections that will eventually lead to (hopefully) a better understanding of yield curves.
First we will look at bonds, what they are and why businesses and investors use them. Then we will look at bonds and interest rates, and why we might consider interest rates when investing in a bond.
Then finally we will look at the yield curve, to understand what it looks like, how it can change, and most importantly, what it means.
&lt;/p&gt;
&lt;p&gt;
Yield curves are used within finance to measure the risk appetite of bond investors.  
A typical yield curve may look like this:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3lhQf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEAAQUCX//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAAIDAAAAAAAAAAAAAAAAAAAQESFB/9oACAEBAAE/IbIer//aAAwDAQACAAMAAAAQUA//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAdEAACAgEFAAAAAAAAAAAAAAAAARExECFBcYGR/9oACAEBAAE/EE2b53FCnHWIXorakH//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Yield curve graph&quot;
        title=&quot;Yield curve graph&quot;
        src=&quot;/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/f93b5/094_yield.jpg&quot;
        srcset=&quot;/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/8ddbc/094_yield.jpg 75w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/75985/094_yield.jpg 150w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/f93b5/094_yield.jpg 300w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/20e5d/094_yield.jpg 450w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/b4294/094_yield.jpg 600w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/6c50f/094_yield.jpg 1672w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The yield curve has two axis, one for yield (return percentage) and one for time to maturity.
We can see that as maturity years change for a bond, the expected yield changes, rising quickly at the start, and then slightly flattening towards the end. But wait, what is a bond?&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Bonds&lt;/h4&gt;
&lt;p&gt;
A bond is a &lt;strong&gt;fixed income instrument&lt;/strong&gt; that represents a loan that an investor makes to a borrower (typically a corporation or government entity). The bond will have a few key properties:&lt;br&gt;
&amp;#8226; Principal: the amount of money the corporation is borrowing.&lt;br&gt;
&amp;#8226; Coupon: a percentage amount of the principal the corporation will pay the investor at fixed intervals (which is typically every 6 months).&lt;br&gt;
&amp;#8226; Maturity: when the bond will expire and the principal amount is repaid to the investor by the corporation.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
We can describe how a bond works using its properties. The corporation or government entity need to raise short-term capital. They can do this using bonds. An investor can purchase a bond, this involves lending the corporation the principal amount, with the promise of being repaid at the maturity date. The maturity date ranges from weeks to decades. As an incentive to the investor to purchase the bond, the corporation that issues the bond will pay a bi-annual fee to the bond holder, which is a percentage of the principal, known as the coupon rate. 
&lt;/p&gt;
&lt;p&gt;
So if I buy a bond with a principal of £100, and a coupon rate of 2% that matures in 3 years, I will receive payments of £2 every 6 months for 3 years before receiving my £100 back from the corporation. 
&lt;/p&gt;
&lt;p&gt;
Bonds fall under the type of investment called &apos;fixed income&apos; as they provide the investor with a fixed stream of income from the investment. This fixed stream is realised in the form of the coupon repayments the investor receives. 
&lt;/p&gt;
&lt;p&gt;
Bonds are considered to be a fairly safe investment when compared with equities. This is because bonds are bought with a promise of the issuer to return the face value of the bond at maturity. Equity does not carry such a guarantee. As a bond-holder, you also sit higher in the liquidation hierarchy than a shareholder for a corporation if they were to default.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Bonds and Interest rates&lt;/h4&gt;
&lt;p&gt;
As a bond-holder, you will be also be conscious to the current interest rate and forecasted interest rate for the maturity of the bond. But why? 
&lt;/p&gt;
&lt;p&gt;
Bonds act as an alternative low-risk investment option for your money when compared to a savings account. Leaving your money in a savings account will guarantee a return of the interest rate, whereas investing the money in a bond will return the coupon rate for that bond. Buying a bond means your capital is tied up with the corporation until maturity, but in return you receive the bi-annual coupon return for the investment.
&lt;/p&gt;
&lt;p&gt;
Using the example from above, if interest rates were 1%, I would probably prefer to invest my £100 by buying a bond, as the coupon rate is 2% and earn more money over 3 years, than just the 1% interest I receive each year from my bank. This is provided I would not like to access my £100 during that time. The benefit of leaving my money in a savings account means I can withdraw it when required, such a benefit does not exist with a bond, unless I sell it within the market.
&lt;/p&gt;
&lt;p&gt;
When we later look at yield curves, we will be able to reflect upon interest rates as their expected long-term behaviour can influence the expected returns an investor has for a bond.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Different Yield Curves&lt;/h4&gt;
&lt;p&gt;
Not all bonds return the same amount to the investor, nor do they last for the same amount of time, or even pay coupons at the same time. Bonds can differ by the corporation they are bought from, and the credit quality deemed for that corporation. Corporations with a lower credit quality may pay a higher coupon rate that reflect the higher risk faced by the investor. Similarly, maturities for bonds can also be different. If a bond has a longer maturity, it means the principal will be repaid to the investor at a different period of time in the future (and the coupon repayments are received for a longer period of time). We are able to visualise the risk of holding a bond using a yield curve. A yield curve illustrates the expected returns investors have for bonds of a similar credit quality, but at different maturities. This allows us to understand exactly what level of return is expected for a bond where the maturity of the bond is the only variable. 
&lt;/p&gt;
&lt;p&gt;
Given any individual yield curve can be reflective of a bond&apos;s credit quality, you will find many different yields curves, such as the Treasury Yield curve that reflects the rates of US treasuries, AAA Municipal yield curve that refelct AAA municipal bonds, or even those for a single issuer such as the IBM Yield curve. When you hear about yield curve in the news and in economics, it is likely they are talking about the Treasury Yield Curve.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The standard Yield curve&lt;/h4&gt;
&lt;p&gt;
We have seen what a normal yield curve looks like earlier in this blog:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3lhQf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEAAQUCX//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABgQAAIDAAAAAAAAAAAAAAAAAAAQESFB/9oACAEBAAE/IbIer//aAAwDAQACAAMAAAAQUA//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAdEAACAgEFAAAAAAAAAAAAAAAAARExECFBcYGR/9oACAEBAAE/EE2b53FCnHWIXorakH//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Yield curve graph&quot;
        title=&quot;Yield curve graph&quot;
        src=&quot;/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/f93b5/094_yield.jpg&quot;
        srcset=&quot;/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/8ddbc/094_yield.jpg 75w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/75985/094_yield.jpg 150w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/f93b5/094_yield.jpg 300w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/20e5d/094_yield.jpg 450w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/b4294/094_yield.jpg 600w,
/static/e3db45cbb3ef9b0d8c28d5a8e47d0efe/6c50f/094_yield.jpg 1672w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Let&apos;s break it down slightly to understand why it is the shape it is. When an investor commits funds to an bond, they are leaving their capital at risk. If you were to invest £10 for 1 month, and were offered £1 back as profit (so £11 return in 1 month), you probably wouldn&apos;t be as happy with an offer of receiving £11 back in 2 months time. Investing money for longer periods of time means your invested capital is at risk (in the event that the principal cannot be repaid) for a longer period, and is therefore at more risk.
As a result, investors therefore expect higher returns when the time to repayment (maturity) is longer. Equally, if the money were left in a savings account, it may accrue more interest in a longer period of time.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The yield curve for a bond is not linear, after all we may not expect the exact same yield for 2 months as we do receive for 1 month. We can see in the graph that the yield for 20 year maturity is only slightly less (3.9%) than the maturity for 30 years (4%).
&lt;/p&gt;
&lt;p&gt;
The yield curve typically increases at a steeper rate during the early period, and then flattens towards the longer maturities from 20 years to 30 years. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Interpreting a yield curve&lt;/h4&gt;
&lt;p&gt;
So what does this all mean, and why do yield curves differ?
Yield curves are often used to understand how the investors expect long term interest rates to behave. Why? Because if an investor is able to establish an opinion on how they expect interest rates to move, they will therefore have an expectation on what yield to demand for a similar maturity of a bond. 
&lt;/p&gt;
&lt;p&gt;
Let&apos;s head back to our example from earlier. Let&apos;s say we expect interest rates to rise to 5% in 1 years time, and 10% in two years time. All of sudden, the current interest rate of 1% doesn&apos;t seem so bad to begin investing with. And all of a sudden, our bond, which pays a 2% coupon every 6 months is not looking so good. If we have good reason to believe interest rates are going to skyrocket in the coming periods, then we can rightfully expect bonds to beat those interest rates for an investment of the same period. Why? Because bonds naturally carry more (be it very little amounts) of risk than a regular savings account. Investors will not be so keen to buy the 2% bond if they expect the interest rates to rise so quickly over the same period of time.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Changing the yield curve&lt;/h4&gt;
&lt;p&gt;
We may see a yield curve begin to flatten when investors expect long term interest rates to fall in comparison with short term interest rates. When this happens, the required rate of return on a bond with long maturity is reduced as the investor does not expect interest rates to be as high in the future. Therefore the right-hand-side of the yield curve may fall, and the curve flattens.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3lhQf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEAAQUCX//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABcQAQADAAAAAAAAAAAAAAAAABAAEUH/2gAIAQEAAT8hlOn/2gAMAwEAAgADAAAAEPAP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAGRAAAwEBAQAAAAAAAAAAAAAAAAERITEQ/9oACAEBAAE/EFXdioo4/IhdaQ//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Flattened yield curve graph&quot;
        title=&quot;Flattened yield curve graph&quot;
        src=&quot;/static/3d604e9637c937f12f6c79d7cde9fc27/f93b5/094_flattenYield.jpg&quot;
        srcset=&quot;/static/3d604e9637c937f12f6c79d7cde9fc27/8ddbc/094_flattenYield.jpg 75w,
/static/3d604e9637c937f12f6c79d7cde9fc27/75985/094_flattenYield.jpg 150w,
/static/3d604e9637c937f12f6c79d7cde9fc27/f93b5/094_flattenYield.jpg 300w,
/static/3d604e9637c937f12f6c79d7cde9fc27/20e5d/094_flattenYield.jpg 450w,
/static/3d604e9637c937f12f6c79d7cde9fc27/b4294/094_flattenYield.jpg 600w,
/static/3d604e9637c937f12f6c79d7cde9fc27/6c50f/094_flattenYield.jpg 1672w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
We may also yield curves steepen:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAEDBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAe8sMgf/xAAWEAADAAAAAAAAAAAAAAAAAAACEDH/2gAIAQEAAQUCR1Hf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFBABAAAAAAAAAAAAAAAAAAAAIP/aAAgBAQAGPwJf/8QAGBABAQEBAQAAAAAAAAAAAAAAAAGxQXH/2gAIAQEAAT8hleMEdYP/2gAMAwEAAgADAAAAEBAP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAGhABAAMBAQEAAAAAAAAAAAAAAQARITFRcf/aAAgBAQABPxAKbahRxOMDPsFUeQZ2P//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Steep yield curve graph&quot;
        title=&quot;Steep yield curve graph&quot;
        src=&quot;/static/85bf2a19e125ac8aff2b6553c2258534/f93b5/094_steepYield.jpg&quot;
        srcset=&quot;/static/85bf2a19e125ac8aff2b6553c2258534/8ddbc/094_steepYield.jpg 75w,
/static/85bf2a19e125ac8aff2b6553c2258534/75985/094_steepYield.jpg 150w,
/static/85bf2a19e125ac8aff2b6553c2258534/f93b5/094_steepYield.jpg 300w,
/static/85bf2a19e125ac8aff2b6553c2258534/20e5d/094_steepYield.jpg 450w,
/static/85bf2a19e125ac8aff2b6553c2258534/b4294/094_steepYield.jpg 600w,
/static/85bf2a19e125ac8aff2b6553c2258534/6c50f/094_steepYield.jpg 1672w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A steepening yield curve will see yields on bonds that mature at dates further into the future return significantly higher yields than those in the short term. A steepening curve reflects a steepening long maturity or a shallowing short maturity. It is indicative of expectations for higher interest rates in the future, so corporations are willing to provide greater returns on their long-term bonds. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Lastly, we can also see an inverted yield curve:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAEF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB3lhQf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEAAQUCX//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABcQAQADAAAAAAAAAAAAAAAAABAAMUH/2gAIAQEAAT8hlOn/2gAMAwEAAgADAAAAEPAP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAGxAAAgIDAQAAAAAAAAAAAAAAAAERIRAxUWH/2gAIAQEAAT8QUtu4sS4rzEIW1kH/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Inverted yield curve graph&quot;
        title=&quot;Inverted yield curve graph&quot;
        src=&quot;/static/c9762084bf8b7edf5449079f0ad3f79e/f93b5/094_invertYield.jpg&quot;
        srcset=&quot;/static/c9762084bf8b7edf5449079f0ad3f79e/8ddbc/094_invertYield.jpg 75w,
/static/c9762084bf8b7edf5449079f0ad3f79e/75985/094_invertYield.jpg 150w,
/static/c9762084bf8b7edf5449079f0ad3f79e/f93b5/094_invertYield.jpg 300w,
/static/c9762084bf8b7edf5449079f0ad3f79e/20e5d/094_invertYield.jpg 450w,
/static/c9762084bf8b7edf5449079f0ad3f79e/b4294/094_invertYield.jpg 600w,
/static/c9762084bf8b7edf5449079f0ad3f79e/6c50f/094_invertYield.jpg 1672w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This is a rare case where short term bonds return a greater yield than long term bonds. This means investors expect the interest rates to fall in the future, and are therefore willing to accept smaller returns for the longer maturity bonds. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The yield spread&lt;/h4&gt;
&lt;p&gt;
The term &apos;yield spread&apos; is used to briefly, and effectively compare two bonds. These bonds can have different maturities, different credit ratings, and different risk levels. It allows analysts to understand how yield is currently represented for a bond, and if, compared to its historical points of reference, is a good investment.
&lt;/p&gt;
&lt;p&gt;
A yield spread is quoted by comparing the yield of one bond against another. If you are comparing a bond yield against the US treasury bond, it is known as a &lt;strong&gt;credit spread&lt;/strong&gt;. The US treasury bond is considered a de-facto standard for bonds. If bond &apos;A&apos; has a 5-year yield of 3% and the US Treasury bond has a yield of 2%, then the yield spread is 1%, also expressed as 100 basis points. If bond &apos;A&apos; usually has a yield spread of 0.2% when compared with the US Treasury Bond, then that means it is currently offering a high (or &apos;wider&apos;) spread than usual and is therefore offering a greater return than it historically did.
&lt;/p&gt;
&lt;p&gt;
The yield for corporations that are typically smaller, with poor financial data and backing may be greater than that of a larger organisation or government entity. This is because the bond with the smaller corporation may be perceived as being riskier, thus warranting a larger yield for the bond. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Yield curves today&lt;/h4&gt;
&lt;p&gt;
It can also be interesting to take a look at yield curves today, to understand how investors expect interest rates to change in the future. You can visit &lt;a target=&quot;_blank&quot; href=&quot;http://www.worldgovernmentbonds.com/country/united-kingdom/&quot;&gt;worldgovernmentbonds.com&lt;/a&gt; to get a picture of different bond yields. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The yield curve is used to understand the expected returns of an investor for a given bond at a given maturity. The yield curve helps us to understand future expectations of interest rates as investments with bonds are often compared with risk-free savings. Analyst will create historical yield spreads to compare different bonds, and to understand when some bonds are trading at a premium than they previously have against others. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Customise and format REST API Error responses]]></title><description><![CDATA[Look at how we can customise API error messages we receive from REST API requestes]]></description><link>https://aneesh.co.uk/customise-and-format-rest-api-error-responses</link><guid isPermaLink="false">https://aneesh.co.uk/customise-and-format-rest-api-error-responses</guid><pubDate>Mon, 07 Mar 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Customise Error Responses from a Spring REST API&lt;/h4&gt;
&lt;p&gt;
Learn how you can customise the default error response that a Spring REST service can provide to invalid or incomplete API Requests. This video will visit the ResponseEntityExceptionHandler to understand how the error response is created, and will use annotations such as @ControllerAdvice and @ExceptionHandler to build custom responses to specific thrown Exceptions from the REST controller.
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/REST%20API%20With%20Error%20Formatting/Store&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/REST%20API%20With%20Error%20Formatting/Store&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/qEDf-5F56XU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgT/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABoaUxYFi//8QAGhAAAwADAQAAAAAAAAAAAAAAAAERAgMhMf/aAAgBAQABBQLpGIiuz1Yqf//EABkRAAEFAAAAAAAAAAAAAAAAAAABAhETIf/aAAgBAwEBPwFWRpYf/8QAGhEAAgIDAAAAAAAAAAAAAAAAAQIAAwQRQf/aAAgBAgEBPwFLS7ahxwez/8QAGxAAAgEFAAAAAAAAAAAAAAAAAAEhAhARQbH/2gAIAQEABj8C7JtIgqMW/8QAGxAAAwEAAwEAAAAAAAAAAAAAAREhADFBUaH/2gAIAQEAAT8hVAOTCxfRcTfRzKQO8LZwuMHA+7//2gAMAwEAAgADAAAAEEcf/8QAGREBAAIDAAAAAAAAAAAAAAAAAQARQYHw/9oACAEDAQE/EANl7UGYn//EABsRAAIBBQAAAAAAAAAAAAAAAAARAWFxgbHh/9oACAECAQE/EECoxapIPTp//8QAHBABAAMAAgMAAAAAAAAAAAAAAQARITFxUWHw/9oACAEBAAE/EC/awBYhZGqrXUIqyaWbzuKABoWZ9zBKx8zISED0p//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/7343f1de934e7e41db572fb9f2ce014e/f93b5/093_errorResponse.jpg&quot;
        srcset=&quot;/static/7343f1de934e7e41db572fb9f2ce014e/8ddbc/093_errorResponse.jpg 75w,
/static/7343f1de934e7e41db572fb9f2ce014e/75985/093_errorResponse.jpg 150w,
/static/7343f1de934e7e41db572fb9f2ce014e/f93b5/093_errorResponse.jpg 300w,
/static/7343f1de934e7e41db572fb9f2ce014e/20e5d/093_errorResponse.jpg 450w,
/static/7343f1de934e7e41db572fb9f2ce014e/b4294/093_errorResponse.jpg 600w,
/static/7343f1de934e7e41db572fb9f2ce014e/eea4a/093_errorResponse.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Update a REST Controller with PUT, POST and DELETE endpoints]]></title><link>https://aneesh.co.uk/update-a-rest-controller-with-put-post-and-delete-endpoints</link><guid isPermaLink="false">https://aneesh.co.uk/update-a-rest-controller-with-put-post-and-delete-endpoints</guid><pubDate>Sun, 13 Feb 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Make PUT, POST and DELETE requests with a Spring web server&lt;/h4&gt;
&lt;p&gt;
Learn how to create DELETE, PUT and POST endpoints to a RESTful API service built with Spring Boot. We will create these endpoints in the controller layer, and then use the Postman API Platform to make it easier for us to test the endpoints. We then verify the output from our REST calls with the connected MySQL Database. 
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Store&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Store&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/aCOF_eWWpDw&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMCBP/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAHDpjFJF//EABsQAAIBBQAAAAAAAAAAAAAAAAERAAIDEiEx/9oACAEBAAEFAlvGCIO50Uhf/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAECERP/2gAIAQMBAT8BcKRof//EABoRAAICAwAAAAAAAAAAAAAAAAECAAMEEUH/2gAIAQIBAT8BS0u2occHs//EABkQAAIDAQAAAAAAAAAAAAAAAAAQAhEh4f/aAAgBAQAGPwLpqkUv/8QAGxAAAwEBAAMAAAAAAAAAAAAAAAERITFBcaH/2gAIAQEAAT8hqsbj0trUaXsa2armkoLyLacnBPC+n//aAAwDAQACAAMAAAAQAB//xAAZEQEAAgMAAAAAAAAAAAAAAAABABFBgfD/2gAIAQMBAT8QO5XtQZif/8QAGxEAAgEFAAAAAAAAAAAAAAAAAAERYXGRseH/2gAIAQIBAT8QgFLFqjCdOn//xAAdEAEBAAIBBQAAAAAAAAAAAAABEQAhMVFhcYHw/9oACAEBAAE/EBpwQDYcHE+1bnvCKuoqtecUADBTX3OCVHrNYSEDss//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/5de16c828b8bf2f40a400bce3f9d5ea6/f93b5/092_PutPostDelete.jpg&quot;
        srcset=&quot;/static/5de16c828b8bf2f40a400bce3f9d5ea6/8ddbc/092_PutPostDelete.jpg 75w,
/static/5de16c828b8bf2f40a400bce3f9d5ea6/75985/092_PutPostDelete.jpg 150w,
/static/5de16c828b8bf2f40a400bce3f9d5ea6/f93b5/092_PutPostDelete.jpg 300w,
/static/5de16c828b8bf2f40a400bce3f9d5ea6/20e5d/092_PutPostDelete.jpg 450w,
/static/5de16c828b8bf2f40a400bce3f9d5ea6/b4294/092_PutPostDelete.jpg 600w,
/static/5de16c828b8bf2f40a400bce3f9d5ea6/eea4a/092_PutPostDelete.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Connect a Spring Boot REST Server with a MySQL Database]]></title><description><![CDATA[Connect a Spring REST server with a MySQL Database to return persisted data to a GET request.]]></description><link>https://aneesh.co.uk/connect-a-spring-boot-rest-server-with-a-mysql-database</link><guid isPermaLink="false">https://aneesh.co.uk/connect-a-spring-boot-rest-server-with-a-mysql-database</guid><pubDate>Fri, 11 Feb 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Create a MySQL Database Connection and table&lt;/h4&gt;
&lt;p&gt;
This video will look at how we can connect our Spring Boot Application with a MySQL Database.
We will use the Spring data-jpa package to leverage a connection and to provide out-of-the-box 
methods and functionality to our controller. 
At the end of this video, we will have created 2 REST endpoints that obtain data from our MySQL database, 
and present them to the user as a JSON response.
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Store&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Store&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/KbXeVexjy7A&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMCBP/EABYBAQEBAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAABy6I4JJT/AP/EABkQAAMAAwAAAAAAAAAAAAAAAAABEQIDMf/aAAgBAQABBQKMjEQ2dWKn/8QAGREAAQUAAAAAAAAAAAAAAAAAAAECERMh/9oACAEDAQE/AVZGlh//xAAaEQACAgMAAAAAAAAAAAAAAAABAgADBBFB/9oACAECAQE/AUtLtqHHB7P/xAAZEAADAAMAAAAAAAAAAAAAAAAAEBEBIUH/2gAIAQEABj8C6bqyRf/EABwQAAICAgMAAAAAAAAAAAAAAAERADEhYVFxof/aAAgBAQABPyE1kINiFFQ7hNrIfMR7GC2UqgeA9n//2gAMAwEAAgADAAAAECwf/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQARQf/aAAgBAwEBPxATphnL/8QAGREAAgMBAAAAAAAAAAAAAAAAABEBgeGx/9oACAECAQE/EECorSQfGn//xAAbEAEBAQEBAAMAAAAAAAAAAAABEQAhUTFhcf/aAAgBAQABPxBosNJ8Th1E6qIe4irqC1z93RjaPXCVH2cxIQPpb//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/fe8dcda117d0bb1f77ed4aa954460d76/f93b5/091_SpringToMySqlpng.jpg&quot;
        srcset=&quot;/static/fe8dcda117d0bb1f77ed4aa954460d76/8ddbc/091_SpringToMySqlpng.jpg 75w,
/static/fe8dcda117d0bb1f77ed4aa954460d76/75985/091_SpringToMySqlpng.jpg 150w,
/static/fe8dcda117d0bb1f77ed4aa954460d76/f93b5/091_SpringToMySqlpng.jpg 300w,
/static/fe8dcda117d0bb1f77ed4aa954460d76/20e5d/091_SpringToMySqlpng.jpg 450w,
/static/fe8dcda117d0bb1f77ed4aa954460d76/b4294/091_SpringToMySqlpng.jpg 600w,
/static/fe8dcda117d0bb1f77ed4aa954460d76/eea4a/091_SpringToMySqlpng.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Create a MySQL Database table]]></title><description><![CDATA[Install and download MySQL and MySQL workbench and create a table with data.]]></description><link>https://aneesh.co.uk/create-a-mysql-database-table</link><guid isPermaLink="false">https://aneesh.co.uk/create-a-mysql-database-table</guid><pubDate>Wed, 09 Feb 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Create a MySQL Database Connection and table&lt;/h4&gt;
&lt;p&gt;
Learn how you can download MySQL and the MySQL Workbench to begin storing data with a database.
We will walk through where you can find the downloads, and how you can create a connection, schema and table with the MySQL Workbench.
We will also explore the various characteristics a field can have within a table, and what they mean. 
To conclude, we will create the table and populate it with some data. 
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Store&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Store&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/o4yPYWIRQG4&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgT/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABoaFRYFi//8QAGxAAAgIDAQAAAAAAAAAAAAAAAAECEgMRITL/2gAIAQEAAQUCrIqxEfWTrUVr/8QAGBEBAQADAAAAAAAAAAAAAAAAAQADERP/2gAIAQMBAT8BcYG7pf/EABoRAAICAwAAAAAAAAAAAAAAAAECAAMEEkH/2gAIAQIBAT8BS1mbWHHB7P/EABkQAAIDAQAAAAAAAAAAAAAAAAARAhAh4f/aAAgBAQAGPwLprRhK/wD/xAAcEAACAgIDAAAAAAAAAAAAAAABIQAREDFBcaH/2gAIAQEAAT8hYgbEEFo7QrvYcPGFB1UDQez/2gAMAwEAAgADAAAAEMMf/8QAGREBAAIDAAAAAAAAAAAAAAAAAQAhQYHw/9oACAEDAQE/ELAe1BmJ/8QAGxEAAgEFAAAAAAAAAAAAAAAAAAERIWGx0eH/2gAIAQIBAT8QoMkovsYTh0//xAAdEAEBAAIBBQAAAAAAAAAAAAABEQAhMUFRYXGh/9oACAEBAAE/ELoqTjjlJ3rfRhFULKrXvEtXlv3AG70rrCQgeFn/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/f93b5/090_MySqlTable.jpg&quot;
        srcset=&quot;/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/8ddbc/090_MySqlTable.jpg 75w,
/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/75985/090_MySqlTable.jpg 150w,
/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/f93b5/090_MySqlTable.jpg 300w,
/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/20e5d/090_MySqlTable.jpg 450w,
/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/b4294/090_MySqlTable.jpg 600w,
/static/3f70f4a9c8e21ceb9cd0ba643643c0b9/eea4a/090_MySqlTable.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Create a Responsive REST Controller]]></title><description><![CDATA[Learn how to create a responsive REST service with Spring Boot]]></description><link>https://aneesh.co.uk/create-a-responsive-rest-controller</link><guid isPermaLink="false">https://aneesh.co.uk/create-a-responsive-rest-controller</guid><pubDate>Mon, 07 Feb 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Create a REST Controller with Spring Boot&lt;/h4&gt;
&lt;p&gt;
Get started with the Spring initializer to build a Spring Boot Web Server. 
The web server will contain 
a Controller layer that will store endpoints that we interact with through the browser.
&lt;p&gt;We will register some GET endpoints that will return data to the user, and we will explore how
JSON data appears within the browser.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/c9iKKAZY00g&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMCBP/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAGeuiMUkX//xAAZEAADAAMAAAAAAAAAAAAAAAAAARECAzH/2gAIAQEAAQUCjIxEV2dWKn//xAAYEQADAQEAAAAAAAAAAAAAAAAAARECE//aAAgBAwEBPwF4ip0P/8QAGhEAAgIDAAAAAAAAAAAAAAAAAQIAAwQRQf/aAAgBAgEBPwFLS7ahxwez/8QAGhAAAgIDAAAAAAAAAAAAAAAAABEBECFhcf/aAAgBAQAGPwLmzLVSKv/EABwQAAICAwEBAAAAAAAAAAAAAAERADEhQVFxof/aAAgBAQABPyFhNmkHieoTayH2ancFspVA8B9n/9oADAMBAAIAAwAAABCEH//EABkRAQACAwAAAAAAAAAAAAAAAAEAEUGR8P/aAAgBAwEBPxAbj2oMxP/EABkRAAIDAQAAAAAAAAAAAAAAAAARAYHhsf/aAAgBAgEBPxBAqK0kHxp//8QAHRABAAMAAgMBAAAAAAAAAAAAAQARIVFhMUFx8P/aAAgBAQABPxCt8gAse4LajbRh78Qir2gu2fZQ4RqWZn5glY8zISEDpT//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/4cf6da262a7e6417074f52bdb432e706/f93b5/089_RestController.jpg&quot;
        srcset=&quot;/static/4cf6da262a7e6417074f52bdb432e706/8ddbc/089_RestController.jpg 75w,
/static/4cf6da262a7e6417074f52bdb432e706/75985/089_RestController.jpg 150w,
/static/4cf6da262a7e6417074f52bdb432e706/f93b5/089_RestController.jpg 300w,
/static/4cf6da262a7e6417074f52bdb432e706/20e5d/089_RestController.jpg 450w,
/static/4cf6da262a7e6417074f52bdb432e706/b4294/089_RestController.jpg 600w,
/static/4cf6da262a7e6417074f52bdb432e706/eea4a/089_RestController.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Syntax changes with Java 9]]></title><description><![CDATA[Look at the 3 main changes introduced with Java 9 Syntax]]></description><link>https://aneesh.co.uk/syntax-changes-with-java-9</link><guid isPermaLink="false">https://aneesh.co.uk/syntax-changes-with-java-9</guid><pubDate>Mon, 24 Jan 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Changing default mock behaviour&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;This video will explore the 3 key syntax changes that are introduced with Java 9 from Java 8.
First we will look at how collection factory methods can be written in a neater way for immutable entries.
Then we look at how the stream api has been enhanced to introduce methods to apply filters to an ordered collection, and to remove null values from the stream. Lastly we look at how private methods can be used within an interface, and when they may be required. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/DMUf_aUM5u0&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEEA//EABcBAAMBAAAAAAAAAAAAAAAAAAABAgP/2gAMAwEAAhADEAAAAUqocXsSFH//xAAaEAABBQEAAAAAAAAAAAAAAAABAAMRISIx/9oACAEBAAEFArm0FG3jsc//xAAYEQACAwAAAAAAAAAAAAAAAAAAAhITIf/aAAgBAwEBPwFkjpaf/8QAGhEAAgIDAAAAAAAAAAAAAAAAAAMBEQIEQf/aAAgBAgEBPwFbJZlROtfT/8QAGRAAAgMBAAAAAAAAAAAAAAAAABEBECFB/9oACAEBAAY/AlrNaqRcr//EABkQAQADAQEAAAAAAAAAAAAAAAEAESExUf/aAAgBAQABPyHStV0zInmEdjXLyUFrYik8choVP//aAAwDAQACAAMAAAAQRN//xAAZEQEAAgMAAAAAAAAAAAAAAAABABExQVH/2gAIAQMBAT8QAFnPYU1P/8QAGBEBAQEBAQAAAAAAAAAAAAAAAREAMbH/2gAIAQIBAT8QmISXmBL43//EAB4QAQACAQQDAAAAAAAAAAAAAAEAETEhQVGxYXGR/9oACAEBAAE/EAINyztBFvIKh1AZruyuj6wEQUz7iXECtkIVg8M//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/69c7848b10cb545936db33a1d1022ac4/f93b5/088_syntax.jpg&quot;
        srcset=&quot;/static/69c7848b10cb545936db33a1d1022ac4/8ddbc/088_syntax.jpg 75w,
/static/69c7848b10cb545936db33a1d1022ac4/75985/088_syntax.jpg 150w,
/static/69c7848b10cb545936db33a1d1022ac4/f93b5/088_syntax.jpg 300w,
/static/69c7848b10cb545936db33a1d1022ac4/20e5d/088_syntax.jpg 450w,
/static/69c7848b10cb545936db33a1d1022ac4/b4294/088_syntax.jpg 600w,
/static/69c7848b10cb545936db33a1d1022ac4/eea4a/088_syntax.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Java 9 Modularity - Java Tutorial]]></title><description><![CDATA[Create strong encapsulation with Java 9 modularity.]]></description><link>https://aneesh.co.uk/java-9-modularity-java-tutorial</link><guid isPermaLink="false">https://aneesh.co.uk/java-9-modularity-java-tutorial</guid><pubDate>Mon, 17 Jan 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Changing default mock behaviour&lt;/h4&gt;
&lt;p&gt;
Learn about how Java 9 enables strong encapsulation with modularity.
Modules add a further layer of encapsulation to our packages as we are able to specify which packages can be used by other modules, and which modules can have access to them.
This video will transform a multi-layered application to use modularity with Java 9 to demonstrate the benefits that can be realised.
&lt;p&gt;The source code to this video can be found on GitHub: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/Java9%20Modularity&quot;&gt;https://github.com/4neesh/YouTube-Channel/tree/main/Java9%20Modularity&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/89tplxrXJTU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMFBP/EABYBAQEBAAAAAAAAAAAAAAAAAAIDBP/aAAwDAQACEAMQAAABYmhOythhLn//xAAbEAACAQUAAAAAAAAAAAAAAAABAhIAAxARMf/aAAgBAQABBQKpNsYvkyXn/8QAGREAAQUAAAAAAAAAAAAAAAAAAAECEhNB/9oACAEDAQE/AXJHS0//xAAbEQABBAMAAAAAAAAAAAAAAAARAAECAwQSIf/aAAgBAgEBPwGqbWSGqfHPSv/EABoQAAICAwAAAAAAAAAAAAAAAAAQITERQaH/2gAIAQEABj8CrpNELGl//8QAGhAAAwADAQAAAAAAAAAAAAAAAAERITFBcf/aAAgBAQABPyF19hvjDwS06x7R9Il6aEQ//9oADAMBAAIAAwAAABCfD//EABcRAAMBAAAAAAAAAAAAAAAAAAABESH/2gAIAQMBAT8QelGMh//EABcRAQEBAQAAAAAAAAAAAAAAAAEAIcH/2gAIAQIBAT8QykQrhf/EAB4QAQACAgEFAAAAAAAAAAAAAAEAESExUUFhcaHB/9oACAEBAAE/EKRdm3ui4FlFNTz9jYAtgVTHKsXtMgbXeUFp15n/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/c9b059c86265d9f51c1b53aba6bbcc21/f93b5/087_java9Mod.jpg&quot;
        srcset=&quot;/static/c9b059c86265d9f51c1b53aba6bbcc21/8ddbc/087_java9Mod.jpg 75w,
/static/c9b059c86265d9f51c1b53aba6bbcc21/75985/087_java9Mod.jpg 150w,
/static/c9b059c86265d9f51c1b53aba6bbcc21/f93b5/087_java9Mod.jpg 300w,
/static/c9b059c86265d9f51c1b53aba6bbcc21/20e5d/087_java9Mod.jpg 450w,
/static/c9b059c86265d9f51c1b53aba6bbcc21/b4294/087_java9Mod.jpg 600w,
/static/c9b059c86265d9f51c1b53aba6bbcc21/d0df8/087_java9Mod.jpg 3737w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to use JShell - Java 9 Tutorial]]></title><description><![CDATA[Learn about the key functions and work processes when using JShell]]></description><link>https://aneesh.co.uk/how-to-use-jshell-java-9-tutorial</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-use-jshell-java-9-tutorial</guid><pubDate>Mon, 10 Jan 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;How to use JShell - Java 9 Tutorial&lt;/h4&gt;
&lt;p&gt;
JShell provides a REPL (Read, evaluate, print - loop) service for Java engineers to test their logic in a convenient way through a terminal. This video will look at how we can define different variables, methods and classes within JShell, how we can save our sessions and load external classes, and some of the most useful keywords from JShell.
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eP_a0zRQhZg&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCA//EABYBAQEBAAAAAAAAAAAAAAAAAAQCA//aAAwDAQACEAMQAAAB2pgFp0QlR//EABoQAAICAwAAAAAAAAAAAAAAAAABAgMSITH/2gAIAQEAAQUCHkIcUXacef/EABgRAAIDAAAAAAAAAAAAAAAAAAABAhMi/9oACAEDAQE/AZZRaf/EABsRAAIBBQAAAAAAAAAAAAAAAAARAQIDBBMh/9oACAECAQE/AbU7KkTjvrP/xAAZEAACAwEAAAAAAAAAAAAAAAAAEBEhMUH/2gAIAQEABj8C00pRxf/EABsQAAMAAgMAAAAAAAAAAAAAAAABESFBUXGh/9oACAEBAAE/IXbGhRWZtktOsd04fpirpYIh/9oADAMBAAIAAwAAABDvD//EABgRAAMBAQAAAAAAAAAAAAAAAAABIRGx/9oACAEDAQE/EEo+kTD/xAAXEQEBAQEAAAAAAAAAAAAAAAABAIHB/9oACAECAQE/EBIAYQrhf//EABwQAQEAAgMBAQAAAAAAAAAAAAERACExQVFhwf/aAAgBAQABPxCmwVrYDh6LsPxxsAVoVGvVcXW9/WJZo6Vy4Lh37n//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/d4fff30ff9a65c19b78f79a6bde68ced/f93b5/086_jshell.jpg&quot;
        srcset=&quot;/static/d4fff30ff9a65c19b78f79a6bde68ced/8ddbc/086_jshell.jpg 75w,
/static/d4fff30ff9a65c19b78f79a6bde68ced/75985/086_jshell.jpg 150w,
/static/d4fff30ff9a65c19b78f79a6bde68ced/f93b5/086_jshell.jpg 300w,
/static/d4fff30ff9a65c19b78f79a6bde68ced/20e5d/086_jshell.jpg 450w,
/static/d4fff30ff9a65c19b78f79a6bde68ced/b4294/086_jshell.jpg 600w,
/static/d4fff30ff9a65c19b78f79a6bde68ced/8f737/086_jshell.jpg 3738w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Changing default Mock behaviour]]></title><description><![CDATA[Use matchers to improve the readability of assertions with the assertJ library]]></description><link>https://aneesh.co.uk/changing-default-mock-behaviour</link><guid isPermaLink="false">https://aneesh.co.uk/changing-default-mock-behaviour</guid><pubDate>Mon, 03 Jan 2022 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Changing default mock behaviour&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;This video will explore the 3 different ways that a default Mock Object can be returned to the test class from a method call.
By understanding how mock Object behaviour can be changed, we can transform Null Object responses into mocked, Spy or real Object representations.
As a result, test methods can be more fluent and resistant to empty and null default values. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQCAwX/xAAWAQEBAQAAAAAAAAAAAAAAAAACAQT/2gAMAwEAAhADEAAAAbIt5mZsCI5//8QAGxAAAgEFAAAAAAAAAAAAAAAAAAEhAgMRMTL/2gAIAQEAAQUCljyLTpRe6UL/xAAXEQADAQAAAAAAAAAAAAAAAAAAAQIT/9oACAEDAQE/AaTk1P/EABkRAAIDAQAAAAAAAAAAAAAAAAERAAIDBP/aAAgBAgEBPwHG1dClDzMtz//EABsQAAEEAwAAAAAAAAAAAAAAAAABAhAxQWGh/9oACAEBAAY/Aq6aihW4j//EABoQAQADAQEBAAAAAAAAAAAAAAEAESFBMXH/2gAIAQEAAT8hoBVbrDADnWzWPIv6idGORo0s/9oADAMBAAIAAwAAABAbP//EABcRAQEBAQAAAAAAAAAAAAAAAAERAEH/2gAIAQMBAT8QAq3Ec3//xAAaEQACAgMAAAAAAAAAAAAAAAAAEQEhQWHh/9oACAECAQE/EEtGFY0ya6f/xAAbEAEAAgMBAQAAAAAAAAAAAAABABEhMUFxUf/aAAgBAQABPxApTvB7jkvXc0s+WZIwGzZrK0X9l0tnbLmZ0VFcEICG9z//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/edbc7f970130eed4f625d53e47d7a29c/f93b5/085_changeDefaultMock.jpg&quot;
        srcset=&quot;/static/edbc7f970130eed4f625d53e47d7a29c/8ddbc/085_changeDefaultMock.jpg 75w,
/static/edbc7f970130eed4f625d53e47d7a29c/75985/085_changeDefaultMock.jpg 150w,
/static/edbc7f970130eed4f625d53e47d7a29c/f93b5/085_changeDefaultMock.jpg 300w,
/static/edbc7f970130eed4f625d53e47d7a29c/20e5d/085_changeDefaultMock.jpg 450w,
/static/edbc7f970130eed4f625d53e47d7a29c/b4294/085_changeDefaultMock.jpg 600w,
/static/edbc7f970130eed4f625d53e47d7a29c/1bd04/085_changeDefaultMock.jpg 1699w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to stub void methods]]></title><description><![CDATA[Use matchers to improve the readability of assertions with the assertJ library]]></description><link>https://aneesh.co.uk/how-to-stub-void-methods</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-stub-void-methods</guid><pubDate>Mon, 27 Dec 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;How to stub a void method&lt;/h4&gt;
&lt;p&gt;
This video will look ath the different ways that we can control our system under test when using void methods with Mockito.
This video explores how we would want to handle void methods when using a mock or a spy, and how the answer functional interface also 
presents unique ways to provide custom logic to a void method, and when it would be most appropriate. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/4Vbp7rwQeLA&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIEBf/EABYBAQEBAAAAAAAAAAAAAAAAAAIBBP/aAAwDAQACEAMQAAABdLczM6CEc//EABsQAQABBQEAAAAAAAAAAAAAAAECAAMRITEy/9oACAEBAAEFAtork4xKveo8/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAECE//aAAgBAwEBPwGk5NT/xAAZEQACAwEAAAAAAAAAAAAAAAABEQACAwT/2gAIAQIBAT8BxvXQpQ8zLc//xAAbEAABBAMAAAAAAAAAAAAAAAABAAIQMRFBof/aAAgBAQAGPwKurBqS3Uf/xAAaEAEAAgMBAAAAAAAAAAAAAAABACERMUHw/9oACAEBAAE/IairKXBXV72WpqL5uJsU5Ehws//aAAwDAQACAAMAAAAQmz//xAAXEQEBAQEAAAAAAAAAAAAAAAABABFB/9oACAEDAQE/EANWMcv/xAAaEQACAgMAAAAAAAAAAAAAAAAAEQEhQWHh/9oACAECAQE/EENGFY0ya6f/xAAbEAEBAQEBAQEBAAAAAAAAAAABESEAMVFBYf/aAAgBAQABPxCaFKka/wA/OqCYaLZ8jmA00zVhfvWlt9a726JErDgQh773/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/17fa668e05eab6bc79b76034da6d3353/f93b5/084_stubVoid.jpg&quot;
        srcset=&quot;/static/17fa668e05eab6bc79b76034da6d3353/8ddbc/084_stubVoid.jpg 75w,
/static/17fa668e05eab6bc79b76034da6d3353/75985/084_stubVoid.jpg 150w,
/static/17fa668e05eab6bc79b76034da6d3353/f93b5/084_stubVoid.jpg 300w,
/static/17fa668e05eab6bc79b76034da6d3353/20e5d/084_stubVoid.jpg 450w,
/static/17fa668e05eab6bc79b76034da6d3353/b4294/084_stubVoid.jpg 600w,
/static/17fa668e05eab6bc79b76034da6d3353/e4c81/084_stubVoid.jpg 1697w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[ThenReturn vs DoReturn]]></title><description><![CDATA[Use matchers to improve the readability of assertions with the assertJ library]]></description><link>https://aneesh.co.uk/thenreturn-vs-doreturn</link><guid isPermaLink="false">https://aneesh.co.uk/thenreturn-vs-doreturn</guid><pubDate>Mon, 20 Dec 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;ThenReturn vs DoReturn&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Why would we use thenReturn over doReturn and vice versa with Mockito stubbing? This video compares the two methods to understand exactly how they work,
their pros and cons, and when they are most appropriate to use. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/cLo0oSl_9cU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQCAwX/xAAWAQEBAQAAAAAAAAAAAAAAAAACAQT/2gAMAwEAAhADEAAAAZ1vZmZsCI5//8QAGxAAAgIDAQAAAAAAAAAAAAAAAAECMQMSITL/2gAIAQEAAQUC6zYVOKM3qNf/xAAXEQADAQAAAAAAAAAAAAAAAAAAAQIT/9oACAEDAQE/AaTk1P/EABkRAAIDAQAAAAAAAAAAAAAAAAERAAIDBP/aAAgBAgEBPwHG9dClDzMtz//EABwQAAEDBQAAAAAAAAAAAAAAAAEAEDECESFBof/aAAgBAQAGPwKOq2QGhGnTf//EABsQAQACAgMAAAAAAAAAAAAAAAEAIRFBMXHR/9oACAEBAAE/IQK1lhbY2M+y9OIvtE2KaiQ4Wf/aAAwDAQACAAMAAAAQyy//xAAXEQEBAQEAAAAAAAAAAAAAAAABABFB/9oACAEDAQE/EANWMcv/xAAaEQACAgMAAAAAAAAAAAAAAAAAEQEhQWHh/9oACAECAQE/EENGFY0ya6f/xAAbEAEAAwEAAwAAAAAAAAAAAAABABEhMVFxgf/aAAgBAQABPxAA1m3QbvyUIKasPqmkQDZ1WrRfmXShetdm6KyrYQEId7P/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/a9133bba479f96ee89b72023302f3c12/f93b5/083_thenVsDo.jpg&quot;
        srcset=&quot;/static/a9133bba479f96ee89b72023302f3c12/8ddbc/083_thenVsDo.jpg 75w,
/static/a9133bba479f96ee89b72023302f3c12/75985/083_thenVsDo.jpg 150w,
/static/a9133bba479f96ee89b72023302f3c12/f93b5/083_thenVsDo.jpg 300w,
/static/a9133bba479f96ee89b72023302f3c12/20e5d/083_thenVsDo.jpg 450w,
/static/a9133bba479f96ee89b72023302f3c12/b4294/083_thenVsDo.jpg 600w,
/static/a9133bba479f96ee89b72023302f3c12/53d10/083_thenVsDo.jpg 1696w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Testing the Entity Manager API]]></title><description><![CDATA[Use matchers to improve the readability of assertions with the assertJ library]]></description><link>https://aneesh.co.uk/testing-the-entity-manager-api</link><guid isPermaLink="false">https://aneesh.co.uk/testing-the-entity-manager-api</guid><pubDate>Mon, 13 Dec 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Testing the EntityManager&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Use Mockito to create stubbed query responses when testing implementations of the Entity Manager API.
The Entity Manager API is used to create instances for persistance that fall on the repository layer of a Spring Boot application.
The Entity Manager will manage the lifecycle fo the entity instances for persistance, and can be highly important when persisting and managing data for an application.&lt;/p&gt;
&lt;p&gt;This video will use Mockito to stub an EntityManager and Query to control the response of an EntityManager for testing a simple select query.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/daHupokH4us&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgX/xAAWAQEBAQAAAAAAAAAAAAAAAAACAQT/2gAMAwEAAhADEAAAAbw3mZmwIjn/xAAbEAACAgMBAAAAAAAAAAAAAAAAAQIxAxEhMv/aAAgBAQABBQLrHsVOKM3qNf/EABcRAAMBAAAAAAAAAAAAAAAAAAABAhP/2gAIAQMBAT8BpOTU/8QAGREAAgMBAAAAAAAAAAAAAAAAAREAAgME/9oACAECAQE/Acb10KUPMy3P/8QAGhAAAgIDAAAAAAAAAAAAAAAAADEBEAIRQf/aAAgBAQAGPwJGpVInHlf/xAAaEAEAAgMBAAAAAAAAAAAAAAABACERMUFx/9oACAEBAAE/IajKy7uMBR1cvTUX6ibFORIcLP/aAAwDAQACAAMAAAAQCy//xAAXEQEBAQEAAAAAAAAAAAAAAAABABFB/9oACAEDAQE/EANWMcv/xAAaEQACAgMAAAAAAAAAAAAAAAAAEQEhQWHh/9oACAECAQE/EENGFY0ya6f/xAAcEAEBAQEAAgMAAAAAAAAAAAABESEAMUFRgZH/2gAIAQEAAT8QiEbaZrv110dqKz0U0/eQDTTNWF+XrShfLXe2RInocCEPPnv/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/52384a1ed581e99fb8e1c7498312a101/f93b5/082_entityManager.jpg&quot;
        srcset=&quot;/static/52384a1ed581e99fb8e1c7498312a101/8ddbc/082_entityManager.jpg 75w,
/static/52384a1ed581e99fb8e1c7498312a101/75985/082_entityManager.jpg 150w,
/static/52384a1ed581e99fb8e1c7498312a101/f93b5/082_entityManager.jpg 300w,
/static/52384a1ed581e99fb8e1c7498312a101/20e5d/082_entityManager.jpg 450w,
/static/52384a1ed581e99fb8e1c7498312a101/b4294/082_entityManager.jpg 600w,
/static/52384a1ed581e99fb8e1c7498312a101/53d10/082_entityManager.jpg 1696w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Intro to Mockito Answers]]></title><description><![CDATA[Explore Mockito Answers and understand how they can support dynamic mocking of method calls.]]></description><link>https://aneesh.co.uk/intro-to-mockito-answers</link><guid isPermaLink="false">https://aneesh.co.uk/intro-to-mockito-answers</guid><pubDate>Mon, 06 Dec 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Mockito Answers&lt;/h4&gt;
&lt;p&gt;
Learn how you can achieve dynamic method stubbing with the Answer functional interface and the benefits it can enable over hard-coding stubbed methods. This video will explore the challenges of hard-coded method responses with stubbing and how an Answer implementation can reduce boilerplate code from a test class. 
#Mockito #SoftwareTesting #JUnit
&lt;p&gt;&lt;a href=&quot;https://youtu.be/aariHjpng90&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMFAf/EABYBAQEBAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAABnvXl50ieJ//EABkQAQEBAAMAAAAAAAAAAAAAAAECEQADQv/aAAgBAQABBQKcyc5IJ2gPmVD/xAAYEQEBAQEBAAAAAAAAAAAAAAABAgASQf/aAAgBAwEBPwGUTrMw+b//xAAZEQACAwEAAAAAAAAAAAAAAAAAARESIVH/2gAIAQIBAT8Ba2BW6f/EABcQAQADAAAAAAAAAAAAAAAAABABITH/2gAIAQEABj8CjGn/xAAaEAEAAgMBAAAAAAAAAAAAAAABABEhMUFh/9oACAEBAAE/ITnJfZd1JecwQir3DiAaUn//2gAMAwEAAgADAAAAEOv/AP/EABYRAQEBAAAAAAAAAAAAAAAAAAEAQf/aAAgBAwEBPxAkNlFZ/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAR/9oACAECAQE/EEE5Ayf/xAAbEAEAAwADAQAAAAAAAAAAAAABABEhMUFxgf/aAAgBAQABPxB503bb9yNaAjyVnktRRaDdtX3kPam47j8kbgAZzjP/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/7fad5410bfe2bf9ebd55a15517982ce3/f93b5/081_mockitoAnswer.jpg&quot;
        srcset=&quot;/static/7fad5410bfe2bf9ebd55a15517982ce3/8ddbc/081_mockitoAnswer.jpg 75w,
/static/7fad5410bfe2bf9ebd55a15517982ce3/75985/081_mockitoAnswer.jpg 150w,
/static/7fad5410bfe2bf9ebd55a15517982ce3/f93b5/081_mockitoAnswer.jpg 300w,
/static/7fad5410bfe2bf9ebd55a15517982ce3/20e5d/081_mockitoAnswer.jpg 450w,
/static/7fad5410bfe2bf9ebd55a15517982ce3/b4294/081_mockitoAnswer.jpg 600w,
/static/7fad5410bfe2bf9ebd55a15517982ce3/a8a02/081_mockitoAnswer.jpg 1702w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Test secondary method calls with argument matchers]]></title><description><![CDATA[Use argument matchers to verify the subsequent behaviour of secondary method calls]]></description><link>https://aneesh.co.uk/test-secondary-method-calls-with-argument-matchers</link><guid isPermaLink="false">https://aneesh.co.uk/test-secondary-method-calls-with-argument-matchers</guid><pubDate>Mon, 29 Nov 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Using Mockito argument matchers&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Learn how to use Mockito argument matchers to verify indirect inputs for secondary method calls. This video will walk through an example of how an indirect input is created, where a secondary method call is made, and how we can generalise these behaviours to be tested with Mockito argument matchers. &lt;/p&gt;
&lt;h1&gt;Mockito #SoftwareTesting #JUnit&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/VZYIQgRsaJA&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAUBAgP/xAAWAQEBAQAAAAAAAAAAAAAAAAABAgP/2gAMAwEAAhADEAAAAV+0UvNkLxP/xAAZEAEBAAMBAAAAAAAAAAAAAAABAgADEUL/2gAIAQEAAQUCnnIyQTYs55lQ/8QAGREAAwADAAAAAAAAAAAAAAAAAAECEiFB/9oACAEDAQE/AYeshzD4f//EABkRAAIDAQAAAAAAAAAAAAAAAAABERIhUf/aAAgBAgEBPwFrYFbp/8QAGBAAAgMAAAAAAAAAAAAAAAAAARAREkH/2gAIAQEABj8Cxiph/wD/xAAaEAADAQADAAAAAAAAAAAAAAAAAREhYXGR/9oACAEBAAE/Ic90beuclzo9o6MT9CDG0f/aAAwDAQACAAMAAAAQO/8A/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQARQf/aAAgBAwEBPxA3DsorP//EABcRAQEBAQAAAAAAAAAAAAAAAAEAEXH/2gAIAQIBAT8QfjIGT//EABwQAQEAAwEAAwAAAAAAAAAAAAERACExQVFhkf/aAAgBAQABPxATyjbq5CVF2Oz8yqKKg2svus7QTpJ88xlC+l+8ADO6c//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/40776859e2a6fc073e4f463ebdcda817/f93b5/080_argMatch.jpg&quot;
        srcset=&quot;/static/40776859e2a6fc073e4f463ebdcda817/8ddbc/080_argMatch.jpg 75w,
/static/40776859e2a6fc073e4f463ebdcda817/75985/080_argMatch.jpg 150w,
/static/40776859e2a6fc073e4f463ebdcda817/f93b5/080_argMatch.jpg 300w,
/static/40776859e2a6fc073e4f463ebdcda817/20e5d/080_argMatch.jpg 450w,
/static/40776859e2a6fc073e4f463ebdcda817/b4294/080_argMatch.jpg 600w,
/static/40776859e2a6fc073e4f463ebdcda817/a8a02/080_argMatch.jpg 1702w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Stubbing consecutive calls with Mockito]]></title><description><![CDATA[Use thenReturn from Mockito to efficiently stub different method responses with Mockito]]></description><link>https://aneesh.co.uk/stubbing-consecutive-calls-with-mockito</link><guid isPermaLink="false">https://aneesh.co.uk/stubbing-consecutive-calls-with-mockito</guid><pubDate>Mon, 22 Nov 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Stubbing consecutive methods&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Learn how Mockito supports the testing of multiple method outcomes in a concise and readable way with consecutive stubbed calls. This video will explore when we may need to stub and test different method outcomes, and how you can achieve this with Mockito without repeating multiple lines of test code.&lt;/p&gt;
&lt;h1&gt;Mockito #SoftwareTesting #JUnit&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/cRRwgcX3UVs&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMFAQT/xAAWAQEBAQAAAAAAAAAAAAAAAAABAgP/2gAMAwEAAhADEAAAAeB6svOkTxP/xAAaEAEBAAIDAAAAAAAAAAAAAAABAgMREjFC/9oACAEBAAEFAp1xnuQTKA+ZUP/EABgRAQEBAQEAAAAAAAAAAAAAAAECABJB/9oACAEDAQE/AZROszD5v//EABgRAQADAQAAAAAAAAAAAAAAAAEAERJR/9oACAECAQE/AUbqGuz/xAAXEAEAAwAAAAAAAAAAAAAAAAAQASEx/9oACAEBAAY/Aoxp/8QAGxABAQEAAgMAAAAAAAAAAAAAAREAITFBUWH/2gAIAQEAAT8hPMb+6OAk9mu64QiXvHhgMU3/2gAMAwEAAgADAAAAEGv/AP/EABYRAQEBAAAAAAAAAAAAAAAAAAEAMf/aAAgBAwEBPxAsDZRWf//EABYRAQEBAAAAAAAAAAAAAAAAAAEAEf/aAAgBAgEBPxACqBk//8QAHBABAAMAAgMAAAAAAAAAAAAAAQARITFBYaHB/9oACAEBAAE/ED6MaLT1K6iKv5stFC0G7avvIe1Nx3G+JG4AGc4z/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/e1a7cd6bc13a5463759b3a619070ead0/f93b5/079_multiStub.jpg&quot;
        srcset=&quot;/static/e1a7cd6bc13a5463759b3a619070ead0/8ddbc/079_multiStub.jpg 75w,
/static/e1a7cd6bc13a5463759b3a619070ead0/75985/079_multiStub.jpg 150w,
/static/e1a7cd6bc13a5463759b3a619070ead0/f93b5/079_multiStub.jpg 300w,
/static/e1a7cd6bc13a5463759b3a619070ead0/20e5d/079_multiStub.jpg 450w,
/static/e1a7cd6bc13a5463759b3a619070ead0/b4294/079_multiStub.jpg 600w,
/static/e1a7cd6bc13a5463759b3a619070ead0/a8a02/079_multiStub.jpg 1702w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Exploring Collection API Enhancements from Java 8]]></title><description><![CDATA[Explore the various enhancements introduced with Java 8 to eliminate some of the boilerplate operations we perform on our collections.]]></description><link>https://aneesh.co.uk/exploring-collection-api-enhancements-from-java-8</link><guid isPermaLink="false">https://aneesh.co.uk/exploring-collection-api-enhancements-from-java-8</guid><pubDate>Mon, 15 Nov 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java Collection API Enhancements&lt;/h4&gt;
&lt;p&gt;
Learn how and when to use the Collection API enhancements from Java 8 to reduce repetitive and error-prone code when handling and transforming collections. 
This video will include alternative ways to iterate a list, to navigate and sort a map, and to replace or merge values of a map with new API methods. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/Ez9WNkubfuQ&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAQP/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABV66pLVJRR//EABwQAAICAgMAAAAAAAAAAAAAAAECABEEEhMhQv/aAAgBAQABBQJOPUFTFc1loqzzfX//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARIh/9oACAEDAQE/AZxZ/8QAGREAAgMBAAAAAAAAAAAAAAAAAAECEhMh/9oACAECAQE/AYK3TNH/xAAaEAACAgMAAAAAAAAAAAAAAAABERAhACIx/9oACAEBAAY/Ag081qAQLPZ//8QAGhABAAIDAQAAAAAAAAAAAAAAAQARITFBcf/aAAgBAQABPyHUMGbqBUE3nkAQbOXEDhX1B0ggUs//2gAMAwEAAgADAAAAEBAP/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAERMUH/2gAIAQMBAT8QajJ4f//EABcRAQEBAQAAAAAAAAAAAAAAAAEAESH/2gAIAQIBAT8QAYt+pf/EABwQAQEAAgIDAAAAAAAAAAAAAAERAGEhUTFBof/aAAgBAQABPxAAynzOTveCkCkAp9yTCtKsveMZQR7cY2hG4WCJ3n//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/46b98a61332bdf5d5b372d2d224616e1/f93b5/078_apiEnhance.jpg&quot;
        srcset=&quot;/static/46b98a61332bdf5d5b372d2d224616e1/8ddbc/078_apiEnhance.jpg 75w,
/static/46b98a61332bdf5d5b372d2d224616e1/75985/078_apiEnhance.jpg 150w,
/static/46b98a61332bdf5d5b372d2d224616e1/f93b5/078_apiEnhance.jpg 300w,
/static/46b98a61332bdf5d5b372d2d224616e1/20e5d/078_apiEnhance.jpg 450w,
/static/46b98a61332bdf5d5b372d2d224616e1/b4294/078_apiEnhance.jpg 600w,
/static/46b98a61332bdf5d5b372d2d224616e1/b5c84/078_apiEnhance.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Intro to the Java 8 Optional class]]></title><description><![CDATA[Understand why the Optional class was introduced with Java 8 and how you can use it to improve and reduce verbose code for Null checking.]]></description><link>https://aneesh.co.uk/intro-to-the-java-8-optional-class</link><guid isPermaLink="false">https://aneesh.co.uk/intro-to-the-java-8-optional-class</guid><pubDate>Mon, 08 Nov 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java 8 Optional Class&lt;/h4&gt;
&lt;p&gt;
This video will explore the Optional class and the API methods that are introduced with it in Java 8. 
The Optional class will enable you to check for null, perform default operations if a value returns null, and also use custom logic as a response on a single line.
&lt;p&gt;No longer will you be required to perform repetitive and cumbersome null pointer checks in your code as you can
conveniently handle potential nulls values with the Optional class API.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/79wLNFwZRho&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAGV3kl1TKKP/8QAGxAAAgMAAwAAAAAAAAAAAAAAAQIAERIEIUL/2gAIAQEAAQUCTGVIMVzXLRVnm+v/xAAXEQEAAwAAAAAAAAAAAAAAAAAAARIh/9oACAEDAQE/AZxZ/8QAGREAAQUAAAAAAAAAAAAAAAAAAAECEyEi/9oACAECAQE/AWasjQ//xAAaEAEAAgMBAAAAAAAAAAAAAAABEBEAITFR/9oACAEBAAY/Ai6vPIENvZ//xAAcEAEAAgIDAQAAAAAAAAAAAAABABEhMUFRYXH/2gAIAQEAAT8hwwNPJhAD2bgCDZxcQOFfqDpBApZ//9oADAMBAAIAAwAAABAQD//EABgRAAIDAAAAAAAAAAAAAAAAAAARASFB/9oACAEDAQE/EJNY+H//xAAXEQEBAQEAAAAAAAAAAAAAAAABABEh/9oACAECAQE/EADFv1L/xAAdEAEAAgICAwAAAAAAAAAAAAABABEhMUFRYaHw/9oACAEBAAE/EDVQKrkud/dTcM6KR7lTCuytq+4xlBHLEbwI3CwRXc//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/69abeb364e4629daccfd6599572cdf81/f93b5/077_optional.jpg&quot;
        srcset=&quot;/static/69abeb364e4629daccfd6599572cdf81/8ddbc/077_optional.jpg 75w,
/static/69abeb364e4629daccfd6599572cdf81/75985/077_optional.jpg 150w,
/static/69abeb364e4629daccfd6599572cdf81/f93b5/077_optional.jpg 300w,
/static/69abeb364e4629daccfd6599572cdf81/20e5d/077_optional.jpg 450w,
/static/69abeb364e4629daccfd6599572cdf81/b4294/077_optional.jpg 600w,
/static/69abeb364e4629daccfd6599572cdf81/b5c84/077_optional.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using method references with Java 8]]></title><description><![CDATA[Begin using method references to improve the readability of your Lambda expressions.]]></description><link>https://aneesh.co.uk/using-method-references-with-java-8</link><guid isPermaLink="false">https://aneesh.co.uk/using-method-references-with-java-8</guid><pubDate>Mon, 01 Nov 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Method References in Java 8&lt;/h4&gt;
&lt;p&gt;
Learn how, and when you can use method references to improve the readability of your Lambda functions. 
Introduced with Java 8, method references can be used to replace lambda expressions that point to a class method, an instance method, and a class constructor. 
By understanding how and when method references can be used, you will reduce the boilerplate code written for your lambda expressions. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/UIJLegcCFyw&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAGV3kl1TKKP/8QAGxAAAgMAAwAAAAAAAAAAAAAAAQIAERIEIUL/2gAIAQEAAQUCTGQQYrmuWirPN9f/xAAXEQEAAwAAAAAAAAAAAAAAAAAAARIh/9oACAEDAQE/AZxZ/8QAGREAAQUAAAAAAAAAAAAAAAAAAAECEyEi/9oACAECAQE/AWasjQ//xAAZEAEAAwEBAAAAAAAAAAAAAAABEBEhADH/2gAIAQEABj8CLC+zCBDX2f/EABoQAQADAQEBAAAAAAAAAAAAAAEAESFxMUH/2gAIAQEAAT8hzMLbqM45QBBs+XEDhXqD4ggUs//aAAwDAQACAAMAAAAQEA//xAAYEQACAwAAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxBqMnh//8QAFxEBAQEBAAAAAAAAAAAAAAAAAQARIf/aAAgBAgEBPxAAxb9S/8QAHBABAQEAAgMBAAAAAAAAAAAAAREAIVExQWGx/9oACAEBAAE/EGZsClS84wAD5EX950mFaVZe8xlBHtxm+CNxYIne/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/e67926306aaaeeff02c9957902ae083b/f93b5/076_methodRef.jpg&quot;
        srcset=&quot;/static/e67926306aaaeeff02c9957902ae083b/8ddbc/076_methodRef.jpg 75w,
/static/e67926306aaaeeff02c9957902ae083b/75985/076_methodRef.jpg 150w,
/static/e67926306aaaeeff02c9957902ae083b/f93b5/076_methodRef.jpg 300w,
/static/e67926306aaaeeff02c9957902ae083b/20e5d/076_methodRef.jpg 450w,
/static/e67926306aaaeeff02c9957902ae083b/b4294/076_methodRef.jpg 600w,
/static/e67926306aaaeeff02c9957902ae083b/b5c84/076_methodRef.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using the Java 8 Stream API]]></title><description><![CDATA[Explore the various functionality that can be achieved with the stream API to transform and collect data.]]></description><link>https://aneesh.co.uk/using-the-java-8-stream-api</link><guid isPermaLink="false">https://aneesh.co.uk/using-the-java-8-stream-api</guid><pubDate>Mon, 25 Oct 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java Stream API&lt;/h4&gt;
&lt;p&gt;
This video will take a look at the Java 8 Stream, an incredibly powerful and useful API that enables us to navigate, transform and extract from Collections of Objects into new collections or Objects. 
We will firstly understand the different ways that we can create a stream and their advantages. Second, we will look at the vast array of functions that allow us to operate upon the Collections, and then lastly explore the various ways that we can aggregate our stream into a new Object or Collection. 
&lt;p&gt;Link to the Oracle Stream API documentation: &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/YnhaGNAh8fU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAgP/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABU6bUlqkoo//EABsQAAIDAAMAAAAAAAAAAAAAAAECABESBCFC/9oACAEBAAEFAkxkEGK5rloqzzfX/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAESIf/aAAgBAwEBPwGcWf/EABkRAAEFAAAAAAAAAAAAAAAAAAABAhMhIv/aAAgBAgEBPwFmrI0P/8QAGRABAAMBAQAAAAAAAAAAAAAAARARIQAx/9oACAEBAAY/Ai6vswgQ19n/xAAaEAEAAwEBAQAAAAAAAAAAAAABABEhMUFx/9oACAEBAAE/IcYGmxzG3fYAg2eXEDhX6g8QQKWf/9oADAMBAAIAAwAAABDQD//EABcRAAMBAAAAAAAAAAAAAAAAAAABQRH/2gAIAQMBAT8QajcP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQARIf/aAAgBAgEBPxAAxb9S/8QAGxABAQEAAwEBAAAAAAAAAAAAAREAQVFhITH/2gAIAQEAAT8QKNH9pV9zUNKgkdc6TCtKsveYygjl8zeCNxYIne//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/b549e683c51d59a6b6371f18141a0f07/f93b5/075_stream.jpg&quot;
        srcset=&quot;/static/b549e683c51d59a6b6371f18141a0f07/8ddbc/075_stream.jpg 75w,
/static/b549e683c51d59a6b6371f18141a0f07/75985/075_stream.jpg 150w,
/static/b549e683c51d59a6b6371f18141a0f07/f93b5/075_stream.jpg 300w,
/static/b549e683c51d59a6b6371f18141a0f07/20e5d/075_stream.jpg 450w,
/static/b549e683c51d59a6b6371f18141a0f07/b4294/075_stream.jpg 600w,
/static/b549e683c51d59a6b6371f18141a0f07/b5c84/075_stream.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Out of the box functional interfaces with Java 8]]></title><description><![CDATA[Explore the common and most useful functional interfaces with Java 8.]]></description><link>https://aneesh.co.uk/out-of-the-box-functional-interfaces-with-java-8</link><guid isPermaLink="false">https://aneesh.co.uk/out-of-the-box-functional-interfaces-with-java-8</guid><pubDate>Mon, 18 Oct 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Java 8 functional interfaces&lt;/h4&gt;
&lt;p&gt;
Functional interfaces are an important component to Java 8 in it&apos;s strive towards more functional programming styles to complement the Object-orientated language. 
&lt;p&gt;This video will look at 4 key functional interfaces provided in Java 8 to support Lambda functional writing.
First we will look at why they are needed and how they work, then we will explore the extended functional interfaces such as the unary and binary operator. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/0zcnv4hVvVw&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAQFAf/EABUBAQEAAAAAAAAAAAAAAAAAAAIB/9oADAMBAAIQAxAAAAFbGkhaxKGf/8QAHRAAAgIBBQAAAAAAAAAAAAAAAQIDEQAEEiEiMf/aAAgBAQABBQKEqEX2NuurGzLsLwP/xAAXEQEAAwAAAAAAAAAAAAAAAAAAARIh/9oACAEDAQE/AZxd/8QAGREAAQUAAAAAAAAAAAAAAAAAAAECEyEi/9oACAECAQE/AWasiQ//xAAZEAACAwEAAAAAAAAAAAAAAAABEQAQIQL/2gAIAQEABj8CDVg84zs2v//EABkQAQADAQEAAAAAAAAAAAAAAAEAESFBMf/aAAgBAQABPyG0lr7Nra3kYqNl5ewPds07BWq19icVn//aAAwDAQACAAMAAAAQyz//xAAYEQACAwAAAAAAAAAAAAAAAAAAARExQf/aAAgBAwEBPxB6Mlh//8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARIUH/2gAIAQIBAT8QIKdlupP/xAAZEAEBAQEBAQAAAAAAAAAAAAABEQAxcWH/2gAIAQEAAT8QBjWYizMuh0rh8dWA0JUHuh+sHKnZlyKaeuEBj3f/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/cbcfd4ec471630e2c7d640dda3f56a78/f93b5/074_functionalinterface.jpg&quot;
        srcset=&quot;/static/cbcfd4ec471630e2c7d640dda3f56a78/8ddbc/074_functionalinterface.jpg 75w,
/static/cbcfd4ec471630e2c7d640dda3f56a78/75985/074_functionalinterface.jpg 150w,
/static/cbcfd4ec471630e2c7d640dda3f56a78/f93b5/074_functionalinterface.jpg 300w,
/static/cbcfd4ec471630e2c7d640dda3f56a78/20e5d/074_functionalinterface.jpg 450w,
/static/cbcfd4ec471630e2c7d640dda3f56a78/b4294/074_functionalinterface.jpg 600w,
/static/cbcfd4ec471630e2c7d640dda3f56a78/e4c81/074_functionalinterface.jpg 1697w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating default methods within interfaces]]></title><description><![CDATA[Use default methods to create backward-compatible functionality within interfaces with Java 8.]]></description><link>https://aneesh.co.uk/creating-default-methods-within-interfaces</link><guid isPermaLink="false">https://aneesh.co.uk/creating-default-methods-within-interfaces</guid><pubDate>Mon, 11 Oct 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Interface Default methods&lt;/h4&gt;
&lt;p&gt;
Explore the benefits of the default keyword that has been introduced with interfaces within Java 8. 
We will look at when and how you can use the default keyword, how interfaces will now compare with the abstract class, and examine the 
corner case condition where a class may implement two interfaces with the identical default method signatures. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/T-RqIQTGnes&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAgP/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABU6bUlqkoo//EABsQAQACAgMAAAAAAAAAAAAAAAIAEQESBCFC/9oACAEBAAEFAjpqFcLzXLBM831//8QAFxEBAAMAAAAAAAAAAAAAAAAAAAESIf/aAAgBAwEBPwGcWf/EABkRAAEFAAAAAAAAAAAAAAAAAAABAhMhIv/aAAgBAgEBPwFmrI0P/8QAGhABAAIDAQAAAAAAAAAAAAAAARARACExcf/aAAgBAQAGPwI0XWcDyBDb2f/EABwQAQACAQUAAAAAAAAAAAAAAAEAESExQWFxkf/aAAgBAQABPyHFPKVKHoEAQbNriBwr2g6IIFLP/9oADAMBAAIAAwAAABAQD//EABgRAAIDAAAAAAAAAAAAAAAAAAARASFB/9oACAEDAQE/EJNY+H//xAAXEQEBAQEAAAAAAAAAAAAAAAABABEh/9oACAECAQE/EADFv1L/xAAbEAEBAAIDAQAAAAAAAAAAAAABEQBhITFRQf/aAAgBAQABPxDjEIqg3eFCN7gQ13kmFaVZfcYygj64xtCNwsET3P/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/ccbc482a5b33859d1400a894ed1e660c/f93b5/073_default.jpg&quot;
        srcset=&quot;/static/ccbc482a5b33859d1400a894ed1e660c/8ddbc/073_default.jpg 75w,
/static/ccbc482a5b33859d1400a894ed1e660c/75985/073_default.jpg 150w,
/static/ccbc482a5b33859d1400a894ed1e660c/f93b5/073_default.jpg 300w,
/static/ccbc482a5b33859d1400a894ed1e660c/20e5d/073_default.jpg 450w,
/static/ccbc482a5b33859d1400a894ed1e660c/b4294/073_default.jpg 600w,
/static/ccbc482a5b33859d1400a894ed1e660c/b5c84/073_default.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[When and how to use Lambda expressions]]></title><description><![CDATA[Understand how to create functional interface instances with a Lambda expression with Java 8]]></description><link>https://aneesh.co.uk/when-and-how-to-use-lambda-expressions</link><guid isPermaLink="false">https://aneesh.co.uk/when-and-how-to-use-lambda-expressions</guid><pubDate>Mon, 04 Oct 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Lambda Expressions&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Lambda expressions are a new feature from Java 8 that allow you to efficiently create instances of a functional interface with a new syntax: () -&gt; {}.
This video will take a look at functional interfaces and how we can instantiate them. We will finally look at the lambda expression syntax and how it relates back to the functional interface. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/IKikmm1ikk0&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAQP/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABV3qpLVJRR//EABsQAAEEAwAAAAAAAAAAAAAAAAIAARESBCFC/9oACAEBAAEFAgpQYQm8ZYCK5nX/xAAXEQEAAwAAAAAAAAAAAAAAAAAAARIh/9oACAEDAQE/AZxZ/8QAGREAAQUAAAAAAAAAAAAAAAAAAAECEyEi/9oACAECAQE/AWasjQ//xAAZEAEBAAMBAAAAAAAAAAAAAAABEQAQITH/2gAIAQEABj8CjLnFmhDr7v8A/8QAGxABAAMBAAMAAAAAAAAAAAAAAQAhMRFBUXH/2gAIAQEAAT8hpBVrsvqezsAQenjsQOFfqDiCBxZ//9oADAMBAAIAAwAAABBQD//EABgRAAIDAAAAAAAAAAAAAAAAAAARASFB/9oACAEDAQE/EJtY+H//xAAYEQEAAwEAAAAAAAAAAAAAAAABABEhQf/aAAgBAgEBPxAAp2X6k//EABwQAQEBAAIDAQAAAAAAAAAAAAERACFRMUFh0f/aAAgBAQABPxAzs6UIf3EElOYH140mFaVZe8xlBHtxm+CNxYIne//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/22dfa81d852557f155a39556a9ee5bc0/f93b5/072_lambda.jpg&quot;
        srcset=&quot;/static/22dfa81d852557f155a39556a9ee5bc0/8ddbc/072_lambda.jpg 75w,
/static/22dfa81d852557f155a39556a9ee5bc0/75985/072_lambda.jpg 150w,
/static/22dfa81d852557f155a39556a9ee5bc0/f93b5/072_lambda.jpg 300w,
/static/22dfa81d852557f155a39556a9ee5bc0/20e5d/072_lambda.jpg 450w,
/static/22dfa81d852557f155a39556a9ee5bc0/b4294/072_lambda.jpg 600w,
/static/22dfa81d852557f155a39556a9ee5bc0/b5c84/072_lambda.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[When and how to use the Singleton design pattern]]></title><description><![CDATA[Understand when the singleton design pattern can be useful and implemented within your application]]></description><link>https://aneesh.co.uk/when-and-how-to-use-the-singleton-design-pattern</link><guid isPermaLink="false">https://aneesh.co.uk/when-and-how-to-use-the-singleton-design-pattern</guid><pubDate>Mon, 27 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;The singleton design pattern&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;The singleton design pattern is one of the most common design patterns you may come across within object-orientated software development.
We use the Singleton pattern when we only want a single instance of our Object to be created and coordinated across our application.
The Singleton design pattern is implemented in just 3 simple steps.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/2JF3ESeh6S4&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEDBP/EABcBAAMBAAAAAAAAAAAAAAAAAAECAwT/2gAMAwEAAhADEAAAAZJmmMTQMP/EABoQAAICAwAAAAAAAAAAAAAAAAECEBEAEjH/2gAIAQEAAQUC1MFay4bv/8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQMBAT8Bp//EABYRAQEBAAAAAAAAAAAAAAAAAAAREv/aAAgBAgEBPwHKP//EABkQAAEFAAAAAAAAAAAAAAAAAJEAASAxQf/aAAgBAQAGPwLArYR//8QAGhAAAgIDAAAAAAAAAAAAAAAAAREAECFBUf/aAAgBAQABPyEIGaK0o3Yyd0f/2gAMAwEAAgADAAAAEAzf/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERQf/aAAgBAwEBPxCcRT//xAAWEQEBAQAAAAAAAAAAAAAAAAABAEH/2gAIAQIBAT8QHrE//8QAGxABAAMBAAMAAAAAAAAAAAAAAQARIWFBccH/2gAIAQEAAT8QURkDhuC0jT2/yIq95KShUdgwFIuixLbnif/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/785cb29327cc3a7d5ded3689273375dc/f93b5/069_singleton.jpg&quot;
        srcset=&quot;/static/785cb29327cc3a7d5ded3689273375dc/8ddbc/069_singleton.jpg 75w,
/static/785cb29327cc3a7d5ded3689273375dc/75985/069_singleton.jpg 150w,
/static/785cb29327cc3a7d5ded3689273375dc/f93b5/069_singleton.jpg 300w,
/static/785cb29327cc3a7d5ded3689273375dc/20e5d/069_singleton.jpg 450w,
/static/785cb29327cc3a7d5ded3689273375dc/b4294/069_singleton.jpg 600w,
/static/785cb29327cc3a7d5ded3689273375dc/2c24f/069_singleton.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Basic file manipulation with awk]]></title><description><![CDATA[Filter and manipulate files with the awk scripting language to create new reports from the Linux CLI]]></description><link>https://aneesh.co.uk/basic-file-manipulation-with-awk</link><guid isPermaLink="false">https://aneesh.co.uk/basic-file-manipulation-with-awk</guid><pubDate>Mon, 20 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;An introduction to awk scripting&lt;/h4&gt;
&lt;p&gt;
Learn about the convenient awk scripting language that you can use from the Linux CLI to manipulate, filter and reorder structured files into new reports with a single line command. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/RWDEOwZLfWM&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQBAv/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABh5blMuL/AP/EABkQAAIDAQAAAAAAAAAAAAAAAAECABESIf/aAAgBAQABBQLPKzGU6swdLnTf/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQMBAT8BR//EABYRAQEBAAAAAAAAAAAAAAAAAAARIf/aAAgBAgEBPwHVf//EABsQAAEEAwAAAAAAAAAAAAAAAAABESGhAhAx/9oACAEBAAY/AkmjtE6Rx8pU/8QAHBAAAwACAwEAAAAAAAAAAAAAAAERIXExQVHR/9oACAEBAAE/IU/wBwTualiY9Q6qM+cyOxXpD//aAAwDAQACAAMAAAAQDy//xAAVEQEBAAAAAAAAAAAAAAAAAAAQIf/aAAgBAwEBPxCD/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERIf/aAAgBAgEBPxCJtFZ//8QAGxABAQEBAAMBAAAAAAAAAAAAAREhADFRYaH/2gAIAQEAAT8QcGSD6+dmhTF5VHHEYmYfJ0zWGB1QSDHeZAgih4GH4d//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/4bf31f9964c850094e8c68171794aae0/f93b5/068_awk.jpg&quot;
        srcset=&quot;/static/4bf31f9964c850094e8c68171794aae0/8ddbc/068_awk.jpg 75w,
/static/4bf31f9964c850094e8c68171794aae0/75985/068_awk.jpg 150w,
/static/4bf31f9964c850094e8c68171794aae0/f93b5/068_awk.jpg 300w,
/static/4bf31f9964c850094e8c68171794aae0/20e5d/068_awk.jpg 450w,
/static/4bf31f9964c850094e8c68171794aae0/b4294/068_awk.jpg 600w,
/static/4bf31f9964c850094e8c68171794aae0/2c24f/068_awk.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Introduction to MockitoBDD]]></title><description><![CDATA[Explore the available methods that MockitoBDD offers to support behaviour-driven-development testing.]]></description><link>https://aneesh.co.uk/introduction-to-mockitobdd</link><guid isPermaLink="false">https://aneesh.co.uk/introduction-to-mockitobdd</guid><pubDate>Mon, 13 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Using MocktioBDD&lt;/h4&gt;
&lt;p&gt;
MockitoBDD provides us with special methods that support the communication of a behaviour-driven-development approach to software development and testing.
&lt;/p&gt;
&lt;p&gt;
In this video, you can integrate MocktioBDD with a given-when-then style of test writing to clearly outline how Objects are mocked and instructed to behave, while asserting against outcomes from the test in a clear manner.
&lt;p&gt;Link to source code: &lt;a href=&quot;https://github.com/4neesh/YouTube-Channel/tree/main/MockitoBDD&quot; target=&quot;_blank&quot;&gt;Here&lt;/a&gt;.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/7zC9wvMJAVg&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAEFAwT/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAAB0Xa6ynFIT//EABgQAAIDAAAAAAAAAAAAAAAAAAASARMg/9oACAEBAAEFAkFkrx//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAGBAAAwEBAAAAAAAAAAAAAAAAAAERQZH/2gAIAQEAAT8huYIYuDnCEQ0qf//aAAwDAQACAAMAAAAQYw//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJyVf//EABYRAQEBAAAAAAAAAAAAAAAAAAEQQf/aAAgBAgEBPxAdR//EABoQAAMAAwEAAAAAAAAAAAAAAAABESExQXH/2gAIAQEAAT8QUSxk4GtHHyMaPIiaFbSOQf/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/858da66d8628f84f654cf1ee5c38ac11/f93b5/070_mockitobdd.jpg&quot;
        srcset=&quot;/static/858da66d8628f84f654cf1ee5c38ac11/8ddbc/070_mockitobdd.jpg 75w,
/static/858da66d8628f84f654cf1ee5c38ac11/75985/070_mockitobdd.jpg 150w,
/static/858da66d8628f84f654cf1ee5c38ac11/f93b5/070_mockitobdd.jpg 300w,
/static/858da66d8628f84f654cf1ee5c38ac11/20e5d/070_mockitobdd.jpg 450w,
/static/858da66d8628f84f654cf1ee5c38ac11/b4294/070_mockitobdd.jpg 600w,
/static/858da66d8628f84f654cf1ee5c38ac11/2c24f/070_mockitobdd.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Comment, like and subscribe for more content on Java and software engineering. &lt;/p&gt;
&lt;p&gt;Visit my website to also see my blogs: &lt;a href=&quot;https://aneesh.co.uk&quot;&gt;https://aneesh.co.uk&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Options contract and their pricing]]></title><description><![CDATA[Understand the Options contract is and how it is priced]]></description><link>https://aneesh.co.uk/options-contract-and-their-pricing</link><guid isPermaLink="false">https://aneesh.co.uk/options-contract-and-their-pricing</guid><pubDate>Mon, 13 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand what an Options contract is and why it is used.&lt;br&gt;
&amp;#8226; Take a brief look into the components of an Options contract that are used to derive the price.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is an Options contract&lt;/h4&gt;
&lt;p&gt;
An Options contract is a derivative that defines an agreement between two counterparties where the buyer of the contract has the &lt;u&gt;option&lt;/u&gt; and NOT the obligation, to buy or sell a particular asset at a particular quantity within a certain time frame.
&lt;/p&gt;
&lt;p&gt;
Using the above description, when we create an Options contract, there are 4 key variables defined:&lt;br&gt;
1. If the buyer of the contract has the option to &lt;strong&gt;buy&lt;/strong&gt; or &lt;strong&gt;sell&lt;/strong&gt; the asset.&lt;br&gt;
2. What the price of the asset will be when bought or sold, known as the &lt;u&gt;Strike price&lt;/u&gt;.&lt;br&gt;
3. The &lt;u&gt;quantity&lt;/u&gt; of the asset to be bought or sold.&lt;br&gt;
4. The &lt;u&gt;maturity&lt;/u&gt; of the Options contract to when it is no longer valid.
&lt;/p&gt;
&lt;p&gt;
The times at when the Options contract can be exercised is determined by its type. &apos;European&apos; Options can be exercised only at expiry, whereas &apos;American&apos; Options can be exercised at any time before. &apos;Bermudan&apos; Options on the other hand will have set intervals when it can be exercised leading up to the maturity (such as once per month).
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Buy and Sell contracts&lt;/h4&gt;
&lt;p&gt;
As mentioned above, an Options contract will entitle, but not obligate the buyer to either buy or sell a particular asset at a strike price by the maturity date.&lt;br&gt;
An option contract that entitles the holder to buy an asset is called a &lt;strong&gt;Call option&lt;/strong&gt;. An option contract that entitles the holder to sell an asset is called a &lt;strong&gt;Put option&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
Option contracts are not limited to only a single market or participant and are traded for a variety of purposes due to their many advantages that we will discuss later.
Options contracts exist for a huge range of markets including commodities, stocks, bonds, interest rates, currencies and also swaps, known as &apos;Swaptions&apos;.
&lt;/p&gt;
&lt;p&gt;
One example of Options contracts traded without an exchange are &lt;u&gt;Dealer options&lt;/u&gt;. Dealer options are used to trade commodities over the counter (OTC) without a centralised exchange. The benefit of OTC trading are the personalised terms that can be agreed upon, thus enabling both counterparties to achieve a more bespoke agreement. 
&lt;/p&gt;
&lt;p&gt;
The variety of options contract available to investors make them highly personalised and present one key advantage to using them.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why Options are used&lt;/h4&gt;
&lt;p&gt;
So we can now understand what an Options contract is, what its key properties are and the many forms it can take in the market, but why are they used?
&lt;/p&gt;
&lt;p&gt;
As repeated in this blog, the Options contract provides the right, and not the obligation for a counterparty to transact upon the terms of the derivative.&lt;br&gt;
This property of an Options contract means they can be used with huge flexibility and present ample opportunities for both hedging and speculative incentives. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Hedging strategies&lt;/strong&gt;&lt;br&gt;
A hedging strategy may see a counterparty use an Options contract to protect themselves from both the upward and downward movement of an interest rate.&lt;br&gt;
For example, if we take a floating point interest rate that is currently 0.16%, counterparty A may agree with counterparty B an Options contract that allows counterparty A to receive payment each time the interest rate exceeds 2% and falls below 1.12%. This hedging strategy is known as a cap and floor, where counterparty A are capping the maximum interest rate they are exposed to at 0.2% and the minimum rate of 0.12%.
&lt;/p&gt;
&lt;p&gt;
Counterparty A will pay a premium to counterparty B for the Options contract, but the benefit to counterparty A is the protection from interest rate volatility if it were to exceed either boundary. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Speculative strategies&lt;/strong&gt;&lt;br&gt;
On the contrary, the Options contract can enable an investor to benefit from the movement of an underlying asset&apos;s price, as they are able to exercise with their right when the price is deemed &apos;in the money&apos;.&lt;br&gt;
&lt;i&gt;In the money: An options contract that can be exercised to achieve a profit and has &apos;intrinsic value&apos;.&lt;/i&gt;
&lt;br&gt;
As a speculator, the benefit of an Options contract can present unlimited upside, with a limited downside (the premium paid for the contract). For example, if an Options contract enables an investor to buy 100kg of wheat for £1000, and the cost of the options contract is just £50, the investor will have a limited downside of £50, but a potential upside determined by the upward price of wheat within the market. If 100kg of wheat rises to £2000, the investor can make a £950 profit from exercising their options contract, whereas if the price of wheat remains below £1000, they will only ever lose the £50 premium paid up-front. 
&lt;/p&gt;
&lt;p&gt;
When considering a hedging or speculative strategy, the benefit is realised by the holder of the contract only when it is exercised. At expiry, the Options contract is deemed worthless, and the benefit of the Options contract creation lies with the seller of the contract. With this in mind, you may wonder how the price of an Options contract is derived, and the factors that play into its value throughout it&apos;s life. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Options contract pricing&lt;/h4&gt;
&lt;p&gt;
The &lt;a href=&quot;https://www.investopedia.com/terms/b/blackscholes.asp&quot;&gt;Black Scholes&lt;/a&gt; and Binomial model are popular mathematical models used to calculate the price of an Options contract. The pricing of an Options contract is derived from the probability that the Options contract can be exercised in the money. Each model has its own advantages and approach:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Black Scholes&lt;/strong&gt;&lt;br&gt;
Developed in 1973, the Black-Scholes-Merton model uses risk factors and time-value to price &lt;u&gt;European Call Options&lt;/u&gt; (can only be exercised at expiry).&lt;br&gt;
The analytical model uses 5 variables: strike price, current price, time to expiry, risk free rate of return, and volatility.&lt;br&gt;
The model is based upon the assumption that asset prices have a lognormal distribution (bell curve) with a constant drift and volatility. Using the input variables, the Options price is calculated against the assumptions of constant volatility, unpredictable market movements, and lognormal price distribution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Binomial Model&lt;/strong&gt;&lt;br&gt;
The Binomial model is an iterative model that accounts for multiple periods of time for exercise, and is thus an approach used for American Options. While the Binomial model also uses the inputs from the Black Scholes model, it is less complex as it uses a Binomial tree to calculate the value of an Option given two possibilities: underlying price movement goes up or down. The model, shaped like a lattice, can then be split into intervals that represent that time periods for the underlying price movement to happen, thus creating the iterative Options price predictions. The Binomial will build a map of different possible scenarios for the iterative movement of the underlying price of the asset at different time intervals. Each price movement will then represent a present value. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Options value and sensitivities&lt;/h4&gt;
&lt;p&gt;
The Options pricing models make use of multiple variables that represent the multitudes of variance a standard Options contract will be exposed to.
&lt;/p&gt;
&lt;p&gt;
The complexity of Options pricing has lead to the inception of the &apos;Greeks&apos;, four risk measures used to quantify the sensitivity of the price to an external factor. &lt;br&gt;
The 4 measures are:&lt;br&gt;
1. Delta: price sensitivity of the option to the asset price&lt;br&gt;
2. Theta: price sensitivity of the option with time&lt;br&gt;
3. Gamma: the rate of change of Delta&lt;br&gt;
4. Vega/Kappa: rate of change between an option and the asset implied volatility&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
In my next blog post, I will explore the Greeks and their relationship with &apos;intrinsic&apos; and &apos;extrinsic&apos; value to the investor to further understand how an Options price can be so widely influenced.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
In this post, we have looked at the Options contract and how it can be used for both speculative and hedging purposes to provide a variety of benefits to both sides of the contract. Not only can Options contract be used for a variety of benefits, but they also come in many shapes and sizes with the right to buy or sell across a variety of markets and with varied opportunities for exercise.
&lt;/p&gt;
&lt;p&gt;
The benefit of &apos;optionality&apos; from the contract exposes the price of the Options contract to multiple variances within the market and time that can influence their price. As a result, complex mathematical models are used to best estimate the present value of the Options contract within the market. When reviewing Option prices, the sensitivities that are exposed to the price have led to the use of the &apos;Greeks&apos; a group of well defined variances to the Option price. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Core text file operations with the Linux CLI]]></title><description><![CDATA[Learn about the 5 ways you can view files within the Linux CLI.]]></description><link>https://aneesh.co.uk/core-text-file-operations-with-the-linux-cli</link><guid isPermaLink="false">https://aneesh.co.uk/core-text-file-operations-with-the-linux-cli</guid><pubDate>Mon, 06 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Text file operations with the Linux CLI&lt;/h4&gt;
&lt;p&gt;
Take a look at some of the core commands you can use within a Linux terminal to view and navigate files. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/gfuDvpi_jYY&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIDBAX/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAABzlcKAug//8QAGxAAAgIDAQAAAAAAAAAAAAAAAQIAERIhIjH/2gAIAQEAAQUCrUderMHrnJv/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPwFH/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8BV//EABwQAAEDBQAAAAAAAAAAAAAAAAABEBECITGR4f/aAAgBAQAGPwLhldNDTVdT/8QAHBABAQEAAgMBAAAAAAAAAAAAAREAIVExQWHR/9oACAEBAAE/IVA8ydcIfhyVRv0msU8Yk8e83Fdk3//aAAwDAQACAAMAAAAQRw//xAAVEQEBAAAAAAAAAAAAAAAAAAAAAf/aAAgBAwEBPxBT/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAhEf/aAAgBAgEBPxDQ2Oy//8QAGxABAQADAQEBAAAAAAAAAAAAAREAITFRgaH/2gAIAQEAAT8Qr7MKjh9wche78miQaF8vDWNAvnEVCRpMZAgihwYfhn//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/7bfaf64ab1476990111a8c0007f0a53d/f93b5/067_textOps.jpg&quot;
        srcset=&quot;/static/7bfaf64ab1476990111a8c0007f0a53d/8ddbc/067_textOps.jpg 75w,
/static/7bfaf64ab1476990111a8c0007f0a53d/75985/067_textOps.jpg 150w,
/static/7bfaf64ab1476990111a8c0007f0a53d/f93b5/067_textOps.jpg 300w,
/static/7bfaf64ab1476990111a8c0007f0a53d/20e5d/067_textOps.jpg 450w,
/static/7bfaf64ab1476990111a8c0007f0a53d/b4294/067_textOps.jpg 600w,
/static/7bfaf64ab1476990111a8c0007f0a53d/2c24f/067_textOps.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Avoid the NullPointerException when using the .equals method.]]></title><description><![CDATA[Understand how the structure of a boolean can be designed to avoid a NullPointerException.]]></description><link>https://aneesh.co.uk/avoid-the-nullpointerexception-when-using-the-equals-method</link><guid isPermaLink="false">https://aneesh.co.uk/avoid-the-nullpointerexception-when-using-the-equals-method</guid><pubDate>Mon, 30 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Avoiding the NullPointerException when comparing Objects&lt;/h4&gt;
&lt;p&gt;
The .equals method is often used within if-statements and to return boolean expressions within an application.
By understanding how the structure of the boolean to call the .equals method is made, we are able to design our 
booleans to avoid any null exceptions when comparing two Objects.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/TjqGXmMj8Ik&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMBBP/EABcBAAMBAAAAAAAAAAAAAAAAAAECAwT/2gAMAwEAAhADEAAAAYtaYxdBh//EABkQAAIDAQAAAAAAAAAAAAAAAAABAhIxEf/aAAgBAQABBQKrGmip1mkt/8QAFhEAAwAAAAAAAAAAAAAAAAAAARAR/9oACAEDAQE/AaF//8QAFxEBAAMAAAAAAAAAAAAAAAAAAAEREv/aAAgBAgEBPwHMqf/EABcQAAMBAAAAAAAAAAAAAAAAAABBkSD/2gAIAQEABj8CUFM//8QAGhAAAgIDAAAAAAAAAAAAAAAAAAERIRBBYf/aAAgBAQABPyGhWCLYOLs7EtlOD//aAAwDAQACAAMAAAAQzM//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEVH/2gAIAQMBAT8QyV//xAAXEQADAQAAAAAAAAAAAAAAAAAAAREh/9oACAECAQE/EFbRD//EABwQAAICAgMAAAAAAAAAAAAAAAERACExYUFRgf/aAAgBAQABPxAIwLCrXsIywdLDKC06iMYaEuFo1UIltcT/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/326e5c6072d2f9e0ff2b52a082d490f2/f93b5/066_equals.jpg&quot;
        srcset=&quot;/static/326e5c6072d2f9e0ff2b52a082d490f2/8ddbc/066_equals.jpg 75w,
/static/326e5c6072d2f9e0ff2b52a082d490f2/75985/066_equals.jpg 150w,
/static/326e5c6072d2f9e0ff2b52a082d490f2/f93b5/066_equals.jpg 300w,
/static/326e5c6072d2f9e0ff2b52a082d490f2/20e5d/066_equals.jpg 450w,
/static/326e5c6072d2f9e0ff2b52a082d490f2/b4294/066_equals.jpg 600w,
/static/326e5c6072d2f9e0ff2b52a082d490f2/2c24f/066_equals.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using the Builder design pattern.]]></title><description><![CDATA[Use the Builder design pattern to create complex Objects in a structured and readable way.]]></description><link>https://aneesh.co.uk/using-the-builder-design-pattern</link><guid isPermaLink="false">https://aneesh.co.uk/using-the-builder-design-pattern</guid><pubDate>Mon, 23 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Using the Builder design pattern&lt;/h4&gt;
&lt;p&gt;
The builder design pattern offers a structured and readable way to create complex Objects. 
Rather than using a long and rigid constructor to instantiate multiple variables and Objects for a class instance, 
the builder design pattern enables you to set each variable as it is required when creating an instance.
&lt;br&gt;
While appearing more verbose, the benefits of the builder design pattern will ensure Objects are created in a 
responsible way where each property is assigned as expected.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/PK96XVhBPT0&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgT/xAAXAQADAQAAAAAAAAAAAAAAAAABAgME/9oADAMBAAIQAxAAAAFMWNMUmgYf/8QAGRAAAgMBAAAAAAAAAAAAAAAAAAEQERIx/9oACAEBAAEFAsuMlstj7//EABYRAAMAAAAAAAAAAAAAAAAAAAEQEf/aAAgBAwEBPwGhf//EABcRAQADAAAAAAAAAAAAAAAAAAABERL/2gAIAQIBAT8BzKn/xAAZEAABBQAAAAAAAAAAAAAAAACRAAEgMUH/2gAIAQEABj8CwK2Ef//EABsQAAICAwEAAAAAAAAAAAAAAAAhAREQMUFh/9oACAEBAAE/IUQwtbDNZZ7HK8X/2gAMAwEAAgADAAAAENzP/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAERQf/aAAgBAwEBPxCclv/EABgRAAIDAAAAAAAAAAAAAAAAAAABQaHw/9oACAECAQE/EFNqEP/EABwQAQADAQEAAwAAAAAAAAAAAAEAESExYUFxsf/aAAgBAQABPxApjAvn9ihBm5TfORqhtXkyrj6IIiwulRrbz4n/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/9cb4f98323db803c81f9541584cffc3f/f93b5/065_builder.jpg&quot;
        srcset=&quot;/static/9cb4f98323db803c81f9541584cffc3f/8ddbc/065_builder.jpg 75w,
/static/9cb4f98323db803c81f9541584cffc3f/75985/065_builder.jpg 150w,
/static/9cb4f98323db803c81f9541584cffc3f/f93b5/065_builder.jpg 300w,
/static/9cb4f98323db803c81f9541584cffc3f/20e5d/065_builder.jpg 450w,
/static/9cb4f98323db803c81f9541584cffc3f/b4294/065_builder.jpg 600w,
/static/9cb4f98323db803c81f9541584cffc3f/2c24f/065_builder.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using Evaluate Expression and Watches in IntelliJ.]]></title><description><![CDATA[Use the evaluate expression and watches tools with the IntelliJ debugger to monitor Objects as they are modified frame by frame.]]></description><link>https://aneesh.co.uk/using-evaluate-expression-and-watches-in-intellij</link><guid isPermaLink="false">https://aneesh.co.uk/using-evaluate-expression-and-watches-in-intellij</guid><pubDate>Mon, 16 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Evaluate expression and Watches with IntelliJ&lt;/h4&gt;
&lt;p&gt;
Learn how the evaluate expression and watches tools within IntelliJ can assist with debugging an application. 
The evaluate expression will allow you to perform operations on an Object that is locally stored. For example, you can stream a List, get a value from a Map, or perform mathematical calculations on a value. 
&lt;br&gt;
The Watches tool will allow you to perform an operation on an Object, and to monitor that same operation as the debugger runs frame by frame.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/u5NSgMCkqOg&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMBBP/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAGmXWcyg//EABoQAAICAwAAAAAAAAAAAAAAAAECABIQETH/2gAIAQEAAQUCpKalFbB4ihR//8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQMBAT8BJ//EABYRAQEBAAAAAAAAAAAAAAAAAAABIf/aAAgBAgEBPwGsf//EABgQAAIDAAAAAAAAAAAAAAAAAAAhEBFB/9oACAEBAAY/AjC1KP/EAB0QAAICAQUAAAAAAAAAAAAAAAABESFBEDFhcZH/2gAIAQEAAT8h248IpOHAqJt1pZh9SQpP/9oADAMBAAIAAwAAABBk7//EABcRAQEBAQAAAAAAAAAAAAAAAAEAESH/2gAIAQMBAT8QSvbVv//EABYRAQEBAAAAAAAAAAAAAAAAAAEAMf/aAAgBAgEBPxABkgv/xAAaEAEAAwEBAQAAAAAAAAAAAAABABEhQTGx/9oACAEBAAE/EKtG5nMUEgsqnIBTzLATkABMRGWBxvq/Z//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/4630e33a90b65cee0ea0cf6f4af29014/f93b5/064_evalExp.jpg&quot;
        srcset=&quot;/static/4630e33a90b65cee0ea0cf6f4af29014/8ddbc/064_evalExp.jpg 75w,
/static/4630e33a90b65cee0ea0cf6f4af29014/75985/064_evalExp.jpg 150w,
/static/4630e33a90b65cee0ea0cf6f4af29014/f93b5/064_evalExp.jpg 300w,
/static/4630e33a90b65cee0ea0cf6f4af29014/20e5d/064_evalExp.jpg 450w,
/static/4630e33a90b65cee0ea0cf6f4af29014/b4294/064_evalExp.jpg 600w,
/static/4630e33a90b65cee0ea0cf6f4af29014/2c24f/064_evalExp.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How @InjectMocks works with Mockito]]></title><description><![CDATA[Learn about how you can use @InjectMocks to automatically add services to classes as they are tested with Mockito.]]></description><link>https://aneesh.co.uk/how-injectmocks-works-with-mockito</link><guid isPermaLink="false">https://aneesh.co.uk/how-injectmocks-works-with-mockito</guid><pubDate>Mon, 09 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;How to use @InjectMocks&lt;/h4&gt;
&lt;p&gt;
@InjectMocks will allow you to inject other spies and mocks into a class that requires services when it is being tested. The annotation will scan the class under test and automatically inject all available mocks and spies from within the test class. No more NullPointerExceptions when an unknown service is called!
&lt;p&gt;&lt;a href=&quot;https://youtu.be/KuqYS9reh2I&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAUBAwT/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABsjcVkuGQn//EABgQAAIDAAAAAAAAAAAAAAAAAAASARMg/9oACAEBAAEFAkFkrx//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAgEBPwFX/8QAFRABAQAAAAAAAAAAAAAAAAAAIOH/2gAIAQEABj8Ci//EABoQAAMAAwEAAAAAAAAAAAAAAAABETFBYZH/2gAIAQEAAT8hqLA4Lwc6IRDSp//aAAwDAQACAAMAAAAQAw//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJyVf//EABYRAQEBAAAAAAAAAAAAAAAAAAEQQf/aAAgBAgEBPxAXY//EABsQAAMAAgMAAAAAAAAAAAAAAAABESFBMVGh/9oACAEBAAE/EFMx40GmrxjGjyI6LNVGoP/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/26d8c13a4d989f9d834b535b9ba0f353/f93b5/063_injectmocks.jpg&quot;
        srcset=&quot;/static/26d8c13a4d989f9d834b535b9ba0f353/8ddbc/063_injectmocks.jpg 75w,
/static/26d8c13a4d989f9d834b535b9ba0f353/75985/063_injectmocks.jpg 150w,
/static/26d8c13a4d989f9d834b535b9ba0f353/f93b5/063_injectmocks.jpg 300w,
/static/26d8c13a4d989f9d834b535b9ba0f353/20e5d/063_injectmocks.jpg 450w,
/static/26d8c13a4d989f9d834b535b9ba0f353/b4294/063_injectmocks.jpg 600w,
/static/26d8c13a4d989f9d834b535b9ba0f353/2c24f/063_injectmocks.jpg 817w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Customise the IntelliJ test view]]></title><description><![CDATA[Explore the custom settings in IntelliJ to modify the feedback, reports and navigation options after running a test class.]]></description><link>https://aneesh.co.uk/customise-the-intellij-test-view</link><guid isPermaLink="false">https://aneesh.co.uk/customise-the-intellij-test-view</guid><pubDate>Mon, 02 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Custom test views&lt;/h4&gt;
&lt;p&gt;
Explore some of the on-demand settings that are offered with IntelliJ that enable you to customise 
your test class outputs that can improve how you navigate, review and debug your test methods. 
&lt;p&gt;By the end of the video you will be able to sort and filter the test methods that are returned when running a test class.
You will be able to navigate across test methods with a single click, and to the failing assertion with two clicks. And lastly, you will be able to create customised html reports, straight from the IDE for an easy-to-read interface of the test outcomes. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/JoIWJkb5YY8&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAABcuhsown/xAAaEAACAgMAAAAAAAAAAAAAAAABEgACEBEx/9oACAEBAAEFAkiaii2DyoAH/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8BR//EABcQAAMBAAAAAAAAAAAAAAAAAAAQIRH/2gAIAQEABj8CUxw//8QAHRAAAgIBBQAAAAAAAAAAAAAAAREAIWEQQXGBkf/aAAgBAQABPyHr5EBsIYhvEXGljGItBvP/2gAMAwEAAgADAAAAEPPP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQARQf/aAAgBAwEBPxAHtq3/xAAXEQADAQAAAAAAAAAAAAAAAAAAASFB/9oACAECAQE/EJg0UP/EABoQAQEBAQADAAAAAAAAAAAAAAERACExcbH/2gAIAQEAAT8QlAbPXWEIVU8zwXkoMO7LoocrmKfK/d//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/185af656bf81287767ffc6167d1fcf56/f93b5/062_customtestoutput.jpg&quot;
        srcset=&quot;/static/185af656bf81287767ffc6167d1fcf56/8ddbc/062_customtestoutput.jpg 75w,
/static/185af656bf81287767ffc6167d1fcf56/75985/062_customtestoutput.jpg 150w,
/static/185af656bf81287767ffc6167d1fcf56/f93b5/062_customtestoutput.jpg 300w,
/static/185af656bf81287767ffc6167d1fcf56/20e5d/062_customtestoutput.jpg 450w,
/static/185af656bf81287767ffc6167d1fcf56/b4294/062_customtestoutput.jpg 600w,
/static/185af656bf81287767ffc6167d1fcf56/b5c84/062_customtestoutput.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to use the ArgumentCaptor from Mockito]]></title><description><![CDATA[Use the argumentcaptor from Mockito to extract arguments passed within a method call to later test upon.]]></description><link>https://aneesh.co.uk/how-to-use-the-argumentcaptor-from-mockito</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-use-the-argumentcaptor-from-mockito</guid><pubDate>Mon, 26 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Using the ArgumentCaptor&lt;/h4&gt;
&lt;p&gt;
This video will explore the conditions that would make us interested in testing the arguments passed around our application methods, and howe we can extract the Objects that are passed using the ArgumentCaptor. 
With the ArgumentCaptor, we are able to isolate Object arguments away from the stack that calls it, to then test them on their own back in the test class. 
&lt;p&gt;&lt;a href=&quot;https://youtu.be/fQMRWvgoaCw&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABvDpWWaPif//EABoQAAICAwAAAAAAAAAAAAAAAAABAhIDEDH/2gAIAQEAAQUCoVY8e59//8QAFhEBAQEAAAAAAAAAAAAAAAAAABIB/9oACAEDAQE/AZxD/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEh/9oACAECAQE/AdV//8QAFBABAAAAAAAAAAAAAAAAAAAAIP/aAAgBAQAGPwJf/8QAGxAAAQQDAAAAAAAAAAAAAAAAAAERMZEQYbH/2gAIAQEAAT8hcyRRoShpYzwP/9oADAMBAAIAAwAAABDTH//EABcRAQADAAAAAAAAAAAAAAAAAAABEUH/2gAIAQMBAT8QnJR//8QAFhEBAQEAAAAAAAAAAAAAAAAAARBB/9oACAECAQE/EB1H/8QAGRABAQEBAQEAAAAAAAAAAAAAAREAITFx/9oACAEBAAE/ECZTJOcjTj85pvUx5gKcwE8z/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/84fa51a320775e4bb1832f9301a07f3e/f93b5/060_argument.jpg&quot;
        srcset=&quot;/static/84fa51a320775e4bb1832f9301a07f3e/8ddbc/060_argument.jpg 75w,
/static/84fa51a320775e4bb1832f9301a07f3e/75985/060_argument.jpg 150w,
/static/84fa51a320775e4bb1832f9301a07f3e/f93b5/060_argument.jpg 300w,
/static/84fa51a320775e4bb1832f9301a07f3e/20e5d/060_argument.jpg 450w,
/static/84fa51a320775e4bb1832f9301a07f3e/b4294/060_argument.jpg 600w,
/static/84fa51a320775e4bb1832f9301a07f3e/b5c84/060_argument.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An introduction to basis risk]]></title><description><![CDATA[Understand how basis risk is derived and why it is important to consider with hedging strategies.]]></description><link>https://aneesh.co.uk/an-introduction-to-basis-risk</link><guid isPermaLink="false">https://aneesh.co.uk/an-introduction-to-basis-risk</guid><pubDate>Mon, 26 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; What is basis risk.&lt;br&gt;
&amp;#8226; How basis risk is created.&lt;br&gt;
&amp;#8226; Why basis risk is so frequently encountered.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Spot prices vs Futures prices&lt;/h4&gt;
&lt;p&gt;
To understand how basis risk, we need to first understand the difference between a spot price and a futures price. 
A spot price, is the price paid for an commodity, asset or security at a given time. For example, when you go shopping at a supermarket, you&apos;re in theory paying the spot price for your groceries.&lt;br&gt;
A futures price is the price for an amount of a specific asset to be delivered at a certain date in the future. 
For example, a spot price for a StockA might be £2.00 today, but a futures price for it might be £2.03 to be delivered in 5 weeks time. One security can have many different futures prices for it as the quantity and time-frame for when the asset will be delivered will be different. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The futures contract&lt;/h4&gt;
&lt;p&gt;
A futures contract is a derivative that is traded between two counterparties as an agreement to buy or sell an underlying quantity of a commodity, asset or security at a certain price and at a certain point in time in the future. The futures contract is traded
through an exchange. A similar kind of derivative is a forward contract, which is traded without an exchange, and is instead traded over the counter.&lt;br&gt;
The futures contract serves both counterparties:
&lt;/p&gt;
&lt;p&gt;
First, the counterparty that sells the contract are fixing a price for which a certain amount of an asset at a point in the future will be delivered and sold.&lt;br&gt;
Second, the counterparty buying the contract are securing the delivery for the amount of the asset at a point in the future.&lt;br&gt;
The time which the exchange happens is known as the expiry. The price of the future contract will represent the price of the asset. For example, I could pay £100 for a future contract that represents the delivery of 10kg of chicken breast in 1 weeks time. The buyer of the futures contract (me) is therefore hedging the risk that the price will increase from when the futures contract is agreed upon (that the price of 10kg chicken breast does not rise beyond £100 in 1 weeks time). As a result, the I am protecting myself from &lt;strong&gt;price risk&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
The motivation for buying a futures contract can also be used for speculation. For example if an investor believes the price of an asset may rise to more than the market expect, they can purchase a futures contract to secure the delivery of the asset at a cheaper price than they expect it to rise to, and then can make a subsequent profit from the resale of the asset in the market at expiry. 
&lt;/p&gt;
&lt;p&gt;
How are futures used in the real world?&lt;br&gt;
A common example I refer to would be any airline company that would like to secure the delivery of jet fuel for the immediate and long-term future. By using a futures contract, they can secure the cost of jet fuel, and better estimate their cash requirements and costs for flights in the future. 
&lt;/p&gt;
&lt;p&gt;
A rather interesting example might also be when McDonalds benefitted from a futures contracts that enabled them to create the chicken nugget. You can read the story &lt;a href=&quot;https://www.businessinsider.com/mcdonalds-mcnugget-launch-was-masterminded-by-bridgewater-ray-dalio-2018-4?r=US&amp;IR=T&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Basis Risk&lt;/h4&gt;
&lt;p&gt;
As mentioned above, the counterparty buying the futures contract are eliminating &apos;price risk&apos; on the delivered asset as they secure the cost for its delivery. The price risk is however replaced by a new risk, called &apos;basis risk&apos;.
&lt;/p&gt;
&lt;p&gt;
Basis risk exists as the price difference between the current price of the underlying asset and the price that is agreed upon in the futures contract. Basis risk is therefore a replacement of
price risk as it hedges the investor from the absolute price movement of the asset.
&lt;/p&gt;
&lt;h4&gt;Let&apos;s use an example&lt;/h4&gt;
&lt;p&gt;
If a shop owner is worried about the price volatility of milk, where it is £1.40 per gallon today and expected to rise to £1.50 in 6 months, they may like to buy a futures contract to ensure the delivery of milk at a price of £1.50 per gallon for 6 months time, and to protect themselves of any further unexpected price increases. Let&apos;s say they&apos;re buying 100 gallons worth, the futures contract will be priced at £150 (100 gallons times by £1.50).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The basis risk the shop owner faces is equal to the price agreed within the futures contract 
and the current price of the asset.&lt;/strong&gt; This would be 10 pence, where the agreed upon price is £1.50 and the price of milk when the futures contract is bought, is just £1.40. 
&lt;/p&gt;
&lt;p&gt;
The price agreed on the futures exchange will edge closer and closer to the actual price of the asset as the time to expiry reduces. As time passes, the movement of the 
futures price and the underlying asset can however change. For example, the price of milk may rise to £1.60, therefore the basis risk to the shop owner will actually be -10 pence. What we can see here, is that 
basis risk can actually be positive and negative as the price of the asset changes. As the underlying asset price moves, so do the price of futures contract for the same maturity date that the shop owner had initially agreed upon.
&lt;/p&gt;
&lt;p&gt;
If our shop owner agreed to exchange 6 months from today, which is 1st January 2022, and by December 1st 2021 the price of milk is now £1.60, then the futures price for delivery of milk on the 1st January 2022 is likely to be higher than £1.50 (for those wishing to buy a 1 month maturity future).
&lt;/p&gt;
&lt;p&gt;
The price movements between the underlying asset and the futures market are not always perfectly correlated. There are many factors to consider for a futures contract such as the cost of storing the asset, delivery of the asset and the risk-free rate of return that can all impact price. As a result, the basis risk, which is the difference between the price today for the asset and the agreed upon futures price, can grow and shrink as the market for the asset and its future price move in an uncorrelated fashion. In an ideal world for the shop owner, the price of the milk and the futures price will be well correlated such that basis risk is 0 at expiry. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;If markets are perfect, when does basis risk actually occur?&lt;/h4&gt;
&lt;p&gt;
If we use the above example, where basis risk occurs between the difference between the current price and the futures price of an underlying asset, you may be wondering how such a risk can ever be present at maturity.
After all, markets should be efficient enough, such that the shop owner is only able to buy at a futures price that very closely reflects the expected price of milk 6 months from today. Perhaps it would take a significant market condition to create such a shock in the milk price that a basis risk becomes prevalent (such as a new cow disease).
&lt;/p&gt;
&lt;p&gt;
The reason basis risk is such a prevalent risk to consider, is because the futures contracts that are bought aren&apos;t always perfect. As a buyer of a futures market, you would require a futures contract that specifies all three components perfectly:&lt;br&gt;
1. The exact asset.&lt;br&gt;
2. The exact amount of the asset.&lt;br&gt;
3. The exact maturity required.&lt;br&gt;
4. The exact delivery location of the asset.&lt;br&gt;
&lt;p&gt;Failure to reach all three of these targets in a futures contract will create an &lt;i&gt;imperfect hedging strategy&lt;/i&gt;. This imperfect strategy creates a basis risk for the futures contract buyer as the movement of the futures price may not be perfectly correlated to the movement of the true asset they are trying to hedge against. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
For this reason, futures contracts are not always held to expiry, and are therefore sold in the market when the price risk for the asset has been reduced against time to a significant degree. The imperfect hedging strategy exposes a counterparty to one of 4 different sub-components to basis risk:&lt;br&gt;
1. Price basis risk: Where the price of the asset does not move as expected towards the futures price.&lt;br&gt;
2. Location basis risk: The delivery location of the asset is different to that desired by the buyer of the contract. For example, they would like delivery of milk to England, however the market can only offer the delivery to France, which would reflect a slightly different market price movement.&lt;br&gt;
3. Calendar basis risk: Where the expiry of the futures contract is different to the desired duration from the contract buyer. If the futures contract expires 1 week beyond the desired delivery of the asset, the buyer may sell the futures contract 1 week early, and realise the outstanding basis risk on the contract.&lt;br&gt;
4. Product quality basis risk: Where the asset on the futures contract does not perfectly reflect the asset hedged against. For example, the futures price for semi-skimmed milk may move slightly different to that of 2% milk. 
&lt;/p&gt;
&lt;p&gt;
It is due to these factors that an imperfect hedge may be taken within the market, and thus basis risk is a key exposure faced by the buyer of the contract. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
A futures contract is used to fix the delivery of a specified quantity of a specific asset and at an agreed date in the future. The contract is traded over an exchange.
&lt;/p&gt;
&lt;p&gt;
Basis risk exists at all times within the life of a futures contract where the current spot price for the asset is different to that of the futures price. Therefore basis risk can be both positive and negative. 
&lt;/p&gt;
&lt;p&gt;
Perfect markets would mean basis risk is minimised at all expiry dates of all futures contracts. However future contracts are not always perfect for the risk that is hedged by the buyer of the contract. Factors such as the asset, asset quantity, date of delivery, and market pressures, can all mean the futures contract is sold before its expiry, and therefore expose the selling party to the basis risk of the contract. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Testing private methods with Powermock]]></title><description><![CDATA[Understand how the reflection utils and Powermock library offer ways that we can test private methods.]]></description><link>https://aneesh.co.uk/testing-private-methods-with-powermock</link><guid isPermaLink="false">https://aneesh.co.uk/testing-private-methods-with-powermock</guid><pubDate>Mon, 19 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Testing private methods&lt;/h4&gt;
&lt;p&gt;
Learn how you can test private methods within your application using JUnit. First we explore how the reflection utils from the Java library can be used to control and invoke a private method. Second, we use the Powermock library to condense a large portion of the test to just a single line.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/EUXesPg7qOo&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABUBAQEAAAAAAAAAAAAAAAAAAAID/9oADAMBAAIQAxAAAAHaw581eZ//xAAaEAACAgMAAAAAAAAAAAAAAAAAAQISAxAx/9oACAEBAAEFAqFWPHuff//EABcRAAMBAAAAAAAAAAAAAAAAAAABAhL/2gAIAQMBAT8BcIwf/8QAFhEBAQEAAAAAAAAAAAAAAAAAAAER/9oACAECAQE/AZWv/8QAFBABAAAAAAAAAAAAAAAAAAAAIP/aAAgBAQAGPwJf/8QAHBAAAgICAwAAAAAAAAAAAAAAABEBMRCRIUGx/9oACAEBAAE/IWUVoR1GhU1hcngf/9oADAMBAAIAAwAAABC7H//EABcRAAMBAAAAAAAAAAAAAAAAAAABESH/2gAIAQMBAT8QSWEH/8QAFhEBAQEAAAAAAAAAAAAAAAAAARAh/9oACAECAQE/EHXY/8QAGhABAAMBAQEAAAAAAAAAAAAAAQARMSFBcf/aAAgBAQABPxA3CFeYmpp+Y1b1UArJRwQCeR//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/5f42231260d30a86570eb475c5bb2aa4/f93b5/059_private.jpg&quot;
        srcset=&quot;/static/5f42231260d30a86570eb475c5bb2aa4/8ddbc/059_private.jpg 75w,
/static/5f42231260d30a86570eb475c5bb2aa4/75985/059_private.jpg 150w,
/static/5f42231260d30a86570eb475c5bb2aa4/f93b5/059_private.jpg 300w,
/static/5f42231260d30a86570eb475c5bb2aa4/20e5d/059_private.jpg 450w,
/static/5f42231260d30a86570eb475c5bb2aa4/b4294/059_private.jpg 600w,
/static/5f42231260d30a86570eb475c5bb2aa4/b5c84/059_private.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Testing the content and order of collections]]></title><description><![CDATA[Explore 3 different assertion methods from the AssertJ library that can help us to test the content and order of items within a collection.]]></description><link>https://aneesh.co.uk/testing-the-content-and-order-of-collections</link><guid isPermaLink="false">https://aneesh.co.uk/testing-the-content-and-order-of-collections</guid><pubDate>Mon, 12 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Testing collections&lt;/h4&gt;
&lt;p&gt;
Learn about 3 different test methods that will enable you to assert upon the elements contained within a list, the comparator used to sort a list, and the like-for-like 
elements that are contained within the list to assert upon their order with the AssertJ library.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/Uvi6a_PeMqI&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAHaxWXNXk//xAAaEAACAgMAAAAAAAAAAAAAAAAAAQISAxAx/9oACAEBAAEFAqFWPHuff//EABYRAQEBAAAAAAAAAAAAAAAAAAASAf/aAAgBAwEBPwGcQ//EABYRAQEBAAAAAAAAAAAAAAAAAAARIf/aAAgBAgEBPwHVf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABsQAAICAwEAAAAAAAAAAAAAAAARATEQYZGx/9oACAEBAAE/IWUVw1xwVNYR4H//2gAMAwEAAgADAAAAEMMf/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAERQf/aAAgBAwEBPxCclH//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFR/9oACAECAQE/EI0s/8QAGhABAAMBAQEAAAAAAAAAAAAAAQARITFBYf/aAAgBAQABPxA7hCvMwq0+mNW6qBALwgE5H//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/75c7739608bd63d1c224691f6ec0ea1d/f93b5/058_collections.jpg&quot;
        srcset=&quot;/static/75c7739608bd63d1c224691f6ec0ea1d/8ddbc/058_collections.jpg 75w,
/static/75c7739608bd63d1c224691f6ec0ea1d/75985/058_collections.jpg 150w,
/static/75c7739608bd63d1c224691f6ec0ea1d/f93b5/058_collections.jpg 300w,
/static/75c7739608bd63d1c224691f6ec0ea1d/20e5d/058_collections.jpg 450w,
/static/75c7739608bd63d1c224691f6ec0ea1d/b4294/058_collections.jpg 600w,
/static/75c7739608bd63d1c224691f6ec0ea1d/b5c84/058_collections.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Exploring stack calls with the debugger]]></title><description><![CDATA[Become familiar with the core debugging tools available with IntelliJ.]]></description><link>https://aneesh.co.uk/exploring-stack-calls-with-the-debugger</link><guid isPermaLink="false">https://aneesh.co.uk/exploring-stack-calls-with-the-debugger</guid><pubDate>Mon, 05 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;IntelliJ Debugger&lt;/h4&gt;
&lt;p&gt;
Take a look at 3 of the most commonly used tools of the debugger that allow you to navigate your application run with control.
A break point will enable you to pause your application each time a specific line of code is executed, from there, we can use the step over tool to execute each line, one by one, 
and the step into tool to dive into any other method calls made on that line.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/JnTfQCjq9xM&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAABcuhMow3/xAAaEAACAgMAAAAAAAAAAAAAAAABEgACEBEx/9oACAEBAAEFAkiaii2DyoAH/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8BR//EABgQAAIDAAAAAAAAAAAAAAAAAAAhEBFB/9oACAEBAAY/AjBVKP/EAB0QAAICAQUAAAAAAAAAAAAAAAABESFhEEFxgZH/2gAIAQEAAT8h6+EEmaYHeZuNLOsEaTc//9oADAMBAAIAAwAAABC3z//EABcRAQEBAQAAAAAAAAAAAAAAAAEAESH/2gAIAQMBAT8QB3tq3//EABgRAQEAAwAAAAAAAAAAAAAAAAEAESFB/9oACAECAQE/EHHJBq//xAAbEAEBAAIDAQAAAAAAAAAAAAABEQAhEDFxsf/aAAgBAQABPxCEBs85YQAqp1jwXkoONy2KExXMU9r9z//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/1ac4101900838857fcde01f7fbecd72d/f93b5/057_debug.jpg&quot;
        srcset=&quot;/static/1ac4101900838857fcde01f7fbecd72d/8ddbc/057_debug.jpg 75w,
/static/1ac4101900838857fcde01f7fbecd72d/75985/057_debug.jpg 150w,
/static/1ac4101900838857fcde01f7fbecd72d/f93b5/057_debug.jpg 300w,
/static/1ac4101900838857fcde01f7fbecd72d/20e5d/057_debug.jpg 450w,
/static/1ac4101900838857fcde01f7fbecd72d/b4294/057_debug.jpg 600w,
/static/1ac4101900838857fcde01f7fbecd72d/b5c84/057_debug.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Navigating across IntelliJ]]></title><description><![CDATA[Become familiar with key keyboard shortcuts to search across application files and method calls.]]></description><link>https://aneesh.co.uk/navigating-across-intellij</link><guid isPermaLink="false">https://aneesh.co.uk/navigating-across-intellij</guid><pubDate>Mon, 28 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;IntelliJ Navigation&lt;/h4&gt;
&lt;p&gt;
Discover keyboard shortcuts that can help you to find specific files, methods, classes and instances across your application with higher efficiency with IntelliJ. 
IntelliJ offers different ways that we can instantly specify where we would like to search and what type of item we are looking for (whether it be a class, file, method, variable and so on.)
Lastly, we will look at how you can control-click your way around an application to see where methods are called and what their implementation is.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/rMgGV_nFVdc&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAP/aAAwDAQACEAMQAAABcuhMow3/xAAaEAACAgMAAAAAAAAAAAAAAAABEgACEBEx/9oACAEBAAEFAkiaii2DyoAH/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8BR//EABgQAAIDAAAAAAAAAAAAAAAAAAAhEBFB/9oACAEBAAY/AjBVKP/EAB0QAAICAQUAAAAAAAAAAAAAAAABESFhEEFxgZH/2gAIAQEAAT8h6+EEmaYHeZuNLOsEaTc//9oADAMBAAIAAwAAABC3z//EABcRAQEBAQAAAAAAAAAAAAAAAAEAESH/2gAIAQMBAT8QB3tq3//EABgRAQEAAwAAAAAAAAAAAAAAAAEAESFB/9oACAECAQE/EHHJBq//xAAbEAEBAAIDAQAAAAAAAAAAAAABEQAhEDFxsf/aAAgBAQABPxCEBs85YQAqp1jwXkoONy2KExXMU9r9z//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/1ac4101900838857fcde01f7fbecd72d/f93b5/057_debug.jpg&quot;
        srcset=&quot;/static/1ac4101900838857fcde01f7fbecd72d/8ddbc/057_debug.jpg 75w,
/static/1ac4101900838857fcde01f7fbecd72d/75985/057_debug.jpg 150w,
/static/1ac4101900838857fcde01f7fbecd72d/f93b5/057_debug.jpg 300w,
/static/1ac4101900838857fcde01f7fbecd72d/20e5d/057_debug.jpg 450w,
/static/1ac4101900838857fcde01f7fbecd72d/b4294/057_debug.jpg 600w,
/static/1ac4101900838857fcde01f7fbecd72d/b5c84/057_debug.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using the Rule Annotation]]></title><description><![CDATA[Use the Rule annotation and the TemporaryFolder to create and teardown files for use within unit testing.]]></description><link>https://aneesh.co.uk/using-the-rule-annotation</link><guid isPermaLink="false">https://aneesh.co.uk/using-the-rule-annotation</guid><pubDate>Mon, 21 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;The Rule Annotation&lt;/h4&gt;
&lt;p&gt;
Leverage the Rule annotation to neatly surround your unit tests with bespoke logic for setting up and tearing down components that can be used within each test. Explore the different implementations that come out of the box with JUnit, and dive a little deeper to understand how the TemporaryFolder may be valuable to use when testing with files.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/KzJ89AFewRM&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAHBAAAQMFAAAAAAAAAAAAAAAAAAEQETFBYZGx/9oACAEBAAE/IZxYYE0RLR+B/9oADAMBAAIAAwAAABCbH//EABcRAQADAAAAAAAAAAAAAAAAAAABEUH/2gAIAQMBAT8QnJR//8QAFhEAAwAAAAAAAAAAAAAAAAAAARBR/9oACAECAQE/EBSf/8QAGhABAQEBAAMAAAAAAAAAAAAAAREAITFhcf/aAAgBAQABPxAiCZ6ZMjH5zTepg5gL4wE8z//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/b09092b7493020c654b4bc19cf293dba/f93b5/055_rule.jpg&quot;
        srcset=&quot;/static/b09092b7493020c654b4bc19cf293dba/8ddbc/055_rule.jpg 75w,
/static/b09092b7493020c654b4bc19cf293dba/75985/055_rule.jpg 150w,
/static/b09092b7493020c654b4bc19cf293dba/f93b5/055_rule.jpg 300w,
/static/b09092b7493020c654b4bc19cf293dba/20e5d/055_rule.jpg 450w,
/static/b09092b7493020c654b4bc19cf293dba/b4294/055_rule.jpg 600w,
/static/b09092b7493020c654b4bc19cf293dba/b5c84/055_rule.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Improve test readability with matchers]]></title><description><![CDATA[Create a custom matcher to enhance assertion readability and logical processing with the AssertJ library]]></description><link>https://aneesh.co.uk/improve-test-readability-with-matchers</link><guid isPermaLink="false">https://aneesh.co.uk/improve-test-readability-with-matchers</guid><pubDate>Mon, 14 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Creating a custom matcher&lt;/h4&gt;
&lt;p&gt;
Improve the readability of your unit test assertions with custom assertions, created with the AssertJ library. In this video, we take a look at how we can create a custom assertion to perform custom logic and readability to the assertions we apply within our tests
&lt;p&gt;&lt;a href=&quot;https://youtu.be/K4Q9R-_eMMU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAHaxWXNXk//xAAaEAACAgMAAAAAAAAAAAAAAAAAAQISAxAx/9oACAEBAAEFAqFWPHuff//EABYRAQEBAAAAAAAAAAAAAAAAAAASAf/aAAgBAwEBPwGcQ//EABYRAQEBAAAAAAAAAAAAAAAAAAARIf/aAAgBAgEBPwHVf//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABsQAAMAAgMAAAAAAAAAAAAAAAABETGREGGx/9oACAEBAAE/Iaixo61ok8cRHgf/2gAMAwEAAgADAAAAEIMf/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAERQf/aAAgBAwEBPxCcFH//xAAWEQADAAAAAAAAAAAAAAAAAAABEFH/2gAIAQIBAT8QFJ//xAAaEAEAAwADAAAAAAAAAAAAAAABABEhMVFx/9oACAEBAAE/ECszXGZgmPmNW6qAdQsMIBOR/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/87fcdc2c87a799a2e9363e5782f37d05/f93b5/054_customMatchers.jpg&quot;
        srcset=&quot;/static/87fcdc2c87a799a2e9363e5782f37d05/8ddbc/054_customMatchers.jpg 75w,
/static/87fcdc2c87a799a2e9363e5782f37d05/75985/054_customMatchers.jpg 150w,
/static/87fcdc2c87a799a2e9363e5782f37d05/f93b5/054_customMatchers.jpg 300w,
/static/87fcdc2c87a799a2e9363e5782f37d05/20e5d/054_customMatchers.jpg 450w,
/static/87fcdc2c87a799a2e9363e5782f37d05/b4294/054_customMatchers.jpg 600w,
/static/87fcdc2c87a799a2e9363e5782f37d05/b5c84/054_customMatchers.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Improve test readability with matchers]]></title><description><![CDATA[Use matchers to improve the readability of assertions with the AssertJ library]]></description><link>https://aneesh.co.uk/improve-test-readability-with-matchers</link><guid isPermaLink="false">https://aneesh.co.uk/improve-test-readability-with-matchers</guid><pubDate>Mon, 07 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Using Matchers with assertions&lt;/h4&gt;
&lt;p&gt;
Matchers can be used with external libraries such as Hamcrest, FEST and AssertJ to improve the readability of the assertions we write in our test classes.
Learn about the different matchers that are available as standard from the AssertJ library as we refactor assertions made in JUnit to be more readable.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/K4Q9R-_eMMU&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAGxAAAgIDAQAAAAAAAAAAAAAAAAERMRBhkbH/2gAIAQEAAT8hlCoa1widZ8D/2gAMAwEAAgADAAAAEIsf/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAERQf/aAAgBAwEBPxCcVH//xAAWEQADAAAAAAAAAAAAAAAAAAABEFH/2gAIAQIBAT8QFp//xAAaEAEAAwADAAAAAAAAAAAAAAABABEhMVFx/9oACAEBAAE/ECszXGJglp5jGOqgHRAFMIBOR//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/7224a23c5fd4a4fbebd5313be8954222/f93b5/053_matchers.jpg&quot;
        srcset=&quot;/static/7224a23c5fd4a4fbebd5313be8954222/8ddbc/053_matchers.jpg 75w,
/static/7224a23c5fd4a4fbebd5313be8954222/75985/053_matchers.jpg 150w,
/static/7224a23c5fd4a4fbebd5313be8954222/f93b5/053_matchers.jpg 300w,
/static/7224a23c5fd4a4fbebd5313be8954222/20e5d/053_matchers.jpg 450w,
/static/7224a23c5fd4a4fbebd5313be8954222/b4294/053_matchers.jpg 600w,
/static/7224a23c5fd4a4fbebd5313be8954222/b5c84/053_matchers.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Inside Java: why it works everywhere]]></title><description><![CDATA[Take a look under the hood of Java to understand how code is compiled, interpreted and executed within the machine.]]></description><link>https://aneesh.co.uk/inside-java-why-it-works-everywhere</link><guid isPermaLink="false">https://aneesh.co.uk/inside-java-why-it-works-everywhere</guid><pubDate>Mon, 07 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand how the Java engine works to interpret and execute code.&lt;br&gt;
&amp;#8226; Review the stages of compilation and execution that support the type safety and memory management properties of Java.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Java - a 30 year old language&lt;/h4&gt;
&lt;p&gt;
Java programming language is one of the most popular programming languages in the world that is prevalent across industries and careers. You can gather an idea of
how diverse and important Java has been in shaping peoples lives by searching upon the hashtag #movedByJava on social media platforms such as Twitter. 
&lt;/p&gt;
&lt;p&gt;
Until recently, Java had remained as one the two most highly used languages on GitHub since 2012; it has since been overtaken by the languages Python and JavaScript. 
One of the drivers to the popularity of Python and JavaScript is their syntax design that makes it easier to learn and build with.
&lt;/p&gt;
&lt;p&gt;
As a compiled and statically typed language, Java enables faster runtime speeds than Python and JavaScript, despite this, it is still criticised for its runtime speed in 
comparison to other object-orientated programming languages such as C++. This blog will look &apos;under the hood&apos; of Java to understand what happens to code when we run it and
why it may have a performance disadvantage to its other object-orientated counterparts.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Compiling vs Interpreting&lt;/h4&gt;
&lt;p&gt;
When we write software code, we are, more-often-than-not, writing in a high-level language that is to be converted and understood by the computer.
The high-level code contains easy-to-read syntax that is then compiled into a lower level language (such as machine/binary code) that is to be executed by the CPU.
The process of taking the high-level code and translating it into a lower level is often a defining feature of a programming language that will 
underpin the performance and properties of the language.  
&lt;/p&gt;
&lt;p&gt;
It is important to distinguish between the word &apos;compile&apos; and &apos;interpret&apos; before we begin exploring the Java engine. &apos;Compile&apos; involves converting a language
into a different level of language (often down). Interpret however involves converting the language into a similar-level language.
A good analogy would be an English to French &lt;i&gt;interpreter&lt;/i&gt; who simply converts one language into the other at the same level of understanding. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The Java Compiler&lt;/h4&gt;
&lt;p&gt;
Before Java code is processed by the Java engine, it is first compiled with the Java compiler.
The Java compiler will first inspect the code to ensure it can be processed by the Java engine. Code inspection includes checking for TypeErrors
(such as passing in the incorrect parameter to a method) and Syntax errors. Compile errors are usually captured by a modern IDE such as IntelliJ or Eclipse as it actively analyses code as it is written.
&lt;/p&gt;
&lt;p&gt;
The Java compiler is later used to convert the high-level Java programming language code into a lower level &apos;bytecode&apos; that is similar to machine code; 
as a result, the .java files are compiled into .class files of bytecode (see the /target directory of your project after creating a build). 
The bytecode is later received and processed by the Java engine.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Code Interpretation and Execution&lt;/h4&gt;
&lt;p&gt;
The Java engine that performs the compilation and interpretation of code is known as the Java Virtual Machine (JVM).
The JVM resides within the RAM where it receives the bytecode following the first compilation step. 
The JVM acts as a runtime engine that enables Write Once Run Anywhere (WORA); unlike other languages such as C++, Java code is not converted explicitly for the operating
system that is running the code, but for the JVM which will be identically running the application regardless of the operating system. 
&lt;/p&gt;
&lt;p&gt;
The JVM steps for processing code is split into 3 key sections:
1. Class loader - where the files are loaded into the JVM to be later stored and used. 
2. Runtime data area - where memory area is designated for classes to be stored for execution. 
3. Execution Engine - where bytecode is interpreted into machine code using the Just In Time Compiler and is executed.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Class loader&lt;/h4&gt;
&lt;p&gt;
The Classloader is responsible for loading the .class bytecode files from the target directory into the JVM. This process is known as &lt;i&gt;loading&lt;/i&gt;.
&lt;/p&gt;
&lt;p&gt;
After loading, a second phase called &lt;i&gt;linking&lt;/i&gt; begins. During linking, memory is designated for static variables of each class and the default values are assigned. 
The processing of linking is divided into three steps: first the type safety of Objects are verified, second, the memory required by the Object is allocated, and the final stage of resolution is the process of replacing the symbolic references of classes with real class references.  
&lt;/p&gt;
&lt;p&gt;
The final stage of the class loader is called &lt;i&gt;initialisation&lt;/i&gt;. Initialisation involves invoking the initialisation method of the class (use of the &apos;new&apos; keyword) and the static blocks e.g:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MyClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;//static initialisation block&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Runtime data area&lt;/h4&gt;
&lt;p&gt;
The runtime data area contains the compiled code as well as the methods and properties of classes that are used for execution. 
The sequential instructions (which piece of code to execute) of the application are followed within the runtime data area as Objects are managed and used accordingly.
&lt;/p&gt;
&lt;p&gt;
The Runtime data area is split into 5 different areas with its own responsibility:&lt;br&gt;
1. Method Area: An area shared across all threads of the runtime environment, the method area stores static variables of each class.&lt;br&gt;
2. Heap Space: Stores the objects and instance variables being created and used at runtime. The area is managed using &lt;a href=&quot;https://aneesh.co.uk/how-the-jvm-manages-memory&quot;&gt;Garbage collection&lt;/a&gt;.&lt;br&gt;
3. Stack Memory: Each thread contains its own stack memory. This is where &lt;u&gt;local&lt;/u&gt; variables and memory from method calls are held. When the memory being held on the stack exceeds its limit, a &lt;code&gt;StackOverflowException&lt;/code&gt; is thrown. &lt;br&gt;
4. Program Counter Register: Acts as a pointer to the current instruction of the program.&lt;br&gt;
5. Native method stack: Similar to the stack memory, however it is used for executing methods that are not written with Java. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Execution engine&lt;/h4&gt;
&lt;p&gt;
While the runtime data area manages the Objects and properties of our classes at runtime, the execution engine is responsible for executing the logic 
of the application with the computer, assisted by the various areas of the runtime data area. 
&lt;/p&gt;
&lt;p&gt;
There are 3 main components to the engine:&lt;br&gt;
1. Interpreter: Responsible for reading the bytecode into machine code to be executed in a sequential manner.&lt;br&gt;
2. Just in time (JIT) compiler: Provides support to the interpreter. Rather than repeatedly interpreting the same bytecode from repeated method calls, the JIT will use its profiler to identify &apos;hotspot&apos; areas within the application that are repeatedly executed. The JIT provides a performance enhancement for the interpreter.&lt;br&gt;
3. The garbage collector. Responsible for allocating and removing memory storage for the heap space Objects.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why Java is perceived as &apos;slow&apos;&lt;/h4&gt;
&lt;p&gt;
The broadly incorrect reputation Java acquired from its earlier days with &apos;slow&apos; processing speed came by its comparison with other object-orientated programming languages.
The present existence of Java (currently version 16) includes far greater performance optimisation than its first versions. 
Despite this, Java still retains certain &apos;slow&apos; features that are embedded by its design and implementation. 
The &apos;slow&apos; features include the following:&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
1. Multiple-compilation&lt;br&gt;
The Java code we write is compiled twice: once with the Java compiler into .class files, and sometimes again within the JVM by the JIT compiler.
The JIT compiler acts as an optimizer, preventing the JVM from repeatedly interpreting code again and again. Instead, code is compiled to native machine code
that can then be executed. Compilation happens to be a major driver in execution times, and can therefore impact the overall speed of Java.
&lt;/p&gt;
&lt;p&gt;
2. Java encourages code to be written for correctness and readability, rather than performance.&lt;br&gt;
The linking process of the ClassLoader creates a large startup time (known as warm-up time) as it allocates memory and assigns references to Objects for the application to begin. 
The process of linking occurs each time the application is run, however in other languages such as C, the linking process only occurs once following compilation. 
&lt;/p&gt;
&lt;p&gt;
3. The Java String Object&lt;br&gt;
Java stores and handles Strings in a unique manner with their own memory area called the &lt;a href=&quot;https://aneesh.co.uk/the-string-class&quot;&gt;String constant pool&lt;/a&gt;.
The combination of how Strings are handled as immutable Objects and their use of UTF-16 encoding, contributes to a large memory requirement and inhibition upon performance.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
In this blog, we have taken a look at how the high-level code we write undergoes many transformations and movement within our CPU as it is executed.
Java, like all languages, has its own style and design to compiling and interpreting code that provides the developer with unique features such as WORA and static typing.
&lt;/p&gt;
&lt;p&gt;
When comparing Java with other languages, there will always be arguments for and against where the properties of compilation time, execution time, syntax, and portability all play a 
vital role on a case-by-case basis. 
By understanding these concepts with reference to Java, you can begin to understand why certain technologies can be more appropriate to use in certain contexts, and when Java
can be the technology of choice for you.  
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Enhanced exceptions testing with catch-exception]]></title><description><![CDATA[Use the catch-exception library to enhance the readability and detail of assertions made when testing for exceptions.]]></description><link>https://aneesh.co.uk/enhanced-exceptions-testing-with-catch-exception</link><guid isPermaLink="false">https://aneesh.co.uk/enhanced-exceptions-testing-with-catch-exception</guid><pubDate>Mon, 31 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;The catch-exception library&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Learn about the limitations to conventional exception testing from JUnit and resolve them using the catch-exception library from googlecodes. &lt;/p&gt;
&lt;p&gt;The catch-exception library allows us to control a test method to capture an exception that can then be asserted upon with more detail and upon the runtime behaviour of our test. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/lLjZ3HZQMSI&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 53.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIFBP/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAF13lZTiiJ//8QAGBAAAwEBAAAAAAAAAAAAAAAAAAESEBP/2gAIAQEAAQUChknPWf/EABYRAQEBAAAAAAAAAAAAAAAAAAASAf/aAAgBAwEBPwGcQ//EABYRAQEBAAAAAAAAAAAAAAAAAAARIf/aAAgBAgEBPwHVf//EABYQAQEBAAAAAAAAAAAAAAAAAEEAIP/aAAgBAQAGPwIjP//EABkQAAMBAQEAAAAAAAAAAAAAAAABEZExYf/aAAgBAQABPyHwwNmpMDl8IiIVU//aAAwDAQACAAMAAAAQcA//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJyUf//EABYRAQEBAAAAAAAAAAAAAAAAAAEQQf/aAAgBAgEBPxAdR//EABsQAQACAwEBAAAAAAAAAAAAAAEAESFBYTFx/9oACAEBAAE/EH5WgxYqnm41TlOELjBNSeT/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/3b85c26436860f6cbeba0df2329b4136/f93b5/051_exceptions2.jpg&quot;
        srcset=&quot;/static/3b85c26436860f6cbeba0df2329b4136/8ddbc/051_exceptions2.jpg 75w,
/static/3b85c26436860f6cbeba0df2329b4136/75985/051_exceptions2.jpg 150w,
/static/3b85c26436860f6cbeba0df2329b4136/f93b5/051_exceptions2.jpg 300w,
/static/3b85c26436860f6cbeba0df2329b4136/20e5d/051_exceptions2.jpg 450w,
/static/3b85c26436860f6cbeba0df2329b4136/b4294/051_exceptions2.jpg 600w,
/static/3b85c26436860f6cbeba0df2329b4136/b7f47/051_exceptions2.jpg 1804w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How to choose the tests to write]]></title><description><![CDATA[Understand how to design the tests to write for the purposes of a method and its place within an application.]]></description><link>https://aneesh.co.uk/how-to-choose-the-tests-to-write</link><guid isPermaLink="false">https://aneesh.co.uk/how-to-choose-the-tests-to-write</guid><pubDate>Mon, 24 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Choosing tests to write for a method&lt;/h4&gt;
&lt;p&gt;
Learn about the different types of data values we might want to consider when testing our methods for the different type of contexts that they may be used in. 
Following this video, you will understand the differences between expected, boundary and strange values that can support enhance the test cases you write. 
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eGwQmmHsEoc&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAHBAAAQMFAAAAAAAAAAAAAAAAAAERMRAhQZGx/9oACAEBAAE/IXNgWYTQ0sV4H//aAAwDAQACAAMAAAAQix//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJzUf//EABYRAAMAAAAAAAAAAAAAAAAAAAEQUf/aAAgBAgEBPxAWn//EABkQAQEBAQEBAAAAAAAAAAAAAAERACExcf/aAAgBAQABPxAmCZ5xpdR+c03qYCeGApwwE8z/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/a4861a8496a5575bedc8c5ab92680d9d/f93b5/050_parameters.jpg&quot;
        srcset=&quot;/static/a4861a8496a5575bedc8c5ab92680d9d/8ddbc/050_parameters.jpg 75w,
/static/a4861a8496a5575bedc8c5ab92680d9d/75985/050_parameters.jpg 150w,
/static/a4861a8496a5575bedc8c5ab92680d9d/f93b5/050_parameters.jpg 300w,
/static/a4861a8496a5575bedc8c5ab92680d9d/20e5d/050_parameters.jpg 450w,
/static/a4861a8496a5575bedc8c5ab92680d9d/b4294/050_parameters.jpg 600w,
/static/a4861a8496a5575bedc8c5ab92680d9d/b5c84/050_parameters.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using Mockito Verify]]></title><description><![CDATA[Use Mockito Verify to test method calls made within a method.]]></description><link>https://aneesh.co.uk/using-mockito-verify</link><guid isPermaLink="false">https://aneesh.co.uk/using-mockito-verify</guid><pubDate>Mon, 17 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Mockito Verify&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Learn about the when you would want to use Mockito&apos;s verify assertion within unit testing and how you would use it.  &lt;/p&gt;
&lt;p&gt;In this video, I have introduced the concept of the verify assertion using an example to demonstrate how it can be beneficial to covering behaviour cases of an application where there are no return types or method calls made within the method. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=lbN2nfsC7C4&amp;#x26;feature=youtu.be&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABuNzWWaPif//EABoQAAICAwAAAAAAAAAAAAAAAAABAhIDEDH/2gAIAQEAAQUCoVY8e59//8QAFhEBAQEAAAAAAAAAAAAAAAAAABIB/9oACAEDAQE/AZxD/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8BV//EABUQAQEAAAAAAAAAAAAAAAAAACDh/9oACAEBAAY/Aov/xAAbEAADAAIDAAAAAAAAAAAAAAAAAREhMUGRsf/aAAgBAQABPyGpwMOl0SeiIiPA/9oADAMBAAIAAwAAABCjH//EABYRAQEBAAAAAAAAAAAAAAAAAAABEf/aAAgBAwEBPxCrD//EABYRAAMAAAAAAAAAAAAAAAAAAAEQEf/aAAgBAgEBPxAVP//EABoQAAMBAAMAAAAAAAAAAAAAAAABESExUXH/2gAIAQEAAT8QUyzOMEtR8jpvUF0IVFiETXB//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/fb5c79bcbef4b7b8518fe3816ab20ac4/f93b5/049_verify.jpg&quot;
        srcset=&quot;/static/fb5c79bcbef4b7b8518fe3816ab20ac4/8ddbc/049_verify.jpg 75w,
/static/fb5c79bcbef4b7b8518fe3816ab20ac4/75985/049_verify.jpg 150w,
/static/fb5c79bcbef4b7b8518fe3816ab20ac4/f93b5/049_verify.jpg 300w,
/static/fb5c79bcbef4b7b8518fe3816ab20ac4/20e5d/049_verify.jpg 450w,
/static/fb5c79bcbef4b7b8518fe3816ab20ac4/b4294/049_verify.jpg 600w,
/static/fb5c79bcbef4b7b8518fe3816ab20ac4/b5c84/049_verify.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Mock, Stub, and Spy with Mockito]]></title><description><![CDATA[Use mock, stub and spy to create different types of test doubles with unit testing]]></description><link>https://aneesh.co.uk/mock-stub-and-spy-with-mockito</link><guid isPermaLink="false">https://aneesh.co.uk/mock-stub-and-spy-with-mockito</guid><pubDate>Mon, 10 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Mock, Stub and Spy&lt;/h4&gt;
&lt;p&gt;
Learn about the differences between a mock, a stub, and a spy for the purposes of creating test doubles with the Mockito framework. 
In this video, I have used an example to demonstrate when we would want to use each of the different test doubles and the unique benefits they can offer when automation testing.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/xXO8ft-tsrY&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAGxAAAwACAwAAAAAAAAAAAAAAAAEREDFhkbH/2gAIAQEAAT8hqLQ410ScmIjwP//aAAwDAQACAAMAAAAQix//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJwUf//EABcRAQADAAAAAAAAAAAAAAAAAAABEVH/2gAIAQIBAT8QjSz/xAAZEAEAAwEBAAAAAAAAAAAAAAABABEhMZH/2gAIAQEAAT8QKzNcxMK9TGvaqAVwhYYQCcj/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/3a9afe830df8c27e2941cc40e2bb4a7f/f93b5/048_mockStubSpy.jpg&quot;
        srcset=&quot;/static/3a9afe830df8c27e2941cc40e2bb4a7f/8ddbc/048_mockStubSpy.jpg 75w,
/static/3a9afe830df8c27e2941cc40e2bb4a7f/75985/048_mockStubSpy.jpg 150w,
/static/3a9afe830df8c27e2941cc40e2bb4a7f/f93b5/048_mockStubSpy.jpg 300w,
/static/3a9afe830df8c27e2941cc40e2bb4a7f/20e5d/048_mockStubSpy.jpg 450w,
/static/3a9afe830df8c27e2941cc40e2bb4a7f/b4294/048_mockStubSpy.jpg 600w,
/static/3a9afe830df8c27e2941cc40e2bb4a7f/b5c84/048_mockStubSpy.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Getting started with Mockito]]></title><description><![CDATA[Use the mock, when and thenReturn keywords to get starting with mocking frameworks.]]></description><link>https://aneesh.co.uk/getting-started-with-mockito</link><guid isPermaLink="false">https://aneesh.co.uk/getting-started-with-mockito</guid><pubDate>Mon, 03 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Getting started with Mockito&lt;/h4&gt;
&lt;p&gt;
Learn about what a mocking framework is used for with unit testing, when we it would want to use it, and how we can use the Mockito framework within our tests. 
This tutorial involves testing a class method in isolation despite its dependency upon a database call. We will understand why mocking is useful and how we can approach it in 3 steps.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/MztH6vkeFVk&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAHBAAAQMFAAAAAAAAAAAAAAAAAAEQETFBYZGx/9oACAEBAAE/IZRYY00RLR+B/9oADAMBAAIAAwAAABCLH//EABYRAQEBAAAAAAAAAAAAAAAAAAABEf/aAAgBAwEBPxCnD//EABYRAAMAAAAAAAAAAAAAAAAAAAEQUf/aAAgBAgEBPxAWn//EABkQAQADAQEAAAAAAAAAAAAAAAEAESExcf/aAAgBAQABPxArGa5iYVj5jVuqgFcIAphAJyP/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/ce1c0657c3aab7950582c29d87d10612/f93b5/047_mockito.jpg&quot;
        srcset=&quot;/static/ce1c0657c3aab7950582c29d87d10612/8ddbc/047_mockito.jpg 75w,
/static/ce1c0657c3aab7950582c29d87d10612/75985/047_mockito.jpg 150w,
/static/ce1c0657c3aab7950582c29d87d10612/f93b5/047_mockito.jpg 300w,
/static/ce1c0657c3aab7950582c29d87d10612/20e5d/047_mockito.jpg 450w,
/static/ce1c0657c3aab7950582c29d87d10612/b4294/047_mockito.jpg 600w,
/static/ce1c0657c3aab7950582c29d87d10612/b5c84/047_mockito.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Testing for exceptions with JUnit]]></title><description><![CDATA[Use JUnit to test for exceptions within our class methods.]]></description><link>https://aneesh.co.uk/testing-for-exceptions-with-junit</link><guid isPermaLink="false">https://aneesh.co.uk/testing-for-exceptions-with-junit</guid><pubDate>Mon, 26 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Testing for Exceptions with JUnit&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Learn how you can test for expected exceptions that are thrown from your methods with JUnit.&lt;br&gt;
With just a single line, we can increase our test coverage to expect exceptions to be thrown from our class behaviour.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/3N7-pwSx-HA&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABvDpWWaPif//EABoQAAICAwAAAAAAAAAAAAAAAAABAhIDEDH/2gAIAQEAAQUCoVY8e59//8QAFhEBAQEAAAAAAAAAAAAAAAAAABIB/9oACAEDAQE/AZxD/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEh/9oACAECAQE/AdV//8QAFRABAQAAAAAAAAAAAAAAAAAAIOH/2gAIAQEABj8Ci//EABwQAAIBBQEAAAAAAAAAAAAAAAARARAxQWGRsf/aAAgBAQABPyFlgaI4Km1EeB//2gAMAwEAAgADAAAAEMMf/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAERQf/aAAgBAwEBPxCclH//xAAWEQADAAAAAAAAAAAAAAAAAAABEFH/2gAIAQIBAT8QFJ//xAAZEAEAAwEBAAAAAAAAAAAAAAABABEhMWH/2gAIAQEAAT8QK1c8xESml8xq3VQCuEAphAJyP//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/a30bdc4f3b802ac643fd3aee2981693a/f93b5/046_exceptions.jpg&quot;
        srcset=&quot;/static/a30bdc4f3b802ac643fd3aee2981693a/8ddbc/046_exceptions.jpg 75w,
/static/a30bdc4f3b802ac643fd3aee2981693a/75985/046_exceptions.jpg 150w,
/static/a30bdc4f3b802ac643fd3aee2981693a/f93b5/046_exceptions.jpg 300w,
/static/a30bdc4f3b802ac643fd3aee2981693a/20e5d/046_exceptions.jpg 450w,
/static/a30bdc4f3b802ac643fd3aee2981693a/b4294/046_exceptions.jpg 600w,
/static/a30bdc4f3b802ac643fd3aee2981693a/b5c84/046_exceptions.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[When and how to use parameterized unit testing]]></title><description><![CDATA[Using parameterized testing can enable you to efficiently test different cases for a method with different method arguments.]]></description><link>https://aneesh.co.uk/when-and-how-to-use-parameterized-unit-testing</link><guid isPermaLink="false">https://aneesh.co.uk/when-and-how-to-use-parameterized-unit-testing</guid><pubDate>Mon, 19 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Parameterized unit testing&lt;/h4&gt;
&lt;p&gt;
Learn about how and when you could benefit from creating a test class that uses the JUnitParams package in just 4 steps.  
The JUnitParams package will enable you to increase test coverage of a method without necessarily creating new test methods for it, instead, we can pass in different parameters that use the method differently and express a different expected result.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/wLFS0-MZRfo&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAHBAAAgICAwAAAAAAAAAAAAAAABEBMRAhQZGx/9oACAEBAAE/IWXA01HQqawjwP/aAAwDAQACAAMAAAAQix//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJzUf//EABYRAAMAAAAAAAAAAAAAAAAAAAEQUf/aAAgBAgEBPxAUn//EABsQAQACAgMAAAAAAAAAAAAAAAEAESExUWFx/9oACAEBAAE/ECsGvSVK6fMatyqAcEBeiATiP//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/3bb72aa9bfa6ce5af061e17d1038a5e7/f93b5/045_params.jpg&quot;
        srcset=&quot;/static/3bb72aa9bfa6ce5af061e17d1038a5e7/8ddbc/045_params.jpg 75w,
/static/3bb72aa9bfa6ce5af061e17d1038a5e7/75985/045_params.jpg 150w,
/static/3bb72aa9bfa6ce5af061e17d1038a5e7/f93b5/045_params.jpg 300w,
/static/3bb72aa9bfa6ce5af061e17d1038a5e7/20e5d/045_params.jpg 450w,
/static/3bb72aa9bfa6ce5af061e17d1038a5e7/b4294/045_params.jpg 600w,
/static/3bb72aa9bfa6ce5af061e17d1038a5e7/b5c84/045_params.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using the @Before Annotation with JUnit]]></title><description><![CDATA[The @Before annotation allows us to run custom logic before each unit test of a test class.]]></description><link>https://aneesh.co.uk/using-the-before-annotation-with-junit</link><guid isPermaLink="false">https://aneesh.co.uk/using-the-before-annotation-with-junit</guid><pubDate>Mon, 12 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Using the @Before Annotation with JUnit&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Learn about how and when you would use the @Before annotation with JUnit by exploring its use with an example.
The @Before annotation will enable you to remove repeated code within your test classes, typically when instantiating classes and defining the &apos;given&apos; phase of unit testing.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/q07eioOk4v0&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABuNzWWaPif//EABoQAAICAwAAAAAAAAAAAAAAAAABAhIDEDH/2gAIAQEAAQUCoVY8e59//8QAFhEBAQEAAAAAAAAAAAAAAAAAABIB/9oACAEDAQE/AZxD/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQIBAT8BV//EABUQAQEAAAAAAAAAAAAAAAAAACDh/9oACAEBAAY/Aov/xAAaEAADAQADAAAAAAAAAAAAAAAAARExYZGx/9oACAEBAAE/IaiwcK6JPCIiPA//2gAMAwEAAgADAAAAEKMf/8QAFhEBAQEAAAAAAAAAAAAAAAAAAAER/9oACAEDAQE/EKMP/8QAFREBAQAAAAAAAAAAAAAAAAAAECH/2gAIAQIBAT8Qo//EABoQAQADAQEBAAAAAAAAAAAAAAEAESExUXH/2gAIAQEAAT8QKw55iVDdPzGrdVDwIWGEAnI//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/602aea897bdb65bca093bf8dd83b9633/f93b5/044_before.jpg&quot;
        srcset=&quot;/static/602aea897bdb65bca093bf8dd83b9633/8ddbc/044_before.jpg 75w,
/static/602aea897bdb65bca093bf8dd83b9633/75985/044_before.jpg 150w,
/static/602aea897bdb65bca093bf8dd83b9633/f93b5/044_before.jpg 300w,
/static/602aea897bdb65bca093bf8dd83b9633/20e5d/044_before.jpg 450w,
/static/602aea897bdb65bca093bf8dd83b9633/b4294/044_before.jpg 600w,
/static/602aea897bdb65bca093bf8dd83b9633/b5c84/044_before.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using the different JUnit assertions]]></title><description><![CDATA[JUnit assertions allow us to validate our expected behaviour with the actual application behaviour to create unit tests.]]></description><link>https://aneesh.co.uk/using-the-different-junit-assertions</link><guid isPermaLink="false">https://aneesh.co.uk/using-the-different-junit-assertions</guid><pubDate>Mon, 05 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;JUnit Assertions&lt;/h4&gt;
&lt;p&gt;
Learn about the different assertions that can be used with the unit testing framework: JUnit. 
Understand when and how you can apply different assertions to optimise and improve the readability of your test cases.
&lt;p&gt;&lt;a href=&quot;https://youtu.be/lPHjo3EWeGA&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAVEAEBAAAAAAAAAAAAAAAAAAAg4f/aAAgBAQAGPwKL/8QAHBAAAgICAwAAAAAAAAAAAAAAABEBMRAhQZGx/9oACAEBAAE/IWXA11HQqawjwP/aAAwDAQACAAMAAAAQix//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARFB/9oACAEDAQE/EJxUf//EABYRAAMAAAAAAAAAAAAAAAAAAAEQUf/aAAgBAgEBPxAUn//EABoQAQADAQEBAAAAAAAAAAAAAAEAESExQXH/2gAIAQEAAT8QK1c8xKPD8xjHVQCuEAphAJyP/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/662d6c9195ff2896327617c3b212cf37/f93b5/043_junit1.jpg&quot;
        srcset=&quot;/static/662d6c9195ff2896327617c3b212cf37/8ddbc/043_junit1.jpg 75w,
/static/662d6c9195ff2896327617c3b212cf37/75985/043_junit1.jpg 150w,
/static/662d6c9195ff2896327617c3b212cf37/f93b5/043_junit1.jpg 300w,
/static/662d6c9195ff2896327617c3b212cf37/20e5d/043_junit1.jpg 450w,
/static/662d6c9195ff2896327617c3b212cf37/b4294/043_junit1.jpg 600w,
/static/662d6c9195ff2896327617c3b212cf37/b5c84/043_junit1.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using the Single Responsibility Principle]]></title><description><![CDATA[Use the single responsibility principle development design to reduce the complexity of each class.]]></description><link>https://aneesh.co.uk/using-the-single-responsibility-principle</link><guid isPermaLink="false">https://aneesh.co.uk/using-the-single-responsibility-principle</guid><pubDate>Mon, 29 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Single Responsibility Principle&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;The single responsibility principle is a software development pattern that asserts each class to have a single purpose.
In this tutorial, we use the single responsibility principle by refactoring a multi-purpose class into single-purpose classes and explore the various benefits of this design. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=XHM7FUTn0kk&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQCBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB2sIXNXtT/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAECEgMQMf/aAAgBAQABBQKhVjx7n3//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQMBAT8BnEP/xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8B1X//xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAY/Al//xAAcEAACAQUBAAAAAAAAAAAAAAAAEQEQMUFhkbH/2gAIAQEAAT8hZY4aY4Km1EeB/9oADAMBAAIAAwAAABCLH//EABcRAQADAAAAAAAAAAAAAAAAAAABEUH/2gAIAQMBAT8QnBR//8QAFhEAAwAAAAAAAAAAAAAAAAAAARBR/9oACAECAQE/EBSf/8QAGRABAQEBAQEAAAAAAAAAAAAAAREAITFh/9oACAEBAAE/EJiefCckRj9Oab1MBPDApwwE8z//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/5a93601dfe0c924f1f66e30bc1cd1246/f93b5/042_srp.jpg&quot;
        srcset=&quot;/static/5a93601dfe0c924f1f66e30bc1cd1246/8ddbc/042_srp.jpg 75w,
/static/5a93601dfe0c924f1f66e30bc1cd1246/75985/042_srp.jpg 150w,
/static/5a93601dfe0c924f1f66e30bc1cd1246/f93b5/042_srp.jpg 300w,
/static/5a93601dfe0c924f1f66e30bc1cd1246/20e5d/042_srp.jpg 450w,
/static/5a93601dfe0c924f1f66e30bc1cd1246/b4294/042_srp.jpg 600w,
/static/5a93601dfe0c924f1f66e30bc1cd1246/b5c84/042_srp.jpg 1704w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[OOP Concepts: Polymorphism]]></title><description><![CDATA[A YouTube video on Polymorphism, one of the four concepts of object-orientated programming.]]></description><link>https://aneesh.co.uk/oop-concepts-polymorphism</link><guid isPermaLink="false">https://aneesh.co.uk/oop-concepts-polymorphism</guid><pubDate>Mon, 22 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Polymorphism&lt;/h4&gt;
&lt;p&gt;
Polymorphism is one of the four concepts of object-orientated programming. 
This video is a step-by-step guide on how you can use polymorphism within a Java application. There will be an overview of why polymorphism is appropriate to use and how you can use static and dynamic polymorphism to encourage code reuse with an example within IntelliJ. 
&lt;p&gt;Polymorphism is used in our design of how we reuse method names within the same class and with inheritance to create different implementations of the method. The details of how the methods are defined with parameters, and how the class that is calling it is created will ultimately determine which implementation of the method is used at runtime.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/nXRJMkmRRYE&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgP/xAAWAQEBAQAAAAAAAAAAAAAAAAAEAAL/2gAMAwEAAhADEAAAAd7INylCROv/xAAaEAACAwEBAAAAAAAAAAAAAAABAgADEjEy/9oACAEBAAEFAqgq1nDxwuqvUPf/xAAYEQACAwAAAAAAAAAAAAAAAAAAAQMRIf/aAAgBAwEBPwFQ0zUf/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQACIf/aAAgBAgEBPwEXVy//xAAbEAACAQUAAAAAAAAAAAAAAAABIQARIDFxkf/aAAgBAQAGPwIYjotmIDln/8QAGxABAAICAwAAAAAAAAAAAAAAAQARIXEQQVH/2gAIAQEAAT8hSK2UIe58ELTEh56jxf/aAAwDAQACAAMAAAAQwD//xAAXEQEBAQEAAAAAAAAAAAAAAAABACER/9oACAEDAQE/EAwW5gN//8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERIf/aAAgBAgEBPxB7opUh/8QAHBABAAICAwEAAAAAAAAAAAAAAREhADFBYaFx/9oACAEBAAE/EGoLFy+9cvWBCz0DZHwcE0zdg8uMZBage4bZVg5c35//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/b53b81ebc4ad9e5372ad84bb020c4f0a/f93b5/041_polymorphism.jpg&quot;
        srcset=&quot;/static/b53b81ebc4ad9e5372ad84bb020c4f0a/8ddbc/041_polymorphism.jpg 75w,
/static/b53b81ebc4ad9e5372ad84bb020c4f0a/75985/041_polymorphism.jpg 150w,
/static/b53b81ebc4ad9e5372ad84bb020c4f0a/f93b5/041_polymorphism.jpg 300w,
/static/b53b81ebc4ad9e5372ad84bb020c4f0a/20e5d/041_polymorphism.jpg 450w,
/static/b53b81ebc4ad9e5372ad84bb020c4f0a/b4294/041_polymorphism.jpg 600w,
/static/b53b81ebc4ad9e5372ad84bb020c4f0a/eea4a/041_polymorphism.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[OOP Concepts: Abstraction]]></title><description><![CDATA[A YouTube video on Abstraction, one of the four concepts of object-orientated programming.]]></description><link>https://aneesh.co.uk/oop-concepts-abstraction</link><guid isPermaLink="false">https://aneesh.co.uk/oop-concepts-abstraction</guid><pubDate>Mon, 15 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Abstraction&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Abstraction is one of the four concepts of object-orientated programming.
This video is a step-by-step guide on how you can use abstraction within a Java application. There will be an overview of why abstraction is appropriate to use and how you can enhance the simplicity of your code with an example within IntelliJ. &lt;/p&gt;
&lt;p&gt;Abstraction is used in our design of how we group behaviours of classes together. The details of how the behaviours are used by the class is delegated to the class itself, however abstraction defines a contract that enforces such behaviours to be handled by the class. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/0fqoJGPcZRg&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAECAwQF/8QAFgEBAQEAAAAAAAAAAAAAAAAAAgAB/9oADAMBAAIQAxAAAAHp0Sy4kIj/AP/EABoQAAICAwAAAAAAAAAAAAAAAAABAgMREjP/2gAIAQEAAQUCcjYdsc2JC6H/xAAWEQEBAQAAAAAAAAAAAAAAAAARARD/2gAIAQMBAT8BINz/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAaEAACAgMAAAAAAAAAAAAAAAAAEQEhEJHh/9oACAEBAAY/AqOsp6IkWP/EAB0QAAEEAwEBAAAAAAAAAAAAAAEAESExYXGxQVH/2gAIAQEAAT8hqL2w3SiM0+3CMgYlaD0jP0GVZO/IX//aAAwDAQACAAMAAAAQDw//xAAWEQEBAQAAAAAAAAAAAAAAAAABABH/2gAIAQMBAT8QBGll/8QAFhEBAQEAAAAAAAAAAAAAAAAAEQAB/9oACAECAQE/ENUm/8QAHBABAAMBAAMBAAAAAAAAAAAAAQARITFBYZFx/9oACAEBAAE/EMLpiLQH7wiOm+2n1xUc5VGLvmXNKqa6S5TrhqvsFaqUgFT4T//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/f0639766f2e5f23f732f60c3ca9452d2/f93b5/040_abstraction.jpg&quot;
        srcset=&quot;/static/f0639766f2e5f23f732f60c3ca9452d2/8ddbc/040_abstraction.jpg 75w,
/static/f0639766f2e5f23f732f60c3ca9452d2/75985/040_abstraction.jpg 150w,
/static/f0639766f2e5f23f732f60c3ca9452d2/f93b5/040_abstraction.jpg 300w,
/static/f0639766f2e5f23f732f60c3ca9452d2/20e5d/040_abstraction.jpg 450w,
/static/f0639766f2e5f23f732f60c3ca9452d2/b4294/040_abstraction.jpg 600w,
/static/f0639766f2e5f23f732f60c3ca9452d2/eea4a/040_abstraction.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[OOP Concepts: Inheritance]]></title><description><![CDATA[A YouTube video on Inheritance, one of the four concepts of object-orientated programming.]]></description><link>https://aneesh.co.uk/oop-concepts-inheritance</link><guid isPermaLink="false">https://aneesh.co.uk/oop-concepts-inheritance</guid><pubDate>Mon, 08 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Inheritance&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;Inheritance is one of the four concepts of object-orientated programming.
This video is a step-by-step tutorial on how you can develop software using inheritance with Java.
I use a demonstration within IntelliJ by creating a football team where inheritance is used to make it easier to create and update classes with similar properties and behaviours. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p9eKQgs0ox4&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAIBAwQF/8QAFgEBAQEAAAAAAAAAAAAAAAAAAgAB/9oADAMBAAIQAxAAAAFdXHWUlJo//8QAGhABAAIDAQAAAAAAAAAAAAAAAQIDAAQSEP/aAAgBAQABBQLXrgwStLuO4LHGyb5//8QAFxEAAwEAAAAAAAAAAAAAAAAAARARIf/aAAgBAwEBPwGCav/EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EAB0QAAEEAgMAAAAAAAAAAAAAAAABAhESECEjMmH/2gAIAQEABj8Cu7fhFWnHpBYOy4//xAAdEAACAgIDAQAAAAAAAAAAAAABIQARMUEQUZGx/9oACAEBAAE/IUxuawXsLVDmiT8Eu1IYrcKEq6mc4P/aAAwDAQACAAMAAAAQXx//xAAXEQEBAQEAAAAAAAAAAAAAAAABABEh/9oACAEDAQE/EAQ67N//xAAXEQEAAwAAAAAAAAAAAAAAAAABEBEx/9oACAECAQE/EKXI/8QAHRABAAICAgMAAAAAAAAAAAAAAQARITFRsUFx8P/aAAgBAQABPxA2QMFQryrz6gVwVHN/WYcGglgKbS1Y6gqGj3EqpcDXUcquWf/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/41e28a61fcef72d7bd115b372e5a3ab0/f93b5/039_Inheritance.jpg&quot;
        srcset=&quot;/static/41e28a61fcef72d7bd115b372e5a3ab0/8ddbc/039_Inheritance.jpg 75w,
/static/41e28a61fcef72d7bd115b372e5a3ab0/75985/039_Inheritance.jpg 150w,
/static/41e28a61fcef72d7bd115b372e5a3ab0/f93b5/039_Inheritance.jpg 300w,
/static/41e28a61fcef72d7bd115b372e5a3ab0/20e5d/039_Inheritance.jpg 450w,
/static/41e28a61fcef72d7bd115b372e5a3ab0/b4294/039_Inheritance.jpg 600w,
/static/41e28a61fcef72d7bd115b372e5a3ab0/eea4a/039_Inheritance.jpg 1280w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[OOP Concepts: Encapsulation]]></title><description><![CDATA[A YouTube video on Encapsulation, one of the four concepts of object-orientated programming.]]></description><link>https://aneesh.co.uk/oop-concepts-encapsulation</link><guid isPermaLink="false">https://aneesh.co.uk/oop-concepts-encapsulation</guid><pubDate>Mon, 01 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;h4&gt;Encapsulation&lt;/h4&gt;
&lt;p&gt;
In this video, I have taken a step-by-step approach to using encapsulation in software development.
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=9y5N95VqygM&amp;#x26;feature=youtu.be&quot;&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAwb/xAAVAQEBAAAAAAAAAAAAAAAAAAACAP/aAAwDAQACEAMQAAABfXzpS58sCH//xAAaEAACAwEBAAAAAAAAAAAAAAABAgADEiEx/9oACAEBAAEFAiAQVqMY99iVrmxF1//EABcRAAMBAAAAAAAAAAAAAAAAAAABERL/2gAIAQMBAT8BypWZP//EABYRAQEBAAAAAAAAAAAAAAAAAAAREv/aAAgBAgEBPwGtv//EABwQAAEEAwEAAAAAAAAAAAAAAAABEUGRITEy0f/aAAgBAQAGPwLKK1EX4SOaOUP/xAAcEAEAAwACAwAAAAAAAAAAAAABABEhMXFBkcH/2gAIAQEAAT8h2g6fUcMz4LRZmFsLXiJs67gWHpn/2gAMAwEAAgADAAAAEAw//8QAGBEBAQADAAAAAAAAAAAAAAAAAQAhMUH/2gAIAQMBAT8QBPay03//xAAYEQACAwAAAAAAAAAAAAAAAAAAAREhcf/aAAgBAgEBPxCSdGD/xAAeEAACAgEFAQAAAAAAAAAAAAABEQAhkTFBcYGxwf/aAAgBAQABPxDvlAA8krxK1FTOQD7GCqK0hE1liHSBgm+m5eOBZghSz//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;YouTube video link&quot;
        title=&quot;YouTube video link&quot;
        src=&quot;/static/0115bf66dddcc5b4b2c64269f0e18584/f93b5/038_encapsulation.jpg&quot;
        srcset=&quot;/static/0115bf66dddcc5b4b2c64269f0e18584/8ddbc/038_encapsulation.jpg 75w,
/static/0115bf66dddcc5b4b2c64269f0e18584/75985/038_encapsulation.jpg 150w,
/static/0115bf66dddcc5b4b2c64269f0e18584/f93b5/038_encapsulation.jpg 300w,
/static/0115bf66dddcc5b4b2c64269f0e18584/20e5d/038_encapsulation.jpg 450w,
/static/0115bf66dddcc5b4b2c64269f0e18584/b4294/038_encapsulation.jpg 600w,
/static/0115bf66dddcc5b4b2c64269f0e18584/33064/038_encapsulation.jpg 837w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Modern portfolio theory and fund diversification]]></title><description><![CDATA[Understand the use of modern portfolio theory to find an optimal mix of assets and how diversification can be achieved with different types of assets.]]></description><link>https://aneesh.co.uk/modern-portfolio-theory-and-fund-diversification</link><guid isPermaLink="false">https://aneesh.co.uk/modern-portfolio-theory-and-fund-diversification</guid><pubDate>Mon, 15 Feb 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand modern portfolio theory and its application to portfolio construction.&lt;br&gt;
&amp;#8226; Use statistical measures such as variance, covariance and standard deviation to simulate portfolio risk and return profiles.&lt;br&gt;
&amp;#8226; Understand the properties and differences between the asset classes that can be combined to diversify a portfolio.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Modern portfolio theory&lt;/h4&gt;
&lt;p&gt;
Modern portfolio theory (MPT) was first introduced in 1952 by Harry Markowitz to describe how risk-averse investors can achieve optimal returns from their investments at a given level of risk.
Harry later received a Nobel prize for MPT, which is still valued to the present day.
&lt;/p&gt;
&lt;p&gt;
MPT involves a process of identifying how the assets within a portfolio of investments contribute to the funds overall profile for risk and return. Through MPT, we are able to understand how the assets can be combined to create different risk and return values, and where an optimal mix of the assets can achieve the highest level of expected return for the fund at a given level of risk. 
&lt;/p&gt;
&lt;p&gt;
It is often accepted that a larger appetite for risk may yield larger returns for the investor. Despite this, a portfolio of assets may not always yield the same increments of returns for the additional risk taken, thus the relationship between risk and return is not linear. 
&lt;/p&gt;
&lt;p&gt;
In this blog, I will look at the some of the key statistical measures involved with MPT to understand what they tell us about a portfolio of assets, and what their criticisms are. You can find the formulae for all calculations from my Excel sheet available on &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/Modern%20Portfolio%20Theory&quot; target=&quot;_blank&quot;&gt;GitHub&lt;/a&gt;.
I will also review how the optimal mix of assets can be found for a portfolio. 
I will conclude the blog by reviewing the different types of assets that can be included within a portfolio and how MPT has since been adapted and applied within industry.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Asset and portfolio risk&lt;/h4&gt;
&lt;p&gt;
We can divide risk into two types: systematic and unsystematic risk. Systematic risk, also known as undiversifiable risk, is implicit with the market; it includes risk from market observables (such as interest rate or inflation). Unsystematic risk, also known as diversifiable risk, is present with the nature of the asset and &lt;strong&gt;can&lt;/strong&gt; be mitigated. 
One example of an unsystematic risk may be the change in regulations to the airline industry which subsequently caused the likes of Ryanair and EasyJet stock prices to tumble during Covid-19 lockdowns.
&lt;/p&gt;
&lt;p&gt;
MPT calculates the risk of an asset from its &lt;u&gt;variance&lt;/u&gt; which is later transformed into the &lt;u&gt;standard deviation&lt;/u&gt;. The variance of an asset is used to understand how &apos;spread out&apos; the values are within the data set. With MPT, variance is used to understand how the average change in stock price differs from the mean change of all the stock prices.
In this blog I will use the stock prices of 2 different assets (Company A and Company B) to demonstrate MPT. I will construct the values based upon the past 6 days of their stock price, however in reality a much larger time frame will be used for more meaningful statistical measures of the two assets.
&lt;/p&gt;
&lt;p&gt;
The past 6 stock prices for Company A are: 70.22, 88.21, 84.98, 85.43, 78.81, 88.20.&lt;br&gt;
The past 6 stock prices for Company B are: 122.65, 121.11, 132.56, 124.5, 140.7, 130.5.&lt;br&gt;
For the two assets, the mean values derived represent the average percentage change in the stock price from the previous stock price. For Company A, this is 5.41%, and for Company B, this is 1.58%. 
&lt;/p&gt;
&lt;p&gt;
To calculate the variance, I will subtract the percentage change in stock price for each price away from the asset&apos;s mean and square the value.
I will then take the average of the individual variance values to obtain the variance for the asset.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 38.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcSAAAXEgFnn9JSAAACLElEQVQozx1Q205aURQ8f2drYrSx+g2aPrdGTTR9EquBIrQPJL4ZA03TF00FRcullgNFqCJyuOvhug8IyG2DctZ0w04ma8/MyspkpI/714Pdw2R/cz/O35n+8U+HSb57mOAfrGE+1kz2FN86UISe5l9+5Pia7Y6//xrjnx1pbhC64SDJzd9zvY39BC0br48kORxHtd6H1higUu+h3npGKlfGpRxFSesKPhR+D6l8FclcBYl0CWqljVpzgJrw1HILiUyJxl7mvnYquX1R5MsdKrA+3YtZrA3oKpanM89fypbaVNQ4ZdQGub1h+umWyXURottUhQpCV6s9CoQTdHTiHR07/fgdUlySP3CDotal6iMXiXpgjSFiigrfn6hI0pnwcQq/4GeeEM59V1AyZWjN4WQ/ELqF2xPU3b+CkCNJl3TqiSBbbFO+1EG28ISHah/RuArnRRBptYX7Sg9KrgZvIIaT8yCOXZe4UcpQGUeu1IZfFgd9Ed15LsMj37mk22QBj+0XvTsEPfWJGp0RPZSbFE8VxF+nsd7s6qKOJmULdcqqNRLdktYaEmtwUkR/13f5kdhHIltxShubm1heWsL09GssLi5ibXUVa+vrWFlZwcLCAmZmZjA7O4s3c3N4Oz+PV1NTsDscGD8+eMaLDoxIQJ/ALZlMZs2wZWBGo5FZrVZmtzuYzWZjOzs7bM9iZWazWWBvMvcsFra9vc28Xi8T95iu64yIxqgKPAl8+w8KRxLU7MAUBwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Company A Variance values&quot;
        title=&quot;Company A Variance values&quot;
        src=&quot;/static/428d1b23d1a9cba70fc3a78f8abca1a9/5a46d/037_companyAVar.png&quot;
        srcset=&quot;/static/428d1b23d1a9cba70fc3a78f8abca1a9/1db73/037_companyAVar.png 75w,
/static/428d1b23d1a9cba70fc3a78f8abca1a9/8a4e8/037_companyAVar.png 150w,
/static/428d1b23d1a9cba70fc3a78f8abca1a9/5a46d/037_companyAVar.png 300w,
/static/428d1b23d1a9cba70fc3a78f8abca1a9/fc2a6/037_companyAVar.png 450w,
/static/428d1b23d1a9cba70fc3a78f8abca1a9/0a47e/037_companyAVar.png 600w,
/static/428d1b23d1a9cba70fc3a78f8abca1a9/d7542/037_companyAVar.png 810w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 33.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABcSAAAXEgFnn9JSAAABsklEQVQoz1WN4U/aYBDG+7+67yz+BQuJLEtGNj+YjE2cSf1oliGWoc7plm5TVKhQqFBKa4FCKy3IC76UtXd7qXHTS355Lvc8d8etbddn7z5rNL6p0IVu7Or01ZZCl1cL9CVfo5s5g77PtOiHrEE3BIOu8HW6nm3RdFana5+a0d6Lj1d3jJB5CldtdND2Ztgb3EU4Qx+N7hDFQhlVw8Gb0RyNjoeVKxOlqo6KakX5vksZM+zYBCW5heeXKpaq1zpXlK/RuqHQtqcRXdbr3REc/yyB3LCg781BafZA2Bdhd0+Ek2IDHvLWYAaa6UL+8HeYEY7wUJQ0TqqZ2LYnYFhjNPsT1k8jFU8ryA5iz/WxrjvIHuD+UQELJRXZ0yi3UK3t4Y9fUnhwfIbfT+QW17VHyAoCBp3f4wcIHWsAjjeB2+kfcEcU5JoKZ0UZ6k0Txmw2JH7kOS6BstwIL6QqXpRVjUsmkxiLxRjP8TXr37xdxUQigUtLzzCXyy2eYRAE+LgA4BFPLINLr6cnqVRqzPM8EQSBfMnnyc5OhsTjcXLw9RthIRIGAWHL/1jM/hPNbhk+o/IXhBrpSklYXysAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Company B Variance values&quot;
        title=&quot;Company B Variance values&quot;
        src=&quot;/static/501eb1001279784c9865bb0c4db03c98/5a46d/037_companyBVar.png&quot;
        srcset=&quot;/static/501eb1001279784c9865bb0c4db03c98/1db73/037_companyBVar.png 75w,
/static/501eb1001279784c9865bb0c4db03c98/8a4e8/037_companyBVar.png 150w,
/static/501eb1001279784c9865bb0c4db03c98/5a46d/037_companyBVar.png 300w,
/static/501eb1001279784c9865bb0c4db03c98/fc2a6/037_companyBVar.png 450w,
/static/501eb1001279784c9865bb0c4db03c98/0a47e/037_companyBVar.png 600w,
/static/501eb1001279784c9865bb0c4db03c98/fbf08/037_companyBVar.png 962w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
I have used Excel to calculate the variance values, you will also see the field &apos;SD&apos; which stands for standard deviation. 
The variance of Company A and Company B is 0.0149 and 0.0067 respectively; these values, however, can become distorted as the difference between each stock price deviation and the mean is squared. 
&lt;/p&gt;
&lt;p&gt;
The standard deviation provides a &lt;u&gt;normalised&lt;/u&gt; version of the variance to reduce the impact of an outlier on the final value.
The standard deviation is the square root of the variance and provides a contextual value. For Company A and Company B, the standard deviation is 0.122 and 0.082 respectively.
The standard deviation tells us that the Company A stock deviates from the mean percentage movement on average by 0.122%, whereas the Company B stock deviates from the mean percentage movement by 0.082%.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Constructing risk measures in a portfolio of assets&lt;/h4&gt;
&lt;p&gt;
The risk profile of a portfolio is derived from the &lt;u&gt;combined&lt;/u&gt; risk of the assets along with the respective weight they contribute to portfolio. 
Before we begin constructing a portfolio of assets, we must first understand how the risk profile of two assets move with respect to each other. 
&lt;/p&gt;
&lt;p&gt;
&lt;u&gt;Covariance&lt;/u&gt; is a statistical measure used to determine the &lt;u&gt;directional&lt;/u&gt; relationship between two assets. Covariance does not capture the strength of a relationship between the assets, but just the direction of the movement. If two asset prices on average move up together, they will have a positive covariance value. Alternatively, if the assets tend to move in different directions to each other, they will have a negative covariance.
&lt;/p&gt;
&lt;p&gt;
The formula for covariance is below:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 26.666666666666668%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABcSAAAXEgFnn9JSAAAA8klEQVQY022Q2YqFMAyGff/38fLolV6JKHrhysyoiOKK23FrMkmRAwMT+PqnSdqmURBxJYC4AUAw5ItxHEUYhqJpGjHPs4wxfd+LoijENE2CjWPHcVx1XSPpl3Jd175tG57niWx0odSu69CyLPQ8D03TxGEY+AC6rouapqFhGOj7vjyXJAmoqorruv4oQRC8X68XOo4DaZpC27ZAHQB1CLZtAxeXZSljvM/z/ONHUQRVVUGWZYLqcFmWb+7wuO9bvk5Jqf8Zd6LrOvLXaAQYx/GfHz2WKbSMxE5sxJuhIgmNSCrH6OGPz7rvu8w/+/WZf/gLMd16J5Mo4X4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Covariance formula&quot;
        title=&quot;Covariance formula&quot;
        src=&quot;/static/6005709cb3c2361b0907abcc0f8c75ce/5a46d/037_covariance.png&quot;
        srcset=&quot;/static/6005709cb3c2361b0907abcc0f8c75ce/1db73/037_covariance.png 75w,
/static/6005709cb3c2361b0907abcc0f8c75ce/8a4e8/037_covariance.png 150w,
/static/6005709cb3c2361b0907abcc0f8c75ce/5a46d/037_covariance.png 300w,
/static/6005709cb3c2361b0907abcc0f8c75ce/fc2a6/037_covariance.png 450w,
/static/6005709cb3c2361b0907abcc0f8c75ce/0a47e/037_covariance.png 600w,
/static/6005709cb3c2361b0907abcc0f8c75ce/b2982/037_covariance.png 729w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We describe cov(x,y) as the covariance between x and y. This is the sum of each point of x less its mean, multiplied by each data point of y less its mean, all divided by the sample size less 1. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The covariance of Company A and Company B is calculated below:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 32%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAABcSAAAXEgFnn9JSAAABhElEQVQY0y2Oy0/qUBjE+x8bN25dXl0pMfH62BhNTNT4uG4MmhgRa4sFSlsKXmspkSptoYgi/VoeZzxFF5Pf+WbOJCOsHlmfm/8a8fqJTdvnDmUO/tOfnTIt72q0dvxMPKO/pzZtcWYOLVrZN2hxq0SZI4s2zmxa2nukhQ2D5lYKNL9aIMEPaRy8xwh6hA6n1/2CrBhQ1Dq/k5kf9H7y7scIWtXCnVyBz730/083hs79q7wOoeUPE9ePwMlSuUHEpGKNiUpt9n7xvljLG87oBsTUqsNuJZ29+NEsTztppho2u8ybTHjrREm7S2h3I+aFBNcf8AU6RMVE+Dnhq8bo9EecE74mgVysISeW8dqJ0g68MEY4YDDqDrI5vrDa6Cem8wEu9kvIegt3pQZuChauZQv5koPrwhNyio17tQmx7ID3UG8OUH4MkM2bOL24x96ZDCFORsloPAEX+yWmDBhGBFWtoKRqqJp1aJqJYrGCZtNF770PXsJ0CrQ9H6IoQZZkSA8VfAPyMKeOqWi/cgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Covariance calculation&quot;
        title=&quot;Covariance calculation&quot;
        src=&quot;/static/72a96fd48cebdb68256522f1a81fad97/5a46d/037_covarianceCal.png&quot;
        srcset=&quot;/static/72a96fd48cebdb68256522f1a81fad97/1db73/037_covarianceCal.png 75w,
/static/72a96fd48cebdb68256522f1a81fad97/8a4e8/037_covarianceCal.png 150w,
/static/72a96fd48cebdb68256522f1a81fad97/5a46d/037_covarianceCal.png 300w,
/static/72a96fd48cebdb68256522f1a81fad97/fc2a6/037_covarianceCal.png 450w,
/static/72a96fd48cebdb68256522f1a81fad97/0a47e/037_covarianceCal.png 600w,
/static/72a96fd48cebdb68256522f1a81fad97/a4262/037_covarianceCal.png 814w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The covariance value of -0.007509 tells us that there is a slight negative relationship between Company A and Company B. As a result, the average movement of one of the stocks would create a slight opposite movement in the other stock (if one goes up, the other goes down).&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The covariance tells us the portfolio would expect a lower risk profile when the assets are combined than when they are separate. 
A negative covariance would mean the portfolio value does not exclusively fall to the same amount that an individual asset value would fall; the negative covariance in the other asset would offer a protection to some degree against the fall of one asset as it will theoretically rise in value. The relationships are implied from the historical data, however future behaviour between the assets may not always be so true.
&lt;/p&gt;
&lt;p&gt;
The combination of differing assets within the same portfolio creates &lt;u&gt;diversification&lt;/u&gt;. 
The portfolio becomes more diversified as it adds assets which have contradicting risk exposures, thus reducing the combined risk of the portfolio.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using MPT to find the optimal mix of assets&lt;/h4&gt;
&lt;p&gt;
In this section, we will look at how the key statistical measures can be combined to create a portfolio with a single risk and return value. 
The portfolio can be created in many different ways by combining the assets with different weights. For example, creating a portfolio with 10% of Company A stock and 90% Company B stock. 
&lt;/p&gt;
&lt;p&gt;
We can simulate many different portfolio combinations in Excel by changing the weights of the assets in the portfolio. 
The portfolio expected return percentage will be a sum of each asset mean percentage multiplied by the asset weight in the portfolio.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 33.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABcSAAAXEgFnn9JSAAABu0lEQVQozx1Ry2rbUBT0zxZKoBBKV6XtsptuAw0hhUApBdNVS1+25civxq4Tv1TL8UOxbMkP2ZYtv5TFsc/06C4G5s6dO8w5NxYm39P2y2vyPpzQ9ts7ClPntI2/pM2n57T9/IKCj6fkXZ3S/utb8q+e0fjyhNzzJ7SMv6EwcUb732cUxF/R+OKpuosdVxMcVmOY5RxCbwAOpnKeKGAzw9rtwShmFB+366jm0yjpSXiWCaw95Q/EU8lpKGeSiB0XQ6aZzeaNxuHYYvYdPswHChHfOW1ul3TmpcuT1i3fpb9zVf/F826DIVr0PrBNNvJJrug/OXZcujgsHO7c5vE47YOXIxx80XwHLM13ox66dwXFvU4NRkGD8ecai4cmINpR/Bung2ZRh3FzjSiQSQLNos7htC9NRiyBAoclhHdul+//ZhSf9xpczSa4pP1gr2uwBCr/ym5xPZdiCZSG0oTmA9yXdISTB8iY0nioIGNi53bQLmdVc98yUMsmUMslMe8ZkJERvV/bLWmeUnq0Q6JZnySQHicWRWfZnwL7Q9qPOmRV8sIdCvpNMotpahQ08q1/Sov8a9sk+QOqi/4fTU7voweOwisAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Portfolio return table&quot;
        title=&quot;Portfolio return table&quot;
        src=&quot;/static/b5feb047863cf4d92bd3ef7109dd5aa7/5a46d/037_portfolioReturn.png&quot;
        srcset=&quot;/static/b5feb047863cf4d92bd3ef7109dd5aa7/1db73/037_portfolioReturn.png 75w,
/static/b5feb047863cf4d92bd3ef7109dd5aa7/8a4e8/037_portfolioReturn.png 150w,
/static/b5feb047863cf4d92bd3ef7109dd5aa7/5a46d/037_portfolioReturn.png 300w,
/static/b5feb047863cf4d92bd3ef7109dd5aa7/fc2a6/037_portfolioReturn.png 450w,
/static/b5feb047863cf4d92bd3ef7109dd5aa7/0a47e/037_portfolioReturn.png 600w,
/static/b5feb047863cf4d92bd3ef7109dd5aa7/eb2af/037_portfolioReturn.png 954w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The formula for finding the portfolio variance is slightly more complex: 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 8%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAABcSAAAXEgFnn9JSAAAAaUlEQVQI1z2OOwoAIRBD9/7nE0X8/1AbtRAssziwW4QJL2TI45wDYwxaawghEELAGAO1VvJKKeSc0XtHKQUxRkgpf2aMIea9p/u01qh0A845UkpYa1HhezDnxDmH2JW1ltjem/JvzB33AgufkhepHTXYAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Portfolio variance formula&quot;
        title=&quot;Portfolio variance formula&quot;
        src=&quot;/static/6a7de31a88ad7630c78526e72c90856d/5a46d/037_portfolioVarianceFormula.png&quot;
        srcset=&quot;/static/6a7de31a88ad7630c78526e72c90856d/1db73/037_portfolioVarianceFormula.png 75w,
/static/6a7de31a88ad7630c78526e72c90856d/8a4e8/037_portfolioVarianceFormula.png 150w,
/static/6a7de31a88ad7630c78526e72c90856d/5a46d/037_portfolioVarianceFormula.png 300w,
/static/6a7de31a88ad7630c78526e72c90856d/fc2a6/037_portfolioVarianceFormula.png 450w,
/static/6a7de31a88ad7630c78526e72c90856d/0a47e/037_portfolioVarianceFormula.png 600w,
/static/6a7de31a88ad7630c78526e72c90856d/460fa/037_portfolioVarianceFormula.png 1910w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the formula, &lt;i&gt;w&lt;/i&gt; represents the weight of the asset in the portfolio, and the &lt;i&gt;σ&lt;/i&gt; represents the standard deviation of the asset.&lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Constructing and using the efficient frontier&lt;/h4&gt;
&lt;p&gt;
The &apos;efficient frontier&apos; is a curve that is drawn upon a graph of the different risk and return portfolios that can be achieved with two assets. In the real world, this may be used with two portfolios rather than assets to capture more complex relationships. 
From the table which we have created of the weights, expected return and expected standard deviation of the portfolio, we can create a scatter graph to visualise the information:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcSAAAXEgFnn9JSAAABEklEQVQoz11R0baDIAzj/39T9EE3KlcERYWbdGPHjXMqgTZNKqaUkqZxPGOMx3WdB1dGnOcLHzfM/bou3Rv+rTH8TONUt22rbUHkEyDpHQhak3OuKW113/c3TnVdYw1h1TsjTrLtLA65gFRQUFBQ4PiN1xIQaKYBkubuZ4hqkG+cc7m3PdV4UZ0TDSrTEciqTIxh1C1d033DLcg33vs8DAMJpY3bkmx6bwi3Oi5z9yZfDdFgfz6eap39aL0tnl8a5bPf8w23kVlj6FqcqzLPqoA3rsuyqCJdhRDUdYQ7/nhiOt3gmjWs5eOQ++e9OpzQVDCaAAuKJKUovMM/A07Qc2JtL13XicgsENJc453gEENc/gHfmHCgxPYqLwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Efficient frontier graph&quot;
        title=&quot;Efficient frontier graph&quot;
        src=&quot;/static/6a8c012ef7fbada7ab835b5f6c5e950e/5a46d/037_efficientFrontier.png&quot;
        srcset=&quot;/static/6a8c012ef7fbada7ab835b5f6c5e950e/1db73/037_efficientFrontier.png 75w,
/static/6a8c012ef7fbada7ab835b5f6c5e950e/8a4e8/037_efficientFrontier.png 150w,
/static/6a8c012ef7fbada7ab835b5f6c5e950e/5a46d/037_efficientFrontier.png 300w,
/static/6a8c012ef7fbada7ab835b5f6c5e950e/fc2a6/037_efficientFrontier.png 450w,
/static/6a8c012ef7fbada7ab835b5f6c5e950e/0a47e/037_efficientFrontier.png 600w,
/static/6a8c012ef7fbada7ab835b5f6c5e950e/d4e7f/037_efficientFrontier.png 1416w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
We can break the graph into different sections to illustrate how a portfolio can be constructed:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcSAAAXEgFnn9JSAAABW0lEQVQoz01SXW/CMAzs//9dewVNICYheIABHZQyQd2PNIntnU1Bi3S6a2Nfzm2KlFKoqkpjjJxzlhSj4J24Br+1iCRVycCL31pQwqyomxV934/X61WtUbF4AvqVUWQQ4/tduSxVTifNh4MzH4/Km43ycsm8WCg/HvPifD6PgOIE4baVvN8Lr9cii4XIcumsq5Xoditie9/fknc7Zz4cJN9uwjFmPzxnGP6cx+pyUcSWTKTN6ktbHBCGQYfe0Os4jhpi1GTJgTwxvzRa+TnRvMC4MKx85P9F+B46mCnghuC+6zSEoDklS+NwzewJsyWE0VjXNR5who2NH2Bsawry1rZ42vPaSZvhs0RnBQzSrb5p0zT+IyxB27auu671hKapIdeWhPBpEMRhffbOpiBqPgu4/qKhHUIgaEIRhfGpcZUIJnStazoeT1SWJdlCMyEcIZrvo/aBcTNuzMcfe15rsw8B7BEAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Efficient frontier line&quot;
        title=&quot;Efficient frontier line&quot;
        src=&quot;/static/0eafdeeb35b257666c22681641af1318/5a46d/037_efficientFrontier2.png&quot;
        srcset=&quot;/static/0eafdeeb35b257666c22681641af1318/1db73/037_efficientFrontier2.png 75w,
/static/0eafdeeb35b257666c22681641af1318/8a4e8/037_efficientFrontier2.png 150w,
/static/0eafdeeb35b257666c22681641af1318/5a46d/037_efficientFrontier2.png 300w,
/static/0eafdeeb35b257666c22681641af1318/fc2a6/037_efficientFrontier2.png 450w,
/static/0eafdeeb35b257666c22681641af1318/0a47e/037_efficientFrontier2.png 600w,
/static/0eafdeeb35b257666c22681641af1318/bb543/037_efficientFrontier2.png 1418w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The red line represents the efficient frontier, where the optimal mix of assets can be achieved for a given level of risk.
It is not possible to construct a portfolio with the two assets to achieve a risk and return profile above the red line, however it is possible to construct one that falls below.
If the portfolio risk and return profile is below the line, it is not regarded as optimal with respect to the risk that is taken.
The image below illustrates the construction of the portfolio for 100 different weight combinations, rather than the 9 shown before:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcSAAAXEgFnn9JSAAABOElEQVQoz11S2XLDMAjM/39lH5rGsR2PLx0gCbogt5OWmR1WiOWwfCuwaZyEiFqtteHo+M9r7b61zs23K6fHm6aU1luMsc3zrMxFu4kKLkVELckBzlyVAC4Nvly+asysiapkqhpi2m7DMLRheCo6WVCOwHJGlpAKwJJykS4oggKOd15KRX+0RNOcMwo+hvZ8jpikynqyfjxOXfasKVeF0OGTAdjsd3LznVf9U3CapjaOIwQsti729SRbKb0VJHg7x1z0jOTeYPfSrRfEqvU1v3xl63R1gzWr3jMNF/f7Cz+55u3rE/N2wwvJsiy67bvriLKe5+nPE0LQmJLHj+OwV/R1jWMOG0J36CzGTLqu624FEwKE1yYI0YQJQudmIUQax4k+P+90v3/Rtu2E1Qg6h+mgz/b7ofj8DV9RcR7/VB5UAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Efficient frontier for 100 data points&quot;
        title=&quot;Efficient frontier for 100 data points&quot;
        src=&quot;/static/63ca93c7139caa60d5e82b24ac5c4991/5a46d/037_efficientFrontier3.png&quot;
        srcset=&quot;/static/63ca93c7139caa60d5e82b24ac5c4991/1db73/037_efficientFrontier3.png 75w,
/static/63ca93c7139caa60d5e82b24ac5c4991/8a4e8/037_efficientFrontier3.png 150w,
/static/63ca93c7139caa60d5e82b24ac5c4991/5a46d/037_efficientFrontier3.png 300w,
/static/63ca93c7139caa60d5e82b24ac5c4991/fc2a6/037_efficientFrontier3.png 450w,
/static/63ca93c7139caa60d5e82b24ac5c4991/0a47e/037_efficientFrontier3.png 600w,
/static/63ca93c7139caa60d5e82b24ac5c4991/2a08f/037_efficientFrontier3.png 1422w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We can now see how many more portfolios can be built, and how many more sub-optimal portfolios that are possible below the efficient frontier.
By understanding how the two assets move on average against (or with) each other, we can begin to understand how a portfolio of the assets can be created to maximise returns for a certain risk level.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
A typical portfolio of assets may not just consist of two assets. Once a portfolio has been constructed, it will have its own risk and return profile that is living with the assets it contains.
Further assets may be evaluated before being added to the portfolio by using an understanding of how the risk and return of the asset complements or hinders the expected performance.
&lt;/p&gt;
&lt;p&gt;
When reducing the unsystematic risk from a portfolio, a manager may take an approach of looking at different types of securities, or to look at various asset classes to seek portfolio diversification.
An asset allocation fund is a type of fund that creates diversification by balancing a mix of assets. 
Each asset classes will provide the fund with unique properties from its exposure and return that can be combined to create a well diversified portfolio.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Diversification with asset classes&lt;/h4&gt;
&lt;p&gt;
Depending on where you look, you will find that assets are divided into a varied number classes, typically from 3 to 8.
The advent of new derivatives and exotic investment products have introduced new types of asset classes including commodities, real estate, financial derivatives and most recently, cryptocurrencies. The three largest asset classes are equities, fixed income and cash/cash equivalents. In this section, I will reflect upon 5 asset classes to understand their unique properties:&lt;br&gt;
&amp;#8226; Equities&lt;br&gt;
&amp;#8226; Bonds/fixed income&lt;br&gt;
&amp;#8226; Cash and cash equivalents&lt;br&gt;
&amp;#8226; Real estate and tangible assets&lt;br&gt;
&amp;#8226; Financial Derivatives
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Equity&lt;/strong&gt;&lt;br&gt;
Equity, also known as stocks or shares represent a percentage ownership in a publicly listed company. 
Equities are often classed by their market capitalisation into small-cap, mid-cap and large-cap. 
Investors of equities benefit from a potential dividend and the value of the resale of the equity in the market.
While equity provides benefits such as company ownership and a right to vote on certain decisions, they are viewed as risky investments due to their large dependence upon company performance.
&lt;/p&gt;
&lt;p&gt;
The payments to an equity holder are less certain than other securities such as those of bonds. Common-stock holders receive payment once all other cash flows to the company have been paid, such as workers, taxes and bondholders. The dividend payments are also not a guaranteed payment as they are reflective of company performance. 
The value of the equity can also deteriorate under the company performance as the supply and demand of the stock changes within the market. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Bonds and Fixed Income&lt;/strong&gt;&lt;br&gt;
Bonds represent an issued loan from a company that is repaid over the life of the bond. 
The bond will include a maturity date - when the principal of the bond is repaid to the investor and a coupon rate - the interest repayment to the investor at set intervals until maturity.
Bonds are not always sold at the value of the principal. Bonds can come in many different forms such as zero-coupon bonds, corporate bonds and municipal bonds - all of which offer different levels of return and coupons.
&lt;/p&gt;
&lt;p&gt;
Bonds are often considered less risky than equity for several reasons:&lt;br&gt;
&amp;#8226; Bond investments come with a promise to be repaid at maturity.&lt;br&gt;
&amp;#8226; The fixed interest repayment on the bond is guaranteed (unlike a dividend payment on equity).&lt;br&gt;
&amp;#8226; Bond owners have higher priority during liquidation than equity holders.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Cash and cash equivalents&lt;/strong&gt;&lt;br&gt;
Cash and cash equivalents are financial securities that have two key properties: high credit quality and high liquidity.
Cash equivalents are also known as money market instruments that consist of low risk and short-term securities such as T-bills, certificates of deposit and commercial paper. 
&lt;/p&gt;
&lt;p&gt;
The cash and cash equivalents are often shown as a company health indicator as per the balance sheet as it reflects the ability of the company to pay their short-term debts.
Cash and cash equivalents may also be used to finance an acquisition, however this is not of relevance for a portfolio of assets.
&lt;/p&gt;
&lt;p&gt;
With respect to a portfolio of assets, cash and cash equivalents will have a low risk and return profile with high liquidity. The T Bills and other securities will carry a low duration 
due to their short maturity dates thus providing an opportunity to mitigate portfolio exposure to interest rate risk.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Real estate and tangible assets&lt;/strong&gt;&lt;br&gt;
Real estate and tangible assets reflect securities that derive value from real-life assets that contain an intrinsic value. 
The link between the security value and the asset means real estate and tangible assets offer a hedge against inflation in the currency for a portfolio.
Tangible assets will also typically have very little in common with financial securities to offer a new element of diversification to the portfolio. 
Real estate and tangible assets offer a stability of payments to the portfolio, however a reduced liquidity due to the time it can take to sell the assets.  
&lt;/p&gt;
&lt;p&gt;
A commodity is also considered a tangible asset: an economic good that has a fungibility. 
Commodities are divided into two types: hard commodities that are mined such as oil and gold, and soft commodities such as harvest (rice, wheat) or livestock (meat, cattle).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;FX and other derivatives&lt;/strong&gt;&lt;br&gt;
The foreign exchange (FX) market is a relatively new asset class since a free-floating exchange had been enabled following the collapse of the Bretton Woods system in the 1970s.
FX rates offer a portfolio a hedge against currency risk as the value of other securities can vary with the FX market. 
FX also offers portfolio growth as speculation in a currency against other currencies can create greater values in securities over others.
The FX market is often captured through the derivatives market where they are swapped or secured through contracts.
&lt;/p&gt;
&lt;p&gt;
Derivatives represent a contractual agreement between parties where the value is &apos;derived&apos; from an underlying security or asset. 
Common derivatives include a future or forward contract, which is an agreement to pay a certain price for a certain quantity of an asset at a date in the future.
Other derivatives include options, which provide a right to buy or sell a security at a point in the future, and swaps, which are exchanges of assets over a period of time.
&lt;/p&gt;
&lt;p&gt;
Derivatives are traded both on an exchange and over-the-counter (OTC) which provide various ways for them to be customised to suit the preference of a portfolio.
Derivatives can be used to both hedge a risk (such as with a swap) or to speculate upon a price (such as a call option).
Portfolios can therefore benefit from derivatives to achieve bespoke risk and return benefits. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Modern Portfolio Theory today&lt;/h4&gt;
&lt;p&gt;
An understanding of the different asset classes can provide a small amount of colour to the vastly complex world of portfolio construction and assets. 
MPT has provided a well-respected, yet simplified model at understanding how assets can be combined to create a variety of expected returns at a level of risk. 
Since its inception, limitations have been identified with MPT in-line with other theoretical models of finance: investors are not always rational, buy and hold strategies are not always optimal, past performance is not indicative of future performance, securities are not always available within the market. Amongst the limitations, there are four common criticisms of MPT that may influence the use and perception of the model.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Assumption: Investments upon the efficient frontier are optimal.&lt;/strong&gt; The curve created by the efficient frontier does not leave the manager with a complete array of portfolio combinations that are optimal with regards to the level of risk taken for the return achieved. The efficient frontier is to be better utilised by drawing a capital market line that accounts for the risk free rate of return for an investment (typically the return of a government bond) and finding a tangent to the point on the frontier. I will review the capital market line in a separate blog. The key point to note is that the efficient frontier isn&apos;t equally efficient in risk/return tradeoff at each point along the curve. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Assumption: Returns on assets are normally distributed.&lt;/strong&gt; 
The assumption of a normal distribution of returns is reflected in our expectation that the price of an asset may rise and fall with equal probability. The reality of 
price movement however is often skewed to reflect the general movement of the asset price, be it upwards or downwards as per the asset classes market expectation
at the point in time. You may consider technology-based companies over the past 10 years; we wouldn&apos;t expect the prices of their shares to equally move up and down through the years, let alone when considering inflation.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Limitation: Risk is defined as both the upside and downside of price movement.&lt;/strong&gt; MPT obtains the standard deviation of an asset from both its movement up as well as its movement down.
The standard deviation through this calculation has been criticised as it is used for understanding the risk of the asset, which would otherwise be reflected in just the 
downwards movement of the price, and not the upward movement. &lt;a href=&quot;https://www.investopedia.com/terms/p/pmpt.asp&quot; target=&quot;_blank&quot;&gt;Post-modern portfolio theory&lt;/a&gt; is a 
methodology that was introduced in 1991 where the standard deviation of the risk of an asset is defined from its downward movement, rather than all of its movement.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Limitation: Covariance to establish relationships between asset movements.&lt;/strong&gt;. A limitation of covariance is that is can only be used to measure the 
directional relationship of two assets. The strength of the relationship is not shown, therefore an understanding to the degree at which two assets move with or against 
each other cannot be gathered or applied to the efficient frontier. A greater measure of the relationship can be obtained using a correlation coefficient. The 
correlation will normalise the covariance to provide a signal on the strength of the relationship of a scale of -1 to 1. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
In this blog, we have looked at modern portfolio theory to understand how the risk and return of a portfolio of assets might not always be optimised when grouped together. 
Amongst MPT, we have also looked at how assets will have their own variance and covariance with other securities. The realisation of a negative covariance can enable us to reduce the variance of returns when combining assets. 
&lt;/p&gt;
&lt;p&gt;
The understanding of an asset or portfolio standard deviation will provide an investor an insight into the volatility of the assets together. 
Diversification can come from many different sources including securities with a variety of companies, sectors and geographical regions, 
however an extended range of diversification opportunity can be realised across
different asset classes, all of which have their own unique properties and underlying characteristics within the market. 
&lt;/p&gt;
&lt;p&gt;
MPT has remained resilient with time as a tool to understanding portfolio construction and expectations.
The criticisms of MPT have lead to further developments and extensions of the theory. In a future blog, 
I will write about the capital market line that builds on-top of MPT to develop an understanding of the relationship a portfolio may have with the risk-free
rate of return that could be otherwise be obtained with other securities in the market. To reiterate, you can find the excel sheet from this blog on  &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/Modern%20Portfolio%20Theory&quot; target=&quot;_blank&quot;&gt;GitHub&lt;/a&gt;.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a RESTful web service in Go]]></title><description><![CDATA[Visit the design patterns for creating a RESTful web service in Go.]]></description><link>https://aneesh.co.uk/creating-a-restful-web-service-in-go</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-restful-web-service-in-go</guid><pubDate>Mon, 18 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Use Go to create a RESTful web service step-by-step.&lt;br&gt;
&amp;#8226; Explore some of the key classes and methods from the Go standard library for web services.&lt;br&gt;
&amp;#8226; Handle client HTTP requests to perform CRUD operations.&lt;br&gt;
&amp;#8226; Persist data with a MySQL database.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to Go web service&lt;/h4&gt;
&lt;p&gt;
In my previous &lt;a href=&quot;https://aneesh.co.uk/an-introduction-to-go&quot; target=&quot;_blank&quot;&gt;blog&lt;/a&gt;, I reviewed the drivers and key properties of the programming language &apos;Go&apos;. Go&apos;s most defining properties made it a lightweight programming language that is specifically well supported to creating applications with efficient compile time, efficient runtime and with ease of programming.
&lt;/p&gt;
&lt;p&gt;
One of the most valuable use-cases for Go extend to its built-in capacity to create powerful and efficient web services with its &apos;http&apos; standalone package.
This blog will review the process for creating a web service with Go and persisting the data in a MySQL database. I will also pay close attention to the packages, keywords and design patterns used in the source code.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The HTTP communication protocol&lt;/h4&gt;
&lt;p&gt;
Hypertext Transfer Protocol (HTTP) is a client-server protocol during data transfer across the web. When a client requests information from a server, a HTTP Request is made by the client and in return the server will issue a HTTP Response. The Request and Response consist of structured data to inform the server of the request details and the client of the processed outcome. 
When we design and build Go web services, we are required to specify the request and response details that are encapsulated by HTTP within the source code. Before we create the web services, I will briefly run over the HTTP protocol in a little more detail.
&lt;/p&gt;
&lt;p&gt;
The Go web services use representational state transfer (REST) as the protocol to exchange messages. HTTP in this context is the protocol for transporting the messages.
REST is used to respond to incoming HTTP requests where the sender of the request (client) uses an API uniform resource identifier (URI) to obtain and modify the state of the resource. In our case, the resource will be data that would be typically persisted by the application datastore.
&lt;/p&gt;
&lt;p&gt;
HTTP requests are made in a stateless manner within the system meaning they are executed independently from all other requests. 
This means HTTP transactions are sent, completed, and closed in isolation to support huge scaling as sessions are unrelated to each other.
The HTTP message will include a packet of information between the client and server that includes a start-line, header and body. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 61.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAABcSAAAXEgFnn9JSAAACCklEQVQoz31Ty4oiQRD0jz2JF39AEPQgHhU9eRVRD+pBZAcUFddZdVFB8bF2t/b4bvuZsVnltDvDDhOQFNWVERWZWR24Xq+GbdtUq9W8arXq1et1r1KpeLque0TkAfgvBMQ6GAy8cDjsZbNZ73a7ye8B13XN4/GIfD6PRCKBYDAIFsTrr1cIaJr2jN1uh+12C9M05T4WiyGZTCIej6NQKMj8ALszz+czisUipdNpikQi1Gw2aTweE5/Tcrl8xmq1ptlsRqfTiebzOUWjUSqXy5TL5WSIfCnY6/XETZTJZCAilUqhVCrhO3BlYDGEQiHpcLFYPByyffN+v4tyhAtsNhtZzvV2wdtBR7fXwWg8RP9nH5PpBOPfI5wvJxC3U1TW6XQgeALccwQMwzDFhg+p1Wqh0WhIUQFVUzEcjrBarZjYleRut4vD4Q22Y+HlxwufD9Fut7Hf7x8O2Z35XgV9LMligsDlcsF0OpWE9XotSxXDd1z76cqHdOgL0gPPBEGwHVsKismKl6AoCm7XGxzHgePZ0oLP8XnfClqWBR4aJpMJ+v2+LF04tGyLBZ2nq0+Cfg/5Tb5rPlbuERl3g3hmJAambBVil8T30928E1/4Kd9HgO2bXz+Mf71RVRX8V3D55y/PPyLAEzXEb8ZNd/3Q97qr7VRX0bauqily/aNsZPh7badxru5+5In4C9oAekwJl0h7AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;HTTP Request image&quot;
        title=&quot;HTTP Request image&quot;
        src=&quot;/static/1cd80067978028c397c24d7abf60adb9/5a46d/036_request.png&quot;
        srcset=&quot;/static/1cd80067978028c397c24d7abf60adb9/1db73/036_request.png 75w,
/static/1cd80067978028c397c24d7abf60adb9/8a4e8/036_request.png 150w,
/static/1cd80067978028c397c24d7abf60adb9/5a46d/036_request.png 300w,
/static/1cd80067978028c397c24d7abf60adb9/fc2a6/036_request.png 450w,
/static/1cd80067978028c397c24d7abf60adb9/0a47e/036_request.png 600w,
/static/1cd80067978028c397c24d7abf60adb9/541fe/036_request.png 1654w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
During a HTTP request, the start line will indicate the HTTP method performed (such as GET, POST, PUT, DELETE) along with information about the target server. 
The header in the request may include supporting information for the server to process the request such as authentication tokens. 
The body of a request may include JSON data that is required for processing. The body is used during PUT and POST requests where data is updated or added to the datastore.
&lt;/p&gt; 
&lt;p&gt;
A HTTP response will use the start line to define a status code explain the result. Status codes exist from 100 to 599. Codes in the 200s represent successful responses, codes in 300s represent redirected requests, codes in the 400s represent client-side error (such as 404), and codes in the 500s represent server-side errors.&lt;br&gt;
The response header may provide information to support client-side processing such as a cache or cookies. 
The response body would consist of the requested data (such as for a GET request). 
&lt;/p&gt;
&lt;p&gt;
In the following section, we will begin to build a Go web service using HTTP to customise messages from the server.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating a Go web service&lt;/h4&gt;
&lt;p&gt;
From a high-level view, creating a web service will include the following steps:&lt;br&gt;
1. Create an entity class for storing the instances of the records. &lt;br&gt;
2. Initialise the data store.&lt;br&gt;
3. Define the web service endpoints.&lt;br&gt;
4. Handle GET requests to the server.&lt;br&gt;
5. Handle POST requests to the server.&lt;br&gt;
6. Handle PUT requests to the server.&lt;br&gt;
7. Run the application.&lt;br&gt;
&lt;p&gt;Before we begin writing the bulk of the source code, we must first create the &apos;main&apos; file to launch the web service.
The file will be named &apos;main.go&apos;, it will define the package as &apos;main&apos; and will include a method named as &apos;main&apos;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;package&lt;/span&gt; main

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;1. Creating the entity class&lt;/h4&gt;
&lt;p&gt;
Go defines a &apos;struct&apos; (short for structure) as a collection of fields for custom user-defined types to be used in the application. The struct is used to hold records that are to be included in the application with certain properties.
Similar to a Spring web service by marking a class with @Entity and defining its fields, a struct can be defined with the same purpose as below:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; person &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Id &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    Name &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    Age &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
A struct can be considered as a template that is used for creating data records. 
The use of a struct is made by using the keyword &apos;type&apos; followed by the struct name and lastly by &apos;struct&apos;.
In this struct, the fields are accessible outside of their package due to the capitalisation of their first letter (Id, Name, Age). 
&lt;/p&gt;
&lt;p&gt;
A struct instance can be created in many ways:
&lt;p&gt;Using direct assignments:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; p Person
p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;
p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Age &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using a code block:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;p&lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; Person &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Id &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    Name &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    Age &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or in a single line:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;p&lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; Person &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;2. Initialise a data store&lt;/h4&gt;
&lt;p&gt;
To initialise a storage of multiple Person instances, we will define a variable as an array of {Person}:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; personList &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;Person&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The in-code data storage will be populated by using a special function within Go called &apos;&lt;a href=&quot;https://golang.org/doc/effective_go.html#init&quot;&gt;init&lt;/a&gt;&apos;.
Init functions are used for initialising variables, checking states and running other one-time computations. Despite how many times the package is referenced,
the &apos;init&apos; function will only ever be called once:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    personJson &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;`[
        {
            &quot;Id&quot; : 1,
            &quot;Name&quot;:&quot;Alice&quot;,
            &quot;Age&quot;: 25,
        },
        {
            &quot;Id&quot; : 2,
            &quot;Name&quot;:&quot;Bob&quot;,
            &quot;Age&quot;: 22,
        },
        {
            &quot;Id&quot; : 3,
            &quot;Name&quot;:&quot;Charlie&quot;,
            &quot;Age&quot;: 23,
        }
    ]`&lt;/span&gt;
    err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;personList&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
So what have we achieved?&lt;br&gt;
Firstly, the personJson declaration defines a JSON within back ticks. 
&lt;/p&gt;
&lt;p&gt;
From lines 3 to 19, the JSON data is used to initialise 3 people into the list with their Id, Name, and Age.
&lt;/p&gt;
&lt;p&gt;
From line 20, a second method call is made that returns an error code &apos;err&apos;. The method call to &lt;code&gt;json.Unmarshal&lt;/code&gt; will transform the JSON into the array of Person instances: personList.
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;Unmarshal&lt;/code&gt; method parses JSON-encoded data and stores the result within the value pointed to by the second argument. The &lt;a href=&quot;https://golang.org/pkg/encoding/json/#Unmarshal&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt; defines the use of two parameters and to return an error code:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Unmarshal method performs the reverse of the Marshal method (later visited) by transforming the byte array &apos;data&apos; into the interface &apos;v&apos;.
From line 20 in the above code snippet, we perform a check upon the error response of the Unmarshal method to verify that it is not equal to Nil. The following code block is frequently used with Go for checking errors:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;3. Handle requests&lt;/h4&gt;
&lt;p&gt;
So far, we have created a data record type (struct) and populated an array of records using the &lt;code&gt;Unmarshal&lt;/code&gt; method for parsing JSON. 
Before we access the data, we must define the endpoints of our web services which are later accessed by the client.
The Go web service endpoints are defined in one of two ways:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Handle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/home&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Steve&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;HandleFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/about&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; aboutHandler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;Handle&lt;/code&gt; function allows us to register a handler that will service the request for the pattern that matches &quot;/home&quot;.&lt;br&gt;
The &lt;code&gt;HandleFunc&lt;/code&gt; allows us to assign a function to directly handle in the incoming request when the pattern matches &quot;/about&quot;.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;The Handle interface from the documentation appears as following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Handle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pattern &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; handler Handler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Handler interface contains a single method: ServeHTTP(ResponseWriter, *Request) which uses a ResponseWriter and a pointer to a Request object to return a response.
To define our own implementation of Handler, we will hang the ServeHTTP method on the Person struct by providing an implementation of the method.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ServeHTTP&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Request&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;On line 1, we first specify a new function with &apos;func&apos;, second, the &apos;(p *Person)&apos; section will specify the Person struct has the method hung from it.
By convention, w is used to denote the http.ResponseWriter. The ResponseWriter is used to construct the HTTP response from the server to the client. The HTTP start line, headers, and body can all be defined here. The http.Request instance is conventionally denoted with &apos;r&apos; and is used to assess the incoming request properties.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The HandleFunc method allows us to handle HTTP methods, however instead of taking a handler as a second argument, it takes a function that has the same signature as &lt;code&gt;ServeHTTP&lt;/code&gt;.
The ServeHTTP function uses parameters of a ResponseWriter and a pointer to a Request object, therefore our function &apos;aboutHandler&apos; must do the same:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;HandleFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/about&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; aboutHandler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;aboutHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Request&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;About page&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;4. Handle a GET request to the server&lt;/h4&gt;
&lt;p&gt;
The &lt;code&gt;personList&lt;/code&gt; can be obtained through the web service by defining a handler to a GET request made on a URL pattern &apos;/people&apos; and returning the personJson array variable to the HTTP body.&lt;br&gt;
First, we will register the request to be handled with HandleFunc:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;HandleFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/people&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; peopleHandler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Second, the peopleHandler method is designed to return the peopleJson in the body. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;peopleHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Request&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    personJson&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Header&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Content-Type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;On line 1, the peopleHandler function is defined with the same signature as the &lt;code&gt;ServeHTTP&lt;/code&gt; implementation from the Handler interface.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
On line 3, the personJson response is defined by calling the json.Marshal method on the initialised personJson array.
The json.Marshal interface is defined in its documentations with the below signature:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Marshal method will do the opposite of the Unmarshal method. The response from Marshal is the JSON data in a byte array and an error code.
On lines 4 to 7, the error is handled with a HTTP start line of StatusInternalServerError.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
On line 8, a good response is deemed to have been received so we set the header to return content type of JSON and on line 9 we write the HTTP response body to be the JSON returned from the Marshal method.
&lt;/p&gt;
&lt;p&gt;
The GET method will now return the JSON data or an empty body and an internal server error (HTTP code 500).
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 65.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcSAAAXEgFnn9JSAAAB9ElEQVQ4y51TzXLaMBD2Q/UN+kZ9jt76DjlySXLKqZCZNIdSMAyhQCBkSmYA28K2ZFvSWl9XNu0UpjNNujM7u1pJn/bnUxBfXBTi5qZOnp9tdHVFL5eX9HJ9TbtOhw6jEcVZRkkckxCCkiShmH1vG9/HDoI+dHJ691HQ+0+CgjwcmXK5RH444DCZoJhOIcIQ6WQMud8jVwp5noMBkKYpFK+llMiyDDKOQbyebmvcrSzu14QgU1KXdY3SGJdWlasAp4hcbo2r2YdrxVhy1lpX856Pm/ncZd3PTt7funo95xg52NwF+/lUR6sVxGbj4vUau6cncPnYsyrORHDmhcxhVYZGnGttFIEWc5jFDIYr0RwiYxGouy9afOtDDYeu4pLL0QjeFt0uSiGgyhJVWcCytniuVZzJMRDQeKw9elOGMSiJYP3OcgVu1ukd5058x61q9PiAt4EZDltA30Muc7fdYisr6MmUAfPfB3+B/en/TQIThg2gLgrfe9Q1tTuPj3A83ZO+vUICPRjqyt/h6aU/Noj2EZKKQLNZU7I7K/WfgPJrX5fsGKaMZm4pJZuJYbEAE7Dt9VsAi35fV3yeGLBk8uZphspP6CzD12ogBwNdHKcsEwHhf4YHYW7+V4a6d6uZg6BR6PDwADAHMfsO9Hr8ZdSbM/wJF3Dnw1uc2WYAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Get request image in Postman&quot;
        title=&quot;Get request image in Postman&quot;
        src=&quot;/static/c66be493bc7ed62ee23b7eb4b7c02f29/5a46d/036_getRequest.png&quot;
        srcset=&quot;/static/c66be493bc7ed62ee23b7eb4b7c02f29/1db73/036_getRequest.png 75w,
/static/c66be493bc7ed62ee23b7eb4b7c02f29/8a4e8/036_getRequest.png 150w,
/static/c66be493bc7ed62ee23b7eb4b7c02f29/5a46d/036_getRequest.png 300w,
/static/c66be493bc7ed62ee23b7eb4b7c02f29/fc2a6/036_getRequest.png 450w,
/static/c66be493bc7ed62ee23b7eb4b7c02f29/0a47e/036_getRequest.png 600w,
/static/c66be493bc7ed62ee23b7eb4b7c02f29/c8ad9/036_getRequest.png 1564w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;5. Handle POST requests to the server&lt;/h4&gt;
&lt;p&gt;
For the POST request, we are aiming to add the JSON data into the array of Person instances. 
We are able to map multiple types of HTTP requests to the same URI to be managed by the handler.
In the peopleHandler function, we can decide how to process the request by assessing the request header. For this, a switch statement can address the different types of methods:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;peopleHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Request&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;method&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodGet&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        personJson&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Header&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Content-Type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;MethodPost&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; newPerson Person
        personDetails&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; ioutil&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ReadAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personDetails&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;newPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; newPerson&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        newPerson&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;nextId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        personList &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personList&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusCreated&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
From line 3, the switch statement begins where the method from the request, r, is cycled upon. 
&lt;/p&gt;
&lt;p&gt;
From lines 5 to 12, the GET request from step 4 is captured in its own block for the &lt;code&gt;http:MethodGet&lt;/code&gt; case.
&lt;/p&gt;
&lt;p&gt;
From lines 13 to 34, the POST request handler is defined. This method appears fairly long due to the multiple error checks performed at each stage.
&lt;/p&gt;
&lt;p&gt;
On line 13 a new Person instance is defined.
&lt;/p&gt;
&lt;p&gt;
On line 14, the &lt;code&gt;ioutil.ReadAll()&lt;/code&gt; method is used to read in the request body and to pass it into a new variable defined as &lt;code&gt;personDetails&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
From lines 15 to 18, the error from the ReadAll method call is reviewed before the personDetails JSON from the request body is passed into the newPerson instance using the &lt;code&gt;Marshal&lt;/code&gt; method on line 19.
&lt;/p&gt;
&lt;p&gt;
The error returned from the Marshal method is evaluated on lines 20 to 23 before evaluating the Person Id is defaulted to 0. The Id is later assigned using a custom function &lt;code&gt;nextId()&lt;/code&gt; to find the highest Id from the list and to increment upon it.
&lt;/p&gt;
&lt;p&gt;
Lastly, the personList is updated to append the newPerson to the list and a response is returned consisting of a 201 code for Status created. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 38.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcSAAAXEgFnn9JSAAABkUlEQVQoz1WRyW4TQRCG55l4BY68GWcuvESCIpQTclAERAoiEZadSQ6M7TZYRPZsvVcvVakZxXHc0qe/urpUWxfb01NolkuoLy5gc3IC/87OWNl3fg6NlFDvaqjrGpqmGXXPjlHdDmarFt59lPDmfQNvP3RQmIcHslpTv1iQLUvS5T2109/kxZLatiatJWOo73tWTdbaEakUGWNIuUi/RKQfVaCfq0CFDYGctdhJiS4EtN5j5x0aZ7H5u0DXbtEajZwADat3DpXWCNZg4DsRHVG4qyuCuzsMsznF2YzCdEpwc0OeC3kIFFjB+1H9sw4dpgCUmZQzxXSgMNWCDHclHx/pf6uo056QkyIiIQdnZm+PuvendOA5bqCw376jL8vsbm+zns9zKu8zXl9n7j8PhxMcM/h4tCNevReRlz2QpKTMJLYDLz+9qvrC0A2vKm+3lKqKYvWH4mZDYRw7Uo78KVrJtfZeKGNE542wMYi27wUACF6y4KoHcl6zbxUmk1p/nUB3+QX6z5/6NpqVArvOshFPR9ZdpzMPZG8AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Post request in Postman&quot;
        title=&quot;Post request in Postman&quot;
        src=&quot;/static/975ceec9c1927614f45382380c2e8400/5a46d/036_postRequest.png&quot;
        srcset=&quot;/static/975ceec9c1927614f45382380c2e8400/1db73/036_postRequest.png 75w,
/static/975ceec9c1927614f45382380c2e8400/8a4e8/036_postRequest.png 150w,
/static/975ceec9c1927614f45382380c2e8400/5a46d/036_postRequest.png 300w,
/static/975ceec9c1927614f45382380c2e8400/fc2a6/036_postRequest.png 450w,
/static/975ceec9c1927614f45382380c2e8400/0a47e/036_postRequest.png 600w,
/static/975ceec9c1927614f45382380c2e8400/d1882/036_postRequest.png 1562w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;6. Handle PUT requests to the server&lt;/h4&gt;
&lt;p&gt;
Before we can send a PUT request, we need to create a new handler and pattern. When we need to update a user, we must specify exactly which one by a unique identifier (Id). The ID is to be passed into the URL for the PUT request and extracted by the source code.
In the &lt;code&gt;main()&lt;/code&gt; method, I will create a new HandleFunc with a pattern using a trailing forward-slash:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;HandleFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/people/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; singlePersonHandler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The forward-slash tells Go to use the handler for all requests that define further information in the &apos;people&apos; endpoint if a more specific pattern is not matched.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;singlePersonHandler&lt;/code&gt; method can be split into two parts, firstly to obtain the Id from the URL, and secondly to perform the update to the peopleList accordingly. Part 1 is below:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;singlePersonHandler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Request&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    urlPathSegments &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; strings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Split&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;URL&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;people/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    personId&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; strconv&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Atoi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;urlPathSegments&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;urlPathSegments&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusNotFound&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; listItemIndex &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;findPersonById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusNotFound&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;findPersonById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personId &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; person &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;range&lt;/span&gt; personList&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; personId&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
On line 3, &lt;code&gt;urlPathSegments&lt;/code&gt; is is obtained by splitting the url path from &apos;people/&apos;.
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;personId&lt;/code&gt; is then extracted on line 4 by using the &lt;code&gt;strconv.Atoi&lt;/code&gt; method to obtain the final parameter from the url. The error is handled accordingly on lines 5 to 8. 
&lt;/p&gt;
&lt;p&gt;
On line 9, the person and their index position in the list are obtained through a method call to the function &lt;code&gt;findPersonById&lt;/code&gt;. findPersonById will loop through the personList
to obtain the person instance and their index position &apos;i&apos;. 
The first section concludes by handling a nil person that would be returned by findPersonId if no such Id is found for a person in the personList.
&lt;/p&gt;
&lt;p&gt;
The second section of the function will perform the PUT update:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; updatedPerson Person
    body&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; ioutil&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ReadAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;updatedPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    person &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;updatedPerson
    personList&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;listItemIndex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;person
    w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusOK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The second section will create a new person instance &lt;code&gt;updatedPerson&lt;/code&gt; and update the list of people with the new request.
&lt;/p&gt;
&lt;p&gt;
On line 3, the &lt;code&gt;ioutil.ReadAll&lt;/code&gt; method is used to read the content of the body from the PUT request. The body of the put request will contain the JSON of the person update. 
The variable &lt;code&gt;body&lt;/code&gt; is therefore populated with the JSON for the updated user. 
&lt;/p&gt;
&lt;p&gt;
The error code from &lt;code&gt;ReadAll&lt;/code&gt; is handled on lines 4 to 7.
&lt;/p&gt;
&lt;p&gt;
On line 8 the JSON body is extracted into the updatedPerson variables by using the &lt;code&gt;json.Unmarshal&lt;/code&gt; method. The error that is returned is then handled on lines 9 to 12.
&lt;/p&gt;
&lt;p&gt; 
The second section concludes where the person instance is overwritten with the &lt;code&gt;updatedPerson&lt;/code&gt; and the index position in the personList is updated to the new person.
A response header of StatusOK is returned. 
&lt;/p&gt;
&lt;p&gt;
Section 2 can be converted into a switch statement to satisfy further HTTP methods requests to the url of &apos;person/&apos; where a GET request would return a single person and a DELETE request would
remove the person from the personList:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Method&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodGet&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        body&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Header&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Content-type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodPut&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; updatedPerson Person
        body&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; ioutil&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ReadAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;updatedPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        person &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;updatedPerson
        personList&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;listItemIndex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;person
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusOK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodDelete&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        personList &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personList&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; listItemIndex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusMethodNotAllowed&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;Person &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; index &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    list&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; list&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; list&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;7. Run the application&lt;/h4&gt;
&lt;p&gt;
Before the application can be executed, we need to specify a port for which the web service will be run. 
To do this, the &lt;code&gt;ListenAndServe&lt;/code&gt; method from the http package is used to direct the application. 
ListenAndServe takes two arguments, first a string literal as the address and second a handler.
The Handler is often placed as &apos;Nil&apos; which will cause the handler to use its default &apos;DefaultServeMux&apos;. 
&lt;/p&gt;
&lt;p&gt;
DefaultServeMux is a HTTP multiplexer. The multiplexer is responsible for matching the url of the incoming request to the registered list of patterns that are served. 
&lt;/p&gt;
&lt;p&gt;
The web service can be started using the following command from the terminal:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;go&lt;/span&gt; run &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;\main&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;go&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you see an error, use the &apos;ls&apos; or &apos;dir&apos; command to ensure you are navigated to the correct directory where the main.go file exists. You can test the web service through the browser or by using an API client such as Postman. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Data persistence&lt;/h4&gt;
&lt;p&gt;
The current web service uses an in-built array that is initialised through the &lt;code&gt;init&lt;/code&gt; function to replicate a datastore. When we restart the web service, array is reinitialised and updates are therefore lost. In the following section, we will utilise the database connectivity packages from Go to persist our information in a MySQL database. We will also update the &lt;code&gt;handler&lt;/code&gt; functions to interact appropriately with the database.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating a MySQL Database&lt;/h4&gt;
&lt;p&gt;
To begin, we will use the MySQL workbench to create a new database and table with the default records of data required using the SQL statement below:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;pre class=&quot;language-sql&quot;&gt;&lt;code class=&quot;language-sql&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;DATABASE&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;go_store&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;go_store&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;people&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;INT&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;AUTO_INCREMENT&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;INT&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;go_store&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;people&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;go_store&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;people&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;go_store&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;people&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token identifier&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using MySQL within Go&lt;/h4&gt;
&lt;p&gt;
Go does not include the database driver for mysql in the standard library so we must download it with the following command:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;go get -u github.com/go-sql-driver/mysql&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The main.go file will import the driver to imply the use of the mysql driver. As the import is not explicitly used within the file, it is marked with an underscore on the import line:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token boolean&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;github.com/go-sql-driver/mysql&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
We will establish our database connection in a separate package and class: &apos;database&apos;. The database.go file will use the &lt;code&gt;sql.Open&lt;/code&gt; method from the sql package to create a connection.
A pool of database connections are defined within the &lt;a href=&quot;https://golang.org/pkg/database/sql/#DB&quot; target=&quot;_blank&quot;&gt;DB&lt;/a&gt; type from the sql Go package while the &lt;a href=&quot;https://golang.org/pkg/database/sql/#Open&quot; target=&quot;_blank&quot;&gt;Open&lt;/a&gt; method from the sql package is used to open a pool of database connections. 
&lt;/p&gt;
&lt;p&gt;
The database.go file will appear as below where a &lt;code&gt;SetupDatabase&lt;/code&gt; function is defined:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;package&lt;/span&gt; database

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&quot;database/sql&quot;&lt;/span&gt;
	&lt;span class=&quot;token string&quot;&gt;&quot;log&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; DbConn &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;sql&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DB

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;SetupDatabase&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; err &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt;

	DbConn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sql&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mysql&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;token string&quot;&gt;&quot;root:java@tcp(127.0.0.1:3306)/go_db&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The var &lt;code&gt;DbConn&lt;/code&gt; is defined within the package and assigned by the &lt;code&gt;sql.Open&lt;/code&gt; method call on line 13.
The first argument to the &lt;code&gt;sql.Open&lt;/code&gt; defines the driver name. As we are using mysql, it is &quot;mysql&quot;, however other driver names can be found &lt;a href=&quot;https://github.com/golang/go/wiki/SQLDrivers&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The second argument to &lt;code&gt;sql.Open&lt;/code&gt; is the data source name. The name is defined using the following pattern:
{database username}:{database password}@tcp(127.0.0.1:{port})/{database name}.
&apos;tcp&apos; stands for transmission control protocol which is the protocol used for sending data. The database is hosted locally, thus the 127.0.0.1 IP address. 
&lt;/p&gt;
&lt;p&gt;
From the main method, we will call the database &lt;code&gt;SetupDatabase&lt;/code&gt; method and run the application to confirm no error has been produced during the connection.
Once the connection is established, we can begin to process user requests using SQL queries. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Making GET requests to the database&lt;/h4&gt;
&lt;p&gt;
The first GET request we will need to make will pull in all Person instances from the database:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getPeople&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	results&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DbConn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Query&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`SELECT Id, Name, Age FROM people`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;defer&lt;/span&gt; results&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Close&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	personList &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; results&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; person Person
		results&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Scan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		personList &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personList&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; personList&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;getPeople()&lt;/code&gt; function will return an array of Person and an error when called.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;a href=&quot;https://golang.org/pkg/database/sql/#DB.Query&quot; target=&quot;_blank&quot;&gt;Query&lt;/a&gt; method from the sql package will return an array of rows from the database and an error following the execution of the query in the argument.
The Query statement is a used to return the defined fields in the order as specified by the struct &lt;code&gt;Person&lt;/code&gt;. The error is evaluated if the request is not served on lines 4 to 6.
&lt;/p&gt;
&lt;p&gt;
When we reach line 7, we know valid results have been returned (following an error code of Nil on lines 4-6). We then use the &lt;a href=&quot;https://golang.org/pkg/database/sql/#Rows.Close&quot; target=&quot;_blank&quot;&gt;Close&lt;/a&gt; method from the Rows package to prevent further access to the result set after the getPeople function is executed.
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;make([]Person, 0)&lt;/code&gt; method is used to create a dynamically-sized array known as a &apos;Slice&apos;. The &lt;code&gt;personList&lt;/code&gt; will be used to store each record as a person instance when it is scanned from the database.
&lt;/p&gt;
&lt;p&gt;
The personList is populated by a for-loop on lines 9 to 13 upon where the database records are iterated upon until there are no further rows. 
&lt;/p&gt;
The &lt;a href=&quot;https://golang.org/pkg/database/sql/#Rows.Scan&quot; target=&quot;_blank&quot;&gt;Scan&lt;/a&gt;method from the Rows package will convert the values from each column into the values as specified in the arguments. 
Therefore the first column will be assigned to the &amp;person.Id, second to &amp;person.Name and third to &amp;person.Age.
The getPeople function completes by returning the personList which will consist of an array populated by each record in the database. 
&lt;/p&gt;
&lt;p&gt;
After the GET request for ALL people in the database is implemented, we can begin to write a GET request to obtain a single person from the database:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getPerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personId &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	row &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DbConn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;QueryRow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`SELECT Id, Name, Age FROM people where Id = ?`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; personId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	person &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Scan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; sql&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ErrNoRows &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;getPerson(personId int)&lt;/code&gt; function will take in an int value to represent the personId and will perform a search for the user. The method will return a Person instance with an error code.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
On line 3, the &lt;code&gt;row&lt;/code&gt; item is defined as a single row by using the &lt;a href=&quot;https://golang.org/pkg/database/sql/#DB.QueryRow&quot; target=&quot;_blank&quot;&gt;QueryRow&lt;/a&gt; method. The QueryRow method differs from the Query method as it will only return the first result from the database. 
&lt;/p&gt;
&lt;p&gt;
On line 4, a new Person is instantiated. The person instance is populated on line 6 with the &lt;a href=&quot;https://golang.org/pkg/database/sql/#Row.Scan&quot; target=&quot;_blank&quot;&gt;row.Scan&lt;/a&gt; method. The &lt;code&gt;row.Scan&lt;/code&gt; method will return the values of the fields as specified by the query. As a result, we pass the values from column 1 into person.Id, column 2 into person.Name and column 3 into person.Age. 
&lt;/p&gt;
&lt;p&gt;
The error returned from &lt;code&gt;row.Scan&lt;/code&gt; is evaluated against there being 0 records and also against any other errors which are otherwise returned. 
Lastly, the complete Person instance is returned with an empty error code.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Making POST requests to the database&lt;/h4&gt;
&lt;p&gt;
The &lt;code&gt;insertPerson&lt;/code&gt; function is used for making POST requests to the database. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;insertPerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person Person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DbConn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Exec&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`INSERT INTO people (Name, Age) 
	VALUES (?, ?);`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	insertId&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;LastInsertId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;insertId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The function uses a single Person parameter and returns an int (the personId) along with an error code.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
On line 2, the INSERT statement is made using the &lt;a href=&quot;https://golang.org/pkg/database/sql/#DB.Exec&quot; target=&quot;_blank&quot;&gt;Exec&lt;/a&gt; method without returning any rows.
The method will use the person parameter to populate the query to add values to the Name and Age. The database will auto-increment the Id of the person to provide an Id value.
&lt;/p&gt;
&lt;p&gt;
The result from the &lt;code&gt;Exec&lt;/code&gt; method will return information such as the number of rows affected. On line 5 to 7, the error code is handled.
&lt;/p&gt;
&lt;p&gt;
On line 8, the &lt;code&gt;lastInsertId&lt;/code&gt; method is called to identify the latest Id from the database which will correspond to the new person.  
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Making DELETE requests to the database&lt;/h4&gt;
&lt;p&gt;
The DELETE function is similar to the &lt;code&gt;insertPerson&lt;/code&gt; function where the &lt;code&gt;Exec&lt;/code&gt; method is used to call a custom SQL query:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;deletePerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personId &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token boolean&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DbConn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Exec&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`DELETE FROM people where Id = ?`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		personId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; err
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The personId is passed into the method as a parameter and is used to perform the DELETE operation. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Making PUT requests to the database&lt;/h4&gt;
&lt;p&gt;
The PUT request is also similar to the POST and DELETE functions as it also uses the &lt;code&gt;Exec&lt;/code&gt; method to perform an UPDATE query upon a user:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;updatePerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person Person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token boolean&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DbConn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Exec&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;`UPDATE people SET Name = ?, Age = ? where Id = ?`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
		person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Age&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; err
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The query will update the Name and Age of the Person instance where the Id is identified within the database. 
The handler will define the new Person instance and pass it into the method. An error is returned as per the SQL result. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Updating the handlers for the CRUD requests&lt;/h4&gt;
&lt;p&gt;
The handlers that were previously defined must now be updated to reference the CRUD methods that are defined above. 
To reflect, there are two handlers to update, one for the &quot;/people&quot; pattern which accesses all records, and one for the &quot;/people/&quot; pattern which accesses an individual person. 
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;SinglePersonHandler&lt;/code&gt; method will be responsible for extracting the final parameter from the URL to make an update to a single row.
As the query uses the personId, just 2 HTTP headers are supported: GET and DELETE. 
The updated method will obtain the Person instance through the new &lt;code&gt;getPerson&lt;/code&gt; method:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getPerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusNotFound&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The switch statement responsible for the GET and DELETE methods are updated as follows:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Method &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodGet&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
		personJson&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Header&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Content-Type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodDelete&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
		err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;deletePerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusOK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusMethodNotAllowed&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The GET method will return the JSON of the Person instance as returned from the &lt;code&gt;getPerson&lt;/code&gt; method by using &lt;code&gt;json.Marshal&lt;/code&gt;.
The Delete method will use the personId from the URL to perform the update to the database with the &lt;code&gt;deletePerson&lt;/code&gt; method.
A default &lt;i&gt;Status Method Not Allowed&lt;/i&gt; error will be returned if the HTTP request is not GET or DELETE.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The PersonHandler will be responsible for handling GET, PUT and POST requests under the &quot;/people&quot; pattern from the URL. 
The GET method is updated to return a list of people using the &lt;code&gt;getPeople()&lt;/code&gt; method:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MethodGet&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
		personList&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getPeople&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		personJson&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personList&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Header&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Content-Type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personJson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The PUT request will use the &lt;code&gt;updatePerson&lt;/code&gt; method to perform the update once the request body has been processed and extracted. 
The person.Id from the request is extracted and performed. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; updatedPerson Person
		bodyBytes&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; ioutil&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ReadAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Body&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bodyBytes&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;updatedPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;updatePerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;updatedPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusOK&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The POST request uses the uses the &lt;code&gt;insertPerson&lt;/code&gt; method to make the POST request:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; newPerson Person

		personDetails&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; ioutil&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ReadAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Body&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Unmarshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;personDetails&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;newPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; newPerson&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Id &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusBadRequest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;insertPerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newPerson&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusCreated&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Once the handler methods are updated, we are able to restart our web service and the send requests to the server to add, update, delete and obtain records from the database.
When the web service is shut down, the results are persisted in the database and accessible upon restarting the web service. Use the MySQL workbench or CLI to verify the updates on the database after each client call to see the result of the HTTP requests. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
This blog has taken us through the process of creating a web service using the functions and support from the Go http package and mysql driver package. 
The blog has also reviewed several keywords of Go to create the CRUD operations using the HTTP protocol. By reviewing the code, line by line, we are able to see patterns for creating endpoints, handling the different types of method requests, and processing the errors and responses into a HTTP response.
&lt;/p&gt;
&lt;p&gt;
If we exclude the SQL driver, we have only used features exclusive to the Go standard library. This out-of-the-box support for web services is what makes Go lightweight to compile and execute. 
Go also provides comprehensive documentation for the available methods and responses that are possible from the documentation. I would encourage you to review the &lt;a href=&quot;https://golang.org/doc/&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt; to understand how Go can be enhanced further with the use of its standard library. 
&lt;/p&gt;
&lt;p&gt;
You can find the source code from this blog on GitHub. The non persisted code: &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/Simple%20go%20web%20service&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; and the source code with MySQL persistance: &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/Persisted%20Go%20web%20service&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An Introduction to Go]]></title><description><![CDATA[An introduction to using Go, the limitations it is was built to resolve and its most valuable use cases.]]></description><link>https://aneesh.co.uk/an-introduction-to-go</link><guid isPermaLink="false">https://aneesh.co.uk/an-introduction-to-go</guid><pubDate>Mon, 04 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the drivers for the invention of Go programming language.&lt;br&gt;
&amp;#8226; Review the popular use-cases for choosing Go in a project.&lt;br&gt;
&amp;#8226; Review the syntax and configuration for Go to begin building applications.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction&lt;/h4&gt;
&lt;p&gt;
Go (often referred to as &apos;Golang&apos;) was created by 3 engineers who worked together at Google during 2009: Ken Thompson, Rob Pike and Robert Griesemer. 
Go has since been developed into an open source programming language that is used upon many major platforms including Docker, Kubernetes and Terraform. But why is Go used in these cases?
&lt;/p&gt;
&lt;p&gt;
Go came about as a programming language to tackle some of the persistent issues of slow compilation and slow application builds faced by the developers of large, distributed servers at Google. 
As a result, Go was designed to be the only programming language to serve all 3 sought-after capabilities:&lt;br&gt;
&amp;#8226; Efficient compilation.&lt;br&gt;
&amp;#8226; Efficient execution.&lt;br&gt;
&amp;#8226; Ease of programming.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Languages such as Java and C++ provide efficient compilation and execution, languages such as JavaScript and Python are able to provide ease of programming with efficient compilation, however before Go, there had
not been a language to offer the tri-standard.&lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABr0lEQVQ4y32Tx6pqYQyF9+sr6MiCDlRUfAAVRcECDuy911dZhy+HwK/Xewbhz05ZWSk7ul6vCuVyuZigHw4H9Xo9pVIp5XI5dbtds33GhRJ9Gjz4fr9rsVioVqupUqmoVCrZO5vNzPcN7B9Ar+rBu91O+/1ep9NJ5/PZdGzfYv/LkIDb7abNZqPlcvmWgA7r7XZrMX+2HFZEAMMeJqIj7nN7CBx9OpgPrU2nU3tZAu3SNqwR94XF/H1j6Judz+dqtVoql8vKZrOq1+vGilbxj8djAz0ej38zhAlgvOv1Wv1+X51OR41GQ81mU4/Hw5iuVivLIfYrQ18EDGiJtqkOK77T6bQymYx9A4KPGIqy9bD1N0AC/HAR2IxGIzvodrttOuzdzxwp+BWQ14dOAInOulAo2IFTMPQRi9/HBU7k7F6vl82IO3MbQnswm0wmprvdZ+i5z+fzlyGzYA6DwUDD4dAGjtCOnwtbRcI/hl+QAoB6Lj5jWCwWFY/HlUgkVK1WrQ3agy2LINGXhI2CgMMun88rmUwqFovZVUSg+l0RjO5b9L/Dl+Dtug0yngdjSPwAmOUHF6eZUUAAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Golang motivation&quot;
        title=&quot;Golang motivation&quot;
        src=&quot;/static/5b3054225beb09747dcf3897c8a5a9d1/5a46d/033_golangVenn.png&quot;
        srcset=&quot;/static/5b3054225beb09747dcf3897c8a5a9d1/1db73/033_golangVenn.png 75w,
/static/5b3054225beb09747dcf3897c8a5a9d1/8a4e8/033_golangVenn.png 150w,
/static/5b3054225beb09747dcf3897c8a5a9d1/5a46d/033_golangVenn.png 300w,
/static/5b3054225beb09747dcf3897c8a5a9d1/fc2a6/033_golangVenn.png 450w,
/static/5b3054225beb09747dcf3897c8a5a9d1/0a47e/033_golangVenn.png 600w,
/static/5b3054225beb09747dcf3897c8a5a9d1/2bef9/033_golangVenn.png 1024w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Go is a statically typed, compiled and lightweight programming language. Go achieves rapid compilation as well as other extensive behaviours to provide developer support with a shallow learning curve, backward compatibility and built-in support for features such as web services and testing.&lt;br&gt;
The extended features of Go can be categorised into 4 key areas:&lt;br&gt;
&amp;#8226; Simplicity of implementation.&lt;br&gt;
&amp;#8226; Concurrency support.&lt;br&gt;
&amp;#8226; Out-of-the-box experience.&lt;br&gt;
&amp;#8226; Cross-system performance.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Simplicity of implementation&lt;/h4&gt;
&lt;p&gt;
Go is able to pair rich features with an elegant approach to language design.
The framework for testing and profiling Go is provided for multiple types of testing by default. 
Unit tests can be written in parallel with the code; Go also provides in-built tools to understand test coverage and code documentation. 
&lt;/p&gt;
&lt;p&gt;
Go is a multi-paradigm programming language using OOP and procedural code. Go supports the use of types and methods similar to OOP, however it does not use a class hierarchy. Go uses imperative features such as statements and loops to change the state of an Object as well as concurrency for multi-core processing.
&lt;/p&gt;
&lt;p&gt;
The simplicity of Go syntax and minimal nature of its keywords means generics are not supported beyond the built-in data structures of slices and maps. If Go were to add support for custom generics, it will counter the simplicity and clarity objectives while driving up the compile time. 
&lt;/p&gt;
&lt;p&gt;
The Go language updates are designed to provide further support for applications that are written in prior versions of the language through backward compatibility. The support for backward compatibility provides confidence to the developers to upgrade with the latest features of Go without risking the functionality of their applications. Other than the exception for security patches and bug-fixes, Go will continue to be completely backward-compatible. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Concurrency&lt;/h4&gt;
&lt;p&gt;
Go was developed during a modern period of computer hardware where the presence of multiple cores to a system were standard by design. Go places the management of multiple cores within its design 
where other languages may fault around the challenges of maintaining database connections and managing caches. Go achieves its capacity for concurrency through the use of &lt;i&gt;goroutines&lt;/i&gt;, a lightweight
alternative to individual threads that consequently support a huge scaling capacity of a Go application.
&lt;/p&gt;
&lt;p&gt;
Go features its own network awareness where threads can be executed in a thread-safe manner. 
Goroutines are able to safely communicate with each other through channels using the Go schedular to assign threads to tasks. Go uses &lt;i&gt;channels&lt;/i&gt; to act as a conduit for goroutines to communicate with each other. Channels prevent the potential for race conditions when multiple goroutines access shared memory. Creating a goroutine can be implemented by adding the keyword &lt;code&gt;go&lt;/code&gt; before calling a method (line 8 below):
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;world&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;go&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hello world&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;goroutine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
In the below example, the &lt;code&gt;&lt;-&lt;/code&gt; syntax is used to assign and use a value that is passed to a channel:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;    names &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;go&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        name &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;John Adams&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    name &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt;names

   fmt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;The name we got is: %s&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The in-built features for handling multi-thread communication and the &lt;code&gt;go&lt;/code&gt; keyword for the creation of goroutines make concurrency easily accessible to the developer. The creation of goroutines has also made concurrency more widely possible without the usual limitation of threads as experienced with Java.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Out of the box experience&lt;/h4&gt;
&lt;p&gt;
Unlike other programming languages, Go does not require extensive libraries and applications to be pulled in to perform the most basic of application designs and uses. The standard library may not be as extensive as other languages, however it focusses on a few core features. Go has been supported by third parties who have built standard libraries to provide support for core components including:&lt;br&gt;
&amp;#8226; String manipulation within the application.&lt;br&gt;
&amp;#8226; Data exportation to create external archives.&lt;br&gt;
&amp;#8226; Web APIs for creating and consuming web services such as the &apos;net/http&apos; package.&lt;br&gt;
&amp;#8226; File handling and operations.&lt;br&gt;
&amp;#8226; Testing APIs, a comprehensive in-built test suite.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The Go CLI provides the developer with ample support across the creation of application builds, artifacts and documentation for their source code. From the command line, the developer can execute the following commands to utilise the complete CLI support from Go:
&lt;/p&gt;
&lt;p&gt;
&amp;#8226; &lt;code&gt;go build&lt;/code&gt; to compile and package a Go application with its dependencies. The resulting executable is named after the first source file.&lt;br&gt;
&amp;#8226; &lt;code&gt;go generate&lt;/code&gt; to generate Go files by processing source.&lt;br&gt;
&amp;#8226; &lt;code&gt;go get&lt;/code&gt; to add dependencies to the current module and to install them.&lt;br&gt;
&amp;#8226; &lt;code&gt;go test&lt;/code&gt; to automate the testing of packages and to print the result to the command line.&lt;br&gt;
&amp;#8226; &lt;code&gt;go doc&lt;/code&gt; prints the package documentation.&lt;br&gt;
&lt;p&gt;The application profile also performs analysis upon the application consumption of CPU, network, goroutines and requirements. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Cross-system performance&lt;/h4&gt;
&lt;p&gt;
Go contains its own runtime library and is deployed as a single executable binary file. Go eliminates the potential for version or dependency mis-match. The performance advantage of Go is realised from the executable binary file that does not rely on a virtual machine to abstract its execution. Without the VM, Go does not have a write-once-run-everywhere ability like the JVM, however Go builds can be adapted for different operating systems from a single parameter instruction within the build:&lt;br&gt;
For Windows
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;GOOS=windows go build main.go&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For MAC OS&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;GOOS=darwin go build main.go&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The architecture for the platform can also be defined with the &lt;code&gt;GOARCH&lt;/code&gt; property.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The benefits of Go as a compiled language can be realised from enhanced execution time where the operating system does not need to interpret the code at runtime. This advantage is also realised from other languages such as C++ over Java where Java uses an interpreter as well as the compiler used by C++. 
&lt;/p&gt;
&lt;p&gt;
A final element of Go&apos;s multi-dimensional features are its garbage collection feature for heap memory management. This example demonstrates how Go combines the benefits of garbage collection from Java with the compile performance of C++ and an enhanced ease for programming. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Use cases for Go&lt;/h4&gt;
&lt;p&gt;
As previously mentioned, Go provides a unison between some of the most popular languages, taking their best features and combining them into a lightweight language.
The features of Go may make it seem appealing for all use-cases, however its standard library may limit its potential. Despite this, there are a few use-cases that really do emphasise 
the benefits to choosing Go as the language for development to tackle the challenges of compilation, concurrency and simplicity that it addresses.
&lt;/p&gt;
&lt;p&gt;
The primary use cases for Go extend to web services, stand-alone tools and DevOps which lend themselves to the support, scaling and performance attributes of Go. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Distributed services&lt;/strong&gt;&lt;br&gt;
Distributed services require a vast capacity for concurrency to support communication and data transfer. Go supports distributed services and networks through goroutines and channels. The message delivery and network awareness features of Go and its &apos;http&apos; package provide extensive support for distributed systems. As a result, Go is a popular language for the creation of APIs and web servers. As a cross-platform language with extensive network features, Go is also popular amongst cloud-native applications.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Stand-alone tools&lt;/strong&gt;&lt;br&gt;
The single binary output of a Go application make them highly suitable as a utility program. The Go application can quickly launch and perform a service to make them optimal for single-service applications.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;DevOps and SRE&lt;/strong&gt;&lt;br&gt;
Go&apos;s &apos;build once and promote everywhere&apos; principle makes it a valuable language when used for building CICD pipelines and enhancing web scalability and durability. Go-based API builds can also be used for communicating with middleware to take advantage of the networking capability of Go and making them powerful within a CICD process. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Building with Go&lt;/h4&gt;
&lt;p&gt;
The following section of the blog will look at how we can develop a Go project on our machines and the appropriate configuration required. To get started, download Go from &lt;a target=&quot;_blank&quot; href=&quot;https://golang.org/&quot;&gt;https://golang.org&lt;/a&gt;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Configuring a Go project&lt;/h4&gt;
&lt;p&gt;
Before we create a new Go Project, we must navigate to the Go src workspace from where packages and classes are defined. First navigate to the Go directory on your machine. You will find 3 sub directories: pkg, src, and bin. The src folder may not already exist and must therefore be created.
&lt;/p&gt;
&lt;p&gt;
Go workspaces (folders) can be created within the src folder for each project. While it is possible to create a Go workspace at any other directory in your machine, we will later review the benefits of using the Go src workspace directory as we understand how utility packages and class references are made with Go.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Utility packages in Go&lt;/h4&gt;
&lt;p&gt;
Utility packages in Go are similar to creating classes in Java that are later referenced or used as a service by another class. In Go, we can achieve a similar behaviour of different classes that can be referenced from different workspaces.
&lt;/p&gt;
&lt;p&gt;
Packages can contain types, variables and functions that we want to use from other Go packages. 
Utility packages in Go allow us to separate responsibilities of classes and to create layers to the application. You could think of the layers being similar to a repository-service-controller layer for a web service. Before we can use a utility package, we must first create the class in the Go workspace and install it into the &apos;pkg&apos; folder of Go using the CLI. 
The below example demonstrates how a &lt;code&gt;main.go&lt;/code&gt; file can access the &lt;code&gt;car.go&lt;/code&gt; class as a service:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;package&lt;/span&gt; main

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;demo/car&quot;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    car&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;MakeSound&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;package&lt;/span&gt; car

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;fmt&quot;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;MakeSound&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    fmt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;vroom&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The current main class will show a compile error:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;failed to import utility package &quot;demo/car&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can resolve this problem by installing the car package into the &apos;pkg&apos; directory on the Go path.
The below command will change into the car directory, then install the car package into the &apos;pkg&apos; folder where it can be referenced.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;cd car
go install&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The compile error on the main class will no longer appear, and a new folder &apos;demo/car&apos; will appear within the pkg folder with a &lt;code&gt;car.a&lt;/code&gt; file. The package can then be used as a service 
for other Go applications. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Go syntax&lt;/h4&gt;
&lt;p&gt;
Go is designed with ease of programming as an objective. Amongst the garbage collector for memory management, Go achieves ease through omission of generics, use of types and the use of complete units to express a statement.
&lt;/p&gt;
&lt;p&gt;
I have found learning the Go syntax easiest when using a reference point of Java. The reflection in Java will then allow me to understand and explain the difference in syntax design.
The Go syntax was built with a mantra of &apos;do more with less&apos; to enable an expansive number of actions with a lightweight design. From my initial understanding of Go, I have found the most evident key differences:&lt;br&gt;
&amp;#8226; Method names start capitalised (inherited from C#)&lt;br&gt;
&amp;#8226; Functions are defined with &lt;code&gt;func()&lt;/code&gt;. The function return type comes after the name.&lt;br&gt;
&amp;#8226; Go does not handle explicit exceptions, only error values which are returned from a method call.&lt;br&gt;
&amp;#8226; Object instances are passed by reference using an ampersand and asterisk syntax. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Creating functions&lt;/strong&gt;&lt;br&gt;
Functions in Go are marked with the keyword &apos;func&apos; at the start followed by a name, expected parameters and return type. 
Unlike Java, Go functions can return more than 1 value, in-fact it is very common for Go methods to return an error as well as a returned value. This is similar to throwing an exception in Java. 
&lt;/p&gt;
&lt;p&gt;
An example of a simple function in Go can appear below:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The equivalent in Java would be:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Some methods will return an error and a response thus causing two outcomes to be handled by the code.
In Java, this is similar to having a try-catch block for a method call, instead with Go, we will express the return as two arguments. 
The below example demonstrates the &lt;code&gt;Marshal&lt;/code&gt; method which will return JSON for a person along with an error from the method call:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-go line-numbers&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;json&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Marshal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    w&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;WriteHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StatusInternalServerError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Lines 2 to 5 in the above Go code snippet is a common pattern for handling errors. The error &lt;code&gt;err&lt;/code&gt; is checked to be nil before returning a http response (or otherwise returning an error message).
The Go function is the equivalent to a try-catch block as below:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;convertToJson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Exception&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Naming conventions&lt;/strong&gt;&lt;br&gt;
Go uses camelCase naming convention for identifiers. The first letter will also determine the visibility of the Object to other packages and classes.&lt;br&gt;
The name provided to Objects, such as &apos;person&apos;, &apos;Animal&apos; etc, are accessible within and outside their package depending on its naming convention. A capital letter at the start of the name (Animal) will allow the class to be accessed outside of its package. Otherwise a lowercase letter (person) will not be accessible outside its package. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Passing values&lt;/strong&gt;&lt;br&gt;
Go uses pass-by-value within the source code. This means when a parameter is passed between functions, it will always create a copy of the value and pass it onwards.
Go however defines two different types of parameters to be passed: the value or a reference.
As Java uses pass by value, where a copy of the Object reference is passed between the methods, the address of the Object is accessed by the calling method:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; alice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;changeName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;alice&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;alice&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;prints:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Bob&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The reference of alice is passed into the changeName method that updates the name to &quot;Bob&quot; of the Object instance. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
In Go, trying to achieve the same objective would not update the instance itself:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    person &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; Person&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;changeName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    fmt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;changeName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p Person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;returns:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Alice&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Instead, to pass the actual reference of the instance to the &lt;code&gt;changeName&lt;/code&gt; method, we need to use an ampersand (&amp;#x26;) to send the reference of the instance, and an asterisk (*) to receive an address of the instance:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;go&quot;&gt;&lt;pre class=&quot;language-go&quot;&gt;&lt;code class=&quot;language-go&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    person &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; Person&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;changeName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    fmt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;changeName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;returns:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Bob&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The syntax behind the ampersand and asterisk is useful to remember for addressing instances within the application.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
Go is a young programming language that has been developed out of a drive to improve compile time and the ease of programming alongside the enhanced execution time offered by a compiled language. The lightweight design of Go along with its enhanced support for concurrency have made it a bespoke language for various use cases including networking and DevOps. In this blog I have looked at how Go came about, its key use-cases and the key syntax design of the language.
&lt;/p&gt;
&lt;p&gt;
The most valuable features of Go exist from its lightweight design and niche use cases. The Go standard library is designed to offer comprehensive support to networking and concurrency that can make building modern and rapid-fast application performance much easier and simple out-of-the-box. This is an important consideration to make before considering Go for a project and the growth of a project with time. 
&lt;/p&gt;
&lt;p&gt;
In my next blog, I will use Go to create a RESTful API service to crete and return JSON data to other services.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Key takeaways from The Phoenix Project]]></title><description><![CDATA[Key takeaways from the best-selling novel on modern IT and software delivery: The Phoenix Project.]]></description><link>https://aneesh.co.uk/key-takeaways-from-the-phoenix-project</link><guid isPermaLink="false">https://aneesh.co.uk/key-takeaways-from-the-phoenix-project</guid><pubDate>Mon, 21 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Review the key points and values that can be taken from The Phoenix Project.&lt;br&gt;
&amp;#8226; Understand the importance of IT across a business when delivering value to a customer.&lt;br&gt;
&amp;#8226; Understand the various elements of DevOps and its importance to creating cohesion between business teams.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;The Phoenix Project by Gene Kim, Kevin Behr and George Spafford&lt;/h4&gt;
&lt;p&gt;
The Phoenix Project is an award-winning novel that explores modern-day IT and its inseparable importance to the business operations. The mutual inclusion of business and IT within The Phoenix Project extend the audience of the book beyond IT professionals to provide valuable lessons for all business leaders on delivering value through their IT capability.
&lt;/p&gt;
&lt;p&gt;
The Phoenix Project introduces us to the protagonist Bill Palmer, a newly promoted IT manager at Parts Unlimited who tackles a series of challenges to transform the productivity and chemistry between teams of the same system. The focal challenge of the job regards a major project delivery known as &apos;Project Phoenix&apos; which acts as milestone to the company and performance review that threatens the IT team to being outsourced.
&lt;/p&gt;
&lt;p&gt;
Amongst the twists and turns of the story, we are introduced to common characters that we find in our own offices that entice us to read further into their professional development. The Phoenix Project introduces the value of DevOps at all corners of the business; unsurprisingly, we will come to find that IT is no longer a business domain to keeping the costs down, but is an interconnected web between all sources of value at Parts Unlimited.
&lt;/p&gt;
&lt;p&gt;
In this blog, I will review the key points and takeaways from the novel that have allowed me to reflect upon the value of DevOps in modern delivery methodologies. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Part 1&lt;/h4&gt;
&lt;p&gt;
&lt;strong&gt;Tracking and managing changes&lt;/strong&gt;&lt;br&gt;
The disorganised state of IT at Parts Unlimited is firstly demonstrated through their handling of an urgent issue where a software change has prevented employee paychecks from being sent. The exact cause of the problem is unbeknownst to anyone amidst the many data sources and their intertwined relationships. There is minimal visibility into the changes made to the system meaning the team were unable to identify who, when or where the critical update was performed. The issue becomes more and more severe as personal identifiable information is at risk thus introducing regulatory pressures to the moral pressure of the company to pay their employees on time.
&lt;/p&gt;
&lt;p&gt;
The insufficient change management process highlighted difficulties in understanding the error which was brashly diagnosed as a &apos;glitch&apos;. Why were Parts Unlimited unable to identify the source of the problem? The IT team did not utilise an approval chain, communication channel or ticketing system to track and document the updates to the system. As a result, the team were unable to understand where to look for the problem or even when it may have first occurred. As we will later review, the time constraints upon the development team meant the ticketing system along with software testing had been disregarded.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The Three Ways of DevOps&lt;/strong&gt;&lt;br&gt;
The Phoenix Project introduces three underlying principles of DevOps that are implemented by Parts Unlimited throughout the story. The underlying principles are explored within this blog as a component to maximising workflow from the business to IT, to operations, and lastly the customer.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;DevOps Way 1: Systems thinking&lt;/strong&gt;&lt;br&gt;
The business silos at Parts Unlimited demonstrate their lack of cohesion as marketing publicly communicated a date that was earlier than expected for the deployment of Phoenix. The premature delivery of Phoenix introduced Bill to consequences to breaking the first underlying DevOps way.
&lt;/p&gt;
&lt;p&gt;
Maximising workflow downstream emphasises the importance of the business to act &lt;u&gt;together&lt;/u&gt; and not as individual silos. The first way instils an understanding of the system right across the workflow where business silos do not allow a defect to be passed downstream and subsequently take a view of the system and impacts of changes upon others rather than just themselves. Without the systems thinking, the issues experienced at Parts Unlimited &lt;u&gt;before&lt;/u&gt; the deployment of Phoenix (such as erratic changes and fixes) were unsurprisingly experienced during the Phoenix deployment. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The 4 types of work&lt;/strong&gt;&lt;br&gt;
As Bill tries to make sense of the problems at Parts Unlimited, he receives help from an agile &apos;guru&apos; Erik who uses a plant factory as an analogy to the software delivery at Parts Unlimited. Erik introduces 4 types of work that are constantly worked upon at any business (including Parts Unlimited):&lt;br&gt;
&amp;#8226; Planned work - business projects or enhancements&lt;br&gt;
&amp;#8226; Internal projects - server migrations and software updates&lt;br&gt;
&amp;#8226; Changes - feedback driven updates to the software&lt;br&gt;
&amp;#8226; Unplanned work - escalations and fixes to delivered projects.
&lt;/p&gt;
&lt;p&gt;
Amongst the different types of work, &lt;strong&gt;unplanned work&lt;/strong&gt; is the outlier that cannot be accounted for within a delivery plan and must be minimised as much as possible. It is important for Bill to understand the different types of work so new work can be prioritised accordingly. The optimal distribution of work will see the senior members work on planned work while new-joiners undertake the unplanned work.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Constraints within the system&lt;/strong&gt;&lt;br&gt;
Bill meets Brent, a senior engineer, who is the SME across the IT systems at Parts Unlimited. Brent is soon identified as a constraint to the business who is highly depended upon by multiple work streams as the only person who can resolve their problems. Brent not only has unmanageable levels of work, he is also unable to document his knowledge for others to learn from due to the same time constraint. Brent informs Bill how the ticketing system is also too timely to use thus creating a further dependency upon Brent&apos;s expertise in the future. 
&lt;/p&gt;
&lt;p&gt;
Brent&apos; responsibilities extend to completing unplanned work thus creating further bottlenecks for multiple work streams that depend upon Brent to fix their problems. The vicious cycle of Brent&apos;s knowledge retention and time consumption from fixing problems make the reliance upon him deepen with time. The organisation of the engineers to rely on Brent breaks the optimisation of the team resources where the most senior engineers are to be working solely on enhancing the software and for junior engineers to be learning about the software through unplanned work. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Part 2&lt;/h4&gt;
&lt;p&gt;
&lt;strong&gt;DevOps Way 2: Feedback Loops&lt;/strong&gt;&lt;br&gt;
Part 2 starts on a positive note of leadership bonding followed by a retrospective where Erik emphasises the importance to prioritise and reduce unplanned work. Due-date performance begins to improve however management become anxious to unfreeze their projects. The failure of planned work continues to creates more unplanned work and the business leaders are introduced to the second way of DevOps: increasing feedback loops across the workflow.
&lt;/p&gt;
&lt;p&gt;
The changes within the system are always triggered by failure. Feedback upon changes must therefore be provided across the workflow to reduce the potential for further unplanned work to be created. The leadership are introduced to resilience engineering where the system is designed to withstand failure and the ability to adapt to failure becomes a strength and not a weakness of more unplanned work. 
&lt;/p&gt;
&lt;p&gt;
Feedback loops across the workflow are amplified and addressed directly before it can further impact downstream teams and customers. Feedback is supported across more points within the workflow and within non-production environments. The amplified feedback supports software testing for developers before moving changes to production. From a DevOps perspective, feedback loops can be automated across builds and deployments through a continuous integration and delivery pipeline. The CICD pipeline would further prevent the advancement of a software change to further deployment steps if it is detected to fail at any preceding stage.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Utilising test environments&lt;/strong&gt;&lt;br&gt;
The support for faster feedback at Parts Unlimited came about from a Phoenix change failure that impacted $10 million in sales. The error was traced back to the ineffective testing of a change that had performed as expected in test environments, but differently in production. The need for a preproduction environment, which is a replica of production, became evident to the developers to avoid a similar error in deployment testing. The test environment configuration is therefore important to retain and monitor to support the quality and reliability of testing before deployment to production. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Kanban boards&lt;/strong&gt;&lt;br&gt;
A Kanban board is an agile project management tool used to visualise work across the business. Kanban boards are first used in part 1 to categorise changes when discussed within the CAB meeting. Changes are grouped by volatility, risk and priority. When Kanban boards were first introduced, some 400 changes were scattered across boards to illustrate the hectic schedule of changes at Parts Unlimited &lt;u&gt;before&lt;/u&gt; the deployment of Phoenix.
&lt;/p&gt;
&lt;p&gt;
In part 2, Patty, director of IT service support, begins taking action upon lessons she has picked up from the plant. Kanban boards are used to prioritise changes. The kanban boards allow Patty to visualise the work in progress throughout the organisation and to see how work shifts and does not shift across team boundaries. 
The visualisation allows Patty to identify where work builds up and to strategically change priorities to focus upon  objectives where bottlenecks occur most frequently. As priorities are better aligned with improved visibility across the workflow, the dependency upon Brent is relieved and spread across the teams.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Part 3&lt;/h4&gt;
&lt;p&gt;
&lt;strong&gt;DevOps Way 3: Experiment and fail fast&lt;/strong&gt;&lt;br&gt;
The third way of DevOps requires a culture of continuous experimentation and learning which is fostered through effective support for testing, practice and feedback.
Bill explores the concept of &apos;Takt time&apos;. Takt time is the rate at which a product or feature can be delivered to a customer from its inception. Takt time underpins the agility of Parts Unlimited to adhere to customer demand with their software updates and deliveries. Parts Unlimited set out to achieve the illustrious &apos;10 deploys per day&apos; which requires each team to develop an understanding for where time delays accrue throughout the workflow and how feedback can be retrieved strategically for valuable insight upon deployments.
&lt;/p&gt;
&lt;p&gt;
As the developers review the deployment pipeline, they begin to identify how, step-by-step, it can be automated and optimised from hours to minutes. The developers realise the benefits from an automated pipeline while being encouraged to experiment with new builds that can be tested against prepared environments for feedback. 
&lt;/p&gt;
&lt;p&gt;
The benefits of Cloud technology are later raised as Parts Unlimited experience difficulties with their server capacity for testing. The benefit to the developers are practically demonstrated through the release of a software update that prevented &apos;out of stock&apos; items from being shown on the systems. Further updates including the use of a content delivery network to support greater server usage were considered for enhancing the system thus demonstrating the agile-framed ideas of the team.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Kata&lt;/strong&gt;&lt;br&gt;
Kata is described within The Phoenix Project to describe the lean processing and &apos;ironing out&apos; of inefficiencies throughout the delivery of software. A focus upon kata enables Parts Unlimited to achieve the ten deploys per day. 
The kata of IT at Parts Unlimited demonstrated the presence of IT across the organisation, almost like electricity. The view of IT had shifted from a &apos;cost to reduce&apos; to a source of competitive advantage. Parts Unlimited evolved their change management process, expectation of IT and care upon decisions for the growth and continued improvement of the company as a whole.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The Phoenix Project delivers an exciting introduction to DevOps in the form of a novel that navigates the challenges of change across multiple teams within an organisation. The reality of The Phoenix Project extends to the modern day as businesses change their approach to IT and embrace its potential for achieving a competitive advantage or to even maintain cost competitiveness within the market.
&lt;/p&gt;
&lt;p&gt;
While The Phoenix Project is often described to be an exaggeration of reality, it is highly regarded for capturing the most common pain-points for DevOps to drive change within an organisation.
Beyond the focal point of DevOps, The Phoenix Project taught me about the challenges of IT before services such as Cloud enabled on-demand compute services. The Phoenix Project offers many lessons to the reader such as to understand how DevOps can transform an IT workforce, or to develop an appreciation for how multi-dimensional the challenges are for IT to support across an organisation. The Phoenix Project in this sense has enabled me to learn backwards about the modern history of IT and its transformation since the start of the millennium to foster the continued growth and adoption across the business. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Related reading to The Phoenix Project&lt;/strong&gt;&lt;br&gt;
&lt;a target=&quot;_blank&quot; href=&quot;https://www.bmc.com/blogs/devops-basics-introduction/&quot;&gt;DevOps Guide by Stephen Watts&lt;/a&gt;&lt;br&gt;
&lt;a target=&quot;_blank&quot; href=&quot;https://emilyfreeman.io/book&quot;&gt;DevOps by Emily Freeman&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Service discovery and load balancing with Spring Eureka]]></title><description><![CDATA[Explore the Netflix Eureka discovery service to provide service registration and client-side load-balancing within a distributed system.]]></description><link>https://aneesh.co.uk/service-discovery-and-load-balancing-with-spring-eureka</link><guid isPermaLink="false">https://aneesh.co.uk/service-discovery-and-load-balancing-with-spring-eureka</guid><pubDate>Mon, 07 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the use of Spring Cloud Netflix Eureka for registering, consuming and load-balancing services.&lt;br&gt;
&amp;#8226; Create a Eureka discovery service to register services within a distributed system.&lt;br&gt;
&amp;#8226; Register and consumed microservices through the Eureka service.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Netflix Eureka Registration service&lt;/h4&gt;
&lt;p&gt;
Netflix Eureka is a microservice registration tool developed and open sourced by Netflix. 
Within a microservices environment, microservices will be communicating with each other across different IP addresses and ports thus requiring each service to manage many connection locations that may change with time. 
Service discovery constitutes the process of registering other services within a distributed architecture.
Service discovery supports the horizontal scaling  of services; development teams are able to see the instances/CPU that are available for each service from a single point and are able to scale instances of the service to enhance the capability to consume and process requests. 
Scaling within a monolithic system would have required a vertical scaling across the system to enhance throughput.
&lt;/p&gt;
&lt;p&gt;
The Eureka server is itself a microservice that is used to register and store the location of other microservices within the same system. The services register themselves to the server where they can be identified and located by other microservices when required.
&lt;/p&gt;
&lt;p&gt;
This blog will demonstrate the use of Eureka as a service registry service. The load-balancing feature of the service will be explored as well as the many 
other benefits of Eureka to report upon health information for services and the system.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;What is load-balancing?&lt;/h4&gt;
&lt;p&gt;
Load balancing refers to the efficient distribution of network traffic amongst the available servers, known as a server farm. Within a distributed architecture, individual services are able to communicate with each other, thus constituting network traffic of requests along with external requests made through the system interfaces.
&lt;/p&gt;
&lt;p&gt;
A load-balancer sits in-front of the service instances to monitor the health of the instances and to direct traffic accordingly. If a service instance goes down, the load-balancer will automatically redirect traffic to healthy instances. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAABcSAAAXEgFnn9JSAAAC7ElEQVQoz42TWUxTQRSGTzdoWRNBVpUlgFihpYVauEKBEB4NPhgTF4hCJChEoO2lvbcVJBQsGKMSCmLYTBFXNC7Igw+aIKJBZBEohS5AWR6MRhKWaNRxqD6ZuEzyzUlOzvyZ858ZAAAeholh/Q9cBo7cSNZBfOZtiYg/SUrEJlISNyKXiK4difPCNcCBP6y0iC2AEAI0kM1AY7mAxrPA2lTO1BadAQNdFTBHS79YlAI0rRAgByVCDi2RCUGhfG6PCTG1Dc9YurY3rOqOESaX5wEc5k/RMXU6OCrS4FNNBnzVicCg0sLuYpNbjvxWhJUm9PNq0RW7Smxw0AlNZjUR5byBrmUQLtwwQ13nBFzungMuz92TzQBvrOfaX5riYdPKQt7rMravVcZva1IqI6SlI+FrdJgrFmm1lomeYB7MU/GPLPTeWNAY+rLquqYMeuOEvrZzsrGua/oQFvL183aP2/R3USs97qDEGzNKocOhEqyY6dTHUPTdR002RjhoiW1GKfhmVgjWccufZzWEDKo7RptrO8dRZfMAqjW+Q+e7pnqx0NZQf59YHAOGTgsrzPIYNHgqGk0U89GsWvwCFMgzr9QYYFIn58xriUJMgYUiTg6RqUFQpLu/s7ZrOuOccVKmN5oyqtrHdjXcs8OH+qOsKTIebNSeA3Nq0fgCJepbpITDJirlqlfxqn+96mzwska8ZlXGoBlFDFqihGhBm5jp9LCmYxgu3bXDxdsWwO2Dvu2VcyDL+wFQC0aO0WBInDyBOCmlryNbFYU8C5VI2iipHqOzUok1oypZGASGRHN7pzen/JxV0z7MKm/qd843akcw3v1hKRvAfhgYoEGQWdAdlFlwJ1yc/zQ8T36db6OTemxk7AD2sQ97+NJOJ4n/+g5d2MzfMum4o4/QkLsP1qsJ3qJGMmUvE25YSeHKIi1exe8webPK7dcvYP8LBpPD5nBc2MBwY4f7+3JuHkvw6ysmAvtLiICH+dLAAlkU7wdCszWxT2+fQAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Load balancer image&quot;
        title=&quot;Load balancer image&quot;
        src=&quot;/static/f3017c05ca6adac6db0cd15cb3897ddd/5a46d/033_loadbalancer.png&quot;
        srcset=&quot;/static/f3017c05ca6adac6db0cd15cb3897ddd/1db73/033_loadbalancer.png 75w,
/static/f3017c05ca6adac6db0cd15cb3897ddd/8a4e8/033_loadbalancer.png 150w,
/static/f3017c05ca6adac6db0cd15cb3897ddd/5a46d/033_loadbalancer.png 300w,
/static/f3017c05ca6adac6db0cd15cb3897ddd/fc2a6/033_loadbalancer.png 450w,
/static/f3017c05ca6adac6db0cd15cb3897ddd/0a47e/033_loadbalancer.png 600w,
/static/f3017c05ca6adac6db0cd15cb3897ddd/eb645/033_loadbalancer.png 2500w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Spring Cloud Eureka acts as a load-balancer as well as a discovery service to services within a distributed system.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating the registration server&lt;/h4&gt;
&lt;p&gt;
The Eureka server is first created to maintain the pool of services that consistently send a heartbeat of their existence.
The discovery service acts as a domain name service or a load balancer as instance health is assessed and requests are routed as appropriate by the registration service for the consumers of the service.
&lt;/p&gt;
&lt;p&gt;
To create the registration service, you must include the &lt;code&gt;spring-cloud-starter-eureka-server&lt;/code&gt; dependency to add support for the Eureka server along with &lt;a href=&quot;https://spring.io/guides/gs/client-side-load-balancing/&quot; target=&quot;_blank&quot;&gt;Netflix Ribbon&lt;/a&gt;, a client-side load balancer. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
	&amp;lt;groupId&gt;org.springframework.cloud&amp;lt;/groupId&gt;
	&amp;lt;artifactId&gt;spring-cloud-starter-netflix-eureka-server&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The main class of the new Spring project will be annotated with &lt;code&gt;@EnableEurekaServer&lt;/code&gt;. The annotation instructs the service to act as a Eureka server:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@EnableEurekaServer&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DiscoveryServerApplication&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;SpringApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;DiscoveryServerApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
In the application properties file, the server is configured to a port, and boolean values for registration instructions:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;server.port=9001
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;server.port&lt;/code&gt; specifies the port the Eureka service is located. By default, it is port 8761.&lt;br&gt;
The &lt;code&gt;fetch-registry&lt;/code&gt; will instruct the server not to store the services within its cache. We will look into this with a bit more detail further in the blog.&lt;br&gt;
The &lt;code&gt;register-with-eureka&lt;/code&gt; property will instruct the server to not register itself with itself. By default, you will find the server as the only service registered as it will otherwise register itself as a service.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
If we navigate to localhost:9001, we can find the server without any instances registered to it:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 43.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABqUlEQVQoz12S107jUBCGzyNRQltaKOIGCS2Ii9VKvBMlRCGhBLhaylsAuzdAjIhjI5BSHKIUO9UJKebfmYNsAhef/pl/xnOOPRbjvkGsr65g4/cvrP1chn9hCNOzAxj3DWFi5BPuY491cswH/9QE5qYnsTQ/i0X/DH6MDsua+Pf3BtdXV7i7vYWWSEBV45KEqkKNxz049zyqc6+mJaBrmlS3T2QNA8ViEbVaDY1Gg7Cllq2ylzebTViWiXL5w7NtG9VqFYVCAWapRDVLeowo0bBKpSIb+AGOWfkQVoZrLy/P0HUdudyr7Mnn8xTnYJkmTIKHMoJP4aJ7AzbdQS79A/oPZf2OyOYMunoe3W4Xzvs7HMdBr9fztNPpoN1uU71HuSP7+uE648bi6UmXH/lBUaDEYri/v0P88RGKEqNX1JDNGvJmRiaDdCqFdLoPytk3aA8cZygWF2d/ENjexO7OFkLBAIKBbRxEwtgPh2R+Ej3C5fmZVPYOInvYJ1gjlB9HD3F6fIRwKEgahUgmk0gRvOVW641oya268Fbr9bq33a9Q3f74K2zqZe8/igJmVpfn8tsAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Empty Eureka server&quot;
        title=&quot;Empty Eureka server&quot;
        src=&quot;/static/50ce7f61869ade752ed1ac209d5c26b1/5a46d/033_eureka01.png&quot;
        srcset=&quot;/static/50ce7f61869ade752ed1ac209d5c26b1/1db73/033_eureka01.png 75w,
/static/50ce7f61869ade752ed1ac209d5c26b1/8a4e8/033_eureka01.png 150w,
/static/50ce7f61869ade752ed1ac209d5c26b1/5a46d/033_eureka01.png 300w,
/static/50ce7f61869ade752ed1ac209d5c26b1/fc2a6/033_eureka01.png 450w,
/static/50ce7f61869ade752ed1ac209d5c26b1/0a47e/033_eureka01.png 600w,
/static/50ce7f61869ade752ed1ac209d5c26b1/eb645/033_eureka01.png 2500w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The &apos;Environment&apos; at the top of the page is defaulted to &apos;Test&apos;, however can be overridden within the application properties file with &lt;code&gt;eureka.environment&lt;/code&gt; key. The section at the top right will show the uptime of the server and the renews threshold and renews. The threshold defines the number of expected heart beats it expects per minute. 
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABH0lEQVQoz42QWU/CUBCF7883xkRkESMuiQFKUcGo7+pLi/sPkMRWaLHpRpeU9unI3NqKpSY+fJkz58xMb8qury5xfNiC0GmjLwro934Qe12c9kUMz8+4LuY5S18UurgYDsBeX55xd3uD+5HMGa3Ae1mCLEmpJ//Oi3NPjw9ghmHANE1EUYQ4jtcgfz6fc50kSelMxmKxANOmUyjKOz9Ki67r5lBv2zZUVYVhfPKZ1bwMpus6VEXhy77vw/M8Xn0/reTrmobJ5AOzmf6dlUO7jI6Nx2+gw/Qay7JyqHccO622vZYXoZx12ifY2txAtbKNZqOG3XqV06jtYH+viXq1wnXm/wXNHB20wKzlfwmCAGEY8meTzqrjOPyrpP8D7X0BoY0xQHRiESwAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Empty Eureka server&quot;
        title=&quot;Empty Eureka server&quot;
        src=&quot;/static/a5a83294a7cb906a685b98f52448b273/5a46d/033_eureka02.png&quot;
        srcset=&quot;/static/a5a83294a7cb906a685b98f52448b273/1db73/033_eureka02.png 75w,
/static/a5a83294a7cb906a685b98f52448b273/8a4e8/033_eureka02.png 150w,
/static/a5a83294a7cb906a685b98f52448b273/5a46d/033_eureka02.png 300w,
/static/a5a83294a7cb906a685b98f52448b273/fc2a6/033_eureka02.png 450w,
/static/a5a83294a7cb906a685b98f52448b273/0a47e/033_eureka02.png 600w,
/static/a5a83294a7cb906a685b98f52448b273/eb645/033_eureka02.png 2500w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the second half, information including the available memory and CPUs are also shown with the memory usage and percentage for the registered services.
As we add instances to the Eureka server, we can review how the general information updates. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Configuring a Discovery client&lt;/h4&gt;
&lt;p&gt;
I will use two services to demonstrate how the Eureka server can be used between microservices to consume each other.
The demonstration will include a HR-Service which represents a HR department, and a Phone-book service, which includes a service that returns user names and 
their phone number. 
The HR-Service will consume the phone numbers from the Phone-book service to retrieve contact information. 
&lt;/p&gt;
&lt;p&gt;
Both clients to the Eureka server will register themselves with the help of the &lt;code&gt;spring-cloud-starter-eureka&lt;/code&gt; dependency. The dependency also comes with further properties that can be configured for custom behaviours. The Spring-web dependency is also required for the REST service provided by the client. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
	&amp;lt;groupId&gt;org.springframework.cloud&amp;lt;/groupId&gt;
	&amp;lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;
&amp;lt;dependency&gt;
	&amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
	&amp;lt;artifactId&gt;spring-boot-starter-web&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Adding &lt;code&gt;@EnableDiscoveryClient&lt;/code&gt; can be used to activate the discovery client to the server.
This is a marker annotation for others to know it is a Eureka client. We do not necessarily have to use it. 
Alternatively, you can use @EnableEurekaClient for a direct implementation of discovery server. &lt;code&gt;@EnableDiscoveryClient&lt;/code&gt; will instead select the implementation (Consul, ZooKeeper, Eureka) from the classpath and assign it.
&lt;/p&gt;
&lt;p&gt;
The properties file of the Phone-book service will be configured accordingly with the Eureka server:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;server.port=9002
spring.application.name=phone-book
eureka.client.serviceUrl.defaultZone= http://localhost:9001/eureka/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;application.name&lt;/code&gt; defines the name of the service to be used as a key within the Eureka server.
The &lt;code&gt;serviceUrl.defaultZone&lt;/code&gt; will instruct where the Phone-book service will try to register itself with a Eureka Server.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Both services will include a model class called &apos;Contact&apos; to store a basic Contact Object with name and number. The Phone-book will later create a list of the Contact Objects and the HR-Service will consume them over a HTTP call.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;number &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Within the Phone-book service, the controller class will be used to create and return a list of Contacts:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@RestController&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PhoneController&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	
    &lt;span class=&quot;token annotation punctuation&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/contacts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ResponseEntity&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getContacts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    	
	 &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; contactList &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		contactList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		contactList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;456&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		contactList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;789&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        	
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ResponseEntity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;contactList&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above &lt;code&gt;getContacts()&lt;/code&gt; method simulates a call to a data store.
The &lt;code&gt;ResponseEntity&lt;/code&gt; return type of the controller defines a HTTP response that can be consumed and transformed by another service.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
If we navigate to the &apos;/contacts&apos; endpoint from the Phone-book service, we can retrieve the list of Contact instances:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 61.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAABaklEQVQoz51SW07DMBD00bgP5+ACHIMT0BvwzweffDRRAk2c+P1Mht2goqoKUlVLI9ur3fHMekXf95BSYpomdH2HpmnQti2aY4Ou67Y4Y57nv/MvZqhZ4vil8Phi8PA84enVQVjnEEKAtRbGGATvobWG9W6LebrzzjFHuXxncG6kuhAL3j4LDh8Z712F0OMJMcYtWWmFXDOUUnDRo9aKdV2QS6HCiJwzCp05zoTaE2mNABgZKBZiaI8YhnGzMQwDRjliHAkUc87SIxrBKFR64HKt60qPlw2xMCpqiqSQ7DDZZo8ssFIG39k+tyQFvyWfiRj/LVGXBYWt0R7JanDUo5R3ky+JzsTXEJH6wj3i/kT+lBBhCXvJ16S7ChORFfLPzXajhFYahqxfq7h1iZASEv8eK/U0BiGR4nqTml1CR/a4h5UUmu8T9KwgtcGyrHeRCkfzxTPGs1VIbc40c6ncT7jwL5O6vcJ7LP8A1mmp1hMZkmAAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Contacts endpoint result&quot;
        title=&quot;Contacts endpoint result&quot;
        src=&quot;/static/3622262ebc051fa10918b0218a399e8d/5a46d/033_postmanGet1.png&quot;
        srcset=&quot;/static/3622262ebc051fa10918b0218a399e8d/1db73/033_postmanGet1.png 75w,
/static/3622262ebc051fa10918b0218a399e8d/8a4e8/033_postmanGet1.png 150w,
/static/3622262ebc051fa10918b0218a399e8d/5a46d/033_postmanGet1.png 300w,
/static/3622262ebc051fa10918b0218a399e8d/fc2a6/033_postmanGet1.png 450w,
/static/3622262ebc051fa10918b0218a399e8d/0a47e/033_postmanGet1.png 600w,
/static/3622262ebc051fa10918b0218a399e8d/aa440/033_postmanGet1.png 1500w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
We can also check the Eureka service to confirm the Phone-book service is registered.
The Eureka service will typically take 30 seconds to show the new service registered as it requires 3 consistent heartbeats from a service for it to be registered.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 10.666666666666668%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAe0lEQVQI1z2N2w6CMBBE+f9HSYyN0Ha3W1pBfZJSvHzbuGDiw+RsZjY5zXXMSIMgBgK5HuwdhL3eVjtGThFlfmCtBXWZNQWf9wvPWrVblL9s/bY34yXB9R0COQwS9mQVCBMmla36eL9NIG9VZndGYZy7I07mAGNaZfvfv9obdrPmYnMeAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Phone book registered within server&quot;
        title=&quot;Phone book registered within server&quot;
        src=&quot;/static/f23f5de050582b0fc246f5bcbf3a9bb0/5a46d/033_phoneBookInServer.png&quot;
        srcset=&quot;/static/f23f5de050582b0fc246f5bcbf3a9bb0/1db73/033_phoneBookInServer.png 75w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/8a4e8/033_phoneBookInServer.png 150w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/5a46d/033_phoneBookInServer.png 300w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/fc2a6/033_phoneBookInServer.png 450w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/0a47e/033_phoneBookInServer.png 600w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/e8950/033_phoneBookInServer.png 2000w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The number inside the brackets will tell you how many instances of a service with the same name that are running.
If we add more services, the number will increase and the status will show the address of each of the services:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 10.666666666666668%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAe0lEQVQI1z2N2w6CMBBE+f9HSYyN0Ha3W1pBfZJSvHzbuGDiw+RsZjY5zXXMSIMgBgK5HuwdhL3eVjtGThFlfmCtBXWZNQWf9wvPWrVblL9s/bY34yXB9R0COQwS9mQVCBMmla36eL9NIG9VZndGYZy7I07mAGNaZfvfv9obdrPmYnMeAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Phone book registered within server&quot;
        title=&quot;Phone book registered within server&quot;
        src=&quot;/static/f23f5de050582b0fc246f5bcbf3a9bb0/5a46d/033_phoneBooksInServer.png&quot;
        srcset=&quot;/static/f23f5de050582b0fc246f5bcbf3a9bb0/1db73/033_phoneBooksInServer.png 75w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/8a4e8/033_phoneBooksInServer.png 150w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/5a46d/033_phoneBooksInServer.png 300w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/fc2a6/033_phoneBooksInServer.png 450w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/0a47e/033_phoneBooksInServer.png 600w,
/static/f23f5de050582b0fc246f5bcbf3a9bb0/e8950/033_phoneBooksInServer.png 2000w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now we can consume and test load-balancing to the instances!&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Consuming the Phone-book service&lt;/h4&gt;
&lt;p&gt;
The HR-Service is going to be built out to consume the Phone-book service and to obtain the appropriate Contact instances.
The HR-Service will use the &lt;code&gt;RestTemplate&lt;/code&gt; class to consume the HTTP body of the Phone-book &apos;contacts&apos; endpoint. The RestTemplate is a high-level API to the Spring framework that is used for executing synchronous HTTP requests. The RestTemplate will be defined as a Bean within the main class to be later injected:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getRestTemplate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The properties file will look similar to the Phone-book service, with the addition of &lt;code&gt;eureka.client.fetch-registry&lt;/code&gt;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;numberlines:true&quot;&gt;&lt;pre class=&quot;language-numberlines:true&quot;&gt;&lt;code class=&quot;language-numberlines:true&quot;&gt;server.port=9003
eureka.client.fetchRegistry=true
spring.application.name=hr-service
eureka.client.serviceUrl.defaultZone= http://localhost:9001/eureka/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The fetch-registry property will instruct the service to obtain a list of all services registered to the Eureka server.
The services will be stored within the client cache and is updated every 30 seconds.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The HR-Service controller will use the RestTemplate to make a HTTP call to one of the instances of the Phone-book service with the &apos;contacts&apos; endpoint:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@RestController&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HrController&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
	&lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt; restTemplate&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token annotation punctuation&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/request-contacts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;contacts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	        &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; restTemplate&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getForObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;http://localhost:9002/contacts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
So far, this request from the controller doesn&apos;t actually need the Eureka server. What happens if port 9002 goes down or is changed? How can we better utilise the available instances of the Phone-book service? 
In the next section, the Eureka service is used to perform load-balancing and dynamic access to the services.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Enabling microservices to communicate through Eureka&lt;/h4&gt;
&lt;p&gt;
The Eureka server allows our services to communicate without explicitly knowing their location (including port). With the use of the service name as a key, Eureka is able to consolidate all running instances of a server for load-balancing, and also empowers the clients of the server to refer each other upon their name, rather than address. 
As a result, clients are able to reference each other dynamically, and requests for service consumption can be load-balanced altogether.
&lt;/p&gt;
&lt;p&gt;
The Spring application name, defined the properties of each service, is used as a key within the Eureka server. Before we can allow the RestTemplate to take 
advantage of the load-balancing provided by Eureka, we must annotate it with &lt;code&gt;@LoadBalanced&lt;/code&gt;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@LoadBalanced&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getRestTemplate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We use the &lt;code&gt;@LoadBalanced&lt;/code&gt; annotation to tell Spring to create a Ribbon-backed RestTemplate. The Ribbon slightly changes the way we interact with the service as Spring no longer expects the physical location of the service to be provided, instead it expects the Eureka service ID (Application name) to be called.
Eureka Ribbon will periodically refresh its cache of IP addresses from the Eureka service of service names in an eventually consistent manner every 30 seconds.
The Ribbon-enabled RestTemplate parses the URL as the server name key to query Ribbon for a service instance.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
If we were to call the existing HR-Service request from the Controller, it will now search for a service within its cache under the name of &apos;localhost:9002&apos;. The service will throw an error when we try to access the service name. The advantage is that the service will no longer request the exact port of the request, and can now have the request load-balanced to one of the various service instances.
&lt;/p&gt;
&lt;p&gt;
The HR-Service controller is now updated to call the Phone-book service name as the key to the Eureka server:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;	&lt;span class=&quot;token annotation punctuation&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/request-contacts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;contacts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	        &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; restTemplate&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getForObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;http://phone-book/contacts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
If we make a call, the Eureka server will provide load-balancing for us and return the contacts in JSON as expected from one of the available instances.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
When I first started using Spring Cloud Eureka, I had thought of it as a convenient naming resource that allowed me to reference different services by name, rather than by location.
This blog has allowed me to realise the potential of the discovery server for more than just service registration.
&lt;p&gt;
&lt;p&gt;
The Eureka server can provide useful insight to the health of the many instances running for each service within a distributed system. From the server page, we can see the life of services via the expected and received heartbeats, the capacity of RAM in the system, and also the live update of services as they come up and are brought down.
&lt;/p&gt;
&lt;p&gt;
Spring Cloud Eureka provides load-balancing as a service to the system to support the horizontal scaling of the services as they are required.
The Eureka package has also made it intuitive and simple with bootstrapped annotations to implement the discovery server and to also use the discovery server with auto-configured service discovery implementations and configured properties.&lt;br&gt;
You can find the source code from this blog on GitHub &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/Eureka&quot;&gt;here&lt;/a&gt; where you launch your own Eureka server to open and consume services through a load-balanced server.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Centralising configuration within a distributed application]]></title><description><![CDATA[Create and use a centralised configuration server for a microservices system with Spring Cloud Config.]]></description><link>https://aneesh.co.uk/centralising-configuration-within-a-distributed-application</link><guid isPermaLink="false">https://aneesh.co.uk/centralising-configuration-within-a-distributed-application</guid><pubDate>Mon, 23 Nov 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Use Spring Cloud Config to create an externalised configuration server in a distributed system.&lt;br&gt;
&amp;#8226; Understand how the Config server reads in properties across a filesystem.&lt;br&gt;
&amp;#8226; Implement a client server to consume the Config server.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Centralised configuration&lt;/h4&gt;
&lt;p&gt;
In a distributed system, the configuration of the services are often defined at a local level where they are individually updated and maintained by the responsible team.
The Spring Cloud framework offers the Config server solution to act as a single-stop solution for managing all configurations to the services within a system. The centralised configuration 
server will provide transparency and versioning of configurations that can impact multiple or single services at a time.
&lt;/p&gt;
&lt;p&gt;
We use configurations to represent all the changes that might take place between deployments. For example, configurations could include login credentials, connections to databases, hostnames and also logging levels. Configurations for services within a Spring Boot application are often defined within the &lt;code&gt;application.properties&lt;/code&gt; file or a similar yml file within the resources directory.
With Spring Cloud Config, the distributed services can inject their configurations by scanning the Config server before they are instantiated.
&lt;/p&gt;
&lt;p&gt;
A centralised configuration server offers further benefits such as risk reduction and dependency on local configuration that can otherwise fall out of sync with the other services.
Spring Cloud Config enables environment variables to be tracked centrally, where services can even obtain updated properties at runtime without having to restart.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating a Config Server&lt;/h4&gt;
&lt;p&gt;
The Config server can use many different medium for storing the configuration files including JDBC, Redis, or AWS S3. The default storage is Git and will be used alongside GitHub in this blog.
&lt;/p&gt;
&lt;p&gt;
When a service is subscribed to the config server, they will be able to use the server as a REST service to obtain their configuration values.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 53.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAChUlEQVQoz5WRbUhTURjHnzsN063MnGUpRH0LobBIgpJK1PBbWhL0oY99SSIojCgoNGW50iRfQper6V3u3k036W6ubb7dWb7k1hR1ClqZvW0SlGU6lqfnXET8lh34nT/nPg8/znku8L1sWvtI68nnXmOm1WfKahvisuyj5kzhjSm7pP5OEgBExCnjNmJGUaKi5TQjCSGwloJbjcAwsgwQvIaXpt4GYhloDJv7ni4LHv0y16P5Y+7Xke5pW15R9Q1Yu7LzL8N9fma3Wj/x5F7zpLacm9KUseOmOvt8JpYPgM3HiYauOsI6aha1QmXIKD4OGbrrllBKWJfmPDZtS0iMT8ZMVMQq6Y3jCyuc2Wp2jFwrt5Erdy2kVOshtcLcTaylgPutMDr42U7wdoSKKe7pNtL3wUYc45ZzVBAjj87ATI3cEHUEc2uxdvigWu//pNKNfCzRet+XsWOLVW1fL2BtHz6ZL7AN8w9ejLaoXH6LqmNCotTpN6tdE9YU94wTlNvjY7F5C8MwmzEZnBlzW+NVPGqfV+jcRFFY0RG7MpGjdEtAdiE7EFpQjIe7YfCbFYZ/i9LA5ZtipG4UStkZJFDU4IMK0yw8tASknoSde2gpnW5yZC9CvyiRZLKwCIGqGlhu4kB/8dLqD5HJIqSsFr6DqmkMyli/xP7DObKVlnRwNJSnaoqvntKprucMcrUnxMbK41N29tBsJ5fmtz2Lm7Q3A+Ufi1kV/uxvEcMeMwkNtS5MW+tDwa6m0NLrltCvfiMJiPzpOTcPSMS6hT9eGT0LA0ZCBX5LLfnSoSMolM6BHu7sfwsDovFYUOTygyKfG/ZZ8+YHzLkoysXzmXdOQ9KMywAIs17hX17jW1I329xPAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Spring Config Server diagram from client to git&quot;
        title=&quot;Spring Config Server diagram from client to git&quot;
        src=&quot;/static/dab4be9b3f09dd6175050881140b6302/5a46d/032_configDiagram.png&quot;
        srcset=&quot;/static/dab4be9b3f09dd6175050881140b6302/1db73/032_configDiagram.png 75w,
/static/dab4be9b3f09dd6175050881140b6302/8a4e8/032_configDiagram.png 150w,
/static/dab4be9b3f09dd6175050881140b6302/5a46d/032_configDiagram.png 300w,
/static/dab4be9b3f09dd6175050881140b6302/fc2a6/032_configDiagram.png 450w,
/static/dab4be9b3f09dd6175050881140b6302/0a47e/032_configDiagram.png 600w,
/static/dab4be9b3f09dd6175050881140b6302/59822/032_configDiagram.png 916w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The Config server uses the Spring Cloud Config Server dependency and Spring Web dependency together. The Web dependency provides the REST functionality of the server once all the 
configuration files are read and stored:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
  &amp;lt;groupId&gt;org.springframework.cloud&amp;lt;/groupId&gt;
  &amp;lt;artifactId&gt;spring-cloud-config-server&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;

&amp;lt;dependency&gt;
  &amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
  &amp;lt;artifactId&gt;spring-boot-starter-web&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The config server must be configured to know where to read the properties files from. The configuration is applied in the &lt;code&gt;application.properties&lt;/code&gt; file:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;server.port= 8888
spring.cloud.config.server.git.uri = https://github.com/4neesh/DeveloperBlogDemos
spring.cloud.config.server.git.searchPaths= SpringConfig/Config-files*, SpringConfig/Config-files/prod*, SpringConfig/Config-files/dev*&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The first line will direct the Config server onto port 8888.&lt;br&gt;
Line 2 will specify where the server will look for the Git configuration files. The value for line 2 will be the home directory of the Git repository (where the .git file is located).&lt;br&gt;
Line 3 defines the search paths to find the appropriate properties files from within the Git repository.
The search path will first enter the SpringConfig folder, followed by scanning all files within the Config-files sub-directory, and finally the sub-directory of &apos;prod&apos; and &apos;dev&apos; within Config-files.
The Config server will register all .properties files found within the search path.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The server is activated through a single annotation in the main class &lt;code&gt;@EnableConfigServer&lt;/code&gt;. The annotation will instruct the server to scan the search paths defined and to 
define the endpoints for reading the configuration files. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@EnableConfigServer&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ConfigServerApplication&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;SpringApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ConfigServerApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The following list contains all the .properties files that are obtained by the config server. 
I have manually created these files and placed them within the folders at the Git &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/SpringConfig/Config-files&quot; target=&quot;_blank&quot;&gt;repository&lt;/a&gt; to allow the server to build its key-value pairs of properties:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;- /SpringConfig/Config-files/application-prod.properties
- /SpringConfig/Config-files/application-dev.properties
- /SpringConfig/Config-files/application-default.properties
- /SpringConfig/Config-files/prod/drinks-prod.properties
- /SpringConfig/Config-files/prod/food-prod.properties
- /SpringConfig/Config-files/dev/drinks-dev.properties
- /SpringConfig/Config-files/dev/food-dev.properties&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The below file directory structure is used:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;├── SpringConfig
│   └── Config-files
│         ├── application-prod.properties
│         ├── application-dev.properties
│         ├── application-default.properties
│         └── prod
│         │     ├── drinks-prod.properties
│         │     └── food-prod.properties
│         └── dev
│               ├── drinks-dev.properties
│               └── food-dev.properties&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
When the config server has started, you will be able to send GET requests to the 8888 port endpoints to obtain the relevant value(s). In the next section, we will look at the exact endpoints that are generated by the config server and how the values returned are determined.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Navigating the Config server&lt;/h4&gt;
&lt;p&gt;
When the Config server is launched, it will use the Spring-web starter to expose endpoints that reflect the properties files which it finds from the saerch paths. 
A GET request can be made to endpoints using the following name convention:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;localhost:8000/{application name}/{active profile}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Each property file is conveniently named to address both the &lt;code&gt;application name&lt;/code&gt; and the &lt;code&gt;active profile&lt;/code&gt;. For example, &apos;drink-prod.properties&apos; will have the application name of &apos;drink&apos; and the active profile of &apos;prod&apos;. The same goes for all other files. If there is no application name specified, it will be defaulted to &apos;application&apos; and if there is no profile specified, it will be read as &apos;default&apos;. Therefore all services that use the config server will obtain the values from the &apos;application-default.properties&apos; file (if it exists). We are able to define the application name and profile of each service within the application.properties file. The service will then know which properties file to query in the Config server.  
&lt;/p&gt;
&lt;p&gt;
You can see the config files on GitHub &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/SpringConfig/Config-files&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. They contain values for a database name, user name and password. The database name will reflect the application name and the profile and the user name and password will either be a service account credential or simple value. They are used for demonstration in this blog, however as we will later visit, they can be made to represent real configuration values. 
&lt;/p&gt;
&lt;p&gt;
The following endpoints are exposed from the config server to enable transparency into the values obtained by the services:&lt;br&gt;
/{application}/{profile}&lt;br&gt;
/{application}-{profile}.properties&lt;br&gt;
The first endpoint will return all values that fall under the application and profile together, and also the respective default values as per the application-prod/dev/default.properties files.
&lt;/p&gt;
&lt;p&gt;
The below examples demonstrate how the properties returned from the config server follow a hierarchy of matching from the defined application name and profile:
&lt;p&gt;localhost:8888/food/prod returns:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;{
    &quot;name&quot;: &quot;food&quot;,
    &quot;profiles&quot;: [
        &quot;prod&quot;
    ],
    &quot;label&quot;: null,
    &quot;version&quot;: &quot;f6d1b8edc6e75c5586e369188e6bf6284120f46c&quot;,
    &quot;state&quot;: null,
    &quot;propertySources&quot;: [
        {
            &quot;name&quot;: &quot;https://github.com/4neesh/DeveloperBlogDemos/SpringConfig/Config-files/prod/food-prod.properties&quot;,
            &quot;source&quot;: {
                &quot;Database&quot;: &quot;mysql-food-prod&quot;,
                &quot;User&quot;: &quot;serviceAccount&quot;,
                &quot;Password&quot;: &quot;serviceAccount&quot;
            }
        },
        {
            &quot;name&quot;: &quot;https://github.com/4neesh/DeveloperBlogDemos/SpringConfig/Config-files/application-prod.properties&quot;,
            &quot;source&quot;: {
                &quot;Database&quot;: &quot;mysql-application-prod&quot;,
                &quot;User&quot;: &quot;serviceAccount&quot;,
                &quot;Password&quot;: &quot;serviceAccount&quot;,
                &quot;Environment&quot;: &quot;PROD&quot;
            }
        }
    ]
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The first tags returns the application name and the profile of the request.
The properties from the files on the Config server are presented under &apos;propertySources&apos;. The first set of values come from the food-prod properties file and the second set of values are from the application-prod properties file. The config server has returned the properties files that are appropriate to the application name and the profile of prod first, followed by the default application-prod properties second. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Another example to demonstrate the config server: localhost:8000/drink/dev:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt; &quot;propertySources&quot;: [
        {
            &quot;name&quot;: &quot;https://github.com/4neesh/DeveloperBlogDemos/SpringConfig/Config-files/dev/drink-dev.properties&quot;,
            &quot;source&quot;: {
                &quot;Database&quot;: &quot;mysql-drink-dev&quot;,
                &quot;User&quot;: &quot;user&quot;,
                &quot;Password&quot;: &quot;password&quot;
            }
        },
        {
            &quot;name&quot;: &quot;https://github.com/4neesh/DeveloperBlogDemos/SpringConfig/Config-files/application-dev.properties&quot;,
            &quot;source&quot;: {
                &quot;Database&quot;: &quot;mysql-application-dev&quot;,
                &quot;User&quot;: &quot;user&quot;,
                &quot;Password&quot;: &quot;password&quot;,
                &quot;Environment&quot;: &quot;DEV&quot;
            }
        }
    ]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
If we request an unknown application name with a &apos;dev&apos; profile, we will receive the application-dev.properties file:
&lt;p&gt;localhost:8080/cook/dev:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&quot;propertySources&quot;: [
        {
            &quot;name&quot;: &quot;https://github.com/4neesh/DeveloperBlogDemos/SpringConfig/Config-files/application-dev.properties&quot;,
            &quot;source&quot;: {
                &quot;Database&quot;: &quot;mysql-application-dev&quot;,
                &quot;User&quot;: &quot;user&quot;,
                &quot;Password&quot;: &quot;password&quot;,
                &quot;Environment&quot;: &quot;DEV&quot;
            }
        }
    ]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Consuming the configuration server&lt;/h4&gt;
&lt;p&gt;
We have seen how the config server will return properties as a REST service and how the properties files are returned upon reflection of the application name, profile and defaulted properties files. A client can also perform GET requests to the service to obtain the configuration to be injected at instantiation. 
&lt;/p&gt;
&lt;p&gt;
The client service will require the Web-starter dependency to make the GET request and Config-client dependency to register the Config server:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
  &amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
  &amp;lt;artifactId&gt;spring-boot-starter-web&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;

&amp;lt;dependency&gt;
  &amp;lt;groupId&gt;org.springframework.cloud&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;spring-cloud-starter-config&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The client will need to load the Config server properties &lt;u&gt;before&lt;/u&gt; the application uses its &lt;u&gt;local&lt;/u&gt; properties by defining the Config server in the &lt;strong&gt;bootstrap.properties&lt;/strong&gt; file.
The bootstrap properties file is loaded with precedence before the application has started to override any of the local configurations by default. The bootstrap.properties file is therefore used to 
load the configuration files from the server to provide configuration to the service. 
&lt;/p&gt;
&lt;p&gt;
The bootstrap.properties file will be defined as below to register the config uri, application name and profile:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;cloud.config.uri=http://localhost:8888
spring.application.name=drink
spring.profiles.active=prod&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;cloud.config.uri&lt;/code&gt; will point to the location of the config server.
The &lt;code&gt;spring.application.name&lt;/code&gt; value will determine the application name for which to obtain the properties file for. This property is defaulted to &apos;application&apos;.
The&lt;code&gt; spring.profiles.active&lt;/code&gt; property will tell the application which profile it will assert for the config property search. This property is defaulted to &apos;default&apos;. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
If you check the console for when the application is started, you will see the following line to tell you the config files are being loaded:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Fetching config from server at : http://localhost:8888&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When the Spring client has loaded, the config properties from the localhost:8888/drink/prod will be loaded into the application.
The following values are pre-loaded into the application:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Database=mysql-drink-prod
User=serviceAccount
Password=serviceAccount
Environment=PROD&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;Environment&lt;/code&gt; property has been taken from the application-prod.properties file which is read second to the drink-prod.properties file. As the drink-prod file did not contain a value for the
key of &apos;Environment&apos;, the value is populated from the application-prod file. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using the config values within the client&lt;/h4&gt;
&lt;p&gt;
The client can use a RestController to obtain the values from the config server:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@RestController&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ClientController&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;token annotation punctuation&quot;&gt;@Value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;${Database}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token annotation punctuation&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/database&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getHome&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; database&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;@Value&lt;/code&gt; annotation will obtain the value for the property of &apos;Database&apos; from the configuration file. 
The endpoint of &apos;/database&apos; will return the value as a String to the user. 
Going to the endpoint of &apos;/database&apos; will return &apos;mysql-drink-prod&apos;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Obtaining client properties from the Config Server&lt;/h4&gt;
&lt;p&gt;
You may remember that we are importing the config files from the server within the bootstrap.properties file which is initialised before the service iteself.
As a result, the config server will theoretically populate items into the application.properties file. 
To test this, we can update the drink-prod.properties file from the config server to include a further property that is then pushed to the GitHub repository:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;server.port=7777&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The new property for &lt;code&gt;server.port&lt;/code&gt; will instruct the client to be hosted on the 7777 port. The property would only ever work for a single client as only one client can be registered to port 7777.
Further requests to the port from different services will return an error that the port is unavailable.
If we were to restart the client, we can see it will start on port 7777 from the Spring console output:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Tomcat initialized with port(s): 7777 (http)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By defining the server port through the config server, we have illustrated how the bootstrap.properties file is loaded before the application starts, and how the
application name and profile read from the config server is applied.
The config server can be taken much further by configuring many other static properties across services such as discovery server locations, JDBC drivers, database locations and non-production credentials. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Obtaining updated properties from the Config server&lt;/h4&gt;
&lt;p&gt;
The current client and Config server work in unison where the client pulls in the config server properties upon instantiation. The properties are read in statically and used for the life of the service. 
But what happens if we change a property within the config server? The client will not be able to reflect the update as it&apos;s properties are static. Instead, we will need to restart the client to obtain the latest information from the config server. 
&lt;/p&gt;
&lt;p&gt;
Thankfully, there is an easier way to obtain the latest updates, by using the &lt;code&gt;@RefreshScope&lt;/code&gt; annotation from the Spring Cloud Context package. 
In the RestController, I will add the @RefreshScope annotation to instruct the controller to refresh its beans upon a client refresh. 
We are able to refresh the client by invoking the &apos;refresh&apos; endpoint from the Spring actuator with the below dependency:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
  &amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
  &amp;lt;artifactId&gt;spring-boot-starter-actuator&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By default, not all actuator endpoints are accessible. Therefore in the application.properties file for the client, we are to add the following line to enable the &apos;/refresh&apos; endpoint.
Alternatively, you can add the below line to the appropriate file in the Config server.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;management.endpoints.web.exposure.include=refresh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The RefreshScope will be tested by updating the property of &apos;user&apos; in the drink-prod.properties file:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Database=mysql-drink-prod
User=serviceAccount2.0
Password=serviceAccount
server.port=7777&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The property update is pushed to the Git repository and the client RefreshScope is activated by sending an empty POST request to the refresh endpoint: localhost:7777/actuator/refresh using Postman.
Once the actuator endpoint is called, the RefreshScope will renew its beans to show the updated configuration file reflected in the &apos;/username&apos; endpoint.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
So what does RefreshScope actually do?&lt;br&gt;
The RefreshScope annotation will mark the Controller class as &apos;lazy&apos; where they are initialised when they are &lt;i&gt;used&lt;/i&gt; rather than at the application initialisation. 
The scope of the class will act a cache for storing the initialised values. As a result, the bean can be re-initialised as and when required. 
It is important to consider which other beans are within the scope of the class as they will also be re-initialised when the actuator refresh endpoint is called. 
This consideration can have an impact on the efficiency of the refresh and the side-effect upon beans that are refreshed. 
&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The Spring Cloud Config server is used to act as a REST service for obtaining service configuration at runtime. 
The centralised configuration offers a transparent and controlled service to all clients within the microservice system. 
The Config server is configured to scan a file path to obtain the properties files and uses a hierarchy of application name and active profile values to define configurations to be passed in.
Each client within the system registers its configuration to the server with the bootstrap.properties file and is able to obtain live updates of the configuration through the RefreshScope annotation.
&lt;/p&gt;
&lt;p&gt;
You can get started with centralised configuration from with the source code from this blog on GitHub &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/SpringConfig&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The benefits of using Spring Boot]]></title><description><![CDATA[Understand how Spring Boot emerged as a solution to the challenges that originated from the Spring Framework ]]></description><link>https://aneesh.co.uk/the-benefits-of-using-spring-boot</link><guid isPermaLink="false">https://aneesh.co.uk/the-benefits-of-using-spring-boot</guid><pubDate>Mon, 09 Nov 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the popularity of the Spring framework to developers.&lt;br&gt;
&amp;#8226; Review the core IOC containers provided with Spring.&lt;br&gt;
&amp;#8226; Understand how Spring Boot Autoconfiguration enables convention over configuration.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;The Spring framework&lt;/h4&gt;
&lt;p&gt;
&lt;a href=&quot;https://spring.io/&quot; target=&quot;_blank&quot;&gt;Spring&lt;/a&gt; is an open source application framework used with Java. 
Spring offers comprehensive support to the developer for building applications with in-built support for testing, data access, web frameworks and messaging support. 
The popularity of Spring, which is over 18 years old, has been realised as a from its light-weight design and features that include inversion of control (IOC).
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Key features of the Spring framework&lt;/h4&gt;
&lt;p&gt;
Spring provides an IOC container which serves as the most valuable feature to the developer.
The IOC container is complementary to the final principle of SOLID: &lt;strong&gt;&apos;dependency inversion&apos;&lt;/strong&gt;. Dependency inversion creates loosely coupled classes within the application design. Loose coupling will provide benefits across unit testing and support for application growth and behaviours. 
You can read about dependency inversion in more detail from my previous &lt;a href=&quot;https://aneesh.co.uk/dependency-inversion-principle&quot; target=&quot;_blank&quot;&gt;blog&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In addition to the IOC container, the Spring framework also provides out-of-the-box support for further features:
&lt;/p&gt;
&lt;strong&gt;Aspect-orientated programming&lt;/strong&gt;, also known as AOP, enables cross-cutting concerns to be injected for method calls within the application. 
With AOP, the process of calling methods can be broken down into sections (such as before the call and after the call) where logic can be injected in a modular fashion. You can read more on AOP in my blog &lt;a href=&quot;https://aneesh.co.uk/implementing-aop-in-a-spring-application&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Security&lt;/strong&gt; is supported by Spring for authentication, authorisation and other aspects. Spring security offers popular security mechanisms that can be efficiently implemented and customised into enterprise applications such as authorisation protocols of Kerberos, OAuth and SAML. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Transaction management&lt;/strong&gt; by Spring ensures database transactions are made in a timely manner using the ACID components. When an application makes a transaction to a database, the ACID components of atomicity, consistency, isolation and durability are important to meet to ensure transactions are orderly, complete and correct. The Spring framework provides an abstract layer underpinning both local transactions (in a centralised system) and global transactions (in a distributed system).
Spring provides an object relational mapping of data where support for integration with Hibernate, Java persistence API and data access object implementations can be configured for the IOC container.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Model-view-controller framework&lt;/strong&gt;, also known as MVC, is an industry-standard design pattern used for developing front-end interfaces that support scalable applications. 
MVC defines three components of the application that allow data to be passed through the application for processing: 
The view, which represents the interface that the user interacts with.&lt;br&gt;
The controller, which accepts an input from the user and converts the request to the model.&lt;br&gt;
The model, the central component which manages the request from the controller and manages the business logic to return a response for the user.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAACz0lEQVQoz22S60vTURjHn2mrmU5MIy0KpcQJYiNM0ywtL5VF9aIyRbLaLJ2a5jQx52zednE3De8zdG7uSiVl5bwPNTUip8xdcGoFgv9AL4rgdH6/fBU98DmHA8/3Oed8nwcAwAfjS0Cl+fvSgyPpfvuP0vGZhtnzH3wCDkbRiTyKN9V3R+uH2Y1hAhHeVBq5Hzv7CBLYL+Fm9RRIBt09Ur17Rm5ct2DG5AaPRWZY+yjSOEuz+JNw4o4JqHsDSR3Fyxt2IpxY/LypPgeIPTarc19K6VRmfN7bK0qTZ02ktiHe8wlU3TpOItM5kUjrNMawPtxIK5vOIt6C8ad47aLvFIyA8KjT/kEhYSF0GiUgpWS8L63Mqk4usjyW6lwzMr37l0zn2MJsSwcd2/j8u6HPrjr1cORqOtf66nzR+wKiYHDo8cPkSyleEfBEMgTFwneAEIKEglF1RacTipQ2qOr4fKRWZWPk1M3dvi9cuFwg/ZQo6F1mFDbPBuY0LALz7vCF+HyLJKdmBETdFihtNP/9snDAIZAMuiqlhg0eT+VQCQecNY399mr1GxuNJf4CScUTXRjNGc5o9i3+HPB7bNEizWpFZYetpbLT/kJuWCsUDayWK8zf80IZcSdBaV5HYvUyqm23ogbVPJLpXajF5EGZ/Nmu5BKrGVvQllo6lZ/OndEz743EPetdaWjBGoXBheq6Z5EIawVdc6imbfrnueucDFAYPahZu4rqeuYxC7igGymMayhXMJkbea01lXmxPIiwJyl/6Gkse5jLVy3X44aRF9erFpGwf4lsXJVy7EdiBvsSCDUOjljrYslNm2yZcYMt0jjYTQMOVhFX4E8YjiE6SCWmYAz7LNa6GU1qOwfnkRo8Rqym/pUH2LLsQ2HRMcBrnwW5aQMU5q+gMG2CROsEnATcqkb4N8RLyAvPJ2DPyDyl+Ru+wAmtr7eAU6sjm/IHkttNp/LSu2AAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Model view controller diagram&quot;
        title=&quot;Model view controller diagram&quot;
        src=&quot;/static/8dc1a4b4ebb2a2a563fba49b170d1e80/5a46d/031_mvc.png&quot;
        srcset=&quot;/static/8dc1a4b4ebb2a2a563fba49b170d1e80/1db73/031_mvc.png 75w,
/static/8dc1a4b4ebb2a2a563fba49b170d1e80/8a4e8/031_mvc.png 150w,
/static/8dc1a4b4ebb2a2a563fba49b170d1e80/5a46d/031_mvc.png 300w,
/static/8dc1a4b4ebb2a2a563fba49b170d1e80/fc2a6/031_mvc.png 450w,
/static/8dc1a4b4ebb2a2a563fba49b170d1e80/432e7/031_mvc.png 570w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Spring Beans and IOC&lt;/h4&gt;
&lt;p&gt;
Spring achieves IOC by defining a &apos;container&apos; to store the Objects that are later created and injected to the application. The Objects stored within the container are known as &apos;beans&apos;.
The IOC container can exist with different properties and features, these are largely divided into the &lt;strong&gt;BeanFactory&lt;/strong&gt; and &lt;strong&gt;ApplicationContext&lt;/strong&gt;.
&lt;/p&gt;
&lt;h4&gt;Spring Beans&lt;/h4&gt;
&lt;p&gt;
A Spring Bean contains a set of instructions for creating an Object; by default, the Object is created as a singleton. The bean is later injected as and when it is required. 
Spring Bean definitions are made dependent to the IOC container that is used. Definitions can be made using XML or annotations. For XML files, beans are defined within a configuration file, and are injected by defining an &apos;autowire&apos; property to the class configuration or by accessing the bean through its name from the container in the source code. Alternatively, beans can be defined within a class marked with &apos;@Configuration&apos; and injected by using the &apos;@Autowired&apos; annotation. We will later visit how to define the IOC container for the application. 
An example of the two styles for definitions are below:
&lt;p&gt;XML definition and autowire:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;beans&gt;
    &amp;lt;bean id=&quot;car&quot; class=&quot;com.f1.Car&quot;/&gt;

    &amp;lt;bean id=&quot;race&quot; class=&quot;com.f1.Race&quot; autowire=&quot;byName&quot;&gt;
        &amp;lt;property colour=&quot;yellow&quot;&gt;
    &amp;lt;/bean&gt;
&amp;lt;/beans&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Annotation-based:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; colour&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Configuration&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ConfigClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getCar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Component&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Race&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Car&lt;/span&gt; carToRace&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
When using the Spring Framework, the IOC container is determined by the developer. The ApplicationContext is significantly more popular as recommended by the &lt;a href=&quot;https://docs.spring.io/spring-framework/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory&quot;&gt;documentation&lt;/a&gt;: &lt;i&gt;&apos;use an ApplicationContext unless you have a really good reason for not doing so.&apos;&lt;/i&gt; The motivation for using the BeanFactory over the ApplicationContext is limited to only when memory usage is a valuable metric to the application as the BeanFactory can help reduce the memory-load of default ApplicationContext features.
&lt;/p&gt;
&lt;h4&gt;BeanFactory container&lt;/h4&gt;
&lt;p&gt;
The BeanFactory is an interface that is most commonly used through the &lt;code&gt;XmlBeanFactory&lt;/code&gt; implementation. The BeanFactory is instantiated within the application using the following dependency:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    &amp;lt;dependency&gt;
	    &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;
	    &amp;lt;artifactId&gt;spring-beans&amp;lt;/artifactId&gt;
	    &amp;lt;version&gt;5.2.8.RELEASE&amp;lt;/version&gt;
	&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The BeanFactory instance will take in a Resource instance to locate the configuration file for the beans:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Resource&lt;/span&gt; resource &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ClassPathResource&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;beans.xml&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;BeanFactory&lt;/span&gt; beanFactory &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;XmlBeanFactory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;resource&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As previously seen, the configuration file will use XML to define beans:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; age&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;age &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; age&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; age&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;beans.xml file:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;bean id=&quot;cricketer&quot; class=&quot;com.Person&quot;&gt;
    &amp;lt;constructor-arg name=&quot;name&quot; value=&quot;Ben Stokes&quot;/&gt;
    &amp;lt;constructor-arg name=&quot;age&quot; value=&quot;29&quot;/&gt;
&amp;lt;/bean&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;cricketer&lt;/code&gt; bean will create a Person of name &quot;Ben Stokes&quot; and of age 29.
The beanFactory can be used to obtain the bean:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; person &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;beanFactory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getBean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cricketer&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;h4&gt;ApplicationContext container&lt;/h4&gt;
&lt;p&gt;
The ApplicationContext is a an advanced Spring container available from Spring 3.0 that offers specific framework-orientated enterprise functionality as well as all BeanFactory features.
While the BeanFactory supports only xml-configuration of beans, the ApplicationContext differs as it additionally supports annotation-based configuration that can be used within the application code.
The ApplicationContext extends from the BeanFactory to provide further functionality of bean integration with application-layer features such as:
* ApplicationEvent support for creating and listening to events during runtime
* MessageSource access to support environment-specific configuration and internationalisation
* BeanPostProcessor for the creation and use of new beans before formal bean creation
&lt;/p&gt;
&lt;p&gt;
The ApplicationContext container can be implemented similarly to the XmlBeanFactory with the implementation &apos;ClassPathXmlApplicationContext&apos;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;ApplicationContext&lt;/span&gt; context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ClassPathXmlApplicationContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The annotation-driven configuration can be defined &apos;AnnotationConfigApplicationContext&apos;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;ApplicationContext&lt;/span&gt; context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AnnotationConfigApplicationContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The annotation-based container will perform a component scan to the configuration classes to store the beans that can be later obtained. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Challenges with the Spring framework&lt;/h4&gt;
&lt;p&gt;
While Spring provided developers with some great features to transform their application design and complexity, it introduced some unwanted challenges to the developer.
The complexities of Object management and dependency injection was since replaced with verbose xml configuration, most of which was boilerplate code.
As applications grew and the relationships between beans became more complicated, the burden of configuring the application became more and more apparent. 
&lt;/p&gt;
&lt;p&gt;
One example that captures the laborious configuration required with Spring would be the MVC functionality.
Spring MVC requires a few configurations to be made before it can work as expected in the application. 
This involves the dependency in the pom.xml file and configurations for the DispatcherServlet and InternalResourceViewResolver.
&lt;/p&gt;
&lt;p&gt;
The spring-mvc and java servlet dependencies are first referenced within the pom.xml file:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
    &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;spring-webmvc&amp;lt;/artifactId&gt;
    &amp;lt;version&gt;${spring.version}&amp;lt;/version&gt;
&amp;lt;/dependency&gt;

&amp;lt;dependency&gt;
    &amp;lt;groupId&gt;javax.servlet&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;javax.servlet-api&amp;lt;/artifactId&gt;
    &amp;lt;version&gt;3.1.0&amp;lt;/version&gt;
    &amp;lt;scope&gt;provided&amp;lt;/scope&gt;
&amp;lt;/dependency&gt;

&amp;lt;dependency&gt;
    &amp;lt;groupId&gt;javax.servlet&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;jstl&amp;lt;/artifactId&gt;
    &amp;lt;version&gt;1.2&amp;lt;/version&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The DispatcherServlet is bootstrapped to the application and is responsible for loading the IOC container. 
In the WEB-INF/web.xml file, the developer will need to configure the DispatcherServlet:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;servlet&gt;
    &amp;lt;servlet-name&gt;servlet&amp;lt;/servlet-name&gt;
    &amp;lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&amp;lt;/servlet-class&gt;
    &amp;lt;init-param&gt;
       &amp;lt;param-name&gt;contextConfigLocation&amp;lt;/param-name&gt;
       &amp;lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&amp;lt;/param-value&gt;
    &amp;lt;/init-param&gt;
    &amp;lt;load-on-startup&gt;1&amp;lt;/load-on-startup&gt;
&amp;lt;/servlet&gt;

&amp;lt;servlet-mapping&gt;
    &amp;lt;servlet-name&gt;appServlet&amp;lt;/servlet-name&gt;
    &amp;lt;url-pattern&gt;/&amp;lt;/url-pattern&gt;
&amp;lt;/servlet-mapping&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Component scanning will be configured into the app-config.xml file to direct Spring to the packages to be scanned:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd &quot;&gt;

    &amp;lt;context:component-scan base-package=&quot;com.spring.application&quot; /&gt;

&amp;lt;/beans&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Lastly, the InternalResourceViewResolver bean is registered which will return the view name as specified by the location of the file:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
       xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd &quot;&gt;

    &amp;lt;mvc:annotation-driven /&gt;
    &amp;lt;context:component-scan base-package=&quot;com.spring.application&quot; /&gt;

    &amp;lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
        &amp;lt;property name=&quot;viewClass&quot; value=&quot;org.springframework.web.servlet.view.JstlView&quot;/&gt;
        &amp;lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot; /&gt;
        &amp;lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
    &amp;lt;/bean&gt;

&amp;lt;/beans&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Thankfully, that would be the &lt;i&gt;only&lt;/i&gt; configuration required as default for using MVC with the Spring framework. But surely this can be made easier?
In April 2014, the advent of Spring Boot came as a solution to resolve the heavy configuration required with the Spring framework...
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to Spring Boot&lt;/h4&gt;
&lt;p&gt;
Spring Boot was introduced with a precendent to &lt;i&gt;convention over configuration&lt;/i&gt;: the objective of reducing the number of decisions a developer needs to make when creating an application. But what does this mean?&lt;br&gt;
The design paradigm of convention over configuration will &lt;i&gt;only&lt;/i&gt; require the developer to specify &lt;u&gt;unconventional&lt;/u&gt; configurations for the application. A &lt;i&gt;sensible default&lt;/i&gt; is however defined for all other infrastructure components (which would include DispatcherServlet and component scanning for MVC - yay).
&lt;/p&gt;
&lt;p&gt;
Spring Boot applications can be created out-of-the-box from the &apos;Spring initializer&apos; at &lt;a target=&quot;_blank&quot; href=&quot;https://start.spring.io/&quot;&gt;start.spring.io&lt;/a&gt;. The Spring initializer can also be used through Spring Tool Suite and allows the developer to specify the dependencies that create the environment. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;AutoConfiguration&lt;/h4&gt;
&lt;p&gt;
AutoConfiguration is the core feature from Spring Boot as it resolves the excessive configuration required from the developer for the application purpose. 
&lt;/p&gt;
&lt;p&gt;
As previously visited, Spring MVC is one example where default configuration would always be required to set-up the DispatcherServlet and ViewResolver amongst other infrastructure components.
The same principle applies for Hibernate where we need to manually configure a data source and entity manager factory to our application. 
Rather than configuring the xml ourselves, Spring Boot AutoConfiguration will create the beans automatically upon scanning the jar in the class-path and deciding which default beans are required.
&lt;/p&gt;
&lt;p&gt;
Spring AutoConfiguration is used from the spring-boot-autoconfigure jar in the classpath.
The AutoConfiguration class, which is run after all other beans are configured, will appear like any usual Spring @Configuration class. The difference however lies in annotations such as &lt;code&gt;@ConditionalOnMissingBean&lt;/code&gt; and &lt;code&gt;@ConditionalOnClass&lt;/code&gt; which will configure and activate a bean provided certain classes appear within the classpath. 
&lt;/p&gt;
&lt;p&gt;
Spring Boot AutoConfiguration also plays a role in the default IOC container that is used by the Spring Boot application. Spring Boot will default to the &lt;code&gt;AnnotationConfigApplicationContext&lt;/code&gt; container. If during the Autoconfiguration scan a web starter-project is identified, the IOC container will use the &lt;code&gt;AnnotationConfigServletWebServerApplicationContext&lt;/code&gt; container instead. The latter of the ApplicationContext types will create, initialise and run a webserver as required by the dependencies. The selection of the ApplicationContext further extends the functionality of Autoconfiguration to support the &apos;convention over configuration&apos; framework.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Other key features with Spring Boot&lt;/h4&gt;
&lt;p&gt;
The first main feature from a Spring Boot application is the @SpringBootApplication annotation that is added to the main class as default. 
The single annotation encapsulates 3 other annotations that are useful for the developer to use the main class for:
* @Configuration enables Java-based configuration for the class to enable Beans to be defined
* @ComponentScan to enable component scanning across the application package and sub-packages
* @EnableAutoConfiguration to enable the Spring Boot AutoConfiguration
&lt;/p&gt;
&lt;p&gt;
Spring Boot introduces &apos;starter-dependencies&apos; that can be selected during creation of a Spring Boot Application, or injected to the pom.xml file. Starter-dependencies  includes the related configurations and beans required by the application thus eliminating the need to add multiple related dependencies in the same pom.xml file. The starter-dependencies extend the convention-over-configuration of Spring Boot by further reducing the configuration required by the developer.
If you were to develop a RESTful application, you would require the MVC design pattern which includes Spring MVC, Spring REST and Object to relational mapping. The &lt;i&gt;spring-boot-starter-web&lt;/i&gt; dependency encapsulates all these requirements together to include the jar files with the application classpath.
&lt;/p&gt;
&lt;p&gt;
Spring Boot provides an embedded server into the jar executable application as default. Without Spring Boot, the developer would be required to install a web server that the application artifact is later deployed to. Spring Boot offers a default embedded server of Tomcat, however alternatives such as Jetty and Undertow are also available. Apache Tomcat is an open source web server designed to execute Java servlets and to render Java Server Pages.
&lt;/p&gt;
&lt;p&gt;
Spring Boot enables the use of externalised configuration to provide control of the application runtime properties. Beans can use the &lt;code&gt;@Value&lt;/code&gt; annotation to obtain their value from an external &lt;i&gt;.properties&lt;/i&gt; file. Spring Boot provides a properties file and bootstrap file to priorities configurations that can enable custom application behaviour for different environments. The externalised configuration can be used for changing the URL, username and password accesses, locale for formats and also logging level to the same application in different environments. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
The Spring framework was introduced as a lightweight solution for loose-coupling and management of Java Objects with an IOC container. 
Within Spring, developers are able to create beans to be stored within an IOC container which represent instructions for creating and managing Java Objects. 
The ApplicationContext IOC container introduced annotation-based configuration and enhanced IOC bean management features. 
Following the growth and popularity of Spring, the developer often wrote excessive xml configuration when using the Spring framework thus Spring Boot offered a solution. 
Spring Boot introduced Autoconfiguration that uses convention over configuration to create default beans and configuration for the application. 
As a result, Spring Boot enables the developer to create out-of-the-box production-ready applications with minimal configuration required with the flexibility of customisation. 
You can read more about Spring and its features from the excellent documentation &lt;a href=&quot;https://docs.spring.io/spring-framework/docs/current/reference/html/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using lambda for functional methods]]></title><description><![CDATA[Use lambda expressions to introduce functional programming with Java.]]></description><link>https://aneesh.co.uk/using-lambda-for-functional-methods</link><guid isPermaLink="false">https://aneesh.co.uk/using-lambda-for-functional-methods</guid><pubDate>Mon, 26 Oct 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand how functional interfaces are designed and created.&lt;br&gt;
&amp;#8226; Use anonymous classes to implement functional interfaces.&lt;br&gt;
&amp;#8226; Understand how to create a lambda function for expressive implementations of functional interfaces.&lt;br&gt;
 
&lt;br&gt;
&lt;h4&gt;Lambda and functional programming&lt;/h4&gt;
&lt;p&gt;
Lambda functions were introduced in Java 8 as a step for Java towards functional programming.
Functional programming enables a developer to pass functions around the application without assigning it to a specific  class. Lambda enables Java to use functional programming as functional interface implementations that are dynamically created and executed at runtime.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;What are functional interfaces?&lt;/h4&gt;
&lt;p&gt;
A functional interface is defined as an interface with just a single abstract method. A functional interface can also contain many other methods that are either Static or Default, provided there is just a single abstract method. If the interface contains more than one abstract method, it cannot be considered when creating lambda functions.
&lt;/p&gt;
&lt;p&gt;
Prior to Java 8 (and the introduction of lambda functions), functional interfaces would be used through a class implementation where the abstract method is overridden:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;//functional interface&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;makeCoffee&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EspressoMachine&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;makeCoffee&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Making an espresso&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;EspressoMachine&lt;/span&gt; machine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EspressoMachine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
machine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;makeCoffee&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Making an espresso&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The creation of the &lt;code&gt;EspressoMachine&lt;/code&gt; with a single method, and further instantiation of the &lt;code&gt;machine&lt;/code&gt; enables an implementation of the CoffeeMachine to be used. Considering what we have achieved, in an implementation of a single method, you may consider the creation and instantiation of the class as being heavy to write. An anonymous class may provide a more straight-forward approach.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using anonymous classes&lt;/h4&gt;
&lt;p&gt;
Anonymous classes allow us to reduce the boilerplate code of implementing the interface into a class. As the name suggests, an anonymous class does not have a name as it is implemented in the method body of its declaration:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; espressoMachine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;makeCoffee&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Making an espresso&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The anonymous class enables local variables to the class to be defined and for further methods to be used for the &lt;code&gt;espressoMachine&lt;/code&gt;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt; &lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; espresso &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; numberOfCoffees &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;makeCoffee&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;numberOfCoffees &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;randomNumberCoffees&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;making &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; numberOfCoffees &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; coffee(s)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;randomNumberCoffees&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;numberOfCoffees &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Within the body of the anonymous class, the keyword &lt;code&gt;this&lt;/code&gt; would refer to the implementation espressoMachine as the anonymous class represents an inner class at compile time. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Functional interfaces as a lambda function&lt;/h4&gt;
&lt;p&gt;
The &lt;code&gt;CoffeeMachine&lt;/code&gt; can be implemented as a lambda function as an alternative to an anonymous class. The lambda function ultimately reduces the amount of code to write, however is limited in the complexity of the implementation that can be achieved. Let&apos;s look at what a lambda function might look like for the EspressoMachine implementation:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; espressoMachine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Making an espresso&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Is that it? Yes!&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The syntax of the lambda function may seem strange and the logic behind it may appear unclear. I will now break down the lambda function and explore how it can be adapted to cater for other types of functional interfaces. 
&lt;/p&gt;
&lt;p&gt;
The lambda function is broken down into three areas: parameters, arrow operator, and code block.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a1d8367c285e5c2282c28973b806cb73/00b70/030_lambdaMake.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 37.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABoklEQVQoz2NgYGDgZMANWICYCYkPYrMCMTMO9eoggheIdYFYDYg1gFgbiLWgGAQEoHKKQMwNFeOCiulA1YFoCagaBi42NnZ5bm4eJS5uHgUuLm4QrcjNwysXHBbLwsHJqcnOzm4EVGPOxc2t+P//fwZdfSN2bh4eeZBaoD55oLgKLy+fENAsJQYhIRFWL98ghoKSWoaSykaG/OIahqLyeoaktDyYN3SgLtcEYlWQgROmLWQoq2pmKCitZSgF0g2t/QxWto4MjIyMKiANzJt2HhNcv/2I4oET1+R2HbogD+TLb9p1XH7v0cuyl269krrz9KvU/Rc/pC7cfCG158gl2W37Tstt2X1Sft+xKyA1ckC1St///+cDuxBk47nrzybfePjxP9DAbycvPfx189GnX9fuvwfj6w8+/D537dnvM1ce/waxoWK/rt579+vouTu/zl598u3U5Yf/bz/5sgRooCQ45nYcOGt79OydXKDLUnYeOJd27NydtMOnbwLxrbQjZ2+n7Tx4Lm37vjNgNljszC2w/JbdJ0DiKUD5XKClDkCzlAEkxcAV/ZXPEAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Lambda function make-up&quot;
        title=&quot;Lambda function make-up&quot;
        src=&quot;/static/a1d8367c285e5c2282c28973b806cb73/5a46d/030_lambdaMake.png&quot;
        srcset=&quot;/static/a1d8367c285e5c2282c28973b806cb73/1db73/030_lambdaMake.png 75w,
/static/a1d8367c285e5c2282c28973b806cb73/8a4e8/030_lambdaMake.png 150w,
/static/a1d8367c285e5c2282c28973b806cb73/5a46d/030_lambdaMake.png 300w,
/static/a1d8367c285e5c2282c28973b806cb73/fc2a6/030_lambdaMake.png 450w,
/static/a1d8367c285e5c2282c28973b806cb73/0a47e/030_lambdaMake.png 600w,
/static/a1d8367c285e5c2282c28973b806cb73/00b70/030_lambdaMake.png 733w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code block can wither exist within curly brackets or it can exist without provided it is a single line. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The parameter is used to take in the arguments as defined by the abstract method. The number of parameters can be any number as long as it reflects the abstract method. The arrow separator is used to instruct a lambda function. The final expression is used as the method body. The implementation of the Lambda function for the CoffeeMachine can exist as below:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; espresso &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Making an espresso&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The lambda function could be replaced to accommodate any number of arguments if the abstract method were to require them:
&lt;br&gt;
1 argument (int numberOfCoffee)
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; espresso &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Making &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; espresso(s)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;2 arguments (int numberOfCoffee, String customer)&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; espresso &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Making &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; espresso(s) for &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
If the lambda expression returns an Object or value, the curly braces must be used with the return statement or none together. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;typeOfCoffee&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; latte &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Latte&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;CoffeeMachine&lt;/span&gt; cappuccino &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Cappuccino&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the above example, the &lt;code&gt;latte&lt;/code&gt; uses both the curly braces and the return keyword. The &lt;code&gt;cappuccino&lt;/code&gt; however uses neither. &lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The relative ease of creating lambda functions is enabled by the JVM as it infers the method call that is represented. There are three key properties to the lambda function that enable it to be inferred without compile error by the JVM:&lt;br&gt;
1. The expected representation of the lambda function must replace a functional interface. Therefore the implemented interface must have only a single abstract method.&lt;br&gt;
2. The abstract methods number of parameters must match the number of parameters and cast as represented by the lambda function.&lt;br&gt;
3. The return type of the abstract method must be equal to the return from the code block of the lambda function.
&lt;/p&gt;
&lt;p&gt;
For the functional interface below, each of the three subsequent lambda functions will fail to compile as they breach of the three rules above:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Vehicle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTypeOfVehicle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The below lambda will not compile as an implementation of Driver is expected which is not a functional interface (unlike Vehicle).&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getDriverName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Driver&lt;/span&gt; driver&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; driver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getDriverName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Vehicle is truck&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The below lambda will not compile as an argument is passed in when there should be 0 arguments.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Vehicle is a truck&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The below lambda will not compile as the code block returns a String when the return should be void.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Vehicle is a truck&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Anonymous classes vs Lambda functions&lt;/h4&gt;
&lt;p&gt;
While Lambda expressions reduce the amount code to write, they may not always be the best solution to implement. The considerations to make for an anonymous class extend to performance, readability and purpose/complexity of the function invoked.
&lt;/p&gt;
&lt;p&gt;
The anonymous class is a class of itself and generates a new &apos;.class&apos; method from compilation that adds to the size of the jar executable file. A lambda function however uses the &lt;i&gt;invokedynamic&lt;/i&gt; bytecode instruction to bind the method dynamically to the encapsulating class. Therefore lambda expressions can improve time and performance of the application. 
&lt;/p&gt;
&lt;p&gt;
An anonymous class can be used for other implementations of interfaces and not just a functional interface. They are not bounded to the single abstract method, however the anonymous class will become larger as each method is overridden. 
&lt;/p&gt;
&lt;p&gt;
The anonymous class tends to be more verbose than the lambda expression. A redundant class body must be defined within the anonymous class and therefore must only be used when a class implementation is valuable. Unlike a lambda function, an anonymous class enables class-local variables to be defined as well as further functions for processing.
The anonymous class supports far greater complexity and functionality within the class.
&lt;/p&gt;
&lt;p&gt;
As the differences between lambda and anonymous classes are vast, there are clear and distinct purposes to use one over the other.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;
Functional interfaces are interfaces with a single abstract method. Prior to Java 8, a functional interface would require verbose implementation within a class and instantiation to access its behaviour.&lt;/p&gt;
&lt;p&gt;
Anonymous classes offer a compiled implementation of interfaces within the calling method. The anonymous class is compiled at runtime and enables further complexity of behaviour to be implemented.
&lt;/p&gt;
&lt;p&gt;
Lambda functions introduce functional interface implementations that support behaviours of the application at runtime. The lambda function enables developers to reduce boilerplate code for implementations of functional interfaces using a unique three-part instruction.&lt;br&gt;
Lambda functions can be flexible to support multiple parameters and return types and can be passed around the application and used as arguments to create dynamic and independent behaviours.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How the JVM uses the heap and stack]]></title><description><![CDATA[Explore how the JVM uses the heap space and stack memory to manage the runtime and Objects.]]></description><link>https://aneesh.co.uk/how-the-jvm-uses-the-heap-and-stack</link><guid isPermaLink="false">https://aneesh.co.uk/how-the-jvm-uses-the-heap-and-stack</guid><pubDate>Mon, 19 Oct 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the differences between the heap space and stack memory.&lt;br&gt;
&amp;#8226; Visualise the runtime activity of the stack memory.&lt;br&gt;
&amp;#8226; Understand how the heap and stack communicate with each other.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;How does the JVM store our Objects?&lt;/h4&gt;
&lt;p&gt;
The JVM divides memory between two locations known as the heap space and stack memory for managing the application runtime and Objects. The two locations differ by their design and properties that effect their function and efficiency of memory use. 
&lt;/p&gt;
&lt;p&gt;
In this blog, I am going to review the responsibilities between the heap and stack to the JVM. By understanding the differences between the heap and stack, we may be better positioned to improve our application runtime performance through design. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The heap space&lt;/h4&gt;
&lt;p&gt;
The heap space is created when the JVM starts as a dynamic space for allocating storage of Java Objects. The heap is obtained by the JVM from the operating system as a fixed space for the lifetime of the application. The heap is described as &lt;i&gt;dynamic&lt;/i&gt; as it allocates memory during runtime of the application. 
&lt;/p&gt;
&lt;p&gt;
When Objects are assigned to the heap, they are globally accessible by all threads of the application, however they are not necessarily thread-safe. Objects can be designed to be thread safe as discussed in my blog &lt;a href=&quot;https://aneesh.co.uk/creating-multi-threaded-visibility&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
The heap space is divided into two &apos;generations&apos; for storage: old and new. The generational divide is used to determine how Objects are stored, retained and removed from the heap space at runtime. The heap space uses a garbage collector to mark and sweep Objects when the young generation reaches capacity. You can read more about the garbage collector in my blog &lt;a target=&quot;_blank&quot; href=&quot;https://aneesh.co.uk/how-the-jvm-manages-memory&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
The impact of the garbage collector upon the JVM performance can be significant. Object management from the heap is therefore a valuable area to inspect and resolve for runtime performance enhancement.&lt;br&gt;
When the heap is full, java will throw a &lt;code&gt;java.lang.OutOfMemoryError&lt;/code&gt; exception.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The stack memory&lt;/h4&gt;
&lt;p&gt;
The stack memory contains space for the execution of application threads. The stack is used for temporary memory where variables are stored when methods are invoked. When a method is called, its memory is allocated on top of the stack in a first-in-last-out (FILO) order. Once the method has completed, its memory allocation is cleared from the stack. The diagram below represents 4 different method calls made, where the most recent method call sits at the top of the stack and is currently pointed to by the JVM.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 90.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAADeklEQVQ4y5WTWWwTVxSGTxwWAW0EKAFLEBGgD20eiEIajLPYjpexnQTC5iYOYWkSxwQTYo+dPS1ICMJSNlVFbUDpIiGEKkhs4KHQlrHHHpNAlhbeKqTih/aBF57JzL2ca8cIqWqdXunTmTtzdeY//zkXAGAlssAR+HGbsy/U2NAbrHf2BhsYDb1j9XsH7zVVt3/H4Zkl7+cWvLdwyYpli5b+A3y/fDGeWYeAav/Jx5+1np2irnPTyNRb2r6YoftOSNTgujGxzReCok+GodT5DWxpvAqlbxmGitZbkF/sgCxVdi6s0xxQm33hl1WdYarrEGTDUUHRIyyafNHZCtcoLan/OmLrm1lh8oYLjZ3CGoz577C2quPngt3nXuWguFz4UOdSGzojfx4cmqTeL39TUBlBteTT05OkbmBCLneNUVQlcF2PC0w+kUOKzT6x1MyLHyfxiSVGb1hbe/z52qR9H5S3qo1eMVE3EKeOz8eVnYOPqOPYOGXR3vNIrmgLUu2+EVHvHgWzPw5mPgaYBGMK/AHYB55BobUfsrIXroL8Eucqm194zvFhavEJMscLxDKHLSDOmjx3WELJ1n4drLyQXXX0FxUmUZn4OfC5eNf5bEitPFivbV5t8kUS+DeKH9E3kRi9EYJ7YglIcmUbK3lY2rr3Gujbbmdt2nkhqfBfVl7aw0Tz6Unq/+p35dD5acpg+x2DE3JZ6xjd0jgsabCbOtetrKJdF+E/Vh5sLGtRo6pETY9Et/fFldpeidT1xwmL1q64rHOHaPmBbx8amr8H0+HRzAnXFO3JswfCfyDU5g/LtkCYYExS3R2dtXTcTXpo77gNO3p+zZxwg5YpjCQs/ii18FHFzEeZl9TiFwmHHlbMeajBIZ5XyWkPW85M0sCVp0r7hWl6+OJM0sP0HP4vDzekuvzC1hWj1d0xxd4dI7U9EpYbI1ZUqDsUpGX7RyKGlh/A7BnLnJDNoTU5hwKWnJrDNPYuNochqmm6JpndN6GG/2k+XU7dFHu3hAolBZWishhhkQvE0x7GkiXjHM7XwxfNKQ9l9JAcuTRDDg49Idv7x1+nPLw6pW1igz0fD1EhevgXuylpONZxP+t0jFa6g6Sk/opzs+MyaJpGVBkT5qgLc6o6H5bpPfctes8Do/7IA5POc5/BnrlKd6iovOUm7B76Gz6qOQWZEr4BLhT5hc4LJnQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Stack diagram&quot;
        title=&quot;Stack diagram&quot;
        src=&quot;/static/ec333f84a120d9fba3683df02339714b/5a46d/029_stack.png&quot;
        srcset=&quot;/static/ec333f84a120d9fba3683df02339714b/1db73/029_stack.png 75w,
/static/ec333f84a120d9fba3683df02339714b/8a4e8/029_stack.png 150w,
/static/ec333f84a120d9fba3683df02339714b/5a46d/029_stack.png 300w,
/static/ec333f84a120d9fba3683df02339714b/fc2a6/029_stack.png 450w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The stack uses static memory allocation where the compiler is able to allocate memory to the application before it has been executed. Once the stack has bounded the memory to locations, it is able to execute the threads efficiently with direct addressing. 
&lt;/p&gt;
&lt;p&gt;
The stack will store local variables to the methods and the reference addresses of the Objects within the method. The references will point to the Object location within the heap. When the method is completed, the stack will remove the method from the memory along with the local variables that are referenced by the method. 
&lt;/p&gt;
&lt;p&gt;
A key performance advantage of the stack over the heap is the direct addressing of memory. The stack is able to efficiently assign and remove memory unlike the heap.
When there is insufficient memory on the stack, the JVM throws a &lt;code&gt;java.lang.StackOverflowError&lt;/code&gt; exception.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using the heap and stack together&lt;/h4&gt;
&lt;p&gt;
The below code sample provides a means to explain how the heap space and stack memory coordinate together:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Sport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; players&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Sport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; players&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;players &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; players&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SportBuilder&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Sport&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; players&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Sport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; players&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; players &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; sport &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;tennis&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Sport&lt;/span&gt; tennis &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        tennis &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SportBuilder&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sport&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; players&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The code above defines 3 classes, a Sport entity class, a SportBuilder class with a static method to build a Sport Object and a Main class with the main method. 
&lt;/p&gt;
&lt;p&gt;
The main method from the Main class will instantiate the creation of the stack memory with a single thread. 
On line 3 in the Main class, the main method is defined and added into the stack memory. &lt;br&gt;
On line 5, the primitive int &lt;code&gt;players&lt;/code&gt; is added into the main method of the stack memory. &lt;br&gt;
On lines 6 and 7, Objects of Type String and Sport are created in the heap space; their address is referenced by the main method block in the stack.&lt;br&gt;
On line 8, when the SportBuilder method is called, a new block is added to the stack on top of the main method block. The new block will contain a reference to the Main class instance that is calling it, the local int &lt;code&gt;players&lt;/code&gt; and the reference to the String &quot;tennis&quot;. &lt;br&gt;
In line 3 from the SportBuilder class, a further method is added to the stack as the Sport constructor is called. The third block will contain the primitive int value along with references to the String &quot;tennis&quot;. The third block will also contain a reference to the calling Object instance of SportBuilder.
&lt;br&gt;
When the Sport Object is created, the third block is swept from the stack, followed by the second block from the SportBuilder class, and finally the Sport Object is populated with the String and primitive value in the heap space. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Referencing Objects from the heap into the stack&lt;/h4&gt;
&lt;p&gt;
The code sample above has demonstrated how the heap and stack work together to store and reference Objects through the lifecycle of a JVM thread. The process for referencing Objects from the stack to the heap can impact the efficiency of the JVM and operations that can be performed upon the Objects. This section will review how the two memory areas communicate.
&lt;/p&gt;
&lt;p&gt;
The &lt;i&gt;pass by reference&lt;/i&gt; approach involves passing the address of the Object from the heap space into the stack. 
In the below example, an instance of &lt;code&gt;Student&lt;/code&gt; is created (&apos;alice&apos;) and it is stored in memory location &apos;a&apos; in the heap space. 
On line 5, the Student is referenced by a method called &lt;code&gt;goToClass()&lt;/code&gt; from the stack. The &lt;code&gt;goToClass&lt;/code&gt; method will receive the location &apos;a&apos; of Alice and will 
set the &apos;attendance&apos; boolean of the Object to true on line 9.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; alice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;goToClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;alice&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;goToClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; student&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    student&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setAttendance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACcUlEQVQoz01SS0xTQRSd9ymfttJWARXUhIJRY/yEBVH84Cdq3eBCFo0xYuKKFYu6IZEYglFSDH4q1BgsfbT0hYJIS0uR14fkCdpWKEIxfmjjggRBlIX4I2iuM4806U3OnNw7k3PvnBmEEMrC0GJoKIrSMAyjZRia1NZhZGKk5W7aKjMAoFTgYAp37CV76SlAaozjGIexYClDU0cVDF2oYFka11hyoNE2hFcj6gstXvSHF22+0BenP/LN1Dk8r7O6I7J49sZ8lBp0xckSaq3zZxTjb8jFA0cMdHHpWRQDoHtH5jxWXoL7nACW9iBYHCJ0iok4L85u80aWURM3TCXFMjVq1RbMefZbNRUSdzsw4rhjKTtUtkGbvVlHmjTzo5VdQ3Fw9sdWHL6pVad/avXJUPy379Uc9EizVnFiCQXC8wq7d1IWZJUZ6TmYc81Xq4q7mmorPQ/qT+M8X6XO2ol5/bVGp8nuGYfW7tCfu5z4t9Ul/Au6nb+GuzvAL8baIqFpdBA3dgc/rgkSc2urLtEAy+he3XX1C9fDAr6pjpiiJHacOnd5d7NLSrT1joHt6Wtw+Seh//lb8A1Gf9q7R499BUgTxpd0ncIHFRHcj7FLrczcg7nAVm9qER7dnOXNNQ041+t0OjKl6kT5lZyOgfcNvtBCyPtyPtozusA5wrC9hZesnDe6wnknfrT3vXmWnBA/MMWQafLy9azxvJEt0hfJXyA9Qylfo/xCNXIL75LfBUkJQG2BGYRt6LN7ooCvC11ifCwpqCBgaFqRtEDOSbBpskDjY4HySJ9w3YCqzYNImv7OEHH8UPvcwRkDFjzj8E2W/AcY9BRZGMkbUgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pass by reference diagram&quot;
        title=&quot;Pass by reference diagram&quot;
        src=&quot;/static/8787acfba7cef0c8c9de41021d5dcb47/5a46d/029_heapDiagram.png&quot;
        srcset=&quot;/static/8787acfba7cef0c8c9de41021d5dcb47/1db73/029_heapDiagram.png 75w,
/static/8787acfba7cef0c8c9de41021d5dcb47/8a4e8/029_heapDiagram.png 150w,
/static/8787acfba7cef0c8c9de41021d5dcb47/5a46d/029_heapDiagram.png 300w,
/static/8787acfba7cef0c8c9de41021d5dcb47/fc2a6/029_heapDiagram.png 450w,
/static/8787acfba7cef0c8c9de41021d5dcb47/0a47e/029_heapDiagram.png 600w,
/static/8787acfba7cef0c8c9de41021d5dcb47/f73a1/029_heapDiagram.png 822w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;i&gt;pass by value&lt;/i&gt; process involves passing a copy of the Object itself from the heap space into the stack.
Using the same example as above, the pass by value movement of the Object would involve creating a new copy of the Object and then passing it into the stack.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 43.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACk0lEQVQozzVRa0iTURh+v++b2/yWuelSxFaEDLWgC11ESDKEbqZChEWRPwoqwggKS8rULj8E05pmLm+49emmu7R989vNKXNdnKmV96l/dCD+EIx+hBTl6ZxlB573ec97OM97AwCQY+zB2M0wzE5WKjkYI2O343ssRrRYyooxixFCUFnfE99qG8/Qmkd34RhNYur0vVLsSwAoBrMaNo5axDBpmLdiJGNEkQ9x8YmRT7w/BHph6pbeMbbMOSd+csLEWqd7OmD1h9NaLCMgDK1QNE30YBsxooKcwzRaX4IZgUtc8HInw33GzPzcPBF5fFJnAtfI9/NvA2HUaBhAWmMA1XF+pHPNItvgykyFxsGSpBuHdAYsbnEH5tQH1y4V8vWPPN6mqlqlIi5Fysak4riyptVtareNIr1jYq3NNrluE4J/Ai7vL1/fKDL4lzP4L7+BiOIqI4Ig38RG+HT2AeiqLIKaCxnAbpbL9mfmRLI/b++/bPBMI87Vj9qdXuTwvEMeK48E3jeNUCcEwwgevzAQCRUxZHbqqCgRGXRKWW3Jw5dd1cPlmtJiMk+FUk6qV9W0Oct1Tj6sc1q+2YOLq9ahH/YG+2y+0TPTaPSG9N29c6/yCq9kEUElRVFisUQsw77s/rN76Y0WTc710qskUXT8FiVbXFLOaHRuaDaHco3ehfFOd+h9hzBWUdtky2oxB/EovqJu3zy6UVJ1jgjSGGRFTJLq31ZXUZjEKWJiFQp4I0yKmkyDoDUO3Gw2f0ScMI5MffOo+jWfhrd9t6t3toz/sHT76Imzh/5vh6JoighQ2aeO0EXFF5njZ46RRJCQlAANHX6qzfoJWi1D+zpcU0+7fXOVuKI75L1ncBmGFxHo+M/kmvwXkVQaOHBsYVQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pass by value diagram&quot;
        title=&quot;Pass by value diagram&quot;
        src=&quot;/static/ca5886a3912ddb5373bfcab784b995bc/5a46d/029_pbvDiagram.png&quot;
        srcset=&quot;/static/ca5886a3912ddb5373bfcab784b995bc/1db73/029_pbvDiagram.png 75w,
/static/ca5886a3912ddb5373bfcab784b995bc/8a4e8/029_pbvDiagram.png 150w,
/static/ca5886a3912ddb5373bfcab784b995bc/5a46d/029_pbvDiagram.png 300w,
/static/ca5886a3912ddb5373bfcab784b995bc/fc2a6/029_pbvDiagram.png 450w,
/static/ca5886a3912ddb5373bfcab784b995bc/0a47e/029_pbvDiagram.png 600w,
/static/ca5886a3912ddb5373bfcab784b995bc/ddc81/029_pbvDiagram.png 837w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the diagram above, an identical copy of &lt;code&gt;alice&lt;/code&gt; is made within the heap space and is then passed into the &lt;code&gt;goToClass&lt;/code&gt; method.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;How Java passes Objects between the heap space and stack&lt;/h4&gt;
&lt;p&gt;
The JVM uses a combination of pass-by-reference and pass-by-value for managing Objects. Technically, Java uses pass-by-value as the a copy of the Object is passed to the method call, however Java does not make a copy of the Object, it will pass a copy of the Object address from the heap space.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 43.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACh0lEQVQoz0VRbUiTURS+e9/p3OZyq1w/ooaOppYWGuSPMEzBJGjUD7MMQuxHgSwiJFIsHEEZlpguaWLWtvbOzbnl+7q9W+4Dh83FPljmB8MPkNYHRqMQDKK63TuTDjzn4Zx7n+dwOAAAIEYoRijhkmShkJ91WCQUyFCdg8DP5PEzEfMghODGfbNYa42W9hqn5bjGka8oycLvgMMhEB/EPQ5CATI7gHjPP/BwX7w9l8TC8UAC6B3zKsP4TNLIzm0Y2dl1k2vBaZt8L9NaQsDgmCU2/UAhTtxpUz9nyU2BFY9Z+sE/XJv0m8uPV9WQ+PFO7whgo98v2AOrsJ+ahE9MAfjI4Ic6NgHtwS+RM40dXPA/inASohXliBXtVy7W0xr1hFt778EOiUTO4wsLUH9n91MX/cwehjo69mPQHvlDs6FfU6+8Pz3++G+D7/MhR2JzfYIg0oYgVyJK89nao2CyTwW668qAQCTJLj1SKcArD9piTVbfMrT6FqHFtwIZNgjdozboZHxR+DUO3iQh6Og2YgvFVsrnZnCxe566r/W2xtwVUWvamlEtLS7dj6dlWP2fVGNTa8vjwbUUHUp9s4c36C4qdsrkmn9sci8MWTyLPRXVympsKEVn4ZFcEgu3tdy9VvRQ31nVdL0RDxIJs4V5iPfq2K5m9u2onQkz74bo5z1YeKtz4OSgNQT1TByOeJdgw6WWhvTqCPgAxK7dUgKvmISJreuno1FVByivNjL6egi6ZijIhHVj+N8LZ0RGueZumicSrUzw49Wy8sqKLQ0HB+ZjtRXE+cv1ZLWyisgRC4Dy3AkOFr8M6U47YlTbxJy1nY2bazSWNjDsWgfuaAqEVyEYsASxz76/QNoUb3b8H0AAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pass by value diagram for Java&quot;
        title=&quot;Pass by value diagram for Java&quot;
        src=&quot;/static/27adfbebd92f1bd92cf842afcae4c60a/5a46d/029_javapbv.png&quot;
        srcset=&quot;/static/27adfbebd92f1bd92cf842afcae4c60a/1db73/029_javapbv.png 75w,
/static/27adfbebd92f1bd92cf842afcae4c60a/8a4e8/029_javapbv.png 150w,
/static/27adfbebd92f1bd92cf842afcae4c60a/5a46d/029_javapbv.png 300w,
/static/27adfbebd92f1bd92cf842afcae4c60a/fc2a6/029_javapbv.png 450w,
/static/27adfbebd92f1bd92cf842afcae4c60a/0a47e/029_javapbv.png 600w,
/static/27adfbebd92f1bd92cf842afcae4c60a/27b8e/029_javapbv.png 836w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
We are able to test how Java passes Objects with the below code snippet. The code will attempt to switch the Person Objects:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; alice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; bob &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;alice&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bob&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;alice&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The output from the above code will show:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Alice&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;swap()&lt;/code&gt; method will not work as expected as we are not actually altering the location of alice and bob, but are altering the location of a copy of the reference:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;//(copy of)alice = location a, (copy of)bob = location b&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;//c = location a&lt;/span&gt;
        a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;//a = location b&lt;/span&gt;
        b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;//b = location a&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;//final result: (copy of)alice = location b, (copy of)bob = location a&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
If the actual reference of the Person was passed into the &lt;code&gt;swap()&lt;/code&gt; method, then alice would point to location B and have the name &quot;Bob&quot; and vice versa. However as a &lt;u&gt;copy&lt;/u&gt; of the reference was passed, the actual Person objects of Alice and Bob remain unchanged.
&lt;/p&gt;
&lt;p&gt;
The use of the &lt;code&gt;new&lt;/code&gt; keyword within Java will allocate a new memory location of an Object. When the Object is then passed around the application, it will be the reference location that is copied and sent across. In the below code snippet, the creation of a new Student Object will result in a new Object being referenced with its own address. Assume the Student Alice has been passed in.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;changeStudent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; student&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;//student = (copy of) Alice: location a&lt;/span&gt;
    student&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alex&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;                &lt;span class=&quot;token comment&quot;&gt;//student = location a&lt;/span&gt;
    student &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;//student = location c&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alice&apos;s name is first changed to &quot;Alex&quot;. As the reference location is passed into the method, the name will be changed on the Alice instance.
Once the student is referenced to a &apos;new&apos; Object, the memory location of the Object has been changed and all further method calls to
the Object &apos;student&apos; within the method will apply to the the Student instance of Charlie.
If you were to query the name of Alice after this method has run, the output would remain as &quot;Alice&quot;. The reference of
Charlie will be lost after the method as it is no longer referenced within the method. &lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 43.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACnUlEQVQozz2SXUiTYRTHz9692XTN+ZGKwTQ1XcgaFPRBXUXf4IWUSFd50UV0IxGYlCWmmaMSYzpDna7a/Nzm5ua7D+deXXOz6cCFNjK0D3UkdiEWZJl5et6RHfg9z3MO5zmc838eAIBEQj5BRvP5sl1xsRwS4sfHx0EswDka0QCllW0SzUCoutPxzqxlZlQN7fbDiAgZ2VIByd1J4BPyCEARpFwxsucQ9nAJR6RcHIHcESwHrpzpcoTnmrQstvaNYcNzF760z26Zg+uF+ldfwDW9wdXgLDO6erRKHtP0EFYn7alLbM/pyKheztSLaYwAfNJByaJVgiZ2BjXm0IZSN7o5YPH+dBsNOOT0zxUaMYbr9J9lcUusSBi3n+x7H5ReLbE2VfuG1Y/qUpKTSLdUJplZ0tZp9nf0T2AnE/qtZz9sudy+P27Ghoxraq3RuphevsxNgsDj8aIFszkNRWLRQUG8SE7O+zg/WQyy9LRETgLR3ae2KpNzHIMBJ3rHXLg8lIuTvlbUuH/0sQsIY7ObUK9m/o9MCYQJMY81tQaV/kngfmPFJRI7TmQ8RHROTU2i04gvfGF5W24a+RgxexZWWS/7fdjr62nU+Qu6nWFVtyPc0ud6X5MjlcuAT9MQswNA0aHIr1LeOXZDPSCea4ekFTMYtRVQBMBLSU7gZ/d6msuYKYtlMOiYb9BbqhBNUKOyX1cbX6N28A0aRubxxMmCo1yHNCEq7DYeJb17jYFnOE7JVWUiiHyupbvcLV9N/nZ0TPci41Vov3XBLb+9/LLWuVhpcM/WWXxLNzOy8vK3X4d3vugC/2xxIb+kOJdCFAJOkk8TBrh9ESgq4xRYg7prjlB3nTNsVUwE7xWv26D/lw0OTPmaYXwFQW0MRDX8C9jVMXye8rBUAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Change student diagram&quot;
        title=&quot;Change student diagram&quot;
        src=&quot;/static/4b6ab4e4e77c66657f33e3b6e9b205df/5a46d/029_changeStudent.png&quot;
        srcset=&quot;/static/4b6ab4e4e77c66657f33e3b6e9b205df/1db73/029_changeStudent.png 75w,
/static/4b6ab4e4e77c66657f33e3b6e9b205df/8a4e8/029_changeStudent.png 150w,
/static/4b6ab4e4e77c66657f33e3b6e9b205df/5a46d/029_changeStudent.png 300w,
/static/4b6ab4e4e77c66657f33e3b6e9b205df/fc2a6/029_changeStudent.png 450w,
/static/4b6ab4e4e77c66657f33e3b6e9b205df/0a47e/029_changeStudent.png 600w,
/static/4b6ab4e4e77c66657f33e3b6e9b205df/ddc81/029_changeStudent.png 837w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The heap space and stack memory are two separate areas within the RAM used by the JVM before and during runtime. The two areas work together by using a pass-by-value process where the address of the Objects within the heap space are copied and passed to the stack memory. The pass-by-value process enables the JVM to access and modify Objects without having to return them back to the calling method.
&lt;/p&gt;
&lt;p&gt;
The stack memory is designed with a pointer to quickly navigate methods and remove them once completed. The heap space uses a garbage collector to mark and sweep Objects that are no longer referenced within the application. 
&lt;/p&gt;
&lt;p&gt;
Our understanding of the heap and stack can enable us to better understand runtime errors that may arise and how the application can make use of the stack direct addressing and recursive nature to optimise runtime performance. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How continuous delivery supports functional testing]]></title><description><![CDATA[Explore the value continuous delivery can bring during the phases of functional testing.]]></description><link>https://aneesh.co.uk/how-continuous-delivery-supports-functional-testing</link><guid isPermaLink="false">https://aneesh.co.uk/how-continuous-delivery-supports-functional-testing</guid><pubDate>Mon, 12 Oct 2020 00:00:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the importance of functional testing before software deployment.&lt;br&gt;
&amp;#8226; Identify the challenges that arise with functional testing.&lt;br&gt;
&amp;#8226; Review the benefits of using continuous delivery and the Jenkins pipeline.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is continuous delivery?&lt;/h4&gt;
&lt;p&gt;
Continuous delivery (CD) extends from continuous integration by ensuring the latest software artifact is always in a deployment-ready build to be sent into production. You can read about continuous integration from my previous blog &lt;a href=&quot;https://aneesh.co.uk/how-continuous-integration-helps-developers&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
But what does &apos;deployment-ready&apos; mean? While CI aims to manage the movement of source code branches into new application builds, CD aims to manage the movement of the build through functional testing phases in a safe, sustainable and predictable manner. 
&lt;/p&gt;
&lt;p&gt;
The phases of functional testing can include rigorous testing and deployments across environments to ensure the software has been stressed in every feasible way before moving it into production. The diagram below visualises possible continuous delivery processes in a pipeline:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 72%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAADmUlEQVQ4y42ST2hcRRzHJ7L1IPRfdjfE2IoHER8KpXjuyUORgqeeFBUUTwqCNw+elFqFtohgW4qKUtCmghj7arJp2vzbt9m/b997O/v+zMy+/91sTJPN1t00aWd/zm7qwZsDH37M9zPzm5nHQ3P5W+9rlCxolE5plMlVast2U5NJS5froSGrhMoaEwinMWvo6NDV5LLlDF3FIbLhWfL5S1+8hlSKL1btGPKaBwXdB53EUDCXoGgtQB4XQbPXhHNhRXVBp+GeM+ehWFeBRD3IqQww24BGax0mb/74LlrSch9WaFArO8Fi2QkVlfoKjlXFaqqK7teUCokU4ZSSLRxzlfq/zsNivppdrlrFgumv4ChQLl39+hRauHZxAhqzLwGZfhEcWeLOtHTfLkobOC91rWUJ7BuS8BKQm9IjOyN1rIErSB18R4K1hRc+/+iNE4tXzxyHB+bzp0+dfBZRNX/epS40iAcuC8CnDeh6OuyGBrTdOjRExhx3WEPGoPfYbbiWyEKo6SY4tgstz4XML1feRo6uny3VmzBf9nbmyz4v4YhbSoa7hQy3KkWuez2+rAY8b65zw25yOzvDvYHTqjxbW+NLFY/fKQfc9Vowd+3KmyjUc19uhQxItbi7Sur9rYD0d0Lc341wvxfU+xs+7W/6RFTSb/vOMH8Y1frdwOzf8/byQf07JDA7+f2w4dlOSMEu53aatsE7IeFiE38YY74dmrwdEC4OEZXyTmAP3aO4znvCiYP4Vkj5pvDdyNlraKqVc3bUFb/CGqi0Daa7CawwB35pDnAhB4rRhGzVh2Uthir2wS3eBr84C3W1AnnzHixXXMjVWhAEfw2e/BaySrlPGIvbphVQi8R+gwZxh6lxz1XjlmPE2PTjGmaxUXdjZrP4fqM6dOLzxNiOYoxZVDP9KG54d2/89O1p9Pvk5aOB/ufxCM8cC9UpCYzfUtu+ltqJzCQEpRS0s0nolZMQ3krt6FPpUF1Ie5XF9KY+mwbjehrYTJprv47BZmn0lWMvj6OctfSx2TI8gW3crf6AEEoIkoJDggOClGBskGd/PofapIREQ7ShzSLQryOwZQS7ZfTNpx+IJegwsiu331v31HlBZpWWvhLhk88dfebgkafH94+PpQ4kDx8anRgf2//6yVcTADBCVjIjazj3H767cOaJQbdEIpFEYXn+nQeePr3taX90mfqZyJ/aty8xMTIyMv74dkcEB9H/G6P/AE3lF+1k2e3dAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;CICD pipeline overview&quot;
        title=&quot;CICD pipeline overview&quot;
        src=&quot;/static/d4588067dc8f22064edc5c76e8fab3cf/5a46d/028_cicd.png&quot;
        srcset=&quot;/static/d4588067dc8f22064edc5c76e8fab3cf/1db73/028_cicd.png 75w,
/static/d4588067dc8f22064edc5c76e8fab3cf/8a4e8/028_cicd.png 150w,
/static/d4588067dc8f22064edc5c76e8fab3cf/5a46d/028_cicd.png 300w,
/static/d4588067dc8f22064edc5c76e8fab3cf/fc2a6/028_cicd.png 450w,
/static/d4588067dc8f22064edc5c76e8fab3cf/0a47e/028_cicd.png 600w,
/static/d4588067dc8f22064edc5c76e8fab3cf/1ac29/028_cicd.png 1022w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;CD encourages frequent delivery of software updates in the same manner that CI encourages the frequent integration of source code. Smaller, but more frequent deployments are less risky to creating a regression in production. CD eliminates the repetitive tasks that may arise for functional testing to return tighter feedback loops for the test team while enabling a faster time to market.
&lt;br&gt;
The approach of &apos;continuous deployment&apos; may often be mistaken with continuous delivery; continuous delivery differs as it requires a single manual approval stage prior to production deployment whereas continuous deployment is fully automated end-to-end.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;What are functional requirements?&lt;/h4&gt;
&lt;p&gt;
The functional requirements of a system can be described as the behaviours for inputs and outputs the system is expected to produce. The functional requirements can encapsulate all areas of the system from data processing, data manipulation and algorithms. Functional requirements are captured within use cases and enforced against non-functional requirements such as security and performance metrics.
&lt;/p&gt;
&lt;p&gt;
While unit testing appeals to the developers perspective of the problem, functional testing takes the users perspective. The CD pipeline is responsible for carrying out multiple streams of testing to ensure the latest build adheres to the functional requirements. Functional testing can be divided into further testing domains such as UI testing, load testing, integration testing and API testing. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Integration testing&lt;/strong&gt; involves testing a combination of software modules in unison. While unit testing involves the testing of individual components on their own, integration testing identifies dependent modules within the application and tests scenarios that occur between them. The main objective from integration testing is to test the interfaces between the modules. The value behind integration testing is to ensure each module can interact with each other as expected and to identify inconsistencies between module implementation.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;API testing&lt;/strong&gt; can fall under the integration testing umbrella where external APIs are tested to perform as expected for functionality and reliability. As modules may communicate with external systems, the API testing will cover the external layer of the system of communication. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Load testing&lt;/strong&gt; involves stress testing the system capabilities to handle increased levels of traffic and requests than expected. The typical expected usage of the software may be modelled and simulated as well as upper and lower levels of requests. The load testing can provide metrics for the quality of service provided by a system and identify potential bottlenecks or vulnerabilities of the system for certain traffic or use patterns.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Operating System testing&lt;/strong&gt; will test that the software is able to perform in the operating systems it is designed for as expected.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Release management with continuous delivery&lt;/h4&gt;
&lt;p&gt;
The release management for continuous delivery defines multiple stages of functional testing. The system will be tested within dedicated servers to replicate the production environment to some degree. The separation of a test environment from the production environment enables system changes to be phased into the production environment with confidence that due diligence has been carried out upon each system change.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;User acceptance testing&lt;/strong&gt; (UAT) is a process of verifying the software works for the user. UAT is used to assess if the system can support the day-to-day intended use of the system. The system requirements may be defined within a scope and tested upon both systematically and unsystematically to verify the outcome of the test.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The challenges of continuous delivery&lt;/h4&gt;
&lt;p&gt;
Continuous delivery can constitute a lengthy process where multiple timely tests are completed across multiple test servers. The functional testing process remains largely unchanged despite the size of the system update. As a result, the time consumption for continuous delivery is mostly fixed to the software delivery pipeline.
&lt;/p&gt;
&lt;p&gt;
The complexity of functional testing further extends to the type of tests that are run across environments to verify the software update. Unlike unit tests, integration tests and other functional tests incorporate multiple modules together to cover use cases and behaviours of the system. The tests, as stand alone functions, therefore require greater levels of resources and inspection to verify. 
&lt;/p&gt;
&lt;p&gt;
The configuration of the environments and the system to perform checks upon must remain consistent and reliable, however functional testing may alter the server environment and create inconsistencies. While dedicated environments are used for specific testing, it becomes a challenge to &apos;clean&apos; the environments for testing as and when is appropriate. The responsibility for ensuring tests are performed within a controlled environment introduces further challenges to continuous delivery.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Jenkins as a continuous delivery solution&lt;/h4&gt;
&lt;p&gt;
Jenkins supports CD pipelines to automate the functional testing of builds along with useful tools to resolve the specific challenges related to CD.
Amongst the plugins that come with Jenkins, support for multi-threaded testing and stashing archives make Jenkins a popular tool for CD.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Automated delivery pipeline&lt;/strong&gt;&lt;br&gt;
Similar to the CI pipeline, the Jenkins CD pipeline is an extension of automated procedures to run on the new software update. The CD activities can be lined-up and executed in succession where exceptions are reported back to the appropriate teams. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Concurrent delivery&lt;/strong&gt;&lt;br&gt;
The master-agent model of the Jenkins pipeline supports parallel processing. As functional testing may take place across multiple servers, the CD pipeline utilise parallel processing to divide tests across the servers at the same time. As a result, the CD pipeline can be streamlined for time efficiency. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Consistent configuration&lt;/strong&gt;&lt;br&gt;
Jenkins supports the use of stash to save a set of files to be later used in the workspace. Stashed files are present for the duration of the pipeline before being 
discarded at the end. The stash can support small file saves to a temporary directory to be later unstashed and used to create a consistent environment for testing. 
Some uses of stash can be for dependencies and configuration files. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The continuous delivery pipeline extends from continuous integration to verify the system upgrade meets functional requirements before being deployed into the 
production environment. Software testing is performed across different test environments that are designed to support functional testing and to replicate the production environment.
&lt;/p&gt;
&lt;p&gt;
The importance of continuous delivery is to support a reliable, predictable and safe deployment of software into production. The multitude of testing to be performed across environments for verification can create challenges of time consumption and intervention of testing before deploying system updates. Jenkins supports the continuous delivery pipeline by automating the functional testing of the system updates across the appropriate test environments. Jenkins also supports continuous delivery with parallel processing.
&lt;/p&gt;
&lt;p&gt;
Continuous delivery and continuous deployment play a major role in the modern software delivery cycle where code is rigorously integrated, built, tested and deployed to the production environment. The automation of continuous delivery enhances the feedback loop from functional testing to support frequent, incremental and efficient upgrades to software system. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How continuous integration helps developers]]></title><description><![CDATA[Understand how the value of continuous integration can be realised amongst teams of multiple developers.]]></description><link>https://aneesh.co.uk/how-continuous-integration-helps-developers</link><guid isPermaLink="false">https://aneesh.co.uk/how-continuous-integration-helps-developers</guid><pubDate>Mon, 05 Oct 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the challenges of integrating code between multiple developers.&lt;br&gt;
&amp;#8226; Review the phases of an application build and the responsibility of each phase.&lt;br&gt;
&amp;#8226; Understand the benefits of creating a continuous integration pipeline for code builds.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is continuous integration?&lt;/h4&gt;
&lt;p&gt;
Continuous integration is a development practice that automates the combined actions of compiling, building, testing and packaging source code into software. As software is frequently updated, the time cost of integrating code into the latest build can be high to the developer. Furthermore, the coordination between teams of multiple developers and the presence of merge conflicts can make the integration of code even more burdensome.
&lt;/p&gt;
&lt;p&gt;
Merge conflicts arise when multiple developers attempt to integrate code changes that are applied to the same file(s). Multiple developers may have a different idea for the implementation of a module and therefore a merge conflict must be manually reviewed before the code is updated to the single version. Merge conflicts become increasingly likely as larger integrations are performed at a single time.&lt;/p&gt;
&lt;p&gt;
Continuous integration enables code builds to be performed autonomously and quickly. The process for compiling, testing, building and packaging code is performed through an automated pipeline. As a result, continuous integration encourages the practice of small, daily integrations amongst developers to lower the likelihood and impact of merge conflicts. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Build tools&lt;/h4&gt;
&lt;p&gt;
Build tools automate the individual processes of creating executable builds from the source code. There are individual phases that are used within build tools to separately compile, build, test, package and deploy source code. Some of the popular build tools used are Ant, Maven and Gradle.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The lifecycle of a build&lt;/h4&gt;
&lt;p&gt;
The build contains three different in-built life-cycles that define how a build is constructed: default, clean, and site.&lt;br&gt;
&amp;#8226; default: handles project deployment.&lt;br&gt;
&amp;#8226; clean: cleans the project by removing the files generated from the previous build.&lt;br&gt;
&amp;#8226; site: creates the project&apos;s site documentation and reports.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The build lifecycle consists of multiple phases that enable the developer to have control over how the code is compiled:&lt;br&gt;
&amp;#8226; validate: validate the project includes all the necessary information.&lt;br&gt;
&amp;#8226; compile: compile the source code of the project.&lt;br&gt;
&amp;#8226; test: test the compiled source code with the unit testing framework.&lt;br&gt;
&amp;#8226; package: package the tested code into a distributable format (such as a war or jar file).&lt;br&gt;
&amp;#8226; verify: perform checks upon quality through integration tests.&lt;br&gt;
&amp;#8226; install: install the package into the local repository.&lt;br&gt;
&amp;#8226; deploy: copy the final package into the the remote repository to be shared.
&lt;/p&gt;
&lt;p&gt;
Each lifecycle phase can be run individually, however the former stages will always be run prior to the phase defined. For example, running the install phase will inadvertently run the validate, compile, test, package and verify phases before install.&lt;br&gt;
The continuous integration phases are only relevant up to the package; these are explained further depth for a java project:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Validate&lt;/strong&gt; will not perform any actions unless a &apos;goal&apos; is specified for this phase within the POM.xml file. A simple validation check could reflect the version naming conventions, artifact ids, descriptions and purpose of each class is defined with JavaDocs etc.  
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Compile&lt;/strong&gt; will take the source code of the .java files and compile them into bytecode (.class) files for the JVM to later execute at runtime. The JIT Java compiler will then convert the bytecode into machine code to be executed.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Test&lt;/strong&gt; will compile the unit test code into bytecode and run each of the unit tests for the project. If a test were to fail, the lifecycle will end.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Package&lt;/strong&gt; will take the compiled code from the .class files and package them into a executable format such as a .jar file. The pacakge phase is to be defined within the POM.xml file. For example:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt; &amp;lt;plugin&gt;
    &amp;lt;artifactId&gt;maven-assembly-plugin&amp;lt;/artifactId&gt;
    &amp;lt;configuration&gt;
        &amp;lt;descriptorRefs&gt;
            &amp;lt;descriptorRef&gt;jar&amp;lt;/descriptorRef&gt;
        &amp;lt;/descriptorRefs&gt;
        &amp;lt;archive&gt;
            &amp;lt;manifestFile&gt;src/main/java/META-INF/MANIFEST.MF&amp;lt;/manifestFile&gt;
        &amp;lt;/archive&gt;
    &amp;lt;/configuration&gt;
    &amp;lt;executions&gt;
        &amp;lt;execution&gt;
            &amp;lt;phase&gt;package&amp;lt;/phase&gt;
            &amp;lt;goals&gt;
                &amp;lt;goal&gt;single&amp;lt;/goal&gt;
            &amp;lt;/goals&gt;
        &amp;lt;/execution&gt;
    &amp;lt;/executions&gt;
&amp;lt;/plugin&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Continuous integration tools&lt;/h4&gt;
&lt;p&gt;
CI tools can be used to streamline the process of compiling, building, testing and packaging software within teams. The tools provide an automated pipeline that can execute each of the phases 
while provided appropriate feedback, metrics and outcomes of each build as configured. &lt;strong&gt;Jenkins&lt;/strong&gt; is a popular open source automation server that offers the capacity of continuous integration 
for software systems as well as other convenient plugins that can optimise the pipeline for feedback and diagnostics. 
&lt;/p&gt;
&lt;p&gt;
A continuous integration pipeline is used to stitch the various phases of code integration into a production line to be delivered into environments. The goal behind the pipelines is to support the constant flow of software updates from all members of the team, to reduce the time it takes to integrate the code, and thus to encourage the frequent updates of code.
&lt;/p&gt;
&lt;p&gt;
The CI platform is used as a centralised repository of build reports, status&apos; and historical build updates for a project. Whereas before a CI platform was used, the console output and report from a build phase remained local to the developers machine, the console output is now available for review by all members of the team.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Jenkins support for CI&lt;/h4&gt;
&lt;p&gt;
With the use of the scripting language &lt;i&gt;groovy&lt;/i&gt; and an abundance of plugins, Jenkins provides the opportunity to customise the output of each build to provide insight and functionality to the pipeline.
&lt;/p&gt;
&lt;p&gt;
Jenkins can offer support into the build times for each phase to diagnose bottlenecks and to understand the implication of pipeline updates to the project. Jenkins supports the use of concurrent builds and can therefore be leveraged to resolve bottlenecks from independent stages.&lt;br&gt;
The graphic below provides a clear visualisation of the individual stages that can be defined within the groovy script.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/79584a0ad831334dd69b0ced7c8d35ff/a6d36/027_stages.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 37.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABRklEQVQoz11Ry46DMAzk/3+jn9JjL9UethQKgUB4BgjQ0AYWZpOgVlUtWY4945HtOOfzGcfjEZfLBafTCWmawti2bdZf9p2/at+5Yx7zPL8bPknzvGAYBhRFASml5fV9j7Zt0XUdlkXj42jxUUelZjifAuu6WsFXfD4f4JwjJKEVMKJtw1HkGeq6wjRNFieEoGkam9sJTaNpSJIEjDG7dlmWKKsGCWVIYgqWapGqRlZ08MNMR4GyqHRd85PdRSfgxAbgBsxBKYUQAkKvJe8SrM3h1R7C5oaA+6AiRtDF8NsYN+1xF9m6wf36Cj5yOG2v1xCD/ZDD4WBHV3/7CWpVIlAuPPmLUHlIFgIGArf/QYYIdA51/Yrbw7U8/qj1ytuqj62Q5zlc17V32L9MC8oKweiBjDfrVEZgioJOZI/36I0Fg4dGcvwDKuARPbEKZuEAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Stage report from Jenkins&quot;
        title=&quot;Stage report from Jenkins&quot;
        src=&quot;/static/79584a0ad831334dd69b0ced7c8d35ff/5a46d/027_stages.png&quot;
        srcset=&quot;/static/79584a0ad831334dd69b0ced7c8d35ff/1db73/027_stages.png 75w,
/static/79584a0ad831334dd69b0ced7c8d35ff/8a4e8/027_stages.png 150w,
/static/79584a0ad831334dd69b0ced7c8d35ff/5a46d/027_stages.png 300w,
/static/79584a0ad831334dd69b0ced7c8d35ff/fc2a6/027_stages.png 450w,
/static/79584a0ad831334dd69b0ced7c8d35ff/0a47e/027_stages.png 600w,
/static/79584a0ad831334dd69b0ced7c8d35ff/a6d36/027_stages.png 650w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Project build can be triggered within Jenkins in a variety of ways:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c94dc2e2b499eb20a43b5787a0d76cb8/eb390/027_trigger.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA2klEQVQoz5WSy26DMBBF+f/Pa9UuUGDRECg2b+MnSLeeoVRRFkm9OBohW5dzGbI8z1EUBZRSzLIsmOc5iSmi1AIpBbK2bUH0fY+u6zBNE6y1MMb8GxvRxqK8NkegEILfpLXmC977JELEOI/P8oasaRoQ67pi33eEEPiScy4NH3D7FkdgVVWo65otx3Fk0/vQZ3Z0ToardXi7XJFRXVoGGdI8q6cE0tTW46OsDkMpJYds28ZQ7VdBj4Y6Gr5fYiBVpe2elieplf8C6ZvRrzIMA8/zOany71K+6hY/xEy2Ik8lH4sAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Git trigger in Jenkins&quot;
        title=&quot;Git trigger in Jenkins&quot;
        src=&quot;/static/c94dc2e2b499eb20a43b5787a0d76cb8/5a46d/027_trigger.png&quot;
        srcset=&quot;/static/c94dc2e2b499eb20a43b5787a0d76cb8/1db73/027_trigger.png 75w,
/static/c94dc2e2b499eb20a43b5787a0d76cb8/8a4e8/027_trigger.png 150w,
/static/c94dc2e2b499eb20a43b5787a0d76cb8/5a46d/027_trigger.png 300w,
/static/c94dc2e2b499eb20a43b5787a0d76cb8/fc2a6/027_trigger.png 450w,
/static/c94dc2e2b499eb20a43b5787a0d76cb8/0a47e/027_trigger.png 600w,
/static/c94dc2e2b499eb20a43b5787a0d76cb8/eb390/027_trigger.png 935w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Jenkins can be integrated with Git repositories to initiate builds from commits. You can also set-up periodic builds to run in a more structured manner. As a centralised build tool between teams, the triggers for the builds can be used to share control of the pipeline across all teams to build as frequently, or infrequently, as desired.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Jenkins also supports exception handling within the pipeline. The console provides detailed log messages on each build:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/10b2c960830560bd61d5551362eb82f9/92d15/027_console1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAABYklEQVQoz4VTWW7CMBTM/Q/RS/QSSN0C/egPCQQUluyrY4LD1GMICmlQnzR6jmPPm3m2rSATePvxsXJd+L6PzWYDV49XqzUcx4HneQbn8xmMy+Vi0I/HYdnLEC+vNry1i/l8Dtv+wufHuyZb4nuxwGw2w0Ln8eYpMkNY5DnSJIIQAlI2KGqJsFaIqhZB2aJV141SSpxOJ7Rt+6BwCEOYa8IgCJBlGdI0NeM4SRHHCSKNLMvNvx5FUTxVx3mraRqUZYm6rlFVlVHadUqrkfe+TW1kKKX02u5RIatSVa5zVAj4qUAlpCnQqyaojJlzSZJoBzHojuA3RRnCXiHVXfsk//RmyuKzOUveCGmVDScpc6ftjBf/h/uhUDJth2FoLNAui5CcVpmnVI1P+04YRZG51CTswQIkYkt4XYYbeRjq5mBcxKKCXt12u8XhcDDNPx6P2O12Zo7/ON7v9+bV0NHw5QzjFz7yUw0f5Z9fAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Git trigger in Jenkins&quot;
        title=&quot;Git trigger in Jenkins&quot;
        src=&quot;/static/10b2c960830560bd61d5551362eb82f9/5a46d/027_console1.png&quot;
        srcset=&quot;/static/10b2c960830560bd61d5551362eb82f9/1db73/027_console1.png 75w,
/static/10b2c960830560bd61d5551362eb82f9/8a4e8/027_console1.png 150w,
/static/10b2c960830560bd61d5551362eb82f9/5a46d/027_console1.png 300w,
/static/10b2c960830560bd61d5551362eb82f9/fc2a6/027_console1.png 450w,
/static/10b2c960830560bd61d5551362eb82f9/92d15/027_console1.png 581w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e2346b1f0540b48e30d9be84f569ca6e/99072/027_console2.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 40%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAzUlEQVQoz42RCwuDMAyE+/9/acV3VdSqzfgCNwobbIXj0lfukoS2bW1ZFlvX1Y7jsOu6LOfsXIMz8G2VUt5x6LrOhmGwaZospWTzPDuz1znM2Xmenvy+b3uex6FkYk/YNI2N42h933uMa2HbNhfB/b7vXgms5B8JY4yejEe1cq3+a/FOCDiiLByQEKZXOBBwhcA/yx3SH0oDfIZVnva6k7CAEEz5xO6QHgGmDWtAgHawV4+J63cIIKahBRrPFPUBrqfLPYPTB7kHxLir1wsD0m4hEYsNHwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Git trigger in Jenkins&quot;
        title=&quot;Git trigger in Jenkins&quot;
        src=&quot;/static/e2346b1f0540b48e30d9be84f569ca6e/5a46d/027_console2.png&quot;
        srcset=&quot;/static/e2346b1f0540b48e30d9be84f569ca6e/1db73/027_console2.png 75w,
/static/e2346b1f0540b48e30d9be84f569ca6e/8a4e8/027_console2.png 150w,
/static/e2346b1f0540b48e30d9be84f569ca6e/5a46d/027_console2.png 300w,
/static/e2346b1f0540b48e30d9be84f569ca6e/fc2a6/027_console2.png 450w,
/static/e2346b1f0540b48e30d9be84f569ca6e/0a47e/027_console2.png 600w,
/static/e2346b1f0540b48e30d9be84f569ca6e/99072/027_console2.png 842w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Exceptions can be acted upon with email alerts for build successes and failures to alert the relevant teams and individuals for an end-to-end exception handling process to the software. &lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Continuous integration is a valuable framework used within DevOps with the objective of reducing the friction behind code updates across teams and streamlining the process for 
building a new artefact to be taken through delivery. Continuous integration can be defined by a single pipeline that performs each action upon the.
&lt;/p&gt;
&lt;p&gt;
Continuous integration tools can be used to build pipelines that automate the process for integration code updates into builds in a centralised environment.&lt;br&gt;
Jenkins is one such tool that can be used to build interactive reports to obtain an insight to the step-by-step process for builds. Jenkins also provides useful plugins to trigger builds, visualise performance reports and send email notifications upon events. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Comparing the mutual fund and exchange traded fund]]></title><description><![CDATA[Explore how mutual funds and exchange traded funds are created and traded through exchanges.]]></description><link>https://aneesh.co.uk/comparing-the-mutual-fund-and-exchange-traded-fund</link><guid isPermaLink="false">https://aneesh.co.uk/comparing-the-mutual-fund-and-exchange-traded-fund</guid><pubDate>Mon, 28 Sep 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Explore the benefits mutual funds and exchange traded funds provide investors.&lt;br&gt;
&amp;#8226; Understand the differences between a mutual fund and exchange traded fund.&lt;br&gt;
&amp;#8226; Review the creation redemption mechanism for exchange traded funds.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Diversified funds for investors&lt;/h4&gt;
&lt;p&gt;
Mutual funds and exchange traded funds (ETFs) are pooled investment vehicles that collect capital from multiple investors, both retail and institutional, to invest in a variety of securities to satisfy the fund&apos;s objectives.&lt;br&gt;
The investment funds offer the investor the opportunity to diversify their investment across securities through a single fund rather than manually selecting securities themselves to invest in. The benefit to the investor is their reduced exposure to the volatility of a single security and to spread their exposure across entire industries, regions, and global markets.
&lt;/p&gt;
&lt;p&gt;
The activity of investing within mutual funds and ETFs fall under the asset management division. Visit my blog &lt;a href=&quot;https://aneesh.co.uk/understanding-investment-bank-services&quot;&gt;here&lt;/a&gt; to learn about the different divisions and functions of investment banks. The institution that creates the mutual fund or ETF is known as the &lt;i&gt;investment company&lt;/i&gt;. The two types of funds can be further segmented and designed to meet investor objectives for short and long term as well as for risk and return potential. This blog will explore the properties of each fund and the consideration an investor may take when choosing to invest in either.&lt;/p&gt;
&lt;p&gt;
Below are general definitions of keywords used within this blog:&lt;br&gt;
&lt;i&gt;&lt;strong&gt;Investment company&lt;/strong&gt;&lt;/i&gt;: A corporation responsible for pooling capital from investors into securities.&lt;br&gt;
&lt;i&gt;&lt;strong&gt;Net asset value&lt;/strong&gt;&lt;/i&gt;: the average value of all assets held within a fund, used to value the fund.&lt;br&gt;
&lt;i&gt;&lt;strong&gt;Expense ratio&lt;/strong&gt;&lt;/i&gt;: an annual fee paid by the investor to the investment company as a percentage of the investment they make.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The different types of fund structures&lt;/h4&gt;
&lt;p&gt;
Fund structures define how capital moves into and out of the fund and when shares of the fund are created.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Open ended funds&lt;/strong&gt; do not set a limit on the amount of capital that it contains, and therefore the number of shares that can be purchased for the fund. If more capital flows into the open ended fund from investors, more capital is invested in the securities and the number of shares for the open ended fund grows at the same price.&lt;br&gt;
If the investors choose to sell their shares, the investment company will act as a market maker to buy-back the shares of the open ended fund. The freedom for the portfolio manager to buy and sell securities within the fund mean the investment mix can change.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Closed ended funds&lt;/strong&gt; will issue shares to the fund only once through an initial public offering. Once bought, shares from the fund are then traded through the stock exchange and are not bought back by the investment company itself. The price of the shares may be different to the NAV as a premium or discount reflecting the market&apos;s expectation for the NAV of the fund to grow or shrink with time.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Unit investment trusts&lt;/strong&gt; (UIT) are a less commonly traded fund structures that place a timeline upon the life of the fund. The investment company will buy securities with a defined expiration date for when the assets are sold and the shares of the UIT are returned to the investors.&lt;br&gt;
Shares are purchased at the NAV of the securities within the fund. Similar to closed ended funds, UITs will issue a set number of shares (units) that can be bought. The portfolio of investments for the UIT are established at inception and sold at termination. The investment company of the UIT may also act as a market maker to avoid the depletion of the UIT assets.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to mutual funds&lt;/h4&gt;
&lt;p&gt;
A mutual fund has it&apos;s shares traded at the end of each trading day and is often actively managed. Mutual funds are more often open-ended to support the growth of assets within the fund.
&lt;/p&gt;
&lt;p&gt;
The price of the mutual fund is derived from the average value of the investments that it holds. 
The &apos;Net Asset Value&apos; is used to define the price of each share from the mutual fund calculated as the market value of all assets less the liabilities divided by the total number of fund shares in circulation.&lt;br&gt;
For example if mutual fund &apos;M1&apos; contains:&lt;br&gt;
5 shares of A at £3&lt;br&gt;
10 shares of B at £5&lt;br&gt;
2 shares of C at £11&lt;br&gt;
If there are 10 shares of M1 for sale, the NAV = {(5 x 3) + (10 x 5) + (2 x 11)} / 10&lt;br&gt;
= 87 / 10 = £8.70 per share.&lt;br&gt;
If the share price of A rises to £4, the new NAV of the fund will be £9.20.
&lt;/p&gt;
&lt;p&gt;
As an &apos;actively managed fund&apos;, the strategy behind the mutual fund is typically very different to that of the ETF (as we will later review). Actively managed funds describe a fund strategy from the portfolio manager as to try and outperform the market. Outperforming the market requires the portfolio manager to selectively buy undervalued stocks and to sell overvalued stocks to profit from the bid-offer spread on the security. The portfolio manager will be supported by a team of research and credit analysts who offer advice in which securities to act upon. The objective of the actively managed fund is to therefore offer higher returns than the average market growth. While actively managed funds sound great, the investor will have to pay a higher expense ratio provided the additional expertise required to manage the fund, and can also expect a higher level of risk for the fund returns.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to Exchange Traded Funds&lt;/h4&gt;
&lt;p&gt;
Exchange traded funds differ from mutual funds as they are traded on the exchange &lt;u&gt;during the day&lt;/u&gt; at a price that closely reflect it&apos;s NAV. The function that enables ETFs to be traded during the day, known as the creation-redemption mechanism, is later reviewed in this blog.
&lt;/p&gt;
&lt;p&gt;
ETFs tend to be passively managed, meaning the fund will invest in a basket of securities from a particular group without trying to select the &apos;winner&apos; amongst it. Some passive funds, known as index funds, try to replicate the performance of an index. The performance for the fund is defined against how well they track the index and how low their expense ratio is.
&lt;/p&gt;
&lt;p&gt;
While mutual funds tend to be open-ended, ETFs are often open and closed ended. Index funds will aim to track an index such as the S&amp;P 500 or the FTSE 100 to obtain a broad market exposure for the investor. The  exposure diversifies the fund and reduces the risk of price volatility to the investor. As the securities within each ETF change throughout the day, the NAV of the fund will change, and the investors within the market will be able to trade against the market price of the fund.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The creation redemption mechanism&lt;/h4&gt;
&lt;p&gt;
One key difference between the ETF and mutual fund is the ability to trade the ETF like a stock during the day. How is the ETF priced? And where does the market liquidity originate from?&lt;br&gt;
The creation-redemption mechanism (CRM) that enables the ETF to be traded like a stock is supported by a third entity in the relationship between the market and the investment company, the authorised participant (AP).
An AP can be any large institution with enough buying power to purchase and hold securities to facilitate the liquidity of the ETF. 
&lt;/p&gt;
&lt;strong&gt;Creation&lt;/strong&gt;
&lt;p&gt;
When the market requires shares in the ETF that are otherwise not available, the AP will send an order to the investment company requesting new shares of the ETF. The capital from the investor flows to the AP where new securities are bought that reflect the ETF. The AP will swap the securities for new shares of the ETF with the investment company and return the new ETF shares to the investor. The package of ETF shares and securities are known as a &lt;i&gt;creation unit&lt;/i&gt;.
&lt;/p&gt;
&lt;strong&gt;Redemption&lt;/strong&gt;
&lt;p&gt;
When the market sells large amounts of the ETF shares, the AP will provide capital to the investor and exchange it for shares in the ETF. The AP will exchange the ETF shares with the investment company and will in-turn receive the underlying securities that the shares represented. The securities can then be sold in the market by the AP. 
&lt;/p&gt;
&lt;p&gt;
The creation and redemption mechanism of the ETF enable the shares of the ETF to be traded on the exchange during the day where the AP provides sufficient liquidity to the market for buying and selling shares and underlying securities of the ETF. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Pooled funds offer investors the opportunity to diversify their investments and to reduce their exposure to the volatility of a single security. The mutual fund and exchange traded fund are two popular investment funds that endorse different investment strategies to create varied risk exposure and returns for the investor.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAACbUlEQVQoz1VSy27TQBT1p/Ha8AWwQAKJlg0LKAtYIRXEQ22R2LJAAgkJFqwQQm3aJs2rcZz4ESfxY2wnsRNXaWI7CX0kgmbmcj2IBYujO3POuXPPtSxcelBcXn5Y+g9X10rL26/k5Z3XyvLWizrHzWe15Y11aXntUZl70r6Uu7uhLK+slX7jHa4/PqwIKxsqrG7+gwYrmwo/P/9oweZnB958cWH9gwUvPxFe72399a4gnrxrw9P3JqxuqTR95/7bhiTkSm3Yy+tsJ6uynX2FSWqA8NmPPY1tZ5vs246MVef37xmZlauE7eY0li22WCans+39BjsoGTST0+BQcquCbgwAwbSmD043gU4wA9sbQ8sMoWkOgOC5E0zB6yW8kk6U+lFDHWvLChFH1PYi6PZnkmCYPWibPUacEMbxOUTxHPphAsQNwbB8CAYJRMkCRtE5r4OjCVikz7W017SDtNKUjycLSShWHSiIhJWqLtR1nGYnUNP6UKgQKEse5Cs2aMYIdHMMTSsGGT0F5ETFT/u4p2nHVLcirJEk1NQe1Bs+Q2D0Y7A7U1xnCKLsQV3zQUbedGOwvAS1CbTJiGtKs4+DezggSHVqubhVZyIJvWBET88ZnZ1c0GS6oMlkgfETqqgmHY5+0tMzRiezXzRO5lw7Rs4wu/RoOKUnZ5T3oX4RJXNceS4K6RSx7kJV7uBaQ55Qa4dQqbk8YcpXlQ4YJMKUE+5JE0pqN92Ma+nZxIS2l6j421jOXr5FskWDSFpIdCsmouyTXeQKFYfkyhbJlUyitI5Jw4xIrTEgmYMm2Ud/QXRIFrX8IbE1Y9zD7/z1D4i8mtM7gynvAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Comparison table between mutual fund and ETF&quot;
        title=&quot;Comparison table between mutual fund and ETF&quot;
        src=&quot;/static/438e94513221989b5192698cf57b6e00/5a46d/026_table.png&quot;
        srcset=&quot;/static/438e94513221989b5192698cf57b6e00/1db73/026_table.png 75w,
/static/438e94513221989b5192698cf57b6e00/8a4e8/026_table.png 150w,
/static/438e94513221989b5192698cf57b6e00/5a46d/026_table.png 300w,
/static/438e94513221989b5192698cf57b6e00/fc2a6/026_table.png 450w,
/static/438e94513221989b5192698cf57b6e00/0a47e/026_table.png 600w,
/static/438e94513221989b5192698cf57b6e00/227ba/026_table.png 769w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
By understanding the differences between fund structures, active and passive management and associate costs, it can become easier to differentiate funds by their objectives and suitability to the investors goals.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The Data Access Object pattern]]></title><description><![CDATA[Use the data access object pattern to isolate the service and repository layers within a Spring application.]]></description><link>https://aneesh.co.uk/the-data-access-object-pattern</link><guid isPermaLink="false">https://aneesh.co.uk/the-data-access-object-pattern</guid><pubDate>Mon, 21 Sep 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand when to use the DAO pattern and the benefits it brings to software design.&lt;br&gt;
&amp;#8226; Explore the use of Spring data JPA as a repository layer.&lt;br&gt;
&amp;#8226; Implement the DAO pattern to support multiple repository layer implementations.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;The Data Access Object (DAO) pattern&lt;/h4&gt;
&lt;p&gt;
The DAO pattern is used within software development to separate the service layer from the repository layer of an application. The service layer of an application will apply business logic to a user request that is received from a controller and will send the request to the repository layer where it is persisted into a data store. To learn more about the multiple layers of an application, visit a previous blog &lt;a href=&quot;https://aneesh.co.uk/spring-architecture-layers&quot;&gt;here&lt;/a&gt;.
&lt;p&gt;
&lt;p&gt;
The DAO pattern uses dependency inversion to loosely couple the service layer from the repository layer. The DAO abstraction enables the service layer to remain agnostic to the implementation of the data store, whether that be a relational database, lightweight directory access protocol, business to business data store or any other type of persistance. The DAO supports the use of the open-closed principle where the service layer can remain unchanged while the underlying data store is dynamic.
&lt;/p&gt;
&lt;h4&gt;A top-down view of the DAO pattern&lt;/h4&gt;
&lt;p&gt;
The DAO pattern is divided between the service layer which intercepts a request from the controller, and the repository layer which is used within the service layer to persist the change to the data store.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 165.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAhCAYAAADZPosTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAE30lEQVRIx51W6VNTVxTn3+iXzvRDxw/aztjqTI1SrbZudZy6tp123EalRQGhKIuKLW61jrVabUepICM7ISVMp2OVnQSUUTAhIUEWkQg1eXnZXpKXl4Vf770vSamIpJyZ35zzzvnd88675y4vied5iKIIv9//UtDY5OQkgsHgjJypSKIDXiWhUAh6vR42mw2JSJLP52MGrWIqIpEI83u9XpSUlKC3t5c9U/+L3KlIomXGEsYkFqRisVigVqvR3d0NSZKmcadVOFNCKoIgoKysDKWlpSgqKoJOp4tXOaeEdP6sVivGxsYwPj7OXpBQhTQcDkcQjkxOA5WBx4MIBOTPnYkXib6ENMU/a+f6Db0IBYRZeawpUsCPHjOHqrsjULWOoq5lFMrmJww1TTJu1OtR8dcgs6sbR5imvLqWJ2wM9Q1ZXHKFAdGHtIsPsHBvM5YdbMOS1FYkE70xvwtrD2uxIacTm47dx3qitxbcw4bcTnz0jQZLvm6FgnLT2rBgVyMu15rkhKLfh4xLPVAcaMeHWRp8cKgD6450YufZhzhwUYe0SzqkX9Yh80ofQ+41I3acfoDlGR1YlakhybVYnNKGn2vNckL6yd/fMuDjI+3YdlyLrce02HxUg09PdOKzb0l1+RpsL+hksS0ktq2Aag2zKbaT57XZbWzK5ApFsl+lIKwODzinl0Bg2uYQiE8A7/aht8+EZ8952IlN/TJH+JdLtBQM/XfrvUpGBsjbI0igyxHaFAnmiV40GpVoNTeg1aRmaDHVo8lYhxbz7yj+80f88bCcxOvR3K8imnLqZS6x6VgLPxxrSgAX7mRgR+lC7K9Yhn3lCoIlxF6KbNVGZCrXI1O1FoeUMg5Wr8JeEpd5CqRUJuOLmwtQ030lulN8In5qykJqzXJ5YN0ahoy61bjQnIbie4U435SKXzpycbUjB+caU5CmXBXnZqnWY3+VArUPrkbXoV9CifYMqWQd8tRbkVu/mSGnflPczlNvieupfop8MiajdjVu91XEPlkkp7EIp9sGl8dOwMXhdHNwC3b0m3WwcuPMnhqPg/BCoWDiXX7UPwR/GIntZT85RXwjPXB2VMB1TwlXVy2c2iqCSgZPVw0eVZzDRGMx3MSW/VVwddYwrqtLCaemEuLfQ3KFXrJsuGu7wWW+Di53PrjDb4I/lQz+zArYj78D+4nFcBW+B0ehAvaCdwkWMT+X/xa4I/PA5c2HLe018A0/sGMwyUvm0H59Dxw58+AgA/j8BXBd+gRCaSo8v+2B58ZeuIt2QShLh+fmV8S3G57ifXCdX8O4jhOLwGe/AUfDOTmhLxAEV34Y1qMLYT2ZDOt3SwkUsBYug+3UclgJ7GdXwnZ6BbOZ/8zKKJfwTr6P53lvw9F4PboOyTUa9gsQraMIcBaCsThE21NIRI+beiBMDDObxYhf5lgYKC8siYl3WW80wSsGE+xy7JKi9+0LiJ0I/QY9fIJ7Rt5k9K6e9dbjBYkcAHbcNfK4Y7DD8MxDo6DhmW6+lyaM3XYtJNn2X/VIvWXCl0UGHFcNAlHe/0oYiSa8P+xEVvVj5NYNIb3yMS7eecrOvDknbB9wILVsAPmqYWRWD+L87VFS/VwSRm29xYM9xUbsvGHE59f6cLVpbNq/T0IJ5YmXn6VQBA6vBEEMzTp/VP4BWLsO7/H2e9kAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Layers of the application&quot;
        title=&quot;Layers of the application&quot;
        src=&quot;/static/382055482306a2a7f673e9a6a776bbf0/5a46d/025_layers.png&quot;
        srcset=&quot;/static/382055482306a2a7f673e9a6a776bbf0/1db73/025_layers.png 75w,
/static/382055482306a2a7f673e9a6a776bbf0/8a4e8/025_layers.png 150w,
/static/382055482306a2a7f673e9a6a776bbf0/5a46d/025_layers.png 300w,
/static/382055482306a2a7f673e9a6a776bbf0/fc2a6/025_layers.png 450w,
/static/382055482306a2a7f673e9a6a776bbf0/0a47e/025_layers.png 600w,
/static/382055482306a2a7f673e9a6a776bbf0/0012b/025_layers.png 661w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The DAO pattern introduces an abstracted relationship between the service layer and the data stores such that the service layer becomes agnostic to the implementations of the data store by the DAO:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 173.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAYAAACU9ioYAAAACXBIWXMAAAsTAAALEwEAmpwYAAAGMElEQVRIx6WWi1PU1xXH9//oZNpxMpkmbZqmE8cmfU6MzUwmTZU8nRjb1DYWkkBBJCCIiI3VaiqMTTpKRYtWl0dQHkEealjkTXjvLq9d9gkI7It9vz+9v98uiI1GZnpnvnPOPfd7z+/c3733nKtwuVwEg0H8fv99EQgEiEajRCKRB3LWQxEKhXhYm5qawmKxsJGmkCKQWjwe/xqkJkVWUVFBU1PTA3nroZCWu0pc31b7breb1tZWuru7WV3N+g9+LcL7OVzVw+EwSqWS8vJyysrKaGlp+f8cSsudnJxEo9GgVqsxGAwbdxiLxYnF/wexmDxmMpnWlntf3vp/GAgEH7pzg4MDOOy2h/KkmBUhEaHHF2TR7mHZ4V3Dkl2CB5cnxOCIFp1hDvtKQLat58lcAceKT3apWFhysau4m1fyutiRn8SBLt441MN2IX+T18kbRf3C3sOrBd3yuMRNye++yxf69gOdTBkdKIxzDrZlqXg+s5OtWZ2y3Lavk9S/j5B2aoTsz8ZJLx0R/WE+OqMm76yG94X9BcHdmpwj6T//UMWAdlFyaOdn799iS6qKZ9NU/DgpX87t4bXCPnYU9Aq9l+35vaQc7JNtv87rYcuf2mXes0n+M3+8lXDoWvFS3jjNJ1e0lFZNUFKplSH1T1zWyPLohWH+dmmck0nbySsaSla5Qp4S8nTNBDanfJdXdzl2H0TlEf2MmnBw5Rt4seQey+fw4clBPTKJ2xHYWHJwuZ0o+0o5qyri3O0j/GsdzqgKKbtdxLFr6ZQ075f1M+2FYqx4jSPP6SjmfOdR7G7xD+eWjaRe/iXvXXmOvVd+ImQCkn64aTcFDW+S27CDvPoUcutS+Khuxz28BPen7Lm0BY1lAIVVOMyoepH0z18go2Yb6QIZn2/jw5qtnLiZSll3Icfb9nJalS2wj9L2TJmzykvgV6RV/iLhcNl5h7827aWg/i0ONeyisOHtNRTUvcWBa69zsH6n0N8kP4nC+rfv4ckQNvOyLpEcAgEvKx6bgB2313FXuu0EQh7G1cMsLs3h9btku1uMJzgJKcHjdSU2ZSMlYGLGiC/MxnbZ4XQyfusac11XGfmigvmuWkaFtHbWMtZ0kcW+euo+LWbo6ll0X1ajb69m+qaS6RtKDKoaJtqUmG7XMt5WRSwcFFdvcoyB9zYxn7WJmf1PMVewGZ2QViH1WY9jyd+Mad93seZ8n9k/P4YxYxOGfY9jyP4epszH0Gc8iiXzUbR7v8V0/5coLFOjwskz+P6yBf+ZnQSVWYTqivCXvYO/fA/hhiP4Ln5AsDoH//k9hCqzCFxMI6TMJFiVQ6D8XTzFm3EcfBptVwuKmfFBevd8G9MHj2DO+QG6rCew5DzJjCRFZJJcOvwcxtwfYd7/BLqcp7HkPoU++0nMuT9MRJ3+HaZTH8E41IEiHPRj6r2OtvkSlo4aNNfF/+uoZrTxAraBRtneryylveITlvsbmGz9D2ZVFVrBM7VXMtV2mdlblejaa7HZbKIEBB9cAhadXiZnzTS2tTOoFsVKZ8QTin3zLgeDAeKiGMWjkWR2iWM26Onr7UE9Niq6USbGR/GuOAn6PIwMD/FVfx8LVlMiw4jxeCxCTMy/p0iFIlGW3SGW3GF6h8bFoXbL9qAIqGtQg9YwTzhZOe8sLdMzpGHBFRJzgkSisbXyKt8UqQye77BwtHGWE9cNHG+x0qN3MbvopbBWJN9WC8eajBxr1GNzB7g6tMSxZislrSYO1+mo6Z9fK7Fy1YsIZX/VNK9+Nsbvz2t56dQQVf0LqK1uXjk9yu/OadhVpua3Qi44A3wsPvyO6B/9wsBrYs7xpsQDICo7FBk7FImJfJiI4uNGA0caDNzQ2NBYPWRcnuL4dRMlbWZyqmeYc/j5x00zu89p+cOFCVI+HaO0zbTOYTDxD7Irp9h5Vs275VqZVPPVHcbMK7wsKt5uEdlOUfFe/+cYVruPU2KpmcppiupnSbs0ycnmeyJMJIe64SUO1ekFZkWEs6jnPDh9EU7fMMsRS/h317z4eJzb005ONBsFEpH36l13Xw6rL9h4RCw94Mfn8xENiVeruOhhcaSISunNL49JunzMIokxiRtO9uXXrsB/Ac/STO67lvM4AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Layers of the application with DAO pattern&quot;
        title=&quot;Layers of the application with DAO pattern&quot;
        src=&quot;/static/6263275cd0be287ceea9c96b06386104/5a46d/025_daoPattern.png&quot;
        srcset=&quot;/static/6263275cd0be287ceea9c96b06386104/1db73/025_daoPattern.png 75w,
/static/6263275cd0be287ceea9c96b06386104/8a4e8/025_daoPattern.png 150w,
/static/6263275cd0be287ceea9c96b06386104/5a46d/025_daoPattern.png 300w,
/static/6263275cd0be287ceea9c96b06386104/fc2a6/025_daoPattern.png 450w,
/static/6263275cd0be287ceea9c96b06386104/0a47e/025_daoPattern.png 600w,
/static/6263275cd0be287ceea9c96b06386104/bce72/025_daoPattern.png 633w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using the DAO design pattern&lt;/h4&gt;
&lt;p&gt;
The DAO pattern in this blog is being used to demonstrate an Estate agent application that will use the repository layer to store properties that are for sale.
The application will include a controller API to save, update or delete properties that are stored within the data store.
The DAO implementation is used to encapsulate the data store for the service to use, and to access.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;DAO Participants&lt;/h4&gt;
&lt;p&gt;
The DAO defines 6 key participants involved with the creation and execution of a request with the persisted data store:&lt;br&gt;
&amp;#8226; Business user: The request from the user to obtain or modify data.&lt;br&gt;
&amp;#8226; Service: the business logic service to process the request.&lt;br&gt;
&amp;#8226; Entity object: instances of the data as an Object representation.&lt;br&gt;
&amp;#8226; DAOInterface: interface with repository methods to interact with the data store.&lt;br&gt;
&amp;#8226; DAOImpl: individual implementations of DAOInterface to interact with different data storage options.&lt;br&gt;
&amp;#8226; Data store: the storage of the data in its persisted form.&lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 45.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB50lEQVQoz4WSTWsTURSG54e4cKEbd2JVsP4I1/4FN3XhooJBa6oItlaIHyCouBNjiJpOaUpLQfEjY4yVJDNJIzGFxGby1ckkk4zz8XgziQsh6IGXA/c853Dfc6/kOA6e502X6+ADVm4bY3WJ7sYDjOR9HMvEEwXPdQNuNGMUqqoijZrwPVH1xpmADOQ7dgAa0RDNS0c4CM/SvnoSt1MLzv1Rj+CCGSIKmiYG8v9w9DI9ccu6ItP8ts2vQf+vuj/JhdJ3JPPTS3qbDzGTEazMKm7/gK68hCkv0321iF1VsSs7GGsr1GK32Jfv0dqvYg5sLH0Pc+2OWMdt7OQK2a04UmP5HK3QCVrzx9Aj52kW0ugXD9NaOENj7hDNzcfosRu0hGVj4TTG4llqapq9hkFNWad55TiN0AzmtRkyz64j9YUVK5PASr9muPsRz7bof47TV+JYqSjtisawLix/SfDz7Qt0YXvQ6wYWPeFmxPZSMdydBLkPW0jOZAfeH3njjQxEKlaqtLp92loK4/1zqhtPqL+L0jM6Aef5ftDrTnaoFXeR/vUYrj0YP0oijDl/lMHNWazwKRh2pvI/ymWkbDZLPp+fKk38K7VYIvPmKcrdCyiROZRHl8l+TZNXNcHkAi6Xy1EqlZBlmd9dWoImFxbZ2AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Layers of the application&quot;
        title=&quot;Layers of the application&quot;
        src=&quot;/static/b13e94968b10a91da6b9f52802cf44d0/5a46d/025_diagram.png&quot;
        srcset=&quot;/static/b13e94968b10a91da6b9f52802cf44d0/1db73/025_diagram.png 75w,
/static/b13e94968b10a91da6b9f52802cf44d0/8a4e8/025_diagram.png 150w,
/static/b13e94968b10a91da6b9f52802cf44d0/5a46d/025_diagram.png 300w,
/static/b13e94968b10a91da6b9f52802cf44d0/fc2a6/025_diagram.png 450w,
/static/b13e94968b10a91da6b9f52802cf44d0/0a47e/025_diagram.png 600w,
/static/b13e94968b10a91da6b9f52802cf44d0/cc418/025_diagram.png 1992w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Implementing the DAO&lt;/h4&gt;
&lt;p&gt;
The DAO is implemented as an interface for the CRUD operations to be performed upon the data store. The implementations of the interface enable the growth of the application to support further data stores as the DAO interface is passed as a service to the service layer to use.
&lt;/p&gt;
&lt;p&gt;
The DAO interface exists below with descriptive operations to be performed upon the database:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PropertyDao&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperties&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;saveProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;deleteProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The DAO Implementation uses the Hibernate SessionFactory to create transactions to the data store:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Repository&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PropertyDaoImpl&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PropertyDao&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
	&lt;span class=&quot;token class-name&quot;&gt;SessionFactory&lt;/span&gt; sessionFactory&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperties&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;Session&lt;/span&gt; session &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sessionFactory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getCurrentSession&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;Query&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; query &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;from property order by id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		
		&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; query&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getResultList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;saveProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;Session&lt;/span&gt; session &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sessionFactory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getCurrentSession&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;saveOrUpdate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;Session&lt;/span&gt; session &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sessionFactory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getCurrentSession&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;deleteProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;Session&lt;/span&gt; session &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sessionFactory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getCurrentSession&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;Query&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; query &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;delete from Property where id=:id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		query&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;executeUpdate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Implementing the service layer&lt;/h4&gt;
&lt;p&gt;
The service layer is used to obtain requests from the controller to be passed down to the repository layer.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Service&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PropertyService&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PropertyDao&lt;/span&gt; repositoryService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Transactional&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		repositoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;saveProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;		
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Transactional&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getAllProperties&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; repositoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getProperties&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Transactional&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; repositoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
While it may appear to be an additional and unnecessary layer to access the PropertyDaoImpl, the service layer would typically provide additional functionality to the transaction such as logging, authorisation and security. 
The &lt;code&gt;PropertyDaoImpl&lt;/code&gt; is passed in as a service to the layer to provide access to the data store. Each method is marked as &lt;code&gt;@Transactional&lt;/code&gt; to assign transactional semantics to the method call. The semantics define the scope for a single database transaction using the &lt;code&gt;Session&lt;/code&gt; instance created to synchronize changes that are persisted to the data store.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Using Spring data JPA as a repository layer&lt;/h4&gt;
&lt;p&gt;
In the current application, the service layer creates transactional requests to the repository layer where a session with the data store is used. The DAO pattern is used to define the CRUD methods which are required to the data store.
&lt;/p&gt;
&lt;p&gt;
The use of the Hibernate Session objects in the repository layer requires verbose configuration to create the bean for the Session. The Spring Data JPA abstraction offers an out-of-the-box interface to support the CRUD operations made to a data store thus simplifying the implementation of the repository layer.
&lt;/p&gt;
&lt;p&gt;
The Spring Data JPA package can be obtained within the dependencies:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
    &amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;spring-boot-starter-data-jpa&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The Spring data JPA repository layer is implemented as an extension of the interface which can then be passed into the service layer as a service:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JpaRepositoryImpl&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JpaRepository&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;JpaRepositoryImpl&lt;/code&gt; extends the &lt;code&gt;JpaRepository&lt;/code&gt; interface and introduces two generic values for the interface. The &lt;code&gt;Property&lt;/code&gt; generic is used to identify the entity each record of the data store will coincide with. The &lt;code&gt;Integer&lt;/code&gt; identifies the type the primary key is for the table.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The interface is autowired into the service layer and provides the following methods amongst many others to support interaction with the data store:&lt;br&gt;
&amp;#8226; &lt;code&gt;findAll()&lt;/code&gt; to return a list of all records as Property instances from the data store.&lt;br&gt;
&amp;#8226; &lt;code&gt;findById(int id)&lt;/code&gt; to return a single record as a Property instance, identified by the primary key.&lt;br&gt;
&amp;#8226; &lt;code&gt;save(Property)&lt;/code&gt; to update or save a record to the data store.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
By implementing Spring data JPA into the application, the DAO is pre-defined to cover all the actions required to the data store. As a result, the extension of behaviour from the interface can be defined where the methods are hidden by the JPA abstraction. We can create a new extension of the interface with a different Entity Object as the Generic to define a new data store for the repository layer.&lt;br&gt;
The Spring data package encapsulates all method calls to the interface with @Transaction behind the scenes. As a result, the service layer will no longer require the @Transactional annotation upon each method. 
&lt;/p&gt;
&lt;p&gt;
The updated Service layer will use the Spring data JPA interface:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Service&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PropertyService&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JpaRepositoryImpl&lt;/span&gt; repositoryService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		
		repositoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;property&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;	
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getAllProperties&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; repositoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;findAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;Property&lt;/span&gt; property &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; repositoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;findById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; property&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Realising the benefits of DAO&lt;/h4&gt;
&lt;p&gt;
The DAO pattern generalises the transactions that a Service layer can make to the repository layer thus opening the Repository layer to implementations of the transactions. We can use Spring Hibernate to provide a granular approach to defining data stores, sessions and transactions to handle the different implementations of the Repository layer.
&lt;/p&gt;
&lt;p&gt;
As the extension of the repository layer is enabled by the DAO, we can use alternative strategies for the repository layer including the Spring data JPA repository implementation. Provided the DAO supports all the defined methods, the DAO can represent various Repository implementations across data storage medium, transaction definitions and sessions to benefit the application implementation and developer experience.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The data access object pattern is used to separate the service layer from the repository layer. As a result, multiple implementations of the repository layer can be used to represent different data stores and abstractions of the repository layer.&lt;br&gt;
The Hibernate and Spring data packages support multiple designs of the repository layer to provide varying abstractions of the transactions made to the data store. Furthermore, the DAO pattern enables the support of multiple data stores and repository layers.
&lt;/p&gt;
&lt;p&gt;
This blog has used the DAO pattern to support the growth of an application with multiple data stores. Two different strategies have been used to demonstrate how the DAO can be applied to different levels of abstractions of the repository layer. The specific support for database sessions and transactions can therefore be delegated to the implementations of the DAO to enable the repository layer to remain open to extension.
You can find the source code from this blog on GitHub &lt;a target=&quot;_blank&quot; href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/DAO&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating a Java GUI]]></title><description><![CDATA[Create a GUI from a Java application by using the Java Swing package.]]></description><link>https://aneesh.co.uk/creating-a-java-gui</link><guid isPermaLink="false">https://aneesh.co.uk/creating-a-java-gui</guid><pubDate>Mon, 14 Sep 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Explore the Java Swing package to create a GUI with Java.&lt;br&gt;
&amp;#8226; Understand how the Renderer interface can be used to create custom formatting.&lt;br&gt;
&amp;#8226; Use JTable to summarise array data into a table.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;The Java Swing package&lt;/h4&gt;
&lt;p&gt;
This blog post will use the Java Swing package to create a GUI from array data. The data will be formatted into a table and will render custom styling according to the values.
&lt;/p&gt;
&lt;p&gt;
The following three classes from the Swing package are used to create the GUI:&lt;br&gt;
&amp;#8226; JFrame: a top-level container.&lt;br&gt;
&amp;#8226; JTable: a 2-dimensional table.&lt;br&gt;
&amp;#8226; TableCellRenderer: custom formatting to the cells of the table.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The final GUI will show the English Championship Football table for the 2019-20 season:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 93.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAIAAAAf7rriAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC00lEQVQ4y1WUSXfaQBCE9f9/Sw4GG3jBJjHmgbUijSS0S6MVklwSBy/xLR8a55A5tGbp6qqumSfNtm1dN3w/6Lu+ZshGSsk3ieP9fi+Enxd50zRt29aybpt2PGXZlWWpCV9wVsrScs26k92pOY2jHwYpK9MxuraLk8iyrLqpXeH2fR9Evuu5fddpYeI3dRuk4s6aP+y/rJyF67pwlkWZlanh7zgVkRtFh6LK/dhr684KdD8SqNDiOB6GwfO82c3s7m5lGAZI0zSrqiqKgjlUYRCyRDaTYegd20nTjH0tiiLAlLi+vrYt63g8opnIWZ7nFGXu+35RFhihlkjLsgyUdjgcyCMul8uv9183Dw8XS6TEIZixkyT40zRlHxhLYpIkH2A+1L66ulqtVrqucwas6zqi4/wHZjKC93E8goMg4EOcTCaPj48kMS/y/AJGtjvay05R0LbjOMjGF9WshjAUhmG4WCyQPZvN8Jo7ZJOeUdGPdmZ5Dti0LDBcW5wkF8MAQyI873o6RfZ2uw3C8PImRrAQgiTA+QiGmeUFHMcfYCVsPp/fr9e248BMEt7CD7OyN88y+U82kA/DFDOG3dzcbDYbXKnGoZjVPaueqcX9g+Ge1evQ6J48wNPJ5Pb2drfbAYCKNwsYKlUaJHgEK9kYdpHNDLByG/B07JxsxUy3JKlXgRwFpvpHzzBDwm3T85fVan1/TyMUhhAAsIudQhSjbmUYFdMkZV/TDYMPzLPF4vNyudlu/TCEVj0S9aQ8wOiuKsO0jsNgw4xhMAcU422HwcNiYazXqWVm7r5pG8BVWUTCG47HSAg6llV5oNbpFNhWFkegtFbfFVI27v509WlYLfv5tHQsSKCqkrh2rLyuW9uq4qjK0trUC34Mpl4Gvmwa7ZesXt/fX759O7vOs6zez79f395eGcSnX8/H4eXtz8uP76/P55enp+ehJ/kSf/78fT7/BQeUvckS1pgjAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Final table output&quot;
        title=&quot;Final table output&quot;
        src=&quot;/static/f3d02a1aac9d0bb000cac11eba3a1d45/5a46d/024_finalTable.png&quot;
        srcset=&quot;/static/f3d02a1aac9d0bb000cac11eba3a1d45/1db73/024_finalTable.png 75w,
/static/f3d02a1aac9d0bb000cac11eba3a1d45/8a4e8/024_finalTable.png 150w,
/static/f3d02a1aac9d0bb000cac11eba3a1d45/5a46d/024_finalTable.png 300w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating the GUI container&lt;/h4&gt;
&lt;p&gt;
The &lt;code&gt;JFrame&lt;/code&gt; class is extended by the class &lt;code&gt;Frame&lt;/code&gt; to create the GUI container. The &lt;code&gt;Frame&lt;/code&gt; class has optionally used the Singleton design pattern to ensure we always reference the same container within the application.
&lt;/p&gt;
&lt;p&gt;
The Frame class is created with the Singleton design pattern to ensure the application consistently refers to the same GUI instance.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JFrame&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt; frame&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getFrameInstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frame &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            frame &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; frame&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The main method is used to create an instance of the Frame class. The JFrame class provides further methods that are called by the main method to define key properties for the container:&lt;br&gt;
&amp;#8226; &lt;code&gt;setVisible(boolean)&lt;/code&gt; to make the container visible.&lt;br&gt;
&amp;#8226; &lt;code&gt;setSize(x, y)&lt;/code&gt; to set the size of the frame.&lt;br&gt;
&amp;#8226; &lt;code&gt;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)&lt;/code&gt; to shutdown the application when the frame is closed.&lt;br&gt;
&amp;#8226; &lt;code&gt;setTitle()&lt;/code&gt; to add a title to the window.&lt;br&gt;
&amp;#8226; &lt;code&gt;setResizable(true)&lt;/code&gt; to allow the container to be resized by the mouse.&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt; frame &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Frame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getFrameInstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setVisible&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setSize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setDefaultCloseOperation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;JFrame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;EXIT_ON_CLOSE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setTitle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;My frame&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setResizable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
An alternative to the &lt;code&gt;setSize(x,y)&lt;/code&gt; method is &lt;code&gt;pack()&lt;/code&gt; which will automatically adjust the size of the frame so that all table content is at the preferred size. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Adding a Table to the GUI&lt;/h4&gt;
&lt;p&gt;
The &lt;code&gt;JTable&lt;/code&gt; class from the Swing package will be used to create the table. The JTable works similarly to a spreadsheet where each cell will have an x and y coordinate which is populated with data. 
&lt;/p&gt;
&lt;p&gt;
The class &lt;code&gt;TableBuilder&lt;/code&gt; will be used to create a JTable. It will contain a static instance of the table which will populate data through the JTable constructor that accepts (Object[][], Object[]) to define the data and columns respectively:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TableBuilder&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JTable&lt;/span&gt; table&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;buildTable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        table &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JTable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;David&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Joe&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Column 1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively, the data and columns can be obtained at runtime and passed into the JTable.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The JTable class offers further methods that can be used to modify the table including:&lt;br&gt;
&amp;#8226; &lt;code&gt;addColumn(Object[])&lt;/code&gt; to add in further columns to the table.&lt;br&gt;
&amp;#8226; &lt;code&gt;setValueAt(Object value, int row, int column)&lt;/code&gt; to update the value of a cell.&lt;br&gt;
&amp;#8226; &lt;code&gt;JTable(int row, int column)&lt;/code&gt; constructor to create a table with a set number of columns and rows.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Adding the table to the GUI&lt;/h4&gt;
&lt;p&gt;
The JFrame container is used to display &apos;Components&apos; that render information from the application. 
After creating the instance of the JTable in the main method through the TableBuilder class, the table can be added into the JFrame container through as a component.&lt;br&gt; 
The &lt;code&gt;JScrollPane&lt;/code&gt; class provides a scrollable view component for the GUI. From our main method, we will pass our table into a JScrollPane instance which is then added into the JFrame.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 53.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACEElEQVQoz52S22sTQRjF8y/5oC9FRIlolUBEURGhDz74aFXapFoKXiC2qEFL6YuC1RShQRNEVFArKaaxamwRac3dxGrMfU022exudn9uNiVFaRE9cGYYZuZ83zkzFrEuUiznKVUKFCt5amKNZrOJJEn/Rcv0wjWc/gNcfNKHw2/Hs3CVDnT+hKa3+Bss7hf9DPht3AicwfHIjvtlf1dL30Ra1zWD+oa0TM1f4dTMXs77j3Da28udoMu81FJlNFUxZ1otRKnK40/T/BAyXdENO1Q1BUEs8XF5iXpTwLhO7a2PnMtK/rqdnMGSwfjYHoZu97CcXewU1Fq/d2euNSxmNaNYMpHsVhGe3UQYsyK4bVQnDvNz/CDCqJWyZ2DNvr55hu1BMazF4jFTuX1UCNwlP7yFomsXuQs95C/vID+yldL9ITSzASOChmJQNlyp1MUGpbAPMRvtCKqqSjQaXQ/dyK1Z+oZWLRD5EGIpOEs9l0ZXJHM/sJjD5pjj0HAQ27kQx53PuTW4nei7mY5gG4lEgng8blhPkEylSKW/kvySIbP6nUgswUokZq5X0zHc90Lsd77hxGiYPleYY5fec3RkjkJZXBdsByvLMoqimLMsN80PrmuaSUlqUK832n7wPI2y7eQrdp8N0js4z87+1+xzBClUGmuPouv8C9LZGhMPPjPpizD5MMK4dwXvbMr4XS1+AZ/PE/VUeVdZAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Structure of components&quot;
        title=&quot;Structure of components&quot;
        src=&quot;/static/5cdffa163f70cf04bffdacfc28cfffdf/5a46d/024_structure.png&quot;
        srcset=&quot;/static/5cdffa163f70cf04bffdacfc28cfffdf/1db73/024_structure.png 75w,
/static/5cdffa163f70cf04bffdacfc28cfffdf/8a4e8/024_structure.png 150w,
/static/5cdffa163f70cf04bffdacfc28cfffdf/5a46d/024_structure.png 300w,
/static/5cdffa163f70cf04bffdacfc28cfffdf/fc2a6/024_structure.png 450w,
/static/5cdffa163f70cf04bffdacfc28cfffdf/0a47e/024_structure.png 600w,
/static/5cdffa163f70cf04bffdacfc28cfffdf/4ef49/024_structure.png 890w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;//main method&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;JTable&lt;/span&gt; table &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TableBuilder&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;buildTable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;JScrollPane&lt;/span&gt; viewComponent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JScrollPane&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;table&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;viewComponent&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we run the application, we will see the following GUI with the rows and columns:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAiklEQVQoz+2MvQ7DIAyEef+nSydgASR2foJUJnB7bq12SOiUrSedsA/fp7btRtZaCiGwjTFs7z055/nVWn+yt1//jl/pYlatNdr3SqUUqrVSjJGQzTlpjMHG/L1L1vudcs7ck75CACNIKT2POkEo/BJu0UFXOEvgyqdALOIj4JGOgOIl8Ex/4LXAB+QaBXUNZ70LAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Single Column Table&quot;
        title=&quot;Single Column Table&quot;
        src=&quot;/static/e05d2872b45b237b0ad447180ab0cd69/5a46d/024_table.png&quot;
        srcset=&quot;/static/e05d2872b45b237b0ad447180ab0cd69/1db73/024_table.png 75w,
/static/e05d2872b45b237b0ad447180ab0cd69/8a4e8/024_table.png 150w,
/static/e05d2872b45b237b0ad447180ab0cd69/5a46d/024_table.png 300w,
/static/e05d2872b45b237b0ad447180ab0cd69/fc2a6/024_table.png 450w,
/static/e05d2872b45b237b0ad447180ab0cd69/0a47e/024_table.png 600w,
/static/e05d2872b45b237b0ad447180ab0cd69/2ad15/024_table.png 801w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
We can further enhance the table to include more descriptive column names and values that are passed into the table.
For this example, I will use the final league table of the English Football Championship from 2019-20.
&lt;/p&gt;
&lt;p&gt;
The previous constructor of the JTable took two arguments of Object[][] data and Object[] columns. An alternative constructor can take a DefaultTableModel to build the table. The &lt;code&gt;DefaultTableModel&lt;/code&gt; uses a Vector of Vectors to store the cell values that can then be passed into the JTable as content. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;//TableBuilder class, buildTable() method&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/*data for table */&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; columns &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Pos&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Team&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Won&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Drawn&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Lost&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Points&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;DefaultTableModel&lt;/span&gt; model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DefaultTableModel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; columns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    table &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JTable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; table&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The current table appears below:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 49.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABFUlEQVQoz5WSB46EMAxFuf/xYBYh0XtH9A7e/ZaC2JmtkSzHEL98O5YejzeybYeiKKIwDD/2NnvTNHlvWRaboijkeT65rku+75NhGHzu2aSiKKiua6qqisqyZMMKgoAcx6GmaWjfdxqGgdZ1pXmeaZomatuWtm0j5CNXmJQkyQXDTwH0PI8V9n3PiQDCAwYoYlyU5/knMVKaphddQLFQsqZprBDKhAcIUFwEoFD4AhQfcCMW+oFeCWXjODIQ/jsgl3wHwiM+joPLVVX16hW8UAgo4ueSX4AwxOd5cg/xKPceAoj43woBxHjouv4l8P4ovwLjOL5KlmWZRwpAgJZloa7r/g6EZVnGCjHoKBn9QuL9UX4CvgMTQPbRw00X8QAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Championship Table&quot;
        title=&quot;Championship Table&quot;
        src=&quot;/static/2385109c60820013dd4b886a38ce5389/5a46d/024_smallTable.png&quot;
        srcset=&quot;/static/2385109c60820013dd4b886a38ce5389/1db73/024_smallTable.png 75w,
/static/2385109c60820013dd4b886a38ce5389/8a4e8/024_smallTable.png 150w,
/static/2385109c60820013dd4b886a38ce5389/5a46d/024_smallTable.png 300w,
/static/2385109c60820013dd4b886a38ce5389/fc2a6/024_smallTable.png 450w,
/static/2385109c60820013dd4b886a38ce5389/0a47e/024_smallTable.png 600w,
/static/2385109c60820013dd4b886a38ce5389/764be/024_smallTable.png 806w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;However if we remove the &lt;code&gt;frame.setSize(400, 400)&lt;/code&gt; call from the main method and replace it with &lt;code&gt;frame.pack()&lt;/code&gt;, the JFrame will automatically adapt to the size of the table:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 98.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAACJ0lEQVQ4y3WUh47CMBBE8/8fdkgBRAmiHr333ouPt9JEDuJWshI76/HM7DpBGKbdz0/KFYuR63a7Nlqtlmu3267X67lKuewy6bTL5/Ou0fi1b51Ox743m017ag8jqNfrbjqduuVyZQmVSsXN53PbVK1W3+tL+x5FRduwXq/fwA07bLPZ2LNUKrnJZGL7gv1+7xSj0chAhsOhWywWlkA8Hg+bP59PmwPEGsGBDOJ+v7tgt9vFgDDMZDImgYOUeLvdjJliu93aGsGhzInr9eoCTQjoZ7NZ2wwLHxDZr9fL5sgTIMzJ/QqI5CiKDHi1WlmyAMfjcQwIKwHq8K+ASE6lUq7f7zusmM1mMSC+CpB1Nouh7DBAoRNUFsmwOxwOcSJms9EHvFwu9o4V5MeA+KGgGLQADAGTZAB9D2F7Op1iP+W1AZLoSy4UCtZnJKmlAISVADlIklmXbViTYIjk8vtmwBSGvmTyvkmmQAkPqZ4PSB/C+ng8JiSzUYDskWRyEh7ih4J7GYahtQ0ydFOQInC1lxjCNuHhp+T0+0fAk7aRFK4Z4Lp6KBBDwBI3ZTAYxIC8UxT9MAQIQ/WkAP3GVvGsKD4gxeCm1Go1Y6QexUPJUqucz+eYYeKm4IcvGYaYTmPL7M8+BNC/KQkPfUCKksvlrHWQ6LcNh/ge+m3zb1GQrx8svujXRlH8Ow8AhxDk+BfA/of4wUAmc3qQuZ5UlKE81jXnnQFj9v8B8Yn2R8P+pqUAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Championship flex Table&quot;
        title=&quot;Championship flex Table&quot;
        src=&quot;/static/c6fd70a8c08efeda82407a8aa0c88847/5a46d/024_flexTable.png&quot;
        srcset=&quot;/static/c6fd70a8c08efeda82407a8aa0c88847/1db73/024_flexTable.png 75w,
/static/c6fd70a8c08efeda82407a8aa0c88847/8a4e8/024_flexTable.png 150w,
/static/c6fd70a8c08efeda82407a8aa0c88847/5a46d/024_flexTable.png 300w,
/static/c6fd70a8c08efeda82407a8aa0c88847/fc2a6/024_flexTable.png 450w,
/static/c6fd70a8c08efeda82407a8aa0c88847/f816d/024_flexTable.png 461w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Custom styling of the GUI&lt;/h4&gt;
&lt;p&gt;
The table can be enhanced by providing styling to the cells with the &lt;code&gt;TableCellRenderer&lt;/code&gt; interface from the Java Swing package. The interface is implemented by the &lt;code&gt;TableCellRendererImpl&lt;/code&gt; class and will override a single method &lt;code&gt;getTableCellRendererComponent&lt;/code&gt;. The getTableCellRendererComponent method is called for upon each cell of the table and can process logic according to the table, cell value, isSelected, hasFocus, row and column arguments. The arguments provide a rich interface for fine-grained control of rendering. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TableCellRendererImpl&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TableCellRenderer&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTableCellRendererComponent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;JTable&lt;/span&gt; table&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; isSelected&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; hasFocus&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; column&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The TableCellRendererImpl class will define an instance of the defaultTableCellRenderer which is used to return a styled component back to the table. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TableCellRenderer&lt;/span&gt; RENDERER &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DefaultTableCellRenderer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTableCellRendererComponent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;JTable&lt;/span&gt; table&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; isSelected&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; hasFocus&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; column&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;Component&lt;/span&gt; editor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; RENDERER&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getTableCellRendererComponent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;table&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; isSelected&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hasFocus&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; column&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        editor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setBackground&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;white&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        editor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setForeground&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;black&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;row &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            editor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setBackground&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lightGray&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; topOfLeague &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; table&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getModel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValueAt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; relegationZone &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;table&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getModel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValueAt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; relegated &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;relegationZone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;21&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;topOfLeague&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            editor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setForeground&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;green&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;darker&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;relegated&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            editor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setForeground&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;red&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; editor&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The following business logic is applied to enable the table to reflect the 1st position and final 3 positions in the table:
Line 6: The &lt;code&gt;editor&lt;/code&gt; is instantiated to define the table contents.&lt;br&gt;
Lines 7 &amp; 8: The default background and foreground colours are set.&lt;br&gt;
Lines 10 to 12: A soft background is applied to all even rows for visual effects.&lt;br&gt;
Lines 13 to 21: The foreground colours (red or green) are defined based upon the value of the &quot;Pos&quot; column of the row.
&lt;/p&gt;
&lt;p&gt;
Lastly, the implementation of TableCellRenderer is passed into the table through the &lt;code&gt;setDefaultRenderer&lt;/code&gt; call from the main method. The first argument of &apos;Object.class&apos; is used to set the default cell renderer for the column class. If the table consisted of an entity class, this could be used as the first argument to provide direct access to the class in the renderer.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;//main method&lt;/span&gt;
    table&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setDefaultRenderer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TableCellRendererImpl&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The finished GUI will show the table data with custom styling for the first and final 3 rows to highlight the champions and relegated clubs. As a built-in feature, JTable enables the columns to be switched around with a drag and drop to order the fields as desired.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 93.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAIAAAAf7rriAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC00lEQVQ4y1WUSXfaQBCE9f9/Sw4GG3jBJjHmgbUijSS0S6MVklwSBy/xLR8a55A5tGbp6qqumSfNtm1dN3w/6Lu+ZshGSsk3ieP9fi+Enxd50zRt29aybpt2PGXZlWWpCV9wVsrScs26k92pOY2jHwYpK9MxuraLk8iyrLqpXeH2fR9Evuu5fddpYeI3dRuk4s6aP+y/rJyF67pwlkWZlanh7zgVkRtFh6LK/dhr684KdD8SqNDiOB6GwfO82c3s7m5lGAZI0zSrqiqKgjlUYRCyRDaTYegd20nTjH0tiiLAlLi+vrYt63g8opnIWZ7nFGXu+35RFhihlkjLsgyUdjgcyCMul8uv9183Dw8XS6TEIZixkyT40zRlHxhLYpIkH2A+1L66ulqtVrqucwas6zqi4/wHZjKC93E8goMg4EOcTCaPj48kMS/y/AJGtjvay05R0LbjOMjGF9WshjAUhmG4WCyQPZvN8Jo7ZJOeUdGPdmZ5Dti0LDBcW5wkF8MAQyI873o6RfZ2uw3C8PImRrAQgiTA+QiGmeUFHMcfYCVsPp/fr9e248BMEt7CD7OyN88y+U82kA/DFDOG3dzcbDYbXKnGoZjVPaueqcX9g+Ge1evQ6J48wNPJ5Pb2drfbAYCKNwsYKlUaJHgEK9kYdpHNDLByG/B07JxsxUy3JKlXgRwFpvpHzzBDwm3T85fVan1/TyMUhhAAsIudQhSjbmUYFdMkZV/TDYMPzLPF4vNyudlu/TCEVj0S9aQ8wOiuKsO0jsNgw4xhMAcU422HwcNiYazXqWVm7r5pG8BVWUTCG47HSAg6llV5oNbpFNhWFkegtFbfFVI27v509WlYLfv5tHQsSKCqkrh2rLyuW9uq4qjK0trUC34Mpl4Gvmwa7ZesXt/fX759O7vOs6zez79f395eGcSnX8/H4eXtz8uP76/P55enp+ehJ/kSf/78fT7/BQeUvckS1pgjAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Final table output&quot;
        title=&quot;Final table output&quot;
        src=&quot;/static/f3d02a1aac9d0bb000cac11eba3a1d45/5a46d/024_finalTable.png&quot;
        srcset=&quot;/static/f3d02a1aac9d0bb000cac11eba3a1d45/1db73/024_finalTable.png 75w,
/static/f3d02a1aac9d0bb000cac11eba3a1d45/8a4e8/024_finalTable.png 150w,
/static/f3d02a1aac9d0bb000cac11eba3a1d45/5a46d/024_finalTable.png 300w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The Java Swing package supports the creation of components into a GUI for a Java application. In this blog, JTable has been used to summarise data into a table with further styling from the custom renderer.
The capacity for creating a GUI with Java extends much further than the JFrame and JTable; the Renderer interface supports dynamic styling and the capability for further components such as buttons and mouse-listeners.
&lt;/p&gt;
&lt;p&gt;
Creating a GUI can prove valuable for resolving data analysis and algorithmic solutions; I would encourage you to explore how the capacity for Java as a front-end component can be used to improve user interaction as a stand-alone application.
The source code from this blog can be found on GitHub &lt;a href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/JavaGui&quot;&gt;here&lt;/a&gt;. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Understanding investment bank services]]></title><description><![CDATA[An introduction to financial services and the divisions of an investment bank.]]></description><link>https://aneesh.co.uk/understanding-investment-bank-services</link><guid isPermaLink="false">https://aneesh.co.uk/understanding-investment-bank-services</guid><pubDate>Mon, 07 Sep 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the difference between retail banks and investment banks.&lt;br&gt;
&amp;#8226; Review the divisions that exist within an investment bank.&lt;br&gt;
&amp;#8226; Understand the core capability of each division within an investment bank.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;My first blog on financial services...&lt;/h4&gt;
&lt;p&gt;
For the past 5 months I have written about the software engineering practices and tools I encounter on a daily basis. I have decided to begin writing about financial services to accompany my technical blogs to reflect upon the context of my job. My bachelors degree and first job aligned to investment management and I continue to find the industry interesting, even more so due to the growing importance that technology plays in the long-term strategy for all the players within the industry.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to financial services&lt;/h4&gt;
&lt;p&gt;
Financial services can be divided into many sections, in this blog, I have split financial services by the clientele they serve into retail (also known as commercial banking) and investment banking. Both types of banking involve the matching of &apos;buyers&apos; and &apos;sellers&apos;, however they differ greatly by &apos;who&apos; they serve.
&lt;/p&gt;
&lt;p&gt;
Retail banking constitutes a service that is provided to individuals and small business owners who would like a loan or mortgage for their livelihoods. The bank will offer a loan to the mortgage &lt;strong&gt;buyers&lt;/strong&gt; with an interest rate of &apos;r&apos; (e.g 3%) on the repayments. On the other side, the retail bank will also accept deposits from &lt;strong&gt;sellers&lt;/strong&gt; who wish to save their excess funds. The seller is enticed to deposit their money for an interest repayment on their savings of &apos;s&apos; (e.g 2%). The retail bank will earn a profit through the loan repayments where &apos;r&apos; is greater than &apos;s&apos; (e.g 1%). This is a general idea of retail banking in the public sector, there are many more services provided within retail banking, however the remainder of this post will look into the operations of an investment bank.
&lt;/p&gt;
&lt;p&gt;
An investment bank provides advisory and funding services to customers including corporations, high net worth individuals (HNWI), and government entities. The services provided enable the use of capital markets for the clients and involve much larger volumes of capital. As a result, investment banking can be a more lucrative sector within financial services. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Divisions of an investment bank&lt;/h4&gt;
&lt;p&gt;
The umbrella term &apos;investment bank&apos; can often take different definitions and meaning. &apos;Investment bank&apos; must not be mistaken for the &apos;investment banking division&apos; as the latter is used to describe a group of functions performed by an investment bank. The divisions are used to group the services provided by the bank and are categorised based upon the clients that are dealt with, the services provided, and the style of revenue generation to the bank. The divisions of a bank also supports the regulatory compliance towards information sharing, I will touch upon this topic later.
&lt;/p&gt;
&lt;p&gt;
The image below illustrates the divisions that can exist within an investment bank. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3a5ae02825e09bffa8556bb220f9d25c/01ccc/023_divisions.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADTUlEQVQ4y2WR+0+bdRjF++f4izFGY6Zxw2WY6XQEnTJozKQwoB1QuWwdpawrhXIpHRUmyGWUgo47Hb1Ai7UFNrmMlgLlEtcEWPwnHH07Pn7fV2NM/CYn3/d5vs978pxzVBnplKnIS4x92/TPnbB8GODRbxaGV22MxrvYjw7xcuQ+S4ce+p+aca02K4gfLzMZe8jIWptS+3eGkSQJ1euMRLkzwRs3wuQa4zT6KtGMvY1u/EM0nizmnJ+zX/omtvkiCh+/g3biPCXj53gYNVDr+Yy7vlyqPZ9g8uWRll6hygjC2727vKddQt2YoG3hDhXTWdTOXuGW91OCrmKOmr7GEdJRPn2RGtHXz2Qzut5Oz0od30ercfxawdCqFZlLlZHSGPuTZOlXKLTtYF+oo8pzCYPnKt/5cwgOFXHc+g0dwTKF0DCXQ8XURSY2u4QtLTwI6+lYLFfkn52BSkqnsbgOUDdtonfuix/raQwW0BHWYQjlC0INR8352IOlmBfUOKN6jL5r/LBUR533S8zzBdz15mIOqHmV/lMQig1Ng3vk1K9R2p7EFqih+snH3PNfRzd3WZF83KJWNrztzeFeIJ8azxXca630PW1QJHdG9Ayv2ZAUybKHPbu8WxpFbUnQvmDg1vR5qqYvU/6vh3nCQy3ayQtUzYj+1EeKZHnLpnkNjYEbIu12kCXLHuq7tnmrOMI1UxyLV0/JxDkqJ7O56clmrvsLDvTvKynfHP9A+HdJJH2B7ogBo/crTP48ap9cxSJIFclnryW6Z1Jct6xjHnzB7FYvLeF8kVwh7c90xGaN/GEvwJNwYg2pcUSKcUQ1LKfGcD9voOdZJd0rOibirWQElyDM8PPiCYbebZpHDrCPHWJxx7jv2qRzKonbn2JgJsmg/4g+7x5tjxNY3Vs8GE9hHRGK3JuYHm0w4DsUhGdyyqc4Jw/RtGygc2yKYJ6j7YhTZo9T1ZXANLBL/UCSuh+3aRC33Cu1xygRc/KMzrFFcWsMq2uP09M0KsTp9pzwbdsOOuce2s6kgrLOXVEnaRpNYXb9LjYR9/ALpSe/aZ1/z8l1kX2H5p9SMhWqrXic8NIG3tAq/l8EFv8DUc+H1xQE/vn+34yATyAUWSeR2OYvV93ptVyfhi0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Investment bank divisions&quot;
        title=&quot;Investment bank divisions&quot;
        src=&quot;/static/3a5ae02825e09bffa8556bb220f9d25c/5a46d/023_divisions.png&quot;
        srcset=&quot;/static/3a5ae02825e09bffa8556bb220f9d25c/1db73/023_divisions.png 75w,
/static/3a5ae02825e09bffa8556bb220f9d25c/8a4e8/023_divisions.png 150w,
/static/3a5ae02825e09bffa8556bb220f9d25c/5a46d/023_divisions.png 300w,
/static/3a5ae02825e09bffa8556bb220f9d25c/fc2a6/023_divisions.png 450w,
/static/3a5ae02825e09bffa8556bb220f9d25c/0a47e/023_divisions.png 600w,
/static/3a5ae02825e09bffa8556bb220f9d25c/01ccc/023_divisions.png 1399w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The &apos;Chinese wall&apos; that splits the investment banking division from the others is a virtual information barrier to separate non-public information and public information. The investment banking division will often have access to non-public information due to their research and client services, and must therefore use to a Chinese wall to prevent insider information from spilling into the other divisions.
&lt;/p&gt;
&lt;p&gt;
Some banks will define themselves as an investment bank when they provide services across multiple divisions, however other banks, which specialise in a certain division, will identify themselves to the division. For example Charles Stanley Wealth Management vs Barclays Investment bank. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Core services of the investment bank&lt;/h4&gt;
&lt;p&gt;
In the following section, I will describe the core services of each division. The terms &apos;buy side&apos; and &apos;sell side&apos; are often used to describe the nature of revenue generation for the function, they are briefly defined below:
&lt;br&gt;
&lt;i&gt;Buy side: services involved with directly investing capital to purchase securities.&lt;/i&gt;&lt;br&gt;
&lt;i&gt;Sell side: services involved with issuing, selling, or trading securities and advisory services.&lt;/i&gt;
&lt;/p&gt;
&lt;h5&gt;&lt;strong&gt;Investment banking division (IBD)&lt;/strong&gt;&lt;/h5&gt;
&lt;p&gt;
&lt;strong&gt;Underwriting&lt;/strong&gt;&lt;br&gt;
Underwriting is a primary service within IBD. The process of underwriting sees the bank support a client (business, corporation, government) with raising capital through the sale of stock (equity) or bonds (debt). The investment bank will receive a percentage of the overall value of the underwritten securities as compensation.
&lt;/p&gt;
&lt;p&gt;
The underwriting process can take the form of either an initial public offering (IPO) or a follow-on offerings. An IPO refers to the process of offering the shares of a private corporation to the general public (thus making it a public corporation). The issuance of new stock is used to raise capital for the corporation. A follow-on offering is the issuance of stock after the IPO. During the IPO, the investment bank will make one of three agreements for underwriting:&lt;br&gt;
&amp;#8226; Full commitment. The investment bank will buy all the securities and sell them in a secondary market.&lt;br&gt;
&amp;#8226; Best efforts. A good faith promise is made to sell as much of the security as possible.&lt;br&gt;
&amp;#8226; All or none. The IPO will only go ahead if all the securities are sold.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Market Making&lt;/strong&gt;&lt;br&gt;
As a market maker, an investment bank can act on both sides of the sale for a security. A bid (buy) and ask (sell) price can be quoted by the investment bank and a profit can be realised from the spread (ask - buy) of the security when it is later sold. The investment bank will take on a risk of holding a security as it may depreciate with time, however their market making services provide liquidity to a security that would otherwise not be sold in the market.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Advisory&lt;/strong&gt;&lt;br&gt;
Advisory services include research and guidance on M&amp;A (merger and acquisition) and corporate strategy.
&lt;/p&gt;
&lt;p&gt;
The M&amp;A advisory service includes the negotiation and structuring of a merger or acquisition between two corporations. The corporations can be public or private, and the investment bank are able to provide support through their expansive network to find partners and deep-grained industry knowledge on the markets.
&lt;/p&gt;
&lt;p&gt;
Corporate strategy advisory can be used to support the short and long-term strategic direction of corporations in the ways of financing change.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;&lt;strong&gt;Asset management division&lt;/strong&gt;&lt;/h5&gt;
&lt;p&gt;
The asset management division is responsible for managing a fund of financial holdings from investors that a risk tolerance and short and long term goals. Investments will be made across instruments such as stocks, bonds, real estate, commodities and derivatives (futures, options, swaps). The client can be HNWI as well as high-end institutions. The relationship between a client and the fund manager is important to maintain as the clients may seek support in reevaluating goals or understanding the opportunities that lie ahead. The service of asset allocation provides advice on how to divide investable assets into growth products (stocks) and fixed income produces (bonds).
&lt;/p&gt;
&lt;p&gt;
Funds will be managed by a portfolio manager who will utilise the research and guidance of their teams to make investment decisions upon the fund. The fund will have a specific criteria to adhere to including the risk tolerance, type of securities that are invested upon, and liquidity.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;&lt;strong&gt;Capital Markets division&lt;/strong&gt;&lt;/h5&gt;
&lt;p&gt;
Capital markets are where suppliers with excess capital and those in need of capital meet to satisfy their ambitions. Capital markets in this sense are similar to retail banks with mortgages and savings accounts, however the entities involved are corporations, governments and dedicated individuals who understand and trade upon the market. Securities are exchanged within a primary market of newly issued securities and secondary markets for re-sold securities. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Sales&lt;/strong&gt;&lt;br&gt;
The sales component of capital markets will be responsible for suggesting trading ideas to their clients and to take orders from them. The sales component will also consist of financial analysts that provide support in research and strategies. Once made, the order will be communicated to the trading desks where they are priced and executed.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Proprietary Trading&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&apos;Prop&apos; trading is conducted by a set of traders who invest directly within the capital markets themselves on behalf of the bank, and not the client. The investment bank will directly profit from the market activity as opposed to the margins from client profits they realise. 
&lt;p&gt;
&lt;strong&gt;Structuring&lt;/strong&gt;&lt;br&gt;
Structured products provide access for retail investors to derivatives by using complex strategies to switch the underlying payment flows of a security with a fixed value that is derived from a derivative. The complexity of a structured product will require a blog itself to explain, however the summary is that it provides access for retail investors to complex derivatives.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;&lt;strong&gt;Private Banking division&lt;/strong&gt;&lt;/h5&gt;
&lt;p&gt;
The private banking division is split into two separate services that cater to HNWI: private banking and wealth management.
&lt;/p&gt;
&lt;p&gt;
Private banking provides a personalised and concierge-type service to HNWI for managing their finance. The products offered within private banking are largely identical to those offered within retail banking, however the client to the bank will experience a personalised level of service. Private banks will often use a 6-figure benchmark as a deposit for clients of the bank. The private bank will also offer advisory services for planning, retirement, taxation and inheritance for the client when managing their assets.
&lt;/p&gt;
&lt;p&gt;
Wealth management involves the optimization of a client&apos;s portfolio of assets with regards to the individuals risk, goals and asset preference. Wealth management is more closely aligned to the growth and protection of the clients assets and includes services such as estate planning, retirement planning and education planning. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;&lt;strong&gt;Private Equity division&lt;/strong&gt;&lt;/h5&gt;
&lt;p&gt;
The private equity division will invest in private institutions with the objective of achieving a mark-up in their value and a return on service fees. Private institutions will not have had an IPO and are therefore not traded on the stock exchange. The private equity division will have many funds that consist of investments from HNWI, corporations, and also loans from banks. The fund is used to purchase multiple private institutions to renovate or provide advisory services to. The growth and enhanced performance for the acquired institution will eventually drive the share price up enabling the private equity fund to realise the profits on the resale of the institution. The private equity division will realise profits from a fee of managing the fund and from the percentage share in the fund they had when the private institutions are sold.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Financial services is a broad industry that incorporates a vast array of services from advisory and research to execution and market making. The differences between these services can depend upon the client being served, the institutions being managed, and the active involvement with investments themselves. Services can be split between &apos;buy side&apos; and &apos;sell side&apos;, however the overarching principle within financial services is to aid in the movement of money. This blog has only touched upon the functions of each division, however I will use this as a foundation to reflect upon in later posts when I look slightly deeper into how financial services works and why technology has such an influential role in strategy.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The Dependency inversion principle]]></title><description><![CDATA[Dependency inversion enables loose-coupling of services to promote reusable modules and mitigate the risk of side effects from changes to low level classes.]]></description><link>https://aneesh.co.uk/the-dependency-inversion-principle</link><guid isPermaLink="false">https://aneesh.co.uk/the-dependency-inversion-principle</guid><pubDate>Mon, 31 Aug 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the use of dependency inversion between high and low level classes.&lt;br&gt;
&amp;#8226; Implement dependency inversion with abstraction to decouple class dependencies.&lt;br&gt;
&amp;#8226; Compare the open-closed principle and the dependency-inversion principle to each other.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Dependency inversion principle&lt;/h4&gt;
&lt;p&gt;
The dependency inversion principle (DIP) aims to decouple the high level classes from the low level classes that they use. In this post, I will walk through how high and low level classes can appear within and application, how their tightly-coupled relationship can cause problems, and how dependency inversion can be used to loosely couple the classes. We aim to loosely couple high and low level classes to remove the side-effects that can arise within the high level class when a change in the low-level class is made. The dependency inversion principle is closely linked to the Liskov substitution and open-closed principle of the SOLID design principles. 
&lt;p&gt;
&lt;p&gt;
The term &apos;dependency inversion&apos; must not be confused with the term &apos;dependency injection&apos;. Dependency injection involves a class demanding the use of another class. Whereas dependency inversion involves one class requesting that the dependency is handled by another class. We will cover this small detail a little bit more in the blog.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;High level and low level classes&lt;/h4&gt;
&lt;p&gt;
So what are high level and low level classes? In short, high level classes &lt;i&gt;depend upon&lt;/i&gt; or &lt;i&gt;use&lt;/i&gt; low level classes to carry out their behaviours. We can see this in action with the following example of a class that is used to check if flights to certain continents exist:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FlightChecker&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;EuropeFlights&lt;/span&gt; europeFlights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EuropeFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;AsiaFlights&lt;/span&gt; asiaFlights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AsiaFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;checkFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; flightNumber&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;europeFlights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flightNumber&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Flight to Europe exists&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;asiaFlights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flightNumber&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Flight to Asia exists&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;FlightChecker&lt;/code&gt; is tightly coupled to the lower level classes of &lt;code&gt;EuropeFlights&lt;/code&gt; and &lt;code&gt;AsiaFlights&lt;/code&gt;. As a result of the tight-coupling, if the low level classes were to change (such as changing the signature of the getFlights() method, the FlightChecker would break where the &lt;code&gt;getFlights()&lt;/code&gt; method is called. The &lt;code&gt;FlightChecker&lt;/code&gt; uses &lt;i&gt;dependency injection&lt;/i&gt; as it demands the application to provide an instance.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACW0lEQVQoz22R60uTURzH95/0qqI3kVEIXcjwRdBFmUhQWRCTFKGLgrpIc2XZNm3NdIp5TTIz5mZOMxOzi72JLpqkJGUvytrVZ3sebSu3fXr2NO/+4Mv3cH6/7/d8zzmqUCjEQkWjEYWHJ3vJsx6ksEutQPsoDW1XGgX2VJnVnLceoP6VjtUV81IFg8G4WZRwJKys+z62oXucgXnoApXPczEOZKN/egbDQBamZ+fQ9cm9wTzC8//nI/EgMa91DXtHW9G0JXK59xjabjllTwranlQuOtTkd6Vw1pqM/kk2kXB0Ubuu4cJJX5xjNL8uo/2tSeHS9hyK72ZSN6Sj472ZxuGrDE3YF3VrDWWjaPivjHmFF8oz7ebTyDg+l59Zf5Dx0Qmmv/1YenNlPq6RPVYkXF7OwG9GJqeYcvqIrOp99wX48Pkrv/xza3RxwxDiGzszjgoCAxamO2/wwqDB1X2T2f5KpLFBYheKITDSL++ZcTtMvJRnftr0BPqrZW054jsHwdAfVLMBAVfFETz5G3EXbsF7aSvitV14ixNw5m7AWXNyKXnVcVz5m/AWbUMq24u3ZCeeogQ8BZtxlR9iThJRiT43ntpTCLdSEKqPItRm4DOr8TdmMmM6jKspR7l2DK46DUJlGv6GTHy302XW4G/JQahKx205gSj4UMUeVWo4jVSaiKTfp5wsle1BMiQhlmxHeqhdTCjez0PUyXv6JKTru+WZ/UjGZKQrO5BasuR/iaDqtNmwtt7hgUVPR235StQYsLY10+1wKLDea6LDYljWN8psVLTW1no6bXb+AVxR1NExFGqiAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Tightly coupled classes&quot;
        title=&quot;Tightly coupled classes&quot;
        src=&quot;/static/4104fae0c909ec123abc834cc0a118b1/5a46d/022_coupled.png&quot;
        srcset=&quot;/static/4104fae0c909ec123abc834cc0a118b1/1db73/022_coupled.png 75w,
/static/4104fae0c909ec123abc834cc0a118b1/8a4e8/022_coupled.png 150w,
/static/4104fae0c909ec123abc834cc0a118b1/5a46d/022_coupled.png 300w,
/static/4104fae0c909ec123abc834cc0a118b1/fc2a6/022_coupled.png 450w,
/static/4104fae0c909ec123abc834cc0a118b1/0a47e/022_coupled.png 600w,
/static/4104fae0c909ec123abc834cc0a118b1/7422e/022_coupled.png 697w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
For this example, we are to assume the EuropeFlights and AsiaFlights classes will pull in all the details of available flights from their respective databases to the FlightChecker class.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introducing dependency inversion&lt;/h4&gt;
&lt;p&gt;
The use of dependency inversion is often associated to the following two rules:&lt;br&gt;
&lt;br&gt;
&lt;i&gt;&quot;High level classes do not depend upon low level classes. They depend upon abstractions.&quot;&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
&lt;i&gt;&quot;Abstractions do not depend upon details; details depend upon abstractions.&quot;&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
The two rules imply the high level classes must use abstractions to obtain behaviours, not through direct links with the low level classes. The second rule asserts that the details upon the low level classes are to be defined by the abstractions. Therefore the low level classes use the abstractions to define their own implementation.
&lt;/p&gt;
&lt;p&gt;
To achieve dependency inversion, the high level classes must be abstracted away from the low level. What we are aiming to achieve is a design where a change in the low level class does not break the high level class.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Abstracting the dependency&lt;/h4&gt;
&lt;p&gt;
To create the abstraction for the high level class, we must ask ourselves what &apos;contract&apos; we want to enforce upon the low level classes. As we can see from the current state of the example, both the EuropeFlights and AsiaFlights class use the &lt;code&gt;getFlights()&lt;/code&gt; method. Therefore I will create a new interface that has a single method &lt;code&gt;getFlights()&lt;/code&gt; and I will have the EuropeFlights and AsiaFlights classes implement the interface.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WorldFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EuropeFlights&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WorldFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; flights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

     &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Europe&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AsiaFlights&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WorldFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; flights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Asia&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The abstraction of the &lt;code&gt;WorldFlights&lt;/code&gt; interface is now passed into the &lt;code&gt;FlightChecker&lt;/code&gt;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FlightChecker&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;WorldFlights&lt;/span&gt; worldFlights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;checkFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; flightNumber&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;worldFlights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getClass&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;EuropeFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;
            worldFlights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getClass&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;AsiaFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;worldFlights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;       
                &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Flight to &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; worldFlights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; exist.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The above example has utilised the &lt;code&gt;worldFlights&lt;/code&gt; abstraction for the &lt;code&gt;EuropeFlights&lt;/code&gt; and &lt;code&gt;AsiaFlights&lt;/code&gt; class to create an abstraction between the low level class and the high level class. The class will check the instance type of the abstraction and return a print of the class. The &lt;code&gt;FlightChecker&lt;/code&gt; is now &lt;i&gt;inverting&lt;/i&gt; the responsibility of using the EuropeFlight or AsiaFlight class to the abstraction that will provide the implementation. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 87.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAADc0lEQVQ4y4WUW2yTdRjGmygC3hgviBfGC8UYIyExMRoCwcwoJIt6YfDGCzVjMhksO3XTFYzxlgQJm5NtMoPu4NgBZosb7gCrXRld6CZQtpXuyNh62tzX9bAe1q8//9/3lRF1jjd58vy/9/+8z/f2fdvqotEoKysra4jH4pAEVjeAuFd0D2rC4TBKtLa2ouNfEYgs0nKjkuYb5QIVaf5OPbfYNW6znyG4Iq3VyLKssslk0gxTqRRJOakmb90b4GhzBiXt75Hftg99+7sUXcwU53coEM8FF/aR3fgGo3N2Va/UJZNardFofGgop7SkY3YQw6UDVFuPUXPtKyotpZzo+ZyqfgOnzUVUWQ2UmT5gJG2o1G1o6HTfJKtuF7lNb3Gk6W0+a9jD4aYMDjXuJUfFm2TX72bCO5I2lNc3VKDEajKBNzCBR3LiCdxl/q8xrEOduJecIu9Sc77lSfFRV1kb1/8bKgwziwnG3DFc3jhOT4zm3wfFKIK4fAn1ecIXEy+WNzZMylqHd+YDbC7sZlPRFR4v7FV5a0kfj6XPm4uvoMvrptPhSS/lEYbXp5bYqu9jf/VtXvvWzuunhsiovMme8mF2nx4W/Ceb9H/QYLuvjehRhjZhqMvp4okSC1tKLTz55QC6/D62fW3jKcM1toi87lCPMJzd2FBOL2UhFCPvvINP627z8blhMk/2kHN+jP0nuvio1k5Wg4Os+luM+4LpLf/HUCwiuUpKfEFl9UJOA3yzk9wbH1XP89PjeKZdD34bKhS9UpdMxP/Z4XqhSIYcTlLpwSsxNOIivo42lWaj8Vd0cf804Tt9RJxWImP9hEctxCdsTJlbmRroWOuHRFTk2pjpbyc+fp3wiFnVKwg6rophRjD91oHOX3sYz9Fn8OpfxFP0vMbFL+DTC5TtIOKd0f407Je0nH47HqHxGnbiLd6O94uXmct5mlj/T5i6zeikluMsV35I8IdPCJ7NIvhjNqGfcwnWHkQ69T7hubuq4bLtAlLFAUJ1RwjV56n3oV+KCTUWIp3MJG49h7Hrquiw5iC+/Gfxl70isEPtwH/8VfylL6lvj3gmtQ4HL+IpeI6FYztVraJb+GaXWuPO3UbMfBaj0qEyu6XLFQR6a5B6qpB6qzXu/h7JUk8yHtWWtOQWeXHXfeahTmiUusXOcpDuY+q4zN9e67x722JMPgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Loosely coupled classes&quot;
        title=&quot;Loosely coupled classes&quot;
        src=&quot;/static/7b32df61979756e08682fed6be2e89e3/5a46d/022_decoupled.png&quot;
        srcset=&quot;/static/7b32df61979756e08682fed6be2e89e3/1db73/022_decoupled.png 75w,
/static/7b32df61979756e08682fed6be2e89e3/8a4e8/022_decoupled.png 150w,
/static/7b32df61979756e08682fed6be2e89e3/5a46d/022_decoupled.png 300w,
/static/7b32df61979756e08682fed6be2e89e3/fc2a6/022_decoupled.png 450w,
/static/7b32df61979756e08682fed6be2e89e3/0a47e/022_decoupled.png 600w,
/static/7b32df61979756e08682fed6be2e89e3/7422e/022_decoupled.png 697w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;But isn&apos;t this similar to the open-closed principle?&lt;/h4&gt;
&lt;p&gt;
Our use of abstractions with the DIP can sound familiar to the open-closed principle where we use abstractions to close a contract to modifications while leaving it open to extension of new implementations. Similar to the DIP, the OCP places an abstraction between two classes.
&lt;/p&gt;
&lt;p&gt;
If we take our example of the FlightChecker, the abstraction of the WorldFlights class has removed the direct dependency of the high level class (FlightChecker) from the lower-level classes (EuropeFlights and AsiaFlights). But the question remains: has dependency inversion inadvertently fulfilled the OCP, and if so, why are they separate principles?
&lt;/p&gt;
&lt;p&gt;
If we were to introduce a new class: &lt;code&gt;AmericasFlights&lt;/code&gt;, which will return the flights to America, the current design will support the &lt;u&gt;extension&lt;/u&gt; of behaviours as the AmericaFlights class can implement WorldFlights:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AmericasFlights&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WorldFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; flights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Flights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getFlights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flights&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Americas&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The dependency inversion principle does not however satisfy the &lt;i&gt;closed for modification&lt;/i&gt; principle. The extension of the WorldFlights interface does not naturally fit in with the FlightChecker class. The FlightChecker class requires an update on lines 7 &amp; 8 to check if the instance is of the &lt;code&gt;AmericasFlight&lt;/code&gt; class. As a result, the AmericasFlight class will not evaluate to true in the if-statement.  
&lt;/p&gt;
&lt;p&gt;
The example I have used deliberately evaluates against each implementation of the extension for WorldClass to illustrate how dependency inversion can be applied without satisfying the open-closed principle.
&lt;/p&gt;
&lt;p&gt;
When we think about dependency inversion, we are distributing the responsibility for detail to the lower level classes and generalising the higher level class. The higher level classes can generalise the requirements they have upon a dependency by delegating the responsibility of detail towards the lower level classes through an interface. 
&lt;/p&gt;
&lt;p&gt;
When we think about open-closed principle, we are again using abstractions to enforce certain properties and behaviours to be injected into the higher level class, however we are designing the application to be enhanced with new behaviours that accommodate to the schema for the abstraction. You may find the open-closed principle is supported through dependency inversion, however this would be dependent on the use of the lower level classes within the higher level classes and the requirement for extensibility in the application. For example, what if our lower-level classes used dependencies as well? Then we would need to further abstract behaviours to satisfy the OCP.
&lt;br&gt;
The subtle differences between the principles are important to individually evaluate our design upon. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The dependency inversion principle applies to the relationship between a high level class and the low level classes it uses (or depends upon) to perform certain behaviours. The direct relationship between a high and low level class can create a tight coupling where details of the implementation of behaviour can become lost and complicated between classes. 
&lt;/p&gt;
&lt;p&gt;
The introduction of an abstraction between the high and low level class creates two effects: &lt;br&gt;
&amp;#8226; The higher level class is able to generalise the dependency and behaviours it requires to eliminate any direct relationship with the lower level classes.&lt;br&gt;
&amp;#8226; The lower level class details depend upon the contract from the abstraction. They are no longer capable of breaking the higher level class from change.&lt;/p&gt;
&lt;p&gt;
If you would like to dive deeper into the ways dependency inversion can be applied with Spring, visit my blog &lt;a href=&quot;https://aneesh.co.uk/using-dependency-injection&quot; target=&quot;blank&quot;&gt;here&lt;/a&gt; for more information. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The Interface segregation principle]]></title><description><![CDATA[Interface segregation ensures the behaviours of an interface that are to be implemented are specifically defined to a single responsibility.]]></description><link>https://aneesh.co.uk/the-interface-segregation-principle</link><guid isPermaLink="false">https://aneesh.co.uk/the-interface-segregation-principle</guid><pubDate>Mon, 24 Aug 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the benefit of abstraction that interfaces bring to design.&lt;br&gt;
&amp;#8226; Review the problem of grouping multiple behaviours into a single abstraction.&lt;br&gt;
&amp;#8226; Leverage interface segregation to control the behaviours a class must implement from a combination of interfaces.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Interface segregation&lt;/h4&gt;
&lt;p&gt;
Interfaces allow us to achieve an abstraction of behaviours to be implemented by other classes. The abstraction of interfaces enables us to define the behaviours a class must have when they implement an interface &apos;contract&apos;.
&lt;/p&gt;
&lt;p&gt;
Interfaces have been prevalent throughout the SOLID principles, most notably used with the open-closed principle &lt;a target=&quot;_blank&quot; href=&quot;https://aneesh.co.uk/open-closed-principle&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
One of the differences between abstraction and inheritance is that a class can implement multiple interfaces, thus implementing behaviours from multiple classes, however a class can only ever extend a single class.&lt;br&gt;
The interface segregation principle (ISP) encourages well-defined and meaningful interfaces where the behaviours implemented by the class do not pollute its well-defined responsibility.&lt;/p&gt;
&lt;p&gt;
Much like the Liskov substitution principle, where we aim to inherit classes with the appropriate behaviours, the ISP aims to achieve a similar separation of behaviours for each implementation of an interface.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The contract of interface implementation&lt;/h4&gt;
&lt;p&gt;
The abstract contract of an interface requires the implementing class to take on &lt;u&gt;each&lt;/u&gt; behaviour within the interface, and not just selected behaviours. When we design an interface, we must therefore be considerate to the methods we are assigning &lt;u&gt;together&lt;/u&gt; as they form the responsibility for the implementing class.
&lt;/p&gt;
&lt;p&gt;
The case for interface segregation arises when classes implement an interface with methods that are not required by the class. As a result, the class obtains excessive behaviours that do not conform to the responsibility of the class. You may think &quot;why not just assign the behaviours without the interface?&quot;. This approach will not leverage the benefits of abstraction and can create further issues down the line as the application grows. The Strategy pattern can be valuable to use within our design, therefore we will aim to maintain the use of interfaces while paying consideration to the obstacle of implementing unwanted behaviours.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Interface segregation in action&lt;/h4&gt;
&lt;p&gt;
The below code example demonstrates an interface used for using the DAO design pattern for interacting with a database. The &lt;code&gt;DatabaseOps&lt;/code&gt; interface is used for reading and writing to the database and enables the implementations to make both types of requests.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DatabaseOps&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;testConnection&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
There may be many implementations of the &lt;code&gt;DatabaseOps&lt;/code&gt; interface, however the implementation of a class to only complete &lt;u&gt;one&lt;/u&gt; of the behaviours is not possible. An issue may arise where we would only like to designate the &lt;strong&gt;read&lt;/strong&gt; behaviour to a class, and not the write behaviour as well. The below method illustrates the use of the Strategy pattern where the DatabaseOps interface is a required interface implementation to make calls to the database:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;makeDatabaseTransaction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;DatabaseOps&lt;/span&gt; operation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    operation&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Segregating the interfaces&lt;/h4&gt;
&lt;p&gt;
We are able to achieve interface segregation by dividing the responsibility of the DatabaseOps interface into two further interfaces that &lt;u&gt;extend&lt;/u&gt; from the DatabaseOps interface. The &lt;code&gt;DatabaseOps&lt;/code&gt; interface is now split into two further interfaces, &lt;code&gt;ReadDatabaseOps&lt;/code&gt; and &lt;code&gt;WriteDatabaseOps&lt;/code&gt;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DatabaseOps&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;testConnection&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ReadDatabaseOps&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DatabaseOps&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WriteDatabaseOps&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DatabaseOps&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The image below illustrates the change of the interface design.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 101.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAACxElEQVQ4y5VVWU9TQRTuv/LB4BbjFjcSifrkk4mKWEJc2GR58MHEuMSYKBHjA48+80QhLW1vbxeghRaqCQREtpLS20u3295+3m/KXPaknPTLzJkz891zZs45dei6DsMwUCqVUCwWBarVqg3TNFGpVEChTikUCradtnw+L9aDwSAcJJOytrYmFqenpxGNRuH1erG4uCh0ricSCaiqCkVRkEwmEY/HsbKyYp+fmpqCgx5JSafTcLs91qEghoaGMDw8jNm5OUE+MTEhPkAkk78xMuKy9rqxsDBvn+ceB0OlmDvhSNH1LOoVeSWTk5O7hJRi0cCWlkMmm8d23oCmF8T8IDQ9L/aVy+XDhIWdkFNbOTx8q6Kxy4emHj/uvPZZ8Iu5BHXibq+CWx0+9A1GGZtFaO4hLNQI//zNoKF5FI3dKm52KrjeruBGh4Jrr/y48sKHy899uN0VsGwBXH3pR0PLOO73B6yoSvbr7yOc/6fhUpsbzs8x9P+cRfu3GfT+SKBnsIbu73F0DsyIkfamXhUP3qhWuhlHE9LD009GcdbpxfnWcZxzjuNCq1eAOnGmxSM8vdjmxalHbtzrU1AsHfBQpk06s42ur2E0vw/h2ccwWj6E8VTMIzsI2+vOTxE8fhfCl19xmBVDJP+eV64RVspWglcrh3Oialo/rlfFAwhUzZpulkWV7SOUHjIFmPXhcBixWExUB6tibGzMrgYZmsxY3h8JjwyZhKzrjY0NaJqGTEbD+vq6pacgc3VvjVNIdqyHskHQyDUmK9c4z+Vyu97VS0iC5eVleDwehEIh0QAikQhcLpcYaZfdp24PZSnVK9xfOi5tSJjNZpFKpUQbW11dFY9BbG5uirvlSJ13u7S0JO6anh8bMkkYLvscjX6/X8z56hzZnhg++14gEBAfJ9mRhHT9pCHLh9wXskwJ2cpPAv4VHPTwP0eqq3OlqxQHAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Interface&quot;
        title=&quot;Interface&quot;
        src=&quot;/static/036ebe4bd115bf22c094306cdd7fd4dc/5a46d/021_singleInt.png&quot;
        srcset=&quot;/static/036ebe4bd115bf22c094306cdd7fd4dc/1db73/021_singleInt.png 75w,
/static/036ebe4bd115bf22c094306cdd7fd4dc/8a4e8/021_singleInt.png 150w,
/static/036ebe4bd115bf22c094306cdd7fd4dc/5a46d/021_singleInt.png 300w,
/static/036ebe4bd115bf22c094306cdd7fd4dc/fc2a6/021_singleInt.png 450w,
/static/036ebe4bd115bf22c094306cdd7fd4dc/0a47e/021_singleInt.png 600w,
/static/036ebe4bd115bf22c094306cdd7fd4dc/5f4af/021_singleInt.png 665w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 82.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsTAAALEwEAmpwYAAAC40lEQVQ4y3VUWU8TURjtD/OBF00gKsEthJgo+mLUBCMBBEEkGIFEjTFqCCGGB0x8FY0JqDF2oXuLhdptpnvpMp3pvs60xzu3tHahX3Ly3d7vzHfPfPdMFaIoolqttlCv1yGHJEk0y/VarYb2aHK6n5WhaCfmcjmYzWYYjSaSLbAe/AHDeqE3GKHWaOi+SqWGxWKBw+GgDbpD0X6irCqZ5JESkhDLeWTTHOpSCfkMj0Q0hLQQR+w4CJ7nIQgCUS61FDeh6NxonJJIV7Crj+KnOYE9QxQ/THH8sibx3ZSA1k4a1Xtfv0ehDElqFLd3vRiYUGLksRYXZ/cxNK2mWcbglApMOE15tZPO7U1Pbbj1jcXZSQ1urphw47kZ42sHuLaoJ2sTPcQZ6GzYR+F/gsWVxMKmDasfnVjYsODuyh5Wth1Y3jrCq08OZPLlDmV9FfZGnV5GJpUAFw/jtOg7QznK5QoYhoXT6UIoHKGWyebyjYtKcPD5A4jFE6TuhMfjIfxepYruEyWxipokgmU9KBbyHSbPZFLw+VjUiV1EsULfoFupojHcGlLZEvhMCbmiCLuThTcQRbYo0X1qejK3bEEC44/gyMEiUxDBp0sQyDPNL6ml0PiXw+TbA3IRRwSHmHhtwOIHOx6t2zC3cQi9PU7XMmdm/RD3Xujw4I2V1m6vmfBV05ivVDtp+FkVwpn7SgzP6XDliR5jz8x0PTSjpXlH6Sce1GJweh8j8zqMr1pwddGAy4Q7MKHC5hems+Fva4yYV4NL81rceWnF1HsbxpZ0OD+jxuhTLQxE4a1VEy4QY48uGTD5zobry3oMz2pw7qESO0RQR8MSuV2PL4JghIPDE0QgwtN5JtPF1owC4Th8wThYfxQOJoyEUGhwUkVUqiKZX43yFCfTRCx6jFw2g1DQT26591+E4+IQeI4iSdb9QiH7SQZLPMcwDLxeH/nNwOVyEbgp3G43qbPElyzNMrdZa9ab+AcKEszQGnCRawAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Interface segregation&quot;
        title=&quot;Interface segregation&quot;
        src=&quot;/static/b6a3890c130ab30b7dad05c270305d00/5a46d/021_interfaceSeg.png&quot;
        srcset=&quot;/static/b6a3890c130ab30b7dad05c270305d00/1db73/021_interfaceSeg.png 75w,
/static/b6a3890c130ab30b7dad05c270305d00/8a4e8/021_interfaceSeg.png 150w,
/static/b6a3890c130ab30b7dad05c270305d00/5a46d/021_interfaceSeg.png 300w,
/static/b6a3890c130ab30b7dad05c270305d00/fc2a6/021_interfaceSeg.png 450w,
/static/b6a3890c130ab30b7dad05c270305d00/0a47e/021_interfaceSeg.png 600w,
/static/b6a3890c130ab30b7dad05c270305d00/8ae78/021_interfaceSeg.png 1096w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The change in interface design means the behaviours of reading and writing are separated, however they can both be contracted to a class that implements both the ReadDatabaseOps and WriteDatabaseOps interfaces.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;When to use interface segregation&lt;/h4&gt;
&lt;p&gt;
The interface segregation principle should be considered whenever we create an interface. Questions we may want to ask ourselves when designing an interface include: &quot;do we these behaviours to be grouped together?&quot; or &quot;will these behaviours be required within a class on their own?&quot;.
&lt;/p&gt;
&lt;p&gt;
We can use the extension of interfaces to design single-purpose, but related abstract contracts that prevent classes from defining behaviours they do not require. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The interface segregation principle extends upon previous SOLID principles including the single responsibility principle and the liskov substitution principle. When we define interfaces, the implementations of the class are asserted into implementing the behaviours of the interface together. The interface segregation principle aims to break-down the abstract behaviours of interfaces into smaller pieces that can prevent unwanted behaviours being implemented for a class. 
&lt;/p&gt;
&lt;p&gt;
The interface segregation principle leverages the inheritance of interfaces to break-down and separate behaviours, while class design enables a class to implement multiple interfaces to loosely regroup the behaviours that are required.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Series link: Go to the next SOLID principle: &lt;a href=&quot;https://aneesh.co.uk/dependency-inversion-principle&quot;&gt;Dependency Inversion Principle&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Liskov substitution principle]]></title><description><![CDATA[The Liskov substitution principle emphasises the importance to inspect the behaviours that subclasses inherit from their parents to ensure they can be completely substitutable for all instances of the parent.]]></description><link>https://aneesh.co.uk/liskov-substitution-principle</link><guid isPermaLink="false">https://aneesh.co.uk/liskov-substitution-principle</guid><pubDate>Mon, 17 Aug 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the importance of the Liskov substitution principle to inheritance and shared class behaviour.&lt;br&gt;
&amp;#8226; Review the rules that ensure the inherited behaviours of subclasses from their parents are as expected.&lt;br&gt;
&amp;#8226; Design tests to ensure the behaviour of a parent class is not modified by its subclasses.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is the Liskov Substitution Principle?&lt;/h4&gt;
&lt;p&gt;
Introduced by Barbara Liskov, the Liskov substitution principle (LSP) is used to decide when it is appropriate to extend a class as opposed to other strategies of abstraction to achieve the goal of shared properties between classes. As we have seen in the previous &lt;a href=&quot;https://www.aneesh.co.uk/open-closed-principle&quot; target=&quot;_blank&quot;&gt;blog&lt;/a&gt;, the open-closed principle can be used to abstract the behaviours of a method; the Liskov substitution principle is used to extend upon the &lt;i&gt;design by contract&lt;/i&gt; approach where the behaviour of the class is the primary concern.
&lt;/p&gt;
&lt;p&gt;
The Liskov substitution principle is used to make assertions upon the substitution of behaviours that are implied by a subclass from its parent.
When a class is extended, we are in effect implying that the subclass &quot;IS A&quot; extension of the parent class. While the open-closed principle is a focussed upon the structure of a class, LSP promotes strong &lt;strong&gt;behaviour&lt;/strong&gt; sub-typing of classes to the parent class to ensure the expected behaviour of the methods are applicable to the extension.
&lt;/p&gt;
&lt;p&gt;
The below quote is often used to describe the importance of behaviours between inherited classes:&lt;br&gt;
&lt;i&gt;&quot;If it looks like a duck, quacks like a duck, but needs batteries - you probably have the wrong abstraction&quot;&lt;/i&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Liskov Substitution by design&lt;/h4&gt;
&lt;p&gt;
When we use inheritance to share class methods and properties, we are implying that the subclass can be used to replace instances of the parent class.&lt;br&gt;
The LSP asserts three rules when applying inheritance with subclasses:&lt;br&gt;
&amp;#8226; The subclass cannot enforce stricter rules than the parent class.&lt;br&gt;
&amp;#8226; The return value of a subclass method can only differ as a subclass of the return type from the parent.&lt;br&gt;
&amp;#8226; The subclass of a parent must be &lt;u&gt;completely&lt;/u&gt; capable of substitution for its parent across each behaviour.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Implementing LSP by design&lt;/h4&gt;
&lt;p&gt;
If we reflect upon the rules above, the first two can be validated through the Java compiler and are therefore inherent with Java.
A problem can arise with ensuring the behaviour of each method is appropriate for the subclass of the parent.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Demonstration: Is a Penguin a bird?&lt;/h4&gt;
&lt;p&gt;
The below code example demonstrates how easily behaviour can be modified from the intuition that a subclass IS AN extension of a parent class. When we design an application of Birds, Penguins, and Seagull Objects, we may reflect upon the use of class inheritance by saying, out loud, &quot;a Penguin is a Bird, and a Seagull is also a Bird&quot;. In our source code, we may therefore create the Seagull and Penguin as subclasses of Bird. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACUklEQVQ4y61R3U+SURjn/6m28qoumhdF0Wp9b9zUxKyttWE1c63hWiPzwruWWWnJBisTeOEFxS/Y0lAiy2UxCxCUV0jInEqCycvHr3POCwx03vVsv53nPL+P9+EgE0UR/7NkmUyGNbl8jp1jfh4amxKPhhrwcLBegkMFLUHpTjkNr4Tru7HsLRQKDBWBWXb2fXoMlaEGavMR3OirxU3zMbS7rqN1pJ70cjZTm4+iznAABm97lXfbhtKQ/9KNa28O4TZ3Ao0mOW5xCjTzp9FkOUVmCjaj3NXXB2H83LF7IF2XVnJzFcH4V8wlviH0y8fg9NowPGlFYGkG4d+zjAvGZ7CeXqny7gisJEoVjfzEVloEXWIxEiPCan67hwXSUTaXJyhAzOaQJ4O/ZO4PBLG6tl4Wb6TT+OEP4E8qxTxUmyMe6s3npWDZVnHD6hKxHBfYWbFLuVteipCHS+9wsX+5QH7LYiKFnoEQQRi6wQW0vfLgidEHw4iALvschMQGUpsi9ENh6IcjeG71Q9vlRo9jHjpHGN32EKb90nvK6Jf591HsUzlx8q4H51s+4FzLR5zVeCFvcmNvnRM2dxShWBI1DU7UNo4TjRcX70/hzD0P8UxiT50LbfrZUmAO5ncCCRyFonmCBV5unYLygRfH70wQsROWMQEBYQ37r4zisHocF4qaSwTUQzVanY89i4ymxlZEPLNF8KJfQCcfYX1n8f7UusB4MQ+8dAiSpsiXPB1EMx1MShv29r4Fb+Xg6LfAYbdIZwnFO+U5zsz6gV00dp6DyWTCP80Lc/Hqzs/7AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Image of the classes&quot;
        title=&quot;Image of the classes&quot;
        src=&quot;/static/93c14cd7e0f83348e14471d44ca70c06/5a46d/020_bird.png&quot;
        srcset=&quot;/static/93c14cd7e0f83348e14471d44ca70c06/1db73/020_bird.png 75w,
/static/93c14cd7e0f83348e14471d44ca70c06/8a4e8/020_bird.png 150w,
/static/93c14cd7e0f83348e14471d44ca70c06/5a46d/020_bird.png 300w,
/static/93c14cd7e0f83348e14471d44ca70c06/fc2a6/020_bird.png 450w,
/static/93c14cd7e0f83348e14471d44ca70c06/0a47e/020_bird.png 600w,
/static/93c14cd7e0f83348e14471d44ca70c06/35a31/020_bird.png 1028w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Bird&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; hasWings &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fly&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hasWings&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;    
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Penguin&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Bird&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Seagull&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Bird&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
In our example above, we have confused the &lt;u&gt;properties&lt;/u&gt; of the Bird class with the &lt;u&gt;behaviours&lt;/u&gt; of the Bird class when creating the Penguin class. The Penguin is a bird because it has the properties of a bird: it has wings, lays hard-shelled eggs etc. However the Penguin does not &lt;u&gt;behave&lt;/u&gt; like a bird in the sense that it cannot fly.
&lt;/p&gt;
&lt;p&gt;
The LSP has been breached as the Penguin class is now capable of calling the &lt;code&gt;.fly()&lt;/code&gt; method; a behaviour that should not be possible.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Complying with LSP&lt;/h4&gt;
&lt;p&gt;
To resolve the problem, the behaviour of the Bird class can be further split into two: BirdCanFly and BirdCannotFly:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BirdCanFly&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; hasWings &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fly&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hasWings&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BirdCannotFly&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

   &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; hasWings &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hasWings&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Penguin and Seagull can now extend the appropriate class to obtain the Bird properties with and without the flying behaviours:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Penguin&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BirdCannotFly&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Seagull&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BirdCanFly&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The example above illustrates the potential conflict that can arise with the implied substitution from inheritance. The behaviours of flying can be further imposed by the parent class with methods such as &quot;getTopFlightSpeed&quot; and &quot;land&quot; that would imply further irrelevance for the Penguin class without the redefined &lt;code&gt;BirdCannotFly&lt;/code&gt; class. The 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The LSP is applied to application design when we consider the &lt;u&gt;behaviours&lt;/u&gt; of classes that are inherited from other parent classes. &lt;i&gt;Inheritance&lt;/i&gt; is one of the most valuable OOP concepts and therefore LSP is an important consideration to make upon child classes whenever a class is extended.
&lt;/p&gt;
&lt;p&gt;
While it may seem natural to extend classes that fit into the &quot;IS A&quot; Object construct, we must consider how the &lt;u&gt;behaviours&lt;/u&gt; of a class are inherited, and not just the &lt;u&gt;properties&lt;/u&gt;. LSP can be implemented by reviewing the behaviours each class will inherit before adding behaviours to a parent class or extending them, or they can be implemented through unit testing of a parent class with each of their child classes.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Series link: Go to the next SOLID principle: &lt;a href=&quot;https://aneesh.co.uk/interface-segregation-principle&quot;&gt;Interface segregation Principle&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The Open-closed principle]]></title><description><![CDATA[Software components must be open for extension, but closed to modification. This blog reviews how the open-closed principle can be implemented by design using the Strategy pattern.]]></description><link>https://aneesh.co.uk/the-open-closed-principle</link><guid isPermaLink="false">https://aneesh.co.uk/the-open-closed-principle</guid><pubDate>Mon, 10 Aug 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the importance behind the open-closed principle to code maintainability and growth.&lt;br&gt;
&amp;#8226; Identify when the open-closed principle should be used within an application.&lt;br&gt;
&amp;#8226; Use the Strategy design pattern to avoid breaking the open-closed principle.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is the open-closed principle?&lt;/h4&gt;
&lt;p&gt;
The open-closed principle aims to design behaviours that are open to extension, but also closed for modification. The two behaviours can sound like an oxy-moron, however they create two distinct characteristics that can be applied together.
&lt;/p&gt;
&lt;p&gt;
&lt;u&gt;Open for extension&lt;/u&gt; describes a method where the runtime execution can be modified when new components are required and added to the application.
&lt;/p&gt;
&lt;p&gt;
&lt;u&gt;Closed for modification&lt;/u&gt; design asserts that as new components are added to the application, the dependent components upon the new component do not require modification. The pre-existing behaviours will also work as designed despite new components being added. The new components therefore work themselves around the behaviours of the application.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why the open-closed principle is important&lt;/h4&gt;
&lt;p&gt;
The open-closed principle is important with respects to supporting the growth and enhancement of an application. As applications grow in size, dependencies and complexities arise which can make it difficult to provide additional functionality without breaking the existing co-dependencies. When we design applications, it is important to consider if certain features are likely to grow or exist in different states. The open-closed principle can be applied to ensure the growth of application behaviours are supported by the design.
&lt;/p&gt;
&lt;p&gt;
The below code sample is an example of a Console class that is able to play different games:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;CarGame&lt;/span&gt; game&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CarGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;loading car game&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e06608d5604dc9ea74368c49b5745240/9c2cf/019_closedDesign.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB2klEQVQoz52RTU8TURSG+59cGhMTWbhTUfQXuEBsgiRoadHoXuMHkQIuXDVNp8SE1pUkEKgEUFrLSGmnlWICyVDaSmrotPP5eDtTI8Y0EE/yzHuSmfvOec/14dCzzLaNul+hWCxR3i0jyzKKovT8vt1u46s2VPLqBgU17ZJ3NeNxkKF0uImN+dfBn5pJulAjrdSF1vm0XeO7eoyuC8N36WnuxC4ynrzF2Fw/oeRNgokbLqHkAH6pj/XCIs1Gi0q1itFqEPtQ5NztBa4/XKN/fI0Ld5cYfJbxDFPF9zydH2IqFWJyKdDVMV6LPrwc5OXCCOWDPLYYsqlp4Bisb1UYfpXl8dscTwSjkzIziR3P0LIsHMfGsq0u5onewrbtf3Z1LCJni3U2Sx4ZEX//sIlh6PhM0+S0chzHxbK8G5z/rHLpXoproVWuCi6PrnA/LNNqtf4Y/j7Ui07Ztqcb4iL8L74QmJJdhieyTM91I580PMukbuSWQa58RG73iG3B128/UGsahi4i6+LR2VNnl6ehG97Pkx/3OD+0yMCjVa4EV+gbWRYTZ9C0Jj7+o7I7TQafbxF4o/BgRsE/kSOc2HPf+aLRKJIkEYvFzkw8LhGXvF4SKgmdnY0TiUT4BanPCe3Btq+UAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Current design&quot;
        title=&quot;Current design&quot;
        src=&quot;/static/e06608d5604dc9ea74368c49b5745240/5a46d/019_closedDesign.png&quot;
        srcset=&quot;/static/e06608d5604dc9ea74368c49b5745240/1db73/019_closedDesign.png 75w,
/static/e06608d5604dc9ea74368c49b5745240/8a4e8/019_closedDesign.png 150w,
/static/e06608d5604dc9ea74368c49b5745240/5a46d/019_closedDesign.png 300w,
/static/e06608d5604dc9ea74368c49b5745240/fc2a6/019_closedDesign.png 450w,
/static/e06608d5604dc9ea74368c49b5745240/0a47e/019_closedDesign.png 600w,
/static/e06608d5604dc9ea74368c49b5745240/9c2cf/019_closedDesign.png 1557w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;CarGame&lt;/code&gt; class is injected into the &lt;code&gt;Console&lt;/code&gt; class and the &lt;code&gt;play()&lt;/code&gt; method is used to load the game.&lt;br&gt;
The &lt;code&gt;play()&lt;/code&gt; method takes the CarGame as an argument, therefore it is &lt;i&gt;tightly coupled&lt;/i&gt; to the CarGame class. If a new class, &lt;i&gt;AdventureGame&lt;/i&gt; was introduced, the &lt;code&gt;play()&lt;/code&gt; method will not be able to use the AdventureGame, and a new method would need to be created. The &lt;code&gt;play()&lt;/code&gt; method is &lt;u&gt;not&lt;/u&gt; open to extension as an extension of the AdventureGame is not supported, and it is &lt;u&gt;not&lt;/u&gt; closed to modification as the argument of CarGame will need to be changed to accommodate for the new AdventureGame.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introducing the Strategy design pattern&lt;/h4&gt;
&lt;p&gt;
The Strategy design pattern is used to allow the application to change its behaviour at runtime. Strategy enables different behaviours to be interchangeable in the original context by supporting the extension of the application without updating the original method.
&lt;/p&gt;
&lt;p&gt;
The Strategy design pattern introduces &quot;programming by interface&quot;, away from the default &quot;programming by implementation&quot;. The context of the application becomes dependent upon the behaviours of an interface and not the behaviour of an implementation of the interface.
&lt;/p&gt;
&lt;p&gt;
If we look back at the &lt;code&gt;CarGame&lt;/code&gt; class, we could think of it as an implementation of an interface with just the single method &lt;code&gt;load()&lt;/code&gt;. The Strategy design pattern encourages the use of an interface which is then referenced by the context so that it accepts any implementation of the interface.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Implementing the Strategy design pattern&lt;/h4&gt;
&lt;p&gt;
Before we make the updates to the Console and CarGame classes, I will review some key terms used within the Strategy design pattern:
&lt;/p&gt;
&lt;p&gt;
&amp;#8226;The &lt;strong&gt;context&lt;/strong&gt; class stores the method that is open for extension and closed to modification. The method in the context class must not change as new components are added to the application.&lt;br&gt;
&amp;#8226;The &lt;strong&gt;strategy&lt;/strong&gt; is the interface that is passed in as an argument to the method within the context class method.&lt;br&gt;
&amp;#8226;The &lt;strong&gt;extension&lt;/strong&gt; of the application is created through different implementations of the strategy.
&lt;/p&gt;
&lt;p&gt;
To begin, I will create the &lt;i&gt;strategy&lt;/i&gt; interface which will be used by the context class.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Game&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;Game&lt;/code&gt; interface will be used as the argument in the &lt;i&gt;context&lt;/i&gt; class: Console:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Game&lt;/span&gt; game&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        game&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;i&gt;extension&lt;/i&gt; of the strategy class will now be implemented in the CarGame class. It will implement the Game interface:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CarGame&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Game&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;loading car game&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The CarGame class will override the &lt;code&gt;load()&lt;/code&gt; method from the Game interface.&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
When the application requires further functionality, such as an AdventureGame, the &lt;i&gt;extension&lt;/i&gt; of the strategy class can be taken further by creating a new implementation:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AdventureGame&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Game&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;loading adventure game&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;play()&lt;/code&gt; method of the Console class is now open to extension as it is designed by implementation of the Game class, and is closed to modification as further components of the application will be accommodated for by the &lt;code&gt;play()&lt;/code&gt; method provided they implement the Game interface.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9bc2bad2c553c92d2112272abc61f1d1/8c833/019_openClosed.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACKElEQVQoz6VTTWsTURSdP+ZCKeqqYlYa68pNN8UmGompH2nrRkRUqkYrDQguAi6sH7gQKULahGAEO9OpCklMgtNJ4swkmZlM5vOdvnkhsepG8A6Hc3ncO+/ec+/jHMdBEATwfX/CofX7fTSbTaiqClmWIUkS4/DMMAwQQiY5B8F5nkfTya+PBoZmDS00Gg1omoZWq8V+2Ol0UKlUoOs6iwlIGA+GYJQGjowK+k8jE48rVd/jzsZ5rG5eQyafwqP8AvWv4u5GHO/EHAvyaGu+HzCw6m0PqVUeiYc8ZYHxwhOBnXNvtp9iNncIyZcRJF5MUz6J5HoEc8+PIFe+9VctQeCj1tjDVOwDjie2MH25gKMXNnHsYh666YDrmgq+K1/QUL+h2haxUy9DNmrYlQTsNutQeg5a6gBtbYiePoCqKAh1r0kGhKoGsdZlXP3RZ/pzJCC/SdGWVeZmX9UxFS9i9vYnnLtZxuH5PNbWeRDXREftI53dQSIjIPlYwCXK6axIB+mCcz2XTihg8Gk7PbounmNg7fVXRJdLLCGR4TFz4yOevRURuAbkTg/RxSJrN3KliBOpAqJLJegDZ7w2YOWOV2akFYFpORjQW03LZYIPhzYU5Sdbl5qkg6+otGWNsobKuOXxIv+r+b6HenMPsZXPmF/hEbvPY+7eNuIPeHo5nbJlWXBdF+GL+RO2TeEcgG2DUFlCDc+k8zi9WMDMUhGnrm/h7HIBatfEPm/kFOy/TFopAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Open closed design&quot;
        title=&quot;Open closed design&quot;
        src=&quot;/static/9bc2bad2c553c92d2112272abc61f1d1/5a46d/019_openClosed.png&quot;
        srcset=&quot;/static/9bc2bad2c553c92d2112272abc61f1d1/1db73/019_openClosed.png 75w,
/static/9bc2bad2c553c92d2112272abc61f1d1/8a4e8/019_openClosed.png 150w,
/static/9bc2bad2c553c92d2112272abc61f1d1/5a46d/019_openClosed.png 300w,
/static/9bc2bad2c553c92d2112272abc61f1d1/fc2a6/019_openClosed.png 450w,
/static/9bc2bad2c553c92d2112272abc61f1d1/0a47e/019_openClosed.png 600w,
/static/9bc2bad2c553c92d2112272abc61f1d1/8c833/019_openClosed.png 1609w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The open-closed principle supports the growth of application behaviours with maintainable and robust code. The strategy pattern enables the open-closed principle to be realised through the creation of interfaces and their placement as a dependency.
&lt;/p&gt;
&lt;p&gt;
When we are designing our applications, it is important to always consider if a behaviour is likely to exist in another manner. The strategy pattern becomes a more and more valuable design to use as the application behaviours and complexity grows with the addition of new behaviours. The three components of the strategy pattern: context, strategy and extension are valuable components to understand and practice as they will appear more and more frequently during application design processes. 
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Series link: Go to the next SOLID principle: &lt;a href=&quot;https://aneesh.co.uk/liskov-substitution-principle&quot;&gt;Liskov Substitution Principle&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The Single responsibility principle]]></title><description><![CDATA[The single responsibility principle is the first of the SOLID design principles. The single responsibility of classes promotes modularity and cohesion.]]></description><link>https://aneesh.co.uk/the-single-responsibility-principle</link><guid isPermaLink="false">https://aneesh.co.uk/the-single-responsibility-principle</guid><pubDate>Mon, 03 Aug 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Introduction to the SOLID principles of design.&lt;br&gt;
&amp;#8226; Understand how the principle of Single Responsibility can be applied to individual classes.&lt;br&gt;
&amp;#8226; Review the benefits of the SRP.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;The SOLID principles of design&lt;/h4&gt;
&lt;p&gt;
This blog post will be the first in a series of 5 posts that cover the SOLID principles of design. The SOLID principles were introduced by Robert C. Martin in his paper Design Principles and Design Patterns.
The SOLID acronym was later defined by Michael Feathers.&lt;/p&gt;
&lt;p&gt;
The SOLID principles encourage the creation of maintainable software that prevents code rot from accumulating within object orientated programming. As software applications scale in size, the design principles from SOLID become more and more valuable to prevent complex issues from arising and to enable a more efficient process for application enhancement and bug fixes.
&lt;p&gt;
&lt;p&gt;
The SOLID acronym is defined accordingly:&lt;br&gt;
S - Single responsibility principle&lt;br&gt;
O - Open-closed principle&lt;br&gt;
L - Liskov substitution principle&lt;br&gt;
I - Interface segregation principle&lt;br&gt;
D - Dependency inversion principle&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
This blog post will focus upon the single responsibility principle (SRP).
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Single responsibility principle&lt;/h4&gt;
&lt;p&gt;
The single responsibility principle asserts that any one class must have just one responsibility, and therefore just &lt;strong&gt;one reason to change&lt;/strong&gt;.&lt;br&gt;
Classes that use the SRP will be able to track each of their behaviours back to the single responsibility. Alternatively, classes with multiple responsibilities may contain many reasons to change. As we will later discuss, an update to a class with multiple responsibilities may cause unrelated side effects to the function of the class that can cause further problems within a complex application.
&lt;/p&gt;
&lt;p&gt;
The SRP creates classes that are described as being &lt;i&gt;cohesive&lt;/i&gt; and &lt;i&gt;robust&lt;/i&gt;. A sign that a class has a well defined purpose and singular responsibility can arise when you are naming the class against the responsibility. If you find it difficult to define a name of a class based upon its responsibility, it is likely the class has many responsibilities or is not clear to a single purpose.&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Class cohesion&lt;/h4&gt;
&lt;p&gt;
The word &lt;i&gt;cohesion&lt;/i&gt; describes the action of forming a united whole. In object orientated programming, classes are cohesive when they can be used together without conflict.
Class cohesion is achieved through the design and implementation of a class to a single purpose. Class cohesion facilitates greater potential for class reusability and implementations of the application. 
&lt;/p&gt;
&lt;p&gt;
The implementation of class cohesion can be visually demonstrated through the blocks below:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2848c5e66e126105e1cf1de029d55628/295bd/018_nonCo.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 86.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsTAAALEwEAmpwYAAADRklEQVQ4y5WT309bZRjH+z9o9MoLLxb1RtQlI5lx0UXNLgxXXhlmZCzDoIZuTl1wWxywLUOdi+4Cf2YTCrhkg6gb68gcYEcpdO16xo9CaYHwo0C79pT+bs/px3PenoIsSOJ78p7zvjnP9/N8n/c8xxSPx8lms+TzeZaXl/F6vaysrBAKhQgGg8zMzDA3N8fS0pJ4r8+pqSmxX1hYwNCrOiMWi0kmfVEa0WiU/v5+AdWDJUnC6XRit9vxeDxMTEzg8/mw2Ww4HA6sViuKouhSVb9lMhnJlEwmhYtAICAE+nra7yccDjM/Py8cRSIRZmdnRYwOXFxcFMncbjcul4uxsTFV162trUmmRCKBXwNM+6dx3nMWg7Q5o4lHH0hCrJenJxsZGWFoaEiU7L7vFlANxtjoqKo/RcmZTJrthl6LUdb6KGhXaWVMUbJWrWTK5fKkcyk6Hd/y80ATze02vrg0iaf9PHJbHXHfSFGqqlsmNJDiZTqdlkz5nMJaKsrRqxUcaC3ntSMWXqwZpte8j1jd4zy0/SaE+VyWH7q9nPzxAS3W7/nJ0Yir6yTyJTOy55YAppKJIjCeinGi6x0+sLxORX0nr5od9B57m9CnOwjbuwQwm8lQ2XCXF6r6qPxuPweuldPTUIZc+yShGxeKwES85FDmk2sVVLeVs/fjDs2hk9uH95Goe4yI4VAH1jQPsqe2j0MtNXzY/QY3z+0hdORZVm+1GA7jG2d4Zfgiv/x9hi877nJKO8P77d8gW8zEpoYxeozKRjtlVQNUXtxPdfcurI1lxGufIPxvh3rgf43CI5vWHh/NbeNcvvMrlnvnkW40I3ceJzbaZzhMbgBV8eVVEqkswYcp5IhMPhokJa8SSYSIJaPbJd34yiWgYrTFlV4fu83DNB2/gNKwk4Gv36Tu6lt8dfMj7X9XtMSaWovVDYhWUhUKRk9tBipFYOt1L88dHKT+8FnUY09zu2kX1R2v0PD7e+sNXigUHp1bOSyat/RM8vz7g5w4eg7l82f468zLHOrcy+k/D24Cbip7S6BSDLp8fZId1Q7qzWfhs6e40/gSVR27OfXHu/8PWAoKhuI4xkP4JgOkfUOs+p2ML7gILE+sx2wH/AcieKZKB7kHaQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Non Cohesion&quot;
        title=&quot;Non Cohesion&quot;
        src=&quot;/static/2848c5e66e126105e1cf1de029d55628/5a46d/018_nonCo.png&quot;
        srcset=&quot;/static/2848c5e66e126105e1cf1de029d55628/1db73/018_nonCo.png 75w,
/static/2848c5e66e126105e1cf1de029d55628/8a4e8/018_nonCo.png 150w,
/static/2848c5e66e126105e1cf1de029d55628/5a46d/018_nonCo.png 300w,
/static/2848c5e66e126105e1cf1de029d55628/fc2a6/018_nonCo.png 450w,
/static/2848c5e66e126105e1cf1de029d55628/0a47e/018_nonCo.png 600w,
/static/2848c5e66e126105e1cf1de029d55628/295bd/018_nonCo.png 667w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f0a4b9ad8f62f9c9a88918964a010d90/5f4af/018_co.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 106.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAYAAABG1c6oAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEtUlEQVQ4y4WVfUzVVRjH7/rDP3JrbdbSarm2XDnX7K9clhMzM1OzLaf9Y2kvvpQEyEBSp1m6+sMmLROBwUTjXkFSBAkTQRpCFy73DbiXC9434F7gcoH7Btz3++n3QkP0j57t7OycPb/v73mez3nOUYRCIaLRKOFwmEgkwszMDENDQ7hcLmkeGRnB4/Hg9XqltdvtlvbEtegvfjc9PY1oHR0dKESxh62/vx+tVktbWxudnZ20t7dLe2q1WlrX19djMpke+U6v16NIJBIMDg4KEbmxWPqwWvulMTLsZtw7JkQzLEUjDqfTKfgOYOrpwm63Yezqxmg0otdpSSTiGAwGFPF4HIfDjmvQQb/FjM7Qi1Zvoc82QK/VgdXuIBDwSz8Vo25t68DYY6P/vh1Tlw5zrwl9j4WxSb8sGI1GiMYSmGwT9NrHmPZ2Ep1UM+XUM203EJvyk0imSAqZiBYNOYlNtOAZttBtD+J0uJmxdZIcH5DKoSCVwDUWIi2jhfezbjBa+yQ0Khg9tZyR3BUETXcloVQiIs3T3elwR8HVC9tYcaCHrKx8ArnP4y/9As1/gu6xIFtzmtie+wd9N57Fe/sxhvNeYXT/ywSMjZJQMh6WZq8xm9BfCpSFH7E6S8eRrDME9i7G/+seNBoNCjEVn89HcUk5vxUXkafaRObVNQwtXUhKoWC6rlYSSsTkCK+15/NV+QpOX9xHgbKGmoxMyS/5TposKFIWi15VeYVyVSl5VTtJv7GV0RcWgeAYrqubJ1jZWcRnFWv5SZWNqqaOxtwcyY/1G+SUY7EYkWgMg8mBrseK3tqGziYUva0BX3M9kfExuYZJGYrXa8Fuq8Nmbcdy34Gj28DEnRqiFiM6nU6sYRy3d4oP8tRs+baRrIpdZFdvw/Lzenyn3iBouTcPSrQ3F5oV9NVt41JlPbdLTjN5ciWhK5lCyp2yoEj5vUN3efdQLd9c3kJm5RrMR1Yymf4Sga4mWXAWir8rg8gtBZqrGzh3sYbqs0eZPLCEYMHuWShJGcr5YiWFRedx1j6NR6AcWLaAsAjlz0ehZKuWs/9sJmsPazm2/aRUw9jGtx+E4kOlVFKlPM9Uw1Mk/xYcli4gKUK5OV+woqOAfZWr+CT/KK/nmMn78EcZyrr1sqAIJSpAMZpsdJss+JzXCQ1WEKqpZEqpIir0tHQOZ6E4PX2o79fTbtbQYnTTfU/oqEtlJFtb0UpQknFGxqfY8Z2GT0824LnzGol/niO86kXii5YQbmyYjVC+lW4aysi+voVz145RfesWLT+cIPX4Qtj58VynuDxB0tKb2ZRxjcHqJwQRoXaLhbSFVGZqquelrFL/wq5Lr3KidC+FquvUHjwopZx66805KJM+P+eKrnC2sIRTFRs5XrWavuyVTHy+jIC+abb1ZMFh42E8AuXfi3ewLkfL8ewzBPcsJpC/Zw6K3++j/HIZpWUFfK3czG5VGqbvV+I7LAj2zB6bhHxswt0HoElB1YXNLPvSTEZGPlO5zxAo2T0HRbwTBwYGcQ44sQwJd5xLw2SfmpC5lWhwQhZMJWVBfx/hkTpcwvWmNk9g6bUT6mkmOWqVO0V8F/7PUqnUvPlhSz7wBPwLCWRm++6yVZoAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Cohesion&quot;
        title=&quot;Cohesion&quot;
        src=&quot;/static/f0a4b9ad8f62f9c9a88918964a010d90/5a46d/018_co.png&quot;
        srcset=&quot;/static/f0a4b9ad8f62f9c9a88918964a010d90/1db73/018_co.png 75w,
/static/f0a4b9ad8f62f9c9a88918964a010d90/8a4e8/018_co.png 150w,
/static/f0a4b9ad8f62f9c9a88918964a010d90/5a46d/018_co.png 300w,
/static/f0a4b9ad8f62f9c9a88918964a010d90/fc2a6/018_co.png 450w,
/static/f0a4b9ad8f62f9c9a88918964a010d90/0a47e/018_co.png 600w,
/static/f0a4b9ad8f62f9c9a88918964a010d90/5f4af/018_co.png 665w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
While the blocks are only rotated on the x axis, it is visible that when classes consume just a single responsibility, they become more modular and can be combined with different classes to form different implementations.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Implementing SRP by design&lt;/h4&gt;
&lt;p&gt;
The benefits of SRP will be demonstrated by realising side effects from changing a class with multiple responsibilities. 
&lt;/p&gt;
&lt;p&gt;
In our example, the application will be used for renting a car. The class &lt;code&gt;Car&lt;/code&gt; exists as an entity to be stored within a database of all cars to be rented. The class will also return properties of the car such as brand, colour and engine size. &lt;br&gt;
The Car class will also include a method called &quot;rent&quot; which sends a request to the database to rent the car.
&lt;/p&gt;
&lt;p&gt;
The Car class has more than one responsibility. It is responsible for holding information about each car instance, but it is also responsible for managing the rental of the car with the database.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; brand&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigInteger&lt;/span&gt; price&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; colour&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;InventoryService&lt;/span&gt; inventoryService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inventoryService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getInventory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;code&gt;rent()&lt;/code&gt; method will verify with the Inventory class (not demonstrated in this post) that a car is available before it connects with the database using object to relational mapping to record the update in rental.
&lt;/p&gt;
&lt;p&gt;
The following changes are made to the application:&lt;br&gt;
1. The number of cars has increased in the inventory.&lt;br&gt;
2. The renter must verify their credentials before renting a car.
&lt;/p&gt;
&lt;strong&gt;Increasing the car inventory&lt;/strong&gt;
&lt;p&gt;
The Car class uses the InventoryService to check how many cars are available for rent. When the number of inventory is increased, the Car class, which is responsible for holding information about a car instance, should not be effected. After all, the number of instances of a car does not relate to the properties of a car. As a result, the InventoryService, which is responsible for managing the inventory of the car instances, will be updated. This example demonstrates how SRP creates a single change in a single class (InventoryService) that is responsible for a property (Inventory).
&lt;/p&gt;
&lt;strong&gt;Verify renter credentials before renting&lt;/strong&gt;
&lt;p&gt;
The verification of user credentials is a pre-requisite before enabling a car to be rented. As a result, the Car class must use another service to access the credentials during the &lt;code&gt;rent()&lt;/code&gt; method. The Car class, which is meant to be responsible for the car properties information, will now use the RenterDetails class to access information about the renter. 
&lt;/p&gt;
&lt;p&gt;
The result of verifying credentials of a renter has created unexpected growth and unrelated dependency of the Car class upon the RenterDetails class. The additional responsibility of the &lt;code&gt;rent()&lt;/code&gt; method in the Car class has created two further dependencies on the RenterDetails and InventoryManager classes that otherwise would be completely unrelated to the Car properties.
&lt;/p&gt;
&lt;h4&gt;Reviewing SRP in the Car class&lt;/h4&gt;
&lt;p&gt;
Before the SRP is applied to the Car class, it has a dependency upon the InventoryManager to accommodate for the rent() method. This dependency expands the single responsibility of providing properties of a single car into a further responsibility in renting an instance of the car with the database.
&lt;/p&gt;
&lt;p&gt;
When the application introduces two further requirements, the InventoryManager is not changed, however the Car class must now depend upon the RenterDetails class to verify the person renting the car. The additional dependency breaks the responsibility of the Car class even further:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/32bfaefdf6f1c89c221ed8b93a597048/abe2a/018_beforeCarSRP.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAADEElEQVQ4y31TXUhTYRgedNtVUER3XRRdCdJFN152EQUR/RkZlIlBUBdBpYi5gZUWakIUEUR0ExQSVpaikT9Tt+l0O5vHObc1d/zZpm7zeM7c2c7P03vOfiyMXnj43u973/fheb/3+0yiKJoAlMAwjIlsN+EAYV/B31PA3gL2E3b5/X6jJpvNlmDied6cy+XMmqaZZVm2SJJkjsVij4PBYAcVdK6srLSGQqH2QCDQEQ6H2xYikTaOW2pficZaBDHdxG8KFn5TNAuCaE6n02aTqqpIJBJgWRYcxyGTyeB/JgoC/D4vcpK4I6ZqGkyKosikSiZSmdTKulFMIcU7oJ8LYkaZnFlUvME1xc/x8lyEl9nwhhxPbul1sona1cj5C7oV16Ipav7MG0ppZxrGtLONNu1c47h2vsmmHb8zrD374DPiJl2QniorKhQdqlZCntdgNva6eYJJnK63otJixwWzHZcsDpy4a0Xnxzkjriv87539SaqblFUQWuKxEBUx6QmDDcYRWhawvpG/exq7Cn9kA5+GI/g6uojuEQ49Y0v4NBRBLLFVuuwiYWpTwsDEMgan43j/3YOe0TD6J6KYDaeKhMCr7gAOV/Wj4tYITtXbcOzmMA5d7oOViRtJirLdsms+gbJrAzhaO4iTdXZU3B7Bwco+PHw3UyTUSB2HC0021DxxovaJA9WtTpx/MGYUG4REphYI5zkeV5rtqG5x4gbl1rROgIaDt9+CeUKFhpKRckhspLGaEDDF+LCeEo19TlZK91dsWaazJMX01hk2gMjSKvkZpLekbYXdI4uGuobXXtx74ULdKy+uPnLAHUj+U2Ht00kj5/5LN60MrlNH73p/bQ/ledccymt+oKp5AvS+cO6BDUeqejE4FTWSsjmZ1MqG7/Stofx6Py7Sk9Hz9OdTVj0AyxvP9lBCUQmfx+IYcicxxCRh9abQNRxFKq3t/HpZ4Mt4HD9dSUIC4yy9EGsMMwvpPKHd4YDX44KPdWPG6wJL0Ff/LAO3axpOpxNTBGcBrukpzFEuW8ylWn2vc0xS/DfMv/SO2+BwUQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Multiple responsibility of car&quot;
        title=&quot;Multiple responsibility of car&quot;
        src=&quot;/static/32bfaefdf6f1c89c221ed8b93a597048/5a46d/018_beforeCarSRP.png&quot;
        srcset=&quot;/static/32bfaefdf6f1c89c221ed8b93a597048/1db73/018_beforeCarSRP.png 75w,
/static/32bfaefdf6f1c89c221ed8b93a597048/8a4e8/018_beforeCarSRP.png 150w,
/static/32bfaefdf6f1c89c221ed8b93a597048/5a46d/018_beforeCarSRP.png 300w,
/static/32bfaefdf6f1c89c221ed8b93a597048/fc2a6/018_beforeCarSRP.png 450w,
/static/32bfaefdf6f1c89c221ed8b93a597048/0a47e/018_beforeCarSRP.png 600w,
/static/32bfaefdf6f1c89c221ed8b93a597048/abe2a/018_beforeCarSRP.png 1067w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
If the Car class had a single responsibility, it would not contain a &lt;code&gt;rent()&lt;/code&gt; method and therefore the InventoryManager would not be injected. The &lt;code&gt;rent()&lt;/code&gt; method could belong within a different class, &quot;CarRental&quot;, that is responsible for leasing cars. CarRental will use the InventoryManager to obtain inventory levels for each car.
&lt;/p&gt;
&lt;p&gt;
The further requirements of the application can be introduced by updating the inventory in the InventoryManager and by injecting the UserDetails class into the CarRental class. Each class maintains its single responsibility, however the updates made to the classes have not increased their responsibility and kept the functions of the application separate from each other.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/82b5b661c122873500c08c6ab43dcc02/f2364/018_afterCarSRP.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACuUlEQVQoz21SW2sTQRjdp0YQ/A1KTStiffHJy4OUvviqSEF9KNg+VFq00IJYlfal1IJSBQVbL/WCQgUV7V1oTUxNxKTJtqltiLmnSXaT7Da3ZrM7u8fJ2IqgA4eZ+c6ZM9/3zXDT09MmAAzu1ZBpZOzNrlhSruns7NxTW1t70Gw21zU0NBw42dhk3ruv9hBF/ZXu3t0+f7Dm88KCyWZbpLCZ7Ha7aXJy0sRVKhU+m8nwiY04X1HKPGDwOiG8pml8uVz2KoqyKknSaiQS9lLeSzTVq+tkmVBepajqVFWl58Ank0meowuI6Sz8gTDkXBHlCoGqEfw9VFVDJBpHvqigrBAKDf8b6XQaXCGfI/1PVkjLoIe0314izX0OcnfcV3UkRDeIrinE609S3kVah1ykbchJzt50EIszQpStIslKm6RU2mJ6QRAIF42EMTDmxuV7PHpHvegY9uDxRIDdqBs0+1QCK74NdA4v4erDFVwb8aL9jhuL7igVbKFU2ISUSTG9LNEMBSEFf1SGcz0DF4VzLY1wMs8EhlEtQ0RFNeDxZxlfxZIvg2JZQzBRonEJVlcIK8E8ltcj4EQhidZBB+ovzKGpy4r952bRfd/NDDViICen8SMk43DLHI5eWsAxirrzs7C6E2i99R3H6f5Uj43GLegf+UozTCUxY4/h0UQQrz6FMfoxAIs7tV2ygWxGRDan4Nl0EM9nQngxG2ItiYsFvLXEMPIhgKdTQXouhCnrGrhEYoMZjs9H8d4aY6Y7hqxkMQVRKuE1jb+jfNXkJTWNi8V/XnlTyoDLpAVcHFhEY5cNzX3fcKLDgp4HHiYgOrBVzOGLM8BKO33dgTM3HDjSNg8bL/z+UppOW6OztSCI4AxdRzRVxHpYZo+xHtmk+8J2hgabFfo3q338Gc8xzWpQRr6k/tHs6ERRxC9yZN9agKl/rAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Single responsibility of car&quot;
        title=&quot;Single responsibility of car&quot;
        src=&quot;/static/82b5b661c122873500c08c6ab43dcc02/5a46d/018_afterCarSRP.png&quot;
        srcset=&quot;/static/82b5b661c122873500c08c6ab43dcc02/1db73/018_afterCarSRP.png 75w,
/static/82b5b661c122873500c08c6ab43dcc02/8a4e8/018_afterCarSRP.png 150w,
/static/82b5b661c122873500c08c6ab43dcc02/5a46d/018_afterCarSRP.png 300w,
/static/82b5b661c122873500c08c6ab43dcc02/fc2a6/018_afterCarSRP.png 450w,
/static/82b5b661c122873500c08c6ab43dcc02/0a47e/018_afterCarSRP.png 600w,
/static/82b5b661c122873500c08c6ab43dcc02/f2364/018_afterCarSRP.png 1389w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The benefits of SRP&lt;/h4&gt;
&lt;p&gt;
In the example above, SRP has been used to separate the responsibilities of the classes. The update and enhancement to the application has been made in expected classes and have not expanded their original responsibility. The SRP version of the application promotes the &lt;i&gt;single reason for change&lt;/i&gt; as the enhancements only prompt a single change in the class with the responsibility for handling the inventory and for the user verification.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Debugging&lt;/strong&gt;&lt;br&gt;
If the application were to experience a bug with inventory handling or user validation, the debugging process would initially begin with the expected classes with the responsibility that handles the functions. Without the SRP, the debugging process would have been slightly more complex as the Car class had the excess dependencies upon the InventoryManager and RenterDetails classes. SRP improved debugging an application as responsibilities are specific and adhered to within each class.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Loose coupling&lt;/strong&gt;&lt;br&gt;
The Car class that uses the SRP has decoupled the responsibility of rental from the responsibility of obtaining Car details. The individual classes can be reused whenever car details are required or leasing services are needed. They are services for a specific purpose and can be reused more efficiently than a single class with many responsibilities.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Testing&lt;/strong&gt;&lt;br&gt;
Automated testing (unit testing) is typically designed to test the functionality of a class. Each class will have several tests to ensure it is able to complete it&apos;s responsibilities. SRP will naturally decrease the collective number of tests required for the application as there are fewer behaviours that can be combined within a single class.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The single responsibility principle asserts that each class will have a single defined responsibility and therefore just a single reason to change.&lt;br&gt;
An application that complies with the SRP will enable the developer to understand the class dependencies and relationships in a more natural sense. The SRP will facilitate the debugging and enhancement of the code as new responsibilities and functionality is introduced.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Series link: Go to the next SOLID principle: &lt;a href=&quot;https://aneesh.co.uk/open-closed-principle&quot;&gt;Open-Closed Principle&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Git version control fundamentals]]></title><description><![CDATA[Use Git to store and manage source code amongst teams of developers. Begin using pull requests to contribute to open source software with the commands from this post.]]></description><link>https://aneesh.co.uk/git-version-control-fundamentals</link><guid isPermaLink="false">https://aneesh.co.uk/git-version-control-fundamentals</guid><pubDate>Mon, 27 Jul 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand how to store and manage files using Git.&lt;br&gt;
&amp;#8226; Use Git to manage branches and make pull requests for source code reviews and inclusion.&lt;br&gt;
&amp;#8226; Use Git to update branches with the latest updates from the remote repository.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is Git and version control?&lt;/h4&gt;
&lt;p&gt;
Git is a distributed version-control system that enables you to manage your code and updates in a centralised repository.
Git stores snapshots of the source code each time it is saved to enable you to revise historical updates to source code and track changes.
We use Git and other version control systems to control the updates and revisions made to our source code in a distributed contribution environment. 
&lt;/p&gt;
&lt;p&gt;
Version control is equally valuable to all developer projects regardless of the number of contributors; it formalises the process of creating, updating, and reviewing code before it is submitted to the main application source code.
&lt;/p&gt;
&lt;p&gt;
In this blog post, I will be using GitHub to manage source code. GitHub is a Git repository hosting service that provides a GUI for interacting and collaborating with Git repositories.
Git is used to store local source code in a remote (online) location. The remote location is then accessed by all contributors and updated and reviewed in unison. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/831c2b16f7abb47b88ae2af5f663ad35/5f4af/017_remoteAndLocal.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 90.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAACjklEQVQ4y21UzUpyURT1RXqGHqEnuI/h1EEQglDzJiKNhLSBBOEkcFRIhKVURj9ipFZKpf1qYmVW2u2uztof+3Kvnxs255599l1nnbX3OYF+vw/btnF3d4eLiws0m020Wi30ej38/PxgOByKj0YjfH5+St7z8zOenp7w8vKCx8dHmdMODw8RYDKtVqshn8+j0Whgd3dXgMeNueVyGaVSCScnJzg4OMDe3h6Oj49l/fT0FIHv7284jiOB9/d3VCoVYUL7/f2VNXWNkR1PcXt7i3a7LTEaNwl8fX25DKempjA9PY1UKoW3tzeJK5j+RDbJZBLb29vI5XKIx+M4Oztz14QhjdTn5+cxNzcH6srjeFkq4P7+vjBTOz8/F+3+A+RuNLKbmZkR+groHbnxysqK5FNzMlQNfYDctVgsYm1tDaFQCFos1U5HSkSdg8EgZmdncXl56WouGuqPNLbK/f29O1eQSXO2EdvKG5cqewG9pkccZ6hxbx772Nc22jLUZX19XYoyiaGCsVczmQwKhYLMfYDaNtfX19jc3EQsFkMikXATvaAKyI03NjYQDoelymo+QGq3vLyMhYUFrK6u+lpmHHBnZwfRaBSRSEQKNBGw0+lIOywtLSGdTk/UUec8KttlcXER1WrVD8iiMJG6sfwfHx/i42Deb1aXTklYbc0VQLPgGABna2vLMbfAMdfIMbfEOTo6cv5h+I2xm5sbx/Sc+NXVlcToBtAhQ5bINuxs8zSJmwtvDwYDiRsMGb3fpl9tU0TbvEi2IePGDaDttg215LNEwfWyj782ejS+NuYEcvWovZrcFEPfenh4EH99fbUMQ8vsLqPG1U0nyGiuqWUALSONVa/XJdbtdq1sNmv9AW22IZfMNn+XAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Remote and local copies&quot;
        title=&quot;Remote and local copies&quot;
        src=&quot;/static/831c2b16f7abb47b88ae2af5f663ad35/5a46d/017_remoteAndLocal.png&quot;
        srcset=&quot;/static/831c2b16f7abb47b88ae2af5f663ad35/1db73/017_remoteAndLocal.png 75w,
/static/831c2b16f7abb47b88ae2af5f663ad35/8a4e8/017_remoteAndLocal.png 150w,
/static/831c2b16f7abb47b88ae2af5f663ad35/5a46d/017_remoteAndLocal.png 300w,
/static/831c2b16f7abb47b88ae2af5f663ad35/fc2a6/017_remoteAndLocal.png 450w,
/static/831c2b16f7abb47b88ae2af5f663ad35/0a47e/017_remoteAndLocal.png 600w,
/static/831c2b16f7abb47b88ae2af5f663ad35/5f4af/017_remoteAndLocal.png 665w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating a repository&lt;/h4&gt;
&lt;p&gt;
A repository can first be created in GitHub from the website by selecting &apos;New Repository&apos;.
&lt;/p&gt;
&lt;p&gt;
The git repository is mapped into a local directory. Create and navigate to the directory from the command line and initialise the directory as a Git repository with the following command:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git init&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;Git init&lt;/i&gt; will create a .git file in the directory that creates an empty repository. The initilisation makes other git commands available that can be used to add and manage source code within the local and remote repository.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Adding code updates&lt;/h4&gt;
&lt;p&gt;
I have created a new project in the local directory where &lt;i&gt;git init&lt;/i&gt; was called to complete the following print function.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Application&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from the main branch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The remote GitHub repository is currently empty and the our local repository contains source code that is yet to be added. 
The following command line instructions can be used to move the local source code into GitHub.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git status&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;i&gt;git status&lt;/i&gt; allows you to inspect a local repository. The command will display the state of the directory against which changes are staged and which files are not tracked by git.
The status will inform you which files are staged, unstaged and untracked.&lt;br&gt;
Staged files are files that have been added to the current file history and are to be pushed into the remote repository in GitHub on the next &apos;push&apos; command (more on &apos;push&apos; later)&lt;br&gt;
Unstaged files are files that are currently tracked by Git, have been updated, but are not included in the next &apos;push&apos; command&lt;br&gt;
Untracked files are brand-new files that are not acknowledged by Git at any point.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git add .&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;i&gt;git add .&lt;/i&gt; is the first instruction that tells Git to save a snapshot of the current project state into the git commit history.&lt;br&gt;
When you later review all the updates to the source code, you will be able to review against each state of the source code each time &lt;i&gt;git add .&lt;/i&gt; was called.&lt;br&gt;
The &quot; .&quot; that follows the keyword &apos;add&apos; instructs git to add all updates within the directory and sub-directories for the local repository. It is possible to replace &quot; .&quot; with specific files 
for fine-grained updates.&lt;br&gt;
Once &lt;i&gt;git add .&lt;/i&gt; is called, the files are moved to the staging area and are ready to be committed to the remote repository.
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;git add .&lt;/i&gt; will ensure all untracked files become tracked and all unstaged changed become staged.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git commit -m &quot;my first commit&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;i&gt;git commit -m&quot;my first commit&quot;&lt;/i&gt; is used to create a new revision of the source code that includes the previous version and all the files staged from the previous call(s) to &apos;git add&apos;.
&lt;br&gt;
Each commit command must be made with a comment. &lt;i&gt;-m &quot;my first commit&quot;&lt;/i&gt; is the command instruction to include the comment in the speech marks. It is best practice to include a comment that describes the update made, and the problem that the commit is resolving. 
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git push -u origin &amp;lt;git url.git&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;i&gt;git push&lt;/i&gt; will take the current staged commit and push it into the remote repository. The above command includes three further instructions that are required for the very first commit to the repository.&lt;br&gt;
&lt;i&gt;-u&lt;/i&gt; instructs git that it is making an upstream push into the origin. &apos;origin&apos; is the default name for the remote repository and the url for the origin is passed in as a parameter. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why we branch code&lt;/h4&gt;
&lt;p&gt;
Branching creates a controlled copy of the code to be modified within a safe environment. Branches are used to make granular and specific updates to the code without the risk of creating side-effects or regression to the existing source code. Branches are created and merged into the main source code in a series of events that allow code updates to be reviewed and combined safely.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Branching to make changes&lt;/h4&gt;
&lt;p&gt;
Before we can create a branch from the main remote repository, we need to create a copy of the source code in our personal remote repository. If you own the repository, you can skip this step, however if you are contributing to open source software, the source code will exist in a remote repository that is not your own. Therefore you must create a copy of the remote source code into your own remote location that is linked to the main remote repository. To perform the remote copy, we use &lt;i&gt;git fork&lt;/i&gt;.&lt;br&gt;
Forking is the first part of 4 steps to update an existing source code:&lt;br&gt;
1. (If you do not own the central repository) Fork the repository from the remote central location to the remote personal location.&lt;br&gt;
2. Clone the forked repository from the remote personal location to the local location.&lt;br&gt;
3. Apply a fix or update.&lt;br&gt;
4. Submit a pull request for the update to be included in the remote central location.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
I will first create a fork of the repository by selecting &quot;Fork&quot; on the GitHub repository page.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 118px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0ad8c10cf86a94df7f845f422e30ee2a/9e4e1/017_fork.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 36%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABIUlEQVQoz12RW0vEMBSE9///BMW3XS/sgwqCf0FYxfXyIFtvrKLd1oXaNknTNo5nkrYWA9OEk8nXycmkblpUtoaprJc21TCP18bYwUNVIy9l6wZkTQgrSgWljQdzI8wtmrb15rqrEUCvFi9rHM45WEu4CUAeKJX2m6unV1wt7/EQPSPdxHh8WcOI+TNOkG4zuB+EHzQBFkUrbLPcrwlkGJ+QQJrj5Avzk3N8CODm8gJHp2c42J9hOp1jdnjsD6pRup29XSyWt11dB2BIGOK2Ql1c34W+yPVyud767R1ZVkjKVDxuaAFHkmwEVIWEukvID/tC5YWSA6FPTE3xBkxFEOvfeYGy62PDB5W6Et/QQ34ogv8eJMz/1Xv6ff/aXb3n/AI/sxkYhyTCDQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Fork Repo&quot;
        title=&quot;Fork Repo&quot;
        src=&quot;/static/0ad8c10cf86a94df7f845f422e30ee2a/9e4e1/017_fork.png&quot;
        srcset=&quot;/static/0ad8c10cf86a94df7f845f422e30ee2a/1db73/017_fork.png 75w,
/static/0ad8c10cf86a94df7f845f422e30ee2a/9e4e1/017_fork.png 118w&quot;
        sizes=&quot;(max-width: 118px) 100vw, 118px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The Forked repository will now exist in your own GitHub remote account. Select the repository and copy the URL that appears under the &quot;Code&quot; tab. 
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8e31a400a17180551c53fdc706bf57a2/6146e/017_clone.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 81.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAACfklEQVQ4y41Uu47TUBD191AgUfAJdEh01HR8CEjQsNIKiY5iJSq+ACFBg6hWC7vZzTpPJ1nnQd6JH/HbhzkT28ryEFxpNOO5987MmTPXRp7n4MqyDLtdAM/zsXUcuK6PJE0RJwkcx8V6s4XjukAKnNpnePbpBV59Ocbrr29wJPr555cwZ00YQK7BeNkejmA22zDNBhrNDqIkhet5OK9doXZZR9uyNODJ6TvcPb6PB28f4sn7p3h08hh3ju7hQ/MjjFguFUVW+tdFf5pmWm0i5zf+Fu1ZF9bcQn/Zhb3uobew4AY+DDfIBJKD4WiM+XyB6XSG8WSiYttDrXqz3SL/UzZxrT1gsoagkU9JarhhhtlsjvOLGrpWD612B61WG52uBavXR7vTRX9gS2WJtoaBM0qWYidRZtsYo1Us1SWI4wiGs0uEAFcu3WDEKhcLjMZjDTyU6n5Mp5qo1++LDMS2iiQD0TxzU5UbRjGMXRBhuVwJCS0lJj2QOI5Vl5VRUhIo1apdnNv3OUfEgJ6MSlKMBh204ziRbJGSwKBBECAMQ/VR0xeGkZ4lSYHYTKoBI61C+jifKwnsJ4kgvNF4ovBpU25kn32lj7BJlitzO7DHWrkGZKaSwCzbz+Qe4h4GfQWhFew9KZnqEvatCgkliqKqN9wkXPUVr4V7erFIUuoqidxhHINOwiFMjsq37+doythcX5vyOq5weVVHvX6tNvfMRhN/W3vIUVRlKd/04Qzz+5B5ncc8vwW/bA0RKWTOoe/vRHyxPf0J8A0vVytlvOxnfnD5dykg8wCfHdnlIF/ULtEQWIRtNho6Jv+7KpY5NtVQE6LCzDQzK+TBf0oxmz8BNSvCWm69hsAAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Clone Repo&quot;
        title=&quot;Clone Repo&quot;
        src=&quot;/static/8e31a400a17180551c53fdc706bf57a2/5a46d/017_clone.png&quot;
        srcset=&quot;/static/8e31a400a17180551c53fdc706bf57a2/1db73/017_clone.png 75w,
/static/8e31a400a17180551c53fdc706bf57a2/8a4e8/017_clone.png 150w,
/static/8e31a400a17180551c53fdc706bf57a2/5a46d/017_clone.png 300w,
/static/8e31a400a17180551c53fdc706bf57a2/6146e/017_clone.png 377w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
In the command line, navigate to the directory where you would like to store the cloned repository and use the following command to create a local copy of the forked repository:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git clone &amp;lt;forked repository URL&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d6a0b3833767968eabaa5ebcc9b4475e/ecf19/017_forkClone.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 70.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAACZUlEQVQ4y11Ty05iQRDli1kYlySQwM7EcUzGJQzGlUsHx8RERogJJigBjIgPnurl/RCGhwh9pk5x741OJZ2+t05X9alT1Z7pdGp1u13r/v7eenp6sur1uvXx8WHN53PdR6ORdXt7q1itVrMWi4X6GfPw8GCVy2XFSqWSYh4BMZvN0G63IQd0/2zEm80m5ELdHXt/f4ckwtXVFdLpNBqNhvo9wsTIbrxer/H7/WYymej/arXSxW+fz2c2NjZMv9/Xfy65yCSTSVOpVEwmkzG5XE79HglCtVpFJBLB1tYWUqkUlosFM+qNvW4X4XAYwWAQ8bMzCTHqn4zH+H18DCGg7BOJxJrhcrnUMnu9HgKBAH7JIdrSLq0/Gin+bXsbPw8OXIxpy6+v8EsMyQyHQ7dk/aBjIMHguryEyWZhrq+BTgd/p1P06KeGUoERRlIj8Pz8RW8j7D0Ul9ZutdAZDIC3N5h4HLi4gDk6Al5e0JXLWlI6kxMzguHkBOPTU2Tk0pbdSE3ILpJlLBbDj709NC0L0nashJEhKzn05/wcoVAIdUkOkWjJiyWm9PiIfZFhPxrFytbcI7Ojo0Eddnd3VWDayl60w8NDbG5uoibN01Gy/dJhfN/ZQVQSMo8mZMmk+ix6cK7YNaznZr0EexXxi8XiF4zMSYTaj6Xjxu6+dplDSWYcn0cpw9GVRh8xJr27u3OT0siKybjckp1beLhQKCgTR2AaX0c+n1f85uZGWTnGRPTzhbkJyYaHyIQBnMfPCYnJe0VWxuh/jM2Ud66422VnDuWxq8hOuQSdQFbAsh12jt/YOnI5vn+Gjgs5H3gwQwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Fork Clone diagram&quot;
        title=&quot;Fork Clone diagram&quot;
        src=&quot;/static/d6a0b3833767968eabaa5ebcc9b4475e/5a46d/017_forkClone.png&quot;
        srcset=&quot;/static/d6a0b3833767968eabaa5ebcc9b4475e/1db73/017_forkClone.png 75w,
/static/d6a0b3833767968eabaa5ebcc9b4475e/8a4e8/017_forkClone.png 150w,
/static/d6a0b3833767968eabaa5ebcc9b4475e/5a46d/017_forkClone.png 300w,
/static/d6a0b3833767968eabaa5ebcc9b4475e/fc2a6/017_forkClone.png 450w,
/static/d6a0b3833767968eabaa5ebcc9b4475e/0a47e/017_forkClone.png 600w,
/static/d6a0b3833767968eabaa5ebcc9b4475e/ecf19/017_forkClone.png 948w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The local repository now includes a copy of the personal remote repository. Use the following command to create a new branch on the local repository:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git checkout -b &quot;firstBranch&quot;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can see all the available branches by running the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git branch -a&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
A new branch has now been created called firstBranch.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b2fed610a7c03f6f9b197bb20c9576b0/41be6/017_firstBranch.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACIUlEQVQ4y42Ty08TURTG+zcRF8YAAVGpLlmbSDRBLMrDRA1xIQsWajWGRBNMXGhnoM8hghGVV2MTxpZpp4mlDEaBhXah0SIDTjvPz3vvUEqxEU7y5dx7kvndk/Od8ajqFkzTAhwcKl03UC6X/5GmaaCRSqXgsS0HaqmIYGoEY8mH4D74ERDvgSeZnlkt6YeQHkXJ+IN64TgOy9lsFh7YQP6bhAuBBnQFT+D6xDkMTnagN3oa/YIXV0LNrH6JO46vv9bYh7ZtMUhFlmWxuizLLlApyLgWbcPAhBeP4r0Yk/x4Jt5hGpo+Tx45i76IF4Xiugt07JoO6wI7uWPoJt1cDjaiJ9xKumuHL9yyV+viGwlw43CgaZjQ9B3EV15iNh/BgiLgdZYHNzeCeSWGmVwIb3PjSKy+IjPUGMyyTSKrqv1A6tLBsHXgs7KOo8auJy5QL+tu2+xVk+2Hur2F3HKO3A28V6bw5iOPmeUQURizRHP5KBO9z+cF/N4pMkZGzlQ7rDhGQ1VVrK58wmbpB3zjJ9ETaYUv1EKcP4WrkTZ0B5vIbJvYJtDZS2tx9l06I9UHmqaB9JKMyXdRDE9fxNPF23icuElcH8Lz5DA538CTxC12H5zq2AdM1wL3wHDPhZ8bzF26UrS7/lg7+mJn2H7SPEByJ9eApS8L/++wknWzBEEaxYvFu+BE9w8KiPdrMi8+wPfNgjvDTJ0ZHoQezeaqy38BCluGQBU/KVIAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;First branch from main&quot;
        title=&quot;First branch from main&quot;
        src=&quot;/static/b2fed610a7c03f6f9b197bb20c9576b0/5a46d/017_firstBranch.png&quot;
        srcset=&quot;/static/b2fed610a7c03f6f9b197bb20c9576b0/1db73/017_firstBranch.png 75w,
/static/b2fed610a7c03f6f9b197bb20c9576b0/8a4e8/017_firstBranch.png 150w,
/static/b2fed610a7c03f6f9b197bb20c9576b0/5a46d/017_firstBranch.png 300w,
/static/b2fed610a7c03f6f9b197bb20c9576b0/fc2a6/017_firstBranch.png 450w,
/static/b2fed610a7c03f6f9b197bb20c9576b0/0a47e/017_firstBranch.png 600w,
/static/b2fed610a7c03f6f9b197bb20c9576b0/41be6/017_firstBranch.png 638w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
In the command line, we are able to check the branch we are currently on with &apos;git status&apos;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;on branch firstBranch&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The main branch can be accessed by using the following command:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git checkout main&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The &apos;git status&apos; command will now read as &quot;on branch main&quot;
&lt;/p&gt;
&lt;p&gt;
We will navigate back to the firstBranch and apply a single update to the source code on line 6:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Application&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from the main branch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from the first branch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;                                                                                                                                                                                                                                                 &lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The first branch is updated using the following commands:&lt;br&gt;
&amp;#8226; git add .&lt;br&gt;
&amp;#8226; git commit -m &quot;first branch print line&quot;&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The first branch now has an update that we would like to add to the main branch. You can see the differences between the two branches (main and firstBranch) by using the following command to switch between branches:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git checkout main&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git checkout firstBranch&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The update from firstBranch can be submitted into the main branch through a &lt;i&gt;pull request&lt;/i&gt;:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git push --set-upstream origin firstBranch&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above command uses 3 instructions on the push statement. firstBranch has not specified where it is pushing the latest update to. The upstream target is set to the origin (main) branch and the branch name is specified at the end.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Handling a pull request&lt;/h4&gt;
&lt;p&gt;
The upstream push from the firstBranch has created a new pull request in the main branch.
The pull request can be reviewed in GitHub from the main repository page:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2498ce180d5a17f30992a30c32437d2f/8b69f/017_pullRequest.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 8%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAg0lEQVQI11XMvQ6CMABFYd7/EZz8mRoXFl/AycmERhIclIqoAaFGIwilcGwcjA5f7pmuVz9jmkrR1ormR/s6fLZrEobu5KR/rDnCkBFclohwhr8V+JHAy85ryqvkVkjKXKJda9d3vaHIA6pHBFbRmz2Dib+s2UGfskoWTOWIeThxx2PeLtiSzFzocKQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pull request on main&quot;
        title=&quot;Pull request on main&quot;
        src=&quot;/static/2498ce180d5a17f30992a30c32437d2f/5a46d/017_pullRequest.png&quot;
        srcset=&quot;/static/2498ce180d5a17f30992a30c32437d2f/1db73/017_pullRequest.png 75w,
/static/2498ce180d5a17f30992a30c32437d2f/8a4e8/017_pullRequest.png 150w,
/static/2498ce180d5a17f30992a30c32437d2f/5a46d/017_pullRequest.png 300w,
/static/2498ce180d5a17f30992a30c32437d2f/fc2a6/017_pullRequest.png 450w,
/static/2498ce180d5a17f30992a30c32437d2f/0a47e/017_pullRequest.png 600w,
/static/2498ce180d5a17f30992a30c32437d2f/8b69f/017_pullRequest.png 912w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following page allows you to submit the pull request with comments to the maintainers of the main repository. A maintainers of a repository are dedicated individuals who review and approve updates to the main application source code.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a1337dbc7103eec1f7782a30b7adbacd/a2b88/017_pullRequestReview.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABPUlEQVQoz52SyU7EMAyG+7icuHMCCQlpLrwORySEeIi5DjOle7M0e/LjpNIsgjlApE9Obcdufqdq6w5cSnT9gHGawLmA8x7OnUHfPgSyAT4TTjaESDYWm+OVtQ79MGFmAk3bQxuHmICQiSj7fEBpC+sC4bEoA2M9pNIQiypwqUrzStmAfTujnyTqbkY3LWhHgXYQ6EZZaAaOA+V89azE2OIgdAAnyy6ggjkgqYMxGtZSZ2OglYJSC7Qmv9YlllFqzbu2hI6oFhPLtf6yUko/WAuShtJkUbMj/Zp47fB54ZjW88eCWfz//tX5kjqRhioPhZHgHPtmLuLXHStk34EG1dCAyhDUJTVj2E0DPucRu7HHdt+iiiSg9fmNxfVZ+BPuaEkWyiuQPPlGht7m4/sGd6/3eHh7ws3LLTYfz/gGeku5zuf/K6AAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pull Request review&quot;
        title=&quot;Pull Request review&quot;
        src=&quot;/static/a1337dbc7103eec1f7782a30b7adbacd/5a46d/017_pullRequestReview.png&quot;
        srcset=&quot;/static/a1337dbc7103eec1f7782a30b7adbacd/1db73/017_pullRequestReview.png 75w,
/static/a1337dbc7103eec1f7782a30b7adbacd/8a4e8/017_pullRequestReview.png 150w,
/static/a1337dbc7103eec1f7782a30b7adbacd/5a46d/017_pullRequestReview.png 300w,
/static/a1337dbc7103eec1f7782a30b7adbacd/fc2a6/017_pullRequestReview.png 450w,
/static/a1337dbc7103eec1f7782a30b7adbacd/0a47e/017_pullRequestReview.png 600w,
/static/a1337dbc7103eec1f7782a30b7adbacd/a2b88/017_pullRequestReview.png 908w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If you are the owner or maintainer of the repository, the pull request merge option will be seen as below after any conflicts are reviewed:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5b16ef49b8b833eb63c34a40a2e2e144/6029f/017_mergePull.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 24%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAABCElEQVQY012Py07DMBBF+/97QKwQIMRvACqwY0GFClKpskjbJLbzdFLn5cMkFSyYmaO5d+Erz+Jydc6hjDBZxl4lREqxiyMirQUlPkZlmqZtcL3j2B3/mHw3dnS+oxfc6FikzmBbS1pYeWxRpiDWBYlpRFuMTjEmo8gLqtJS2xpbWaGmqRu89yAzMYwDC5F8lxu+ojU2qTCxoVAFh/2eMAwlTGNSMzP6kf/lp55CpXrfnwLDcsdjsOQ5eGUZvPCwfSLJtfxGTnQ9rh1kD7hZy2mTbk/0w8gw42n77hS41p9cvd9w93HP9eqWs7cLNmpLVpYSbIWaKKtll6g8n/0vtj3SdBOOqq35Aeege1E9HMAdAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Pull Request review&quot;
        title=&quot;Pull Request review&quot;
        src=&quot;/static/5b16ef49b8b833eb63c34a40a2e2e144/5a46d/017_mergePull.png&quot;
        srcset=&quot;/static/5b16ef49b8b833eb63c34a40a2e2e144/1db73/017_mergePull.png 75w,
/static/5b16ef49b8b833eb63c34a40a2e2e144/8a4e8/017_mergePull.png 150w,
/static/5b16ef49b8b833eb63c34a40a2e2e144/5a46d/017_mergePull.png 300w,
/static/5b16ef49b8b833eb63c34a40a2e2e144/fc2a6/017_mergePull.png 450w,
/static/5b16ef49b8b833eb63c34a40a2e2e144/0a47e/017_mergePull.png 600w,
/static/5b16ef49b8b833eb63c34a40a2e2e144/6029f/017_mergePull.png 906w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Updating branches with the latest change&lt;/h4&gt;
&lt;p&gt;
The main branch from the application has now been updated with the additional print line from the firstBranch. The local copy of the main branch has not reflected the update and only contains a single print line. The latest update to the remote repository can be &quot;pulled&quot; into the local repository using the following command:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;git pull&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;i&gt;git pull&lt;/i&gt; performs two actions:&lt;br&gt;
First, git pull performs a &lt;i&gt;git fetch&lt;/i&gt; command to download the latest commits, file, and references from the remote repository locally.
Second, git pull will integrate the new data into the working files. The local repository now reflects the latest updates to the source code that is stored remotely.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
In this post I have used Git version control to clone a remote repository locally, to create a branch for updates, and to submit a pull request for review and inclusion in the main application source code. The updates to the remote repository are then reflected locally by pulling them from the remote repository using Git.
&lt;/p&gt;
&lt;p&gt;
Git is a popular version control tool used across industries in different forms (such as svn or mercurial). The flows of forking a repository, pulling changes from the remote version and submitting pull requests are important to understand and apply appropriately to standardise and formalise the process for code enhancements and bug fixes.
&lt;/p&gt;
&lt;p&gt;
Working with version control is important within a team and open-source environment; use the techniques from this blog to develop patterns of pulling, updating, and submitting code updates in a safe manner across branches and pull requests in open or even your own closed repositories.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating multi-threaded visibility in Java]]></title><description><![CDATA[This post will explore how the race condition arises within multi-threaded applications, and how you can resolve it with atomic values and synchronization.]]></description><link>https://aneesh.co.uk/creating-multi-threaded-visibility-in-java</link><guid isPermaLink="false">https://aneesh.co.uk/creating-multi-threaded-visibility-in-java</guid><pubDate>Mon, 20 Jul 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand how a race condition can arise in a multi-threaded application.&lt;br&gt;
&amp;#8226; Use Atomic values to create thread-safe variables.&lt;br&gt;
&amp;#8226; Use Synchronized blocks to control the accessibility of code blocks to different threads.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is a race condition in multi-threading?&lt;/h4&gt;
&lt;p&gt;
The &lt;i&gt;race condition&lt;/i&gt; is a term that originates from electronics to describe a condition where a systems behaviour is dependent on the timing of uncontrollable events.
&lt;/p&gt;
&lt;p&gt;
In the context of a multi-threaded application, race condition can arise where multiple threads are performing simultaneously upon a stateful session. Stateful sessions contain data that is represented by the current state of the application; the existence of the session variables are dependent on the previous execution of other methods. A stateless session will therefore encounter data that is not impacted by the state or execution of the application.
&lt;/p&gt;
&lt;p&gt;
A static variable is an example of a stateful object. The static variable&apos;s existence is dependent on the interactions it has with other instances of the class, whereas a method-local variable is stateless and does not carry such a relationship. 
&lt;/p&gt;
&lt;h4&gt;Multi-threading on a shared bank account&lt;/h4&gt;
&lt;p&gt;
To describe the how a race condition may arise, I will use a scenario of a shared bank account. The bank account is not allowed to go into overdraft, therefore when a request is made, it should only be processed if there are sufficient funds in the account.
&lt;/p&gt;
&lt;p&gt;
Each transaction will first check the balance and make a withdrawal if the balance exceeds, or is equal to the withdrawal amount.
&lt;/p&gt;
&lt;p&gt;
A race condition can occur when multiple withdrawal requests are made at a similar time to the bank account using different threads.&lt;br&gt;
Each thread will read the bank balance as containing a sufficient amount, however when both transactions are made, the bank account becomes overdrawn. 
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/cd306d8a484fed5537bddea409c27093/a6d36/016_flow.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 144%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAdCAYAAACqhkzFAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEhUlEQVRIx51W+0/TVxTvv7RfNjdEtkydW2LmWHAOE6djCgyDgkiBghgfGzFmCxZa5uAHt5QWRmuRh7xDW+kcUEAYc6OlDwqlVGpbJDzawmf33HqbApUlO8nJufc8Pvfcc8735itZXV1FOBzG5uYmtra2EIlE+JpY6Ik3Nja4jXyEniTx+vo6iKanpyEhhaBAIMCNFLS9vc2DEolAyScajXIfknSIIKvVCklikMlkgsFgQE9PD/R6PYaHh7megok8Hg86OzthNpsxODiIlpYWrhM0MzOzE5AC19bW4HA44PP5QOUgomyFpKwWFxfhdDrh9/v5LYR9B2BikMvlih+SqBdru92Oubm5HYkkBRQBu/e7ZSIQ6d4IKE6fmpqKOwlgYSc9dVPc4o2AQtnb2wu1Wh3v3u7saCpUKhX6+vricUkBiagh1Omuri4sLy8nzZAaQfahoaH4/FGjdgAKZxoXpVKJ+vp6aDQaiBkVdvKl7BsaGrifyFLcZk+GwWAQExMTsFgsPJPd16Y9ZU728fFxhEKh5Bkm1nBpaQlutztpp8Wa7F6vd/8aCiU5U6cTdYkdFpOQeOgeQHY2Szt2vfl5N/tS7K+vwh6D7WichY/D6YB7PgYY3YogEk2oYeLjwL9X9xLsVif2I7J73N49eg5IrddZfsKPvQVQGmSoaruI2/pcti6DvF8K+QDj/mLUDJRwqRgsw/etebjTkc/WpVx/tzsfv9sfwzHrguRFwIti3QmcVx9AtvogLmmP4LLuKLJU7+CC+j3kaFKR23QI3zQe4Gsumw8iW5PC7Cn4tikNZ359C3JjEWwzdgYY9OJa22lcajmCAu0xVPVko9YghdJUCrnhKqoHC3Fv8AoUxhKmK0ONgWXL9LVGKSo7MnlMLgO9b66A7Z9ZSHyBRcgenUS+9jAuaz9CWdvnqOg4hZtdX+FG1xlc6/iS83c9WbjVfY7xWVR2ZuJ299coefQZCnTHkNOUCuWTElgJMPQqgJsdWbigSkGe5kPkqt/nnK1KRU7jodecxveCc9VpXE9+FzWHce7B2/jljyo4bKyGkXAUL1d9cPj+xmLICctzM55OGeFdccG1bMWc38bZTZLtPUEHnowN4E/7GBbYmnQUG97egM02u/PTI/J5X2DB7dl3bJx2F1ZCr5KPze45nHd5WHHt+wJOTz5H0L+SHHBtfQ0Df+nQNCyHbqwODf1VqOu+Ad14HX4bUaBlVMFkLZpHajhrLUooOq/jgfEutMyf7Oqn1ZhcGILd5ojNYZH2OJu/o6jqzYasPQPXH2dC2kod/JiPhbT1BG51nUV5+0ne8dL2dN7hQt0nkLVl4Hzju7hnuIJZPocMsESfjuLWT3F/qJzNVzF+NldCYZLiTl8OZ3KWG4rYPBbGJdlrjFfZuJQir/kD1JqKY3P4MuTHDz0FkGrTUarLQLmeZfDwJCpaMznLHn6BMrZPlDL9qbisaD2NwubjaBqt5rWXiIJGEaY3B+xnhEn2yrB9jCP/yRE2MrGXagES+guwjFoYj/1vHh2x4NmzSWi1WvwLKpjcNDILBbUAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Transaction flow diagram&quot;
        title=&quot;Transaction flow diagram&quot;
        src=&quot;/static/cd306d8a484fed5537bddea409c27093/5a46d/016_flow.png&quot;
        srcset=&quot;/static/cd306d8a484fed5537bddea409c27093/1db73/016_flow.png 75w,
/static/cd306d8a484fed5537bddea409c27093/8a4e8/016_flow.png 150w,
/static/cd306d8a484fed5537bddea409c27093/5a46d/016_flow.png 300w,
/static/cd306d8a484fed5537bddea409c27093/fc2a6/016_flow.png 450w,
/static/cd306d8a484fed5537bddea409c27093/0a47e/016_flow.png 600w,
/static/cd306d8a484fed5537bddea409c27093/a6d36/016_flow.png 650w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The diagram above illustrates how both threads record the balance and make the withdrawal against the balance.&lt;br&gt;
The race condition occurs as the outcome of the transaction is deterministic upon the thread speed and timing between each other.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The possible stateful outcomes&lt;/h4&gt;
&lt;p&gt;
The outcome from the bank account scenario with two threads can be any of following three:&lt;br&gt;
1. Thread 1 can spend the money and Thread 2 is not able to.&lt;br&gt;
2. Thread 2 can spend the money and Thread 1 is not able to.&lt;br&gt;
3. Thread 1 and Thread 2 can spend the money, thus causing the account to become overdrawn.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The race condition is implemented below with a BankAccount class with a withdraw method.&lt;br&gt; 
The below code sample enables a race condition to occur:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BankAccount&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; balance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;withdraw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; spend&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; temp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; withdrawing from balance: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        balance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; temp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; spend&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; has withdrawn &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; spend &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;. New balance: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getBalance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;balance&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The BankAccount instance has an initial balance of 50. The withdraw method will first pass the balance into a variable &lt;code&gt;temp&lt;/code&gt; before making the withdrawal and printing the balance to the console.
&lt;/p&gt;
&lt;p&gt;
If I were to run the following single-threaded test, the application would perform as expected:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;  &lt;span class=&quot;token annotation punctuation&quot;&gt;@Test&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bankAccountWithdraws&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            bankAccount&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;withdraw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token function&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bankAccount&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getBalance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;main withdrawing from balance: 50
main has withdrawn 10. New balance: 40
main withdrawing from balance: 40
main has withdrawn 10. New balance: 30
main withdrawing from balance: 30
main has withdrawn 10. New balance: 20
main withdrawing from balance: 20
main has withdrawn 10. New balance: 10
main withdrawing from balance: 10
main has withdrawn 10. New balance: 0&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Introducing latency and a possible race condition&lt;/h4&gt;
&lt;p&gt;
The current execution of the single thread behaves as expected as the &lt;code&gt;withdraw()&lt;/code&gt; method is only called once at any one time. Latency can be introduced into the application by using the &lt;code&gt;wait()&lt;/code&gt; method which causes all threads to stop before continuing. The &lt;code&gt;withdraw()&lt;/code&gt; method must be marked within a synchronized block to ensure only one thread accesses the method at a single time.
&lt;/p&gt;
&lt;p&gt;
The following update to the &lt;code&gt;withdraw()&lt;/code&gt; method will cause the threads to check the balance, wait for each other, then to complete the transaction. As a result, each thread will read in the opening balance before making a transaction against the balance:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;   &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;withdraw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; spend&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; temp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; withdrawing from balance: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;balance &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; spend &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            balance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; temp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; spend&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; has withdrawn &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; spend &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;. New balance: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The single threaded test will continue to behave as expected, however multiple threads can be used to execute the code by using an Executor Service:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;  &lt;span class=&quot;token annotation punctuation&quot;&gt;@Test&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;counterWithConcurrency&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; numberOfThreads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;ExecutorService&lt;/span&gt; es &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Executors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;newFixedThreadPool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;CountDownLatch&lt;/span&gt; latch &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CountDownLatch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;numberOfThreads&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;numberOfThreads&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
           es&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

               &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                   bankAccount&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;withdraw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
               &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                   e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printStackTrace&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
               &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

               latch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;countDown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
           &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        es&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;shutdown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        latch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bankAccount&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getBalance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;pool-1-thread-1 withdrawing from balance: 50
pool-1-thread-2 withdrawing from balance: 50
pool-1-thread-3 withdrawing from balance: 50
pool-1-thread-4 withdrawing from balance: 50
pool-1-thread-5 withdrawing from balance: 50
pool-1-thread-1 has withdrawn 10. New balance: 40
pool-1-thread-2 has withdrawn 10. New balance: 30
pool-1-thread-3 has withdrawn 10. New balance: 20
pool-1-thread-4 has withdrawn 10. New balance: 10
pool-1-thread-5 has withdrawn 10. New balance: 0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Each thread reads in the balance as 50. Each thread will then assume the balance remains at 50 and makes the withdrawal of 10. &lt;br&gt;
The class appears to be thread-safe as the balance is reduced as expected, however if we introduce a 6th thread on line 4 of the test, the account becomes overdrawn:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;pool-1-thread-1 withdrawing from balance: 50
pool-1-thread-2 withdrawing from balance: 50
pool-1-thread-3 withdrawing from balance: 50
pool-1-thread-4 withdrawing from balance: 50
pool-1-thread-5 withdrawing from balance: 50
pool-1-thread-6 withdrawing from balance: 50
pool-1-thread-1 has withdrawn 10. New balance: 40
pool-1-thread-3 has withdrawn 10. New balance: 30
pool-1-thread-2 has withdrawn 10. New balance: 20
pool-1-thread-4 has withdrawn 10. New balance: 10
pool-1-thread-5 has withdrawn 10. New balance: 0
pool-1-thread-6 has withdrawn 10. New balance: -10&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Resolving the race condition&lt;/h4&gt;
&lt;p&gt;
We can resolve the race condition by asserting that only one thread can access the &lt;code&gt;withdraw()&lt;/code&gt; method at any one time, or by ensuring all threads are aware of the balance as it is updated. In our BankAccount class so far, we have already utilised one of the keywords used to create thread-safety: &lt;code&gt;synchronized&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Synchronized methods&lt;/strong&gt;&lt;br&gt;
The &lt;i&gt;Synchronized&lt;/i&gt; keyword in Java can be applied to either a method signature or a block of code to ensure that only one thread can access it at a single time. The &lt;code&gt;withdraw()&lt;/code&gt; signature is changed to &lt;code&gt;public void synchronized withdraw()&lt;/code&gt; to ensure only one thread can obtain the lock for the method at a single time. When the thread reaches the &lt;code&gt;wait()&lt;/code&gt; method, it releases the lock allowing another thread to enter the method. The use of the &lt;i&gt;synchronized&lt;/i&gt; keyword is not completely effective as it only allows one thread at a time to read the balance. The withdrawal of the fund still remains as a separate action that can be performed by multiple threads at a time.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3f31f40c932fca0e23c70558bf73dfa5/fd28b/016_synchronized.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 134.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAbCAYAAAB836/YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEV0lEQVRIx6VV7U+bVRTvf/H8IfrJGDW+RN1inGbGaRadWURgKw5QlrmFzGj2ImYosi1+I7BkY+DYYOD6QhkTKKQtLy0C5aW8tNgmhbb0aenb0/vznAtPbaWRD57kl3vvOfeee865555jSCQSyGazyGQySKfT2NnZQTKZRC6Xw+7uruSzXFVVxOPxwl4+x/t5nUqlwORwOGDgg8U0OTmJsbExDA8Pw+l0FvjhcBg2mw12ux1DQ0Nwu92IxWJSls/n5Tg9PQ0D38AkhCgc3traQigUgqZpkq/LdKsZfE5XxPuYpqamShUWH9Qt13nFFxYT8w9VODExgfn5+RK+LmOXFxYWSlw9VKHVai3Er5jPZDab4XK5SmRlFeq3zczMoK2tDe3t7QgEAiUW6LKOjg5sbm4e7jITp83s7KwUcloUX8YyfkmPx1OQHVD477Rh4nTgNClHzI9Go2VlUiEnsdwUi2I7sg01oWJpeQlz83NyHiFeNBqRI69n//TAu+ilxCYZ8VnGacZWckgMkUiUYuVHLqMhnaDcogisrWxgZXEVgoxPJegHJbMFZHZzEvo6m9akR6yUk93AlqWSaQSiK2gerEXrswZcG6jC1YFKtDytw0+2Wolm27nCnPHL06/xo/Usfp/toEBCfk35U9hstsK+YsJnd55H5b0XcaHvfVx8fBz1D46i4eG7+KrnHZx/dEzO63uOSpztehWn7jyHJvMZZDM5xNV4qcKJVQuqO19B7YO38P2TU7hhM6J5qAZN1io0DVbjJlneMlxP/Bop4wuq7r+En211UmGJhbuJNDwBO87cew213W/jy643UXP/DRgJNfswdr4u1zpqu4+g6u7L+PVZI/I5dnnfQg4op0FeaFgPL2Ez5oN72YHJBTv+iq0SbxEbW0tlsRZeQCxJL5zTZI7KV2ZTA5sBbG9tI7VDr0z5GvCFsOr1I09ljnnpuHYQqoaMmkc8mpBZEolE9hTqPyWRjpPb45gLOmCb6sUgYT7kkDy3fwxuCok+eiTGMUPrQGRFvnIhsdOZvWprXzbLmHzb/wku95/Epb4TaOg5hjqKVWPfx7jUewIXez/EN48+wPme93Dh4XEZ2xuWcxBaGYV/LD5Gxd0XcNX8Ob1sNa6ZK/Ddk0/R2P8RfrBW4vLASSm7YjqN65YvpJwz4rqpih5F/KOQ+wNTMLqObudtwi10OW9KdLtu4TfX7b35Pp9HBvM7HS0YJ89KXNZj+H+obPkqLet5mUb/RVRaoVEC5nmvli9xmQ0WVHUE5aOg5xdU6wTdKjY2NgRVIkGFVvj9fkGNSwSDQbG+vi6ojYq9cijkXp6TQlGwUO+1nJc6j9sAV5BisBXcV/QWeqCNkmUqKZCgmyT0NW1UaZ9qsVhU6jPqyMiISk1d8qgwq2SEhM6jRq8a1tbWFOoPCrmn8JzBc3JR8Xq9cmxtbVUqKioUo9GojI6OKtT1FJ/PJ89ROOR+qoeKyWRS/gZtq3Xnq2yR0AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Transaction flow diagram&quot;
        title=&quot;Transaction flow diagram&quot;
        src=&quot;/static/3f31f40c932fca0e23c70558bf73dfa5/5a46d/016_synchronized.png&quot;
        srcset=&quot;/static/3f31f40c932fca0e23c70558bf73dfa5/1db73/016_synchronized.png 75w,
/static/3f31f40c932fca0e23c70558bf73dfa5/8a4e8/016_synchronized.png 150w,
/static/3f31f40c932fca0e23c70558bf73dfa5/5a46d/016_synchronized.png 300w,
/static/3f31f40c932fca0e23c70558bf73dfa5/fc2a6/016_synchronized.png 450w,
/static/3f31f40c932fca0e23c70558bf73dfa5/0a47e/016_synchronized.png 600w,
/static/3f31f40c932fca0e23c70558bf73dfa5/fd28b/016_synchronized.png 811w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The problem of the overdrawn account occurs in the following steps:&lt;br&gt;
1. The first thread withdraws the funds.&lt;br&gt;
2. The if statement for all other threads are evaluated to the balance that was read &lt;u&gt;before&lt;/u&gt; thread 1 makes a withdrawal.&lt;br&gt;
3. The bank will become overdrawn as the balance from each thread is not updated with the withdrawal from thread 1.
&lt;/p&gt;
&lt;strong&gt;Thread communication and visibility&lt;/strong&gt;
&lt;p&gt;
The &lt;code&gt;withdraw()&lt;/code&gt; method may be able to stop multiple threads from accessing the bank balance together, however the &lt;code&gt;wait()&lt;/code&gt; method enables withdrawals of the balance to be made by all threads together.. To resolve the remaining race condition problem, all Thread instances must be able to see the updates that are made to the balance as each withdrawal is made by other threads. If the threads are able to see the latest balance update, they will correctly determine if a withdrawal can be made without becoming overdrawn.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Atomic variables&lt;/strong&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;
The atomic variables within Java aim to resolve the visibility problem that can occur between multiple threads of an application. 
The table below illustrates how the threads will obtain the value and perform the operation:
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f2a4fec7ad36986234f86c9fcbdb6f9d/97bfd/016_nonAtomic.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACu0lEQVQ4y3WRyVMTURDG5y8TvVhWufsHePAGKIoEAh6t0guBYhGvXqQsApJAQBIgOwnkSZhskxCWkMyShSyAcrFmks/3XlCBKg+/6n79fd0z1S1M+k2RUU8PGfe9Jhb3c/LO+YzQGpn0mYhlvZv0zd8lJts90m+7T8a9vWQqYCZjNL5deco1VjddQhh2d8Li7sao5wUGFx6jd/4OzIsPMeR4goGFB+ievYkuawe6rbdAB8O88Ijzcu42OmduoGum4wrCp9B7fZpYmtNkxJjyDxkT7gFjxPXKGHb28Di21meMu/uNj/43VB80Jrz9xiTlg8/M/dcRIlKomdESkGSxtV/OIJIKYWH1K9ybLjgDy3AFl7GZCCItJ5DVJOyqKWQpu9fIam2EjBZvpuQdUFppVcSWFIAvuoq0FgOvKxfQfLeYoMTblBKgvWA9Ge6NcoRMLtUsnajQ6nKrWFdQbLTR6m2KPMpgHhLfRCQWxpYYwubOBmKZHWSOJERThHtYn1Csas36zxrqP6otCho0Z7Ba47zO8xqv15GTD7Cfz+KQxr2jLHLKIdSKArlUYP0cQT4+aqr1AtRaoaXRWKjkkC8fgtZxqO3xN/s61aFU89AaLM+3/+iE/X2B+5nO5gjBmLvJdpQobLckuo+NuAfWb9Owr89iOWBHQFyHpIgc2+oM1+acXzCz/Jnr3m0XXGEHkoUokmyH2VLy71HYcsNJP220Ysln48M9313cnFZjcIUcWAkuwrnhwJLXRo+3BjdxIpYjXGdzhNR+4lf5VDPoQvViQ9VLFxQ5ik6PwWP5RNNFaVuPiGE9kRV1egjuq5wWdXq4C7+qC2pFpks/RvWswqmd/csvw44UIkH4Ql5sxwmkvSTS+ymUatqVfqFxXrMdn5bt9PFfmE6b7GIqykkfSHatqtjjadGuVApc+zPjN6HOWhB9rubkAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Without Atomic variable table&quot;
        title=&quot;Without Atomic variable table&quot;
        src=&quot;/static/f2a4fec7ad36986234f86c9fcbdb6f9d/5a46d/016_nonAtomic.png&quot;
        srcset=&quot;/static/f2a4fec7ad36986234f86c9fcbdb6f9d/1db73/016_nonAtomic.png 75w,
/static/f2a4fec7ad36986234f86c9fcbdb6f9d/8a4e8/016_nonAtomic.png 150w,
/static/f2a4fec7ad36986234f86c9fcbdb6f9d/5a46d/016_nonAtomic.png 300w,
/static/f2a4fec7ad36986234f86c9fcbdb6f9d/fc2a6/016_nonAtomic.png 450w,
/static/f2a4fec7ad36986234f86c9fcbdb6f9d/0a47e/016_nonAtomic.png 600w,
/static/f2a4fec7ad36986234f86c9fcbdb6f9d/97bfd/016_nonAtomic.png 1078w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
Atomic variables within Java provide variables that are updated across all threads of the application at the same time.
An atomic value in this context is useful to ensure all the threads can see the latest balance before making a transaction. This way, the if statement will be applied to the most up-to-date value of the balance. The BankAccount is updated to use AtomicInteger accordingly:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BankAccount&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AtomicInteger&lt;/span&gt; balance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AtomicInteger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;withdraw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; spend&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Withdrawing from balance: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;balance&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; spend &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            balance&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;addAndGet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;spend&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&quot; has withdrawn &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; spend &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;. New balance: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; is unable to  withdraw &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; spend &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; from a balance of &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; balance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AtomicInteger&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getBalance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;balance&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The shared cache between the threads means the update to balance that is made is visible across all threads. 
As a result, the second thread is able to see the updated &lt;code&gt;balance&lt;/code&gt; value and will evaluate the if statement appropriately. As a result, stage 4 will never be executed as the balance is to become overdrawn. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/4ad8be5a3f01d3152d345f0cb5dc7609/97bfd/016_atomic.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACxUlEQVQ4y3WRSVMTURSF+5eJpSsLh8LiD7gEVGQ0sLZcKOCMSxdqFUhpaEBBiCQhJqQfCel0p0kCBJL0kDlh2mi3Ob73gKgLF1+d9+49t+rWPcIL/2B4YvUueebrk56s9pIBdzt5tNJFXgdGyFPvPfJg6RatXSUDn9rJ6HwnebU2TL395IV/iIzOdXL/oPtaC+Gxpwtj33owvnoHI3M30TV9AQOz7XDNdXD63VfQM3MR3R/acGfmMoZnr2NYvAGX2IHbM5e4v3u6rYXwJvjw5zsy9us9GXde+lzO5Nqo89Qz6Iwv9zpjS73OxHIf/z9fHXYm/aPUc9957h3ivPS7nFf+EYqrhSBpQSdpKtB0uZkyVGRKKXilFcx73FgJLmLRNw9PaAlEC2Hb0pA2EkhRzvVv0mYCQtKMO4n8JhL6ZlPNRUH/8EaWEVR9SFpxqPkoeJ+qpseQshReT3Ho25SxxTBk7hWSewmn0DBg1vJNq6bDqv/BrJ2TB/NkixlIsRAkeR3hM1W3ZcTTMa7FhgnBqphO/biK2lGlSWGK+nENvEapn9RorcopN0rYyaYp29ilpPdSyJp7yBeyMIp5PiPky/uOUcuB0jSqOb5NtpDh29AeMuY29EqW1k97xUMTxYNTSvTNNmdYDR1sXgjI3xx6P9D7NZVchN/ka3ABUwtv8WVtFp8pUjIAzYgxD5RsBPHsBtWNlrI5lcFumC6orVCYskN7wov4uDSFlfUv8LGAFC8PhEH9FAUpCnszPwuE9di8kNhRftD1HRqCbdUNu3CGxdFb70LDtHPFfZvI63ZEkexQNGBHVGJru4pNg7X3CxmbhmILRimP6lEZlcPSGeUzSv/AwjIrBqTNMILkO8LRENZCPgSlADZkCXoxR0OsQKifVN3lg6JIh/7Leb9QM0VtJyGq6bi4tXuqsURUlLVN0azoIl1M/A39YF0ksc6u9gAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Atomic variable table&quot;
        title=&quot;Atomic variable table&quot;
        src=&quot;/static/4ad8be5a3f01d3152d345f0cb5dc7609/5a46d/016_atomic.png&quot;
        srcset=&quot;/static/4ad8be5a3f01d3152d345f0cb5dc7609/1db73/016_atomic.png 75w,
/static/4ad8be5a3f01d3152d345f0cb5dc7609/8a4e8/016_atomic.png 150w,
/static/4ad8be5a3f01d3152d345f0cb5dc7609/5a46d/016_atomic.png 300w,
/static/4ad8be5a3f01d3152d345f0cb5dc7609/fc2a6/016_atomic.png 450w,
/static/4ad8be5a3f01d3152d345f0cb5dc7609/0a47e/016_atomic.png 600w,
/static/4ad8be5a3f01d3152d345f0cb5dc7609/97bfd/016_atomic.png 1078w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The AtomicInteger is used to communicate across the threads. When the penultimate thread is executed, the balance is atomically updated to 0 across all threads. Therefore the final thread evaluates the bank account balance to 0 and is no longer able to make the final -10 transaction. 
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;pool-1-thread-1 withdrawing from balance: 50
pool-1-thread-2 withdrawing from balance: 50
pool-1-thread-3 withdrawing from balance: 50
pool-1-thread-4 withdrawing from balance: 50
pool-1-thread-5 withdrawing from balance: 50
pool-1-thread-6 withdrawing from balance: 50
pool-1-thread-2 has withdrawn 10. New balance: 40
pool-1-thread-4 has withdrawn 10. New balance: 30
pool-1-thread-5 has withdrawn 10. New balance: 20
pool-1-thread-6 has withdrawn 10. New balance: 10
pool-1-thread-3 has withdrawn 10. New balance: 0
pool-1-thread-1 is unable to  withdraw 10 from a balance of 0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Multi-threading allows our code to utilise the multiple cores of the CPU. The danger of multi-threading however leads to potential errors of visibility and synchronisation between the threads, one of which is known as race condition.&lt;br&gt;
The &lt;i&gt;Synchronized&lt;/i&gt; keyword in Java allows a thread to obtain a key to a code block or method during its execution to ensure no other thread is able to access the code at the same time. The concurrent.atomic package in Java includes different Atomic variables that provide cross-thread visibility to a shared value.
&lt;/p&gt;
&lt;p&gt;
The Atomic package and Synchronized keyword are useful tools that assert an element of determinism about a multi-threaded application. These tools enable execution order to be achieved and cached memory to be shared between threads to reduce non-deterministic behaviour within their domains.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The Spring architecture layers]]></title><description><![CDATA[This post will look into the Spring annotations used to create custom beans for the various layers of a Spring application.]]></description><link>https://aneesh.co.uk/the-spring-architecture-layers</link><guid isPermaLink="false">https://aneesh.co.uk/the-spring-architecture-layers</guid><pubDate>Mon, 13 Jul 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Compare the properties of the different layers within a Spring application.&lt;br&gt;
&amp;#8226; Review the importance of the Spring IoC container, BeanFactory, and ApplicationContext.&lt;br&gt;
&amp;#8226; Understand the differences between the beans created from @Component, @Controller, @Service or @Repository.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Spring application architecture&lt;/h4&gt;
&lt;p&gt;
The architecture of a Spring application may reflect upon the intent and organisation of packages and classes.&lt;br&gt;
The Separation of Concern (SoC) design principle aims to create a modular architecture where delegated classes address well-defined functions of the application. SoC supports the long-term maintenance and growth of the application and discourages code rot.
&lt;/p&gt;
&lt;p&gt;
The Spring framework architecture uses 3 layers to offer a degree to separate concerns between classes based upon their responsibility within the application.&lt;br&gt;
Each layer is placed to communicate with the layer above and/or below it. The layers include:&lt;br&gt;
&lt;p&gt;• &lt;strong&gt;API/Web layer&lt;/strong&gt;. Used to accept and authenticate user input and handle HTTP requests. Spring MVC is used to separate the web view, the servlet controller and the model objects.&lt;br&gt;
• &lt;strong&gt;Service layer&lt;/strong&gt;. Interacts with the API layer and model objects to provide authorization and custom business logic processing. &lt;br&gt;
• &lt;strong&gt;Data access/integration layer&lt;/strong&gt;, also known as Repository layer. The basic CRUD operations are exposed in the data access layer to send operations to the database. Object to relational mapping (ORM) is used to map data to and from the database. &lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Separating layers into a request and response&lt;/h4&gt;
&lt;p&gt;
The diagram below illustrates the layers of a Spring application and how it would handle a request made by a user.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/be0da214a1170b55940fa9a5674064e2/a6d36/015layers.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 173.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAYAAACU9ioYAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFpUlEQVRIx41W+VPTRxTP39Hf+nunP7WOM9a2oNSKCuIxWqtjq44dC3IEUAe1dtQZtaKCMCoqimhhrAeOjeKgVshBQCCQi0RL7vsiCSHkzqe7mwSCEsqb+cx7+/bls2/3bb5vOVNTUwiFQpiZmcmJaDSKcDiMYDC44Pz09DTi8TjUajU4sVgM2ZJMJhmyxeVywe/3I5dk4g0GAzh09WznhxKJRNDZ2Ynu7u4F4+g4kUgwW6/XzxFS0Wg0UCgUDIFAgPnoDnp6ejAwMMDGdOvDw8MMk5OTzEe3uyChWCxmQV6vlxHRlXk8Htra2tDa2gqBQMDi6JnL5XLodLrFCaVS6bzt0LmRkRG2kEgkglKpnJ0zm80wmUyLE0okEpbVQoXJSPZ5GY3GhQkTSRqYhGR0jEwmECd2BrFYfA5kjoKKVqeH3mBM7ySWY8ujQ1iqOKwmuByWeT5GSK+FQuNBv8yOG50vIZJaMaBwsLFgzAah1Aax3D6LfpkNQyoPnv4jB++NEsNqD/ijFpicIXKm5B5aHD6U1AnxVWkvCqpF+LaiD9+U96LwkBDbfx/ApmP9KODy8W15H8OqKj7yK/tYXB5BPsGX+1+D2yyD0aAFx2D1YmOdCPlVIqyuEjDSvEohaq8qcLpdjYONUuw5K8HhFgVO3lHjFPH9eGqYxNBYIdbUiLCitA+VTeQaaTVZGZb1YjXNMJ3JmmryA66AZULt9UdE2HJczDJnmZHF8wlp3ocZRskZyjVuclZWci4ydkZiuY2cJQGx6ZhqOi8YszI9qHTihfAdXoon8HbcCb6E3EnnDExGw/wqj8ukS66yUachlTbP8+lolelfzD/tg9Nnx7Oev2HzWOD02nPCQeALTkKulkL+TsZsm9uCWCJKtmwEx+1z4CRvN6oerQX34TocflLCUP14PWoYipiu7Spi4D4qZKh5vIGhtqsYZfdX4aboBIx6EzhWjxFHnmxGxaPvCApwlLcVLaKjaOLX4NKbCjT1VaOZfwhnevYxNPNrcU1Yh8ZeLqoekyQIDtz/Gpf7uNBqdODYPCZU/lWIPfeW4ZfOFQQryQKbWIZVD9eyrGjG5Q8KmO833g4ce7qNEBViX8dy7O9YgZ1tn6P+dRn0WlKUUGgGovfP0S27h4f9LXgh78BzaTuey+4yH9V0nLGfUVt+F11DN8GTtOOl8j54o20Yt70lVTaTokTnWsCEWrf0KmuscFg8H/yXyZaj9CuSiJMvRhivXvUgFA4hGouwcUpHUjqNcCREP/zka6Mh10Sb/uAG53rKjM8D561SOC6VQH+uCPb6DXBc3AjHhWI46ovmNPVfILphM5wk1nK2EJZzhcTeBNsf6xB4dh46owWcabsezhPL4D25HIEbP8F/fTd8zVvhrf8evqYt8F1cz7T/ynb4GorhPZMHX2MJ/E2b4Sdx1HZXf4qpll2Y0JKLHSCEjuNfYLLuM/gbNjAy36WiWSLveULcuDEF4p88vRJeoqeu7UDg+i5MnlsNV8UnbDyho1UO+OF5cgautoPQXN4N161f4bpdlkYp8zPNQHx3yuG5cxDGK3thuLIHnruVcLQeQFDYntpyLN0PaAcZkSqQSNuJHIinK6r6VwvVROpWRNPtJ6vRJxlGSZNK0iZEGxRrVGk7PQ/W1FOURvJj82zXy9FTRj/qeikdjsYRicXTpEvoehkZGxub/6aZiuC2wERgQSvfjAdvrZgOpbKhZP/blwcHB1MvsVCYHFgUEq0HxU1j2Ht7HLtuKvBzqwJ6R+rRpCIvLUqwKKFKpYJMJoNCOQ6VQoqu10PY1qLEzhtK/HBdiX23FBAOy/H+nYot7na7cxNmXgpU0wZPRW6aQmHDKHYQsi1X5SxLmze0tNdX9vMjo+3+ME50TaC8g3S/P9Vo6NEjGI59FL8oYTYyEmPPk8RHj9KFCP8DNo1/LQUo7QYAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Spring layers&quot;
        title=&quot;Spring layers&quot;
        src=&quot;/static/be0da214a1170b55940fa9a5674064e2/5a46d/015layers.png&quot;
        srcset=&quot;/static/be0da214a1170b55940fa9a5674064e2/1db73/015layers.png 75w,
/static/be0da214a1170b55940fa9a5674064e2/8a4e8/015layers.png 150w,
/static/be0da214a1170b55940fa9a5674064e2/5a46d/015layers.png 300w,
/static/be0da214a1170b55940fa9a5674064e2/fc2a6/015layers.png 450w,
/static/be0da214a1170b55940fa9a5674064e2/0a47e/015layers.png 600w,
/static/be0da214a1170b55940fa9a5674064e2/a6d36/015layers.png 650w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
A user request is first intercepted by the API layer. The API layer is used to handle HTTP requests such as GET, PUT, DELETE and POST.
&lt;/p&gt;
&lt;p&gt;
The request can then be processed by the service layer to implement business logic. Such logic may include checking authorisation, transforming the request or logging. 
&lt;/p&gt;
&lt;p&gt;
The data access/integration layer transforms the request using ORM to interact with the database. The database is persisted outside the layer and will return a response that is then relayed back to the user.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Spring Inversion of Control Container&lt;/h4&gt;
&lt;p&gt;
The Spring IoC Container is a core Spring Framework component responsible for creating, wiring, managing and configuring objects to be used by the application. The components enable Spring to perform dependency injection as they are wired into classes that require them.
&lt;/p&gt;
&lt;p&gt;
The objects that are configured by the IoC container are known as Spring beans. Each bean contains metadata that includes information for how to create the bean, the bean&apos;s dependencies, and the lifecycle details. 
&lt;/p&gt;
&lt;p&gt;
There are two types of containers within the Spring Framework:&lt;br&gt;
&amp;#8226; BeanFactory.&lt;br&gt;
&amp;#8226; ApplicationContext.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;BeanFactory&lt;/strong&gt;&lt;br&gt;
The BeanFactory is responsible for maintaining the registry of different beans and their dependencies.&lt;br&gt;
Beans are injected into the classes that depend upon them with lazy loading and is therefore only used when memory consumption is critical.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;ApplicationContext&lt;/strong&gt;&lt;br&gt;
The ApplicationContext container can perform all the functions of the BeanFactory with additional functionality:&lt;br&gt;
&amp;#8226; Load file resources.&lt;br&gt;
&amp;#8226; Publish events to event listeners.&lt;br&gt;
&amp;#8226; Resolve messages for internationalisation support.&lt;br&gt;
&amp;#8226; Support for Annotation-based dependency injection.&lt;br&gt;
&amp;#8226; Integration with Spring AOP.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Unlike the lazy-loading of bean from the BeanFactory, the ApplicationContext will load all beans upon instantiation and is therefore considered heavy-weight in comparison to BeanFactory.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Spring class annotations&lt;/h4&gt;
&lt;p&gt;
The Spring class-level annotations are used to register beans into the Spring IoC container. Class-level annotations are detected by the ApplicationContext container during a classpath scan of the application.&lt;br&gt;
The following annotations are applied at the class level according to layer they are responsible within. Each annotation except for @Service contains unique properties to the bean that support their function at the layer.
&lt;/p&gt;
&lt;strong&gt;@Component&lt;/strong&gt;&lt;br&gt;
&lt;p&gt;
The &lt;strong&gt;@Component&lt;/strong&gt; annotation is the parent annotation to the other layer-level annotations &lt;strong&gt;@Controller&lt;/strong&gt;, &lt;strong&gt;@Service&lt;/strong&gt; and &lt;strong&gt;@Repository&lt;/strong&gt;.&lt;br&gt;
Classes marked with &lt;strong&gt;@Component&lt;/strong&gt; will be automatically detected by the Spring framework and added to the ApplicationContext.&lt;br&gt;
The &lt;strong&gt;@Controller&lt;/strong&gt;, &lt;strong&gt;@Service&lt;/strong&gt;, and &lt;strong&gt;@Repository&lt;/strong&gt; annotations all use &lt;strong&gt;@Component&lt;/strong&gt; in their definition, therefore their use also results in the class being added into the ApplicationContext.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/bb8a5940e92d28c9938f04713ad4f51f/6da96/015Annotations.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 58.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACa0lEQVQoz31S3U/TUBzdX2Lio4kPPqgIERV5MXvwAR+M8UGjLyLxI0GB+RFwjAEmEyP6IInCg4qRhBhw+MUUow7dcKvtgAgKJNsQ13ZsLW23td3xtoU6/DrJ6f3l/s4595fb61AUBaqqolAomNRInRFkfEtkVpm1OVdSZ0XF1No+TYMgCHDouo416EVr7Xu6gIq6MdRc+gBn43s4G35xv2sc5bVjeBiIr/MYMIZzGMkGisUitNXu9YFZVJx8g7ouCgdbQjjaQeHYVQqH3CEcbg1hy/FX6BmaM7WaVjS9BmRZXh+orwaOjC/iiDeMM90MTvgiqGkYwYHGZ6j1RXH6BmP2RieWrAn1vwaSTV2zuYbsModkfAFTsc/4OjuNRHwe2Qxn922PppYEqip+B7+Sx8xCEvF4AsbZLMuRCxfJfRXNvZn5BDgx94dPliUSSByaLECkR6FMjmHK34fJ4TsQpt6akxv4nkwizbG2UZz5iGn/XcSITp58jRU6AF3KQMnlyV8mAokEsU2bwLdsB9+8FUJrGVLNZSisZMyARGoZHHlKBtR8DmxbFQT3NqK1mDq7AVLkCRRyvhXIBMxGun0v+LZd4L178MNdiey7fogvuhEf8GJxsBPi824In/xIeastXXsV0h3VYOs3QgoNWoEq+eSW5sH1N4F/dNkiqblhH9jeU+CulCPt2QneUwnWtRnc/fNIv+wB/6DB1nP9LuSSXyDlVTjwHyhDHkiduyF1OSFd2wfJswM5f+c/9cbzcVAUBYamQVPREkZAMzGEHvcieLMewdsXCC8ieOscwsP3zB4djazTxxgG4fAEfgJHlkAdeBgn4AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Annotation relationships&quot;
        title=&quot;Annotation relationships&quot;
        src=&quot;/static/bb8a5940e92d28c9938f04713ad4f51f/5a46d/015Annotations.png&quot;
        srcset=&quot;/static/bb8a5940e92d28c9938f04713ad4f51f/1db73/015Annotations.png 75w,
/static/bb8a5940e92d28c9938f04713ad4f51f/8a4e8/015Annotations.png 150w,
/static/bb8a5940e92d28c9938f04713ad4f51f/5a46d/015Annotations.png 300w,
/static/bb8a5940e92d28c9938f04713ad4f51f/fc2a6/015Annotations.png 450w,
/static/bb8a5940e92d28c9938f04713ad4f51f/0a47e/015Annotations.png 600w,
/static/bb8a5940e92d28c9938f04713ad4f51f/6da96/015Annotations.png 922w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;u&gt;@Controller&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;@Controller&lt;/strong&gt; is a specialised version of &lt;strong&gt;@Component&lt;/strong&gt; to be used for classes at the API/web layer. When a class is marked with &lt;strong&gt;@Controller&lt;/strong&gt;, the ApplicationContext will register the class as a bean, and the class will be looked upon differently for web-specific tools and services.&lt;br&gt;
When a client request is made to the Spring Application, the dispatcher servlet, which is responsible for forwarding requests to the MVC controllers, will scan all classes marked as &lt;strong&gt;@Controller&lt;/strong&gt; to find the appropriate &lt;strong&gt;@RequestMapping&lt;/strong&gt; annotation to handle the request.
&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;&lt;u&gt;@Service&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;p&gt;
&lt;strong&gt;@Service&lt;/strong&gt; is a wrapper version of &lt;strong&gt;@Component&lt;/strong&gt; and is used to mark the class as a service.
The service class will provide business logic functionality to process requests before passing them to the data access layer for persistence.
&lt;/p&gt;
&lt;p&gt;
The &lt;strong&gt;@Service&lt;/strong&gt; annotation does have include further behaviour that is different to &lt;strong&gt;@Component&lt;/strong&gt;, however service classes should be marked with &lt;strong&gt;@Service&lt;/strong&gt; to mark the intent of the class within the application.
&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;&lt;u&gt;@Repository&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;p&gt;
The &lt;strong&gt;@Repository&lt;/strong&gt; annotation differs from &lt;strong&gt;@Component&lt;/strong&gt; for the single purpose of catching persistence-specific exceptions.&lt;br&gt;
The &lt;strong&gt;@Repository&lt;/strong&gt; annotation will import the ORM beans into the ApplicationContext container and change the unchecked exceptions eligible for translation into a DataAccessException.
The annotation provides enhanced functionality for exception handling.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The Spring IoC container allows the framework to provide dependency injection across the application.&lt;br&gt;
The Spring framework is constructed of 3 layers that separate the responsibility of the class when handling requests and responses.&lt;br&gt;
The ApplicationContext container is a heavy-weight framework for dependency injection that supports annotation-based injection. The &lt;strong&gt;@Component&lt;/strong&gt; annotation is used to register a bean at the class-level into the ApplicationContext. 
The Spring framework offers three further annotations that extend from &lt;strong&gt;@Component&lt;/strong&gt; to provide unique properties the bean generated to assist with the responsibility of each layer.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using a RESTful API in Spring]]></title><description><![CDATA[Consume RESTful APIs with a Spring service and deserialise the JSON data into custom values.]]></description><link>https://aneesh.co.uk/using-a-restful-api-in-spring</link><guid isPermaLink="false">https://aneesh.co.uk/using-a-restful-api-in-spring</guid><pubDate>Mon, 06 Jul 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand what an API is and how they work.&lt;br&gt;
&amp;#8226; Use Spring to make GET requests to the API.&lt;br&gt;
&amp;#8226; Explore how the GET request can be broken down and used within a service.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is an API?&lt;/h4&gt;
&lt;p&gt;
An &quot;application programming interface&quot;, abbreviated to &quot;API&quot; is a software intermediary between applications. I like to think of an API as the waiter between you (the client) and your meal (the data) in a restaurant.&lt;br&gt;
APIs allow applications to speak to each other. When an application request is made, it will send the data message to the server where it is interpreted and a response is returned. The application will then interpret the response and display the output to the user.&lt;br&gt;
APIs are all around us in the applications we use; they provide new bounds of opportunities for the information an application can retrieve and interact with.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;RESTful APIs and CRUD&lt;/h4&gt;
&lt;p&gt;
The term &quot;REST&quot; in computer systems stands for &quot;Representational state transfer&quot;. REST defines a standard to make it easier for computer systems to communicate over the internet. REST-compliant systems are stateless and separate of concerns between client and server.&lt;br&gt;
A RESTful API uses HTTP requests to access and modify the data. As a result, RESTful APIs expose data to the be consumed by our applications.&lt;/p&gt;
&lt;p&gt;
CRUD operations are the four basic functions of persistent storage. RESTful APIs allow CRUD operations to be performed:&lt;/p&gt;
&lt;p&gt;
&amp;#8226; CREATE: A new data entry can be made with the &lt;strong&gt;POST&lt;/strong&gt; HTTP request.&lt;br&gt;
&amp;#8226; READ: Data can be read with a &lt;strong&gt;GET&lt;/strong&gt; HTTP request.&lt;br&gt;
&amp;#8226; UPDATE: Data can be updated with a &lt;strong&gt;PUT&lt;/strong&gt; HTTP request.&lt;br&gt;
&amp;#8226; DELETE: Data can be deleted with a &lt;strong&gt;DELETE&lt;/strong&gt; HTTP request.&lt;br&gt;
&lt;/p&gt;&lt;p&gt;
In the following example, I will send a GET request using the Spring framework to access a RESTful API service.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Performing a GET Request with Spring&lt;/h4&gt;
&lt;p&gt;
In this example, I will be using the free RESTful API service by jsonplaceholder.typicode.com to obtain the some sample data of a to do list. &lt;br&gt;
&lt;/p&gt;
&lt;strong&gt;&lt;u&gt;Setting up Spring&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;p&gt;
The Spring &lt;strong&gt;RestTemplate&lt;/strong&gt; class offers template methods for making HTTP requests of RESTful API services. 
The RestTemplate class is obtained through the &lt;code&gt;spring-boot-starter-web&lt;/code&gt; dependency in a Spring boot application:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
    &amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;spring-boot-starter-web&amp;lt;/artifactId&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
In the main application class, I will create an instance of the RestTemplate in a bean to be later injected:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDoGetDemo&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;SpringApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ToDoGetDemo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getRestTemplate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The RestTemplate is injected into a class that contains a RestController. The API is then called upon with a GET Mapping:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@RestController&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;APIController&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RestTemplate&lt;/span&gt; restTemplate&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token annotation punctuation&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/todo-items&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getPrices&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; url &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://jsonplaceholder.typicode.com/todos&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;restTemplate&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getForObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;url&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
There are quite a few points to take from the class above, so I will break it down line by line:
&lt;br&gt;
On line 1, the &lt;strong&gt;@RestController&lt;/strong&gt; annotation is used to mark the class as a request handler. The annotation will be responsible for mapping request data to the defined request handler method.&lt;br&gt;
On line 4 and 5, the RestTemplate is injected into the class. The RestTemplate will be used as a service for making the GET request.&lt;br&gt;
On line 7, the &lt;strong&gt;@GetMapping&lt;/strong&gt; annotation is used to map the request to the &quot;/todo-items&quot; handler. GetMapping was introduced in Spring 4.3 as an amalgamation of &quot;@RequestMapping&quot; and &quot;method=RequestMethod.GET&quot;.&lt;br&gt;
On line 9, the URL for the RESTful API is defined. The URL can be tested with an API development tool such as Postman to validate the information we expect to see.&lt;br&gt;
Line 10 uses the RestTemplate instance by passing in the url and mapping the response to an instance of the String class.
&lt;/p&gt;
&lt;p&gt;
Once the class is built, the Spring application can be run and the output from the API can be found on localhost:8080/todo-items. The output will appear as a large String in the browser:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 46.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABWklEQVQozz2SzY7CMAyE+/5PB03a/CdIrHqrRBck2JN3xkk5RCl4Mv4yzpRzkhij5JwlhCC1FLm1mzQsfnvvJaU09q7LuUhBjedqbRLHuefzKVOAcLGLmgXfCxQsyyIrljVWzViP4TRMkmCmOmuhW+V+/5F932XK6D7Psx5064rd6O6dU7JWq5rw93y9yuVyEQsAGlHHWi1VG2zbJlMtWQkcDqwQOF1Ou5Ko5H41fvMG1JGei9p16Gn6eb9hCHznvBYoInGny+iaYJi/eZFYo8EqSpaVUBvGIMdxIEMUDa4ZQEDsM0OK2IQZaxS84qBLY4iMo7WmDTzoP5+/niEPMB8acBAk9M5rkz6EniFjoM6YGbmbPn38X2FM08cDhESnKc1ISkOd3KBivufT4VJyTh81DpA7zzG21+slUwxeDKZMIcNl8OfzYPc+eat1UpfxXmneB+m+9MfxK/9EgH7OVmOS3QAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Get with String class&quot;
        title=&quot;Get with String class&quot;
        src=&quot;/static/153e483bde00b6013e19b1a1c9e1e747/5a46d/014StringGet.png&quot;
        srcset=&quot;/static/153e483bde00b6013e19b1a1c9e1e747/1db73/014StringGet.png 75w,
/static/153e483bde00b6013e19b1a1c9e1e747/8a4e8/014StringGet.png 150w,
/static/153e483bde00b6013e19b1a1c9e1e747/5a46d/014StringGet.png 300w,
/static/153e483bde00b6013e19b1a1c9e1e747/fc2a6/014StringGet.png 450w,
/static/153e483bde00b6013e19b1a1c9e1e747/0a47e/014StringGet.png 600w,
/static/153e483bde00b6013e19b1a1c9e1e747/9cab2/014StringGet.png 864w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Converting JSON to a POJO&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;p&gt;
The JSON API content can be converted into a POJO (plain old java object) to allow us to navigate and use the data. 
Rather than passing the GET request into an item of the String class, we will create our own class to store the values within. The class will then be held in an array of instances.&lt;/p&gt;
&lt;p&gt;
A new class, &quot;ToDo&quot; is created to store the fields of the API call according to their content type from the request:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; userId&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; completed&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;//getters and setters...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The RestController is updated to reference an array of the ToDo class as the response mapping to a new handler &quot;todo-objects&quot;:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@GetMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/todo-objects&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getPostAsObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; url &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://jsonplaceholder.typicode.com/todos&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;restTemplate&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getForObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;url&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
When the &quot;/todo-objects&quot; request is made in the browser, the output is in a JSON format:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 90.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsklEQVQ4y51UiXbiMAzM/39od7uEkNuHJM+OnIRCly20fk/EOGbQjEZqliBYkyFlg5lBi6GU8uNoGHjvFDGDBwKJCVDulcDCMF74xmr84zwoxpXZMcOlHZC7FTomSB9gSevFlzOMSTAshm4yZGa1xInAO8j+r59/dJw9zNBIqZ+pofhlhVHDW7BH639gV8q/qeESC0wVaztudC8Bcg7QyalH7jcZnmaYKbxn6JQT99PSQ0Rqil6QK1Xbo9xKgH+iyVkrYEh+QsrQJ3X8uuqNF/G9J2XaJohi7lfESRDGjDBlxEBLUdaoZY/P+/to/MXbWfF2Mcw0ePvnhGUIBCUY9XNAHiPx3hb4Mqpt5lDYLa6JE5Zv07yj7OJeJsIQ3ajhXUGOoly9iKdRAX+RcjsaQVnl86V2iNvErbNZ5LVOqRkGEl/pwTlsgEniQ4IPAQ4vlo87tVNO/TYczIeDygcAyssSXjP07mgJ6D70tgvdXDukdscYa3ccg0LHeN89He8NsX432fzLKmvtktOwdcowd8gp0+NlG2G6F8dH2THSjqfs7/VGQ9fNR1fYbXMMh58sB/0LQZKOQwVOYs4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Get with ToDo class&quot;
        title=&quot;Get with ToDo class&quot;
        src=&quot;/static/b1854c39b6bb66f7409b30263c72a75a/5a46d/014ToDoGet.png&quot;
        srcset=&quot;/static/b1854c39b6bb66f7409b30263c72a75a/1db73/014ToDoGet.png 75w,
/static/b1854c39b6bb66f7409b30263c72a75a/8a4e8/014ToDoGet.png 150w,
/static/b1854c39b6bb66f7409b30263c72a75a/5a46d/014ToDoGet.png 300w,
/static/b1854c39b6bb66f7409b30263c72a75a/0ff56/014ToDoGet.png 407w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Custom deserialisation with Jackson&lt;/h4&gt;
&lt;p&gt;
The current GET request is deserialised by our application into a POJO. One way we may want to refactor the output of the request could be to make the &quot;completed&quot; boolean into a descriptive String.&lt;br&gt;
The &lt;strong&gt;JsonDeserializer&lt;/strong&gt; class from the jackson.databind package enables custom deserialisation of the API response to transform the data.&lt;/p&gt;
&lt;p&gt;
The custom deserialisation will take place in a new class &quot;ToDoDeserialiser&quot; which extends the JsonDeserialiser class.&lt;br&gt;
The ToDo class will make use of the custom deserialisation class with the &lt;strong&gt;@JsonDeserialize&lt;/strong&gt; annotation where the ToDoDeserialiser is referenced (line 1):&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@JsonDeserialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;using &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDoDeserialiser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; userId&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; completed&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The &quot;completed&quot; field is marked as a String to reflect the new data type we wish to return. Ensure the getter and setter fields, along with the argument-constructor is updated to reflect the change in type.
&lt;/p&gt;
&lt;p&gt;
The ToDoDeserialiser class overrides the deserialize method of the JsonDeserializer class to provide custom logic for deserialisation:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDoDeserialiser&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JsonDeserializer&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;deserialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;JsonParser&lt;/span&gt; jsonParser&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
				 &lt;span class=&quot;token class-name&quot;&gt;DeserializationContext&lt;/span&gt; deserializationContext&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;JsonNode&lt;/span&gt; node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; jsonParser&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getCodec&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;readTree&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;jsonParser&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; userId &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;userId&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;intValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;intValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;textValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; completed &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;completed&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;asBoolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; isCompleted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; completed &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Is Completed&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Not Completed&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ToDo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;userId&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; isCompleted&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The deserialize method first obtains a new JsonNode &lt;code&gt;node&lt;/code&gt; on line 7 from the JsonParser. The JsonParser provides read-only access to the JSON data. Each JsonNode represents an instance of ToDo in the request.&lt;br&gt;
Lines 8 to 11 obtains the values from the node which are later passed into a constructor for the ToDo item that is returned.&lt;br&gt;
Line 12 will convert the boolean &quot;completed&quot; into a descriptive String as per custom logic.&lt;br&gt;
The new String conversion is returned in the ToDo constructor and as a result, the GET mapping translates into a new JSON output where the &quot;completed&quot; value is a String:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 85.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsTAAALEwEAmpwYAAABlElEQVQ4y6VUi3KbQAz0/39mm9ahMeZxwD0k3UbSOZkyjluS4JHRwNyilXZ1CmvBEhkpM5g1iCEsnosIqlTUejxOogeGIMgEfcCgqsk3rpP9dVfGuIhXlM4B+bwg/w4oLxsktg8crjAXRtjEAYkqZMqgMYL6CNE23B1q6J5/WCFzxVUpx6xgRhn/pvwIaEe5n3Qoxb6qvQxa2UbgkD08n3OLrbwDPqywkOAyCbqBURSUnlftYUD6OTtg1fdG3Xpp97/7+SGg9XCJ9UZZD+vv25Sfe6PcZJPmFVV1yapPXvLdlN9BH1VoL4dZfDCsUy4mmU7jV/BpH6G5A4yJMK3iweoQKa1XkvgwzbsKJ9MgN8pFyg5oT/OgU84Xcsrm4fzH6M5IPyaUftsB/i8cMN2mbJRJRV6VKs2pSaZ+bjE4IFEbiAlbdDEUlC8vBvdy1YVwGRlbvjmlX0GXiHJVP09a6VLcJTRobj4foldPY/JwB90M0ISte+tFXdKN5hSl3a2+afLT7DqsxSZOzSmJ9/e33JaINMqvtqI+bn6ZojgAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Get with ToDo Deserialised&quot;
        title=&quot;Get with ToDo Deserialised&quot;
        src=&quot;/static/6c5a65b78e1c689d26ca6aacbe9c0021/5a46d/014ToDoDes.png&quot;
        srcset=&quot;/static/6c5a65b78e1c689d26ca6aacbe9c0021/1db73/014ToDoDes.png 75w,
/static/6c5a65b78e1c689d26ca6aacbe9c0021/8a4e8/014ToDoDes.png 150w,
/static/6c5a65b78e1c689d26ca6aacbe9c0021/5a46d/014ToDoDes.png 300w,
/static/6c5a65b78e1c689d26ca6aacbe9c0021/e7c18/014ToDoDes.png 408w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
RESTful APIs provide a bridge that enables services to interact with other services in a standardised manner.
Spring frameworks can be used to interact with RESTful API services that can expand the capabilities of an application. Building microservices that provide RESTful endpoints can also enable you to share data between services. &lt;br&gt;
The JSON format of data can be passed into a POJO that can be used to resolve business logic. The JsonDeserializer class can also be extended to provide custom modification and processing of data from the REST endpoint.
&lt;br&gt;
The source code from this blog can be found on GitHub &lt;a target=&quot;_blank&quot; href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/ConsumingRestAPIDemo&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using dependency injection with Spring]]></title><description><![CDATA[Dependency injection is a design pattern used to decouple classes within an application and to delegate the implementation of an application to a framework. Explore how dependency injection can be implemented within class design and by using the Spring framework.]]></description><link>https://aneesh.co.uk/using-dependency-injection-with-spring</link><guid isPermaLink="false">https://aneesh.co.uk/using-dependency-injection-with-spring</guid><pubDate>Mon, 29 Jun 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the importance of dependency injection and why it supports modular code design.&lt;br&gt;
&amp;#8226; Implement dependency injection with three different techniques.&lt;br&gt;
&amp;#8226; Review the benefits and drawbacks to each dependency injection technique.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is dependency injection?&lt;/h4&gt;
&lt;p&gt;
Dependency injection is a technique used to decouple classes that depend upon each other. Class dependency arises when once class uses the functionality (method) of another class &apos;as a service&apos;. 
&lt;br&gt;Classes are deemed to have a &lt;i&gt;hard dependency&lt;/i&gt; if their invocation creates a &apos;new&apos; instance of another class. 
Dependency injection falls under the broader design of &apos;inversion of control&apos;. Inversion of control enforces the fifth principle of SOLID whereby a client&apos;s implementation is transferred from the class itself to a framework. 
In the example below, the client, &lt;code&gt;Student&lt;/code&gt; has a hard dependency on the &lt;code&gt;SchoolService&lt;/code&gt;. The SchoolService is used to return a List of classes the student would have within the learn() method.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            schoolService &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArtSchool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getClasses&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The Student Object will create the ArtSchool at compile time, however a problem may arise if the we want the Student to instead attend the LiteratureSchool. The class will need to change its dependency implementation accordingly:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    schoolService &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LiteratureSchool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 96%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAADA0lEQVQ4y7WU2U9TQRTG++A/ojEqQsFgNBHZSilBE5ElESkEIXFBodu9LEV6WYoxbgioiIgUjCZgjP4FCrSlpbYYH1R48YU3EehCIgaB+Txz2yIgEF58+GXOnJnz3e+0M6NIKOjco9IPt6uN9lCafhTp+lGmMthZhtGONN0oKJahPHiOj3yeQXui+6h2njSaGr1QKCjoyBTGoDI6oW31saIWL07VusFz55q9yK5x4XSdG/nSB2gol2fx4Ez9OLJEl7wvq9rF1JRXm5wgrToFd6YRx5Cmd6xWtH1Cy8AUM/d8YdWPP+Phm29o7p+C5dlXNNomUWT1gtZgfT4FqW+SWWkvr0nVOVY09IEMw+g0F+RuGHeY2+Bh58mhlgpLbvhQenMCheSSuyqQPMghZ3xeTGtaqw/F1FG+5KG2ncgUXUylew8uyHh7UVFyugZ9WR5VBifSCb6ebnBs2ENrVM9xgQTZmiD9BlHhNTSb5tvlw7V/BaPJLYt3Q7hWFsT/FnSCH4FMijWCY/eYHLLGP4JRMT4mm7w4aZrYJT6qcZPgu00O14leFl9AJ/ahSrTtiI64Kg4gW6BbphveKMjtJ9PXasVHWLDEICTFyixEsUSQ4uR5iOKgpMSStB+dtY04pnNvFMwS7EiiFu7VWPHTcggz9QmYrVcSsZg1E9fjw5gPUy6OYiW+m+Ox1LAPQ+ZKJFaNY8M5zCKHSaaPuCU0Y7FNDX9XEYJdhQh05iHQnoPg0zIEu4sReFCAQMdZmbn2XCzfSWIvaypxtMrDthFsweLtFARsVxB8UoKQ7RJC/RUI9pQi2FsehoT56O+9yJbvq0jw2pqgfO0iLbMTcsskaDmAH+Y4zFG7c9SiDI/leQyN8pzNmJXsd8NevDJXssQqj9xyKOJwNfyneJkgdrN5Szz80hHME/6tYbTG5qRE/JIOrtytacVxvXdafg/X3RK5/Qw69VrhLcqFQVwQhlC2I4OsRHhNR40/viN1ivUvtjr8+jJOqtGNFOP4TrAIPKYXe6SpYgKKPzjeuAqJS7jWAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Hard dependency&quot;
        title=&quot;Hard dependency&quot;
        src=&quot;/static/845132dd2bc7a99442bee218970c0a1e/5a46d/013HardDependency.png&quot;
        srcset=&quot;/static/845132dd2bc7a99442bee218970c0a1e/1db73/013HardDependency.png 75w,
/static/845132dd2bc7a99442bee218970c0a1e/8a4e8/013HardDependency.png 150w,
/static/845132dd2bc7a99442bee218970c0a1e/5a46d/013HardDependency.png 300w,
/static/845132dd2bc7a99442bee218970c0a1e/e7c18/013HardDependency.png 408w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Why use dependency injection?&lt;/h4&gt;
&lt;p&gt;
By using dependency injection, the application will delegate the responsibility of creating Objects away from the individual classes, and into a single framework.&lt;br&gt;
The task execution is decoupled from implementation and a further degree of modularity is achieved. &lt;br&gt;
Further benefits can be seen during unit-testing where components can be mocked and passed with dependency injection for more concise and light-weight tests.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Implementing dependency injection&lt;/h4&gt;
&lt;p&gt;
Dependency injection design can be implemented by refactoring the Student class to reference a pre-existing instance of the SchoolService.
&lt;/p&gt;
&lt;p&gt;
Dependency injection can be achieved in one of many way, including, but not limited to:&lt;br&gt;
&amp;#8226; Constructor injection: using the client constructor to take in the service as an argument.&lt;br&gt;
&amp;#8226; Spring constructor injection: using a Spring bean to inject the bean as a dependency.&lt;br&gt;
&amp;#8226; Spring setter injection: using a setter method within a Spring bean to instantiate the dependency.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;u&gt;Constructor dependency injection&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
A constructor parameter can take a dependency as an argument to be used by the class.
Rather than the Student class defining the type of SchoolService it depends upon, the responsibility is delegated to the class that instantiates the Student instance:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolServiceArgument&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;schoolService &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; schoolServiceArgument&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
From the main method, the SchoolService can be passed in as an argument:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; myService &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArtSchool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; myStudent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;myService&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        myStudent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 53.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAChElEQVQoz22SXU/TYBiG90c8M56IKJEowShTRMJA5ENj4pmJB2oiUwhjMLaAoGICGv0BnqKBM5OdGVa30XY4E3FsXTdgk8Fot5byNXEdvX37DglGm1x53qZPr9zvk8dSKpUA7BOMfzFKKBaL0HWdYnb+j6LZTp5YLAbLvmHgW0IB+10Gv5CjcAQ2IkPe+EkbD/qxtxLFbpRBQQigEPPTar7vZZP0e0IUYfnwKY26LgZ3noZwe4hHu4ul5yZHEHdfhLBb2KPNO+RnebAK6suGMuM2KGP1UJ9bIQ3XwlBXkFhKwTLxXkDtQwadbg6tAyxanLNoH+Rgc8ziujOIVWmLCjeDk8g7ThBBHdTRixRl5AKtsvMk9KUQEukMLG+nRZy778PV7gCuPAmg/gCr3U/SziIrl4Vb3DRyvcehuKuguCrLDJ6myAOnoKfCSKSI8M2UiPNEeK3HD2uXj3LZzuDSIx9u9AeOCKeQJ8K8pxqy68xfrDsrUPojNBPWPPChsYdB20AQbSSVmay5L4AO12esHUloXllyV1Nk91nIRJ4jddVZCX05jKQpHCczrLo3g4ZuBh1kjjc9IXR6eDJLFs29PmTWNSrUApPIPT4GiSTKjzVAm7Bh85UN2ngT1voq8GuRR9Kc4Uw4S4bvR6uTQYuDQaPdiyb7R5pw6N08DKO8NMWsCGWiBfIzK9ZHrciO1EHsr8GqpwaZ17dg7G4gnkjCQvdLJ1cqGCiQKqs7mF9IYjmTh6puYHt7C5qmYXOnAFVag/JDhJpZhLQch5Qm+6dvH25qOkXWJhJZgBCLIi6UMc9fw3P4MseD4ziwLEcrx7LgQyHwc+FDItEYooII0xEXBHi9XvwGQZi8riybB7EAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Constructor injection&quot;
        title=&quot;Constructor injection&quot;
        src=&quot;/static/a01162aee01c597f71b1aef9576dbd38/5a46d/013Constructor.png&quot;
        srcset=&quot;/static/a01162aee01c597f71b1aef9576dbd38/1db73/013Constructor.png 75w,
/static/a01162aee01c597f71b1aef9576dbd38/8a4e8/013Constructor.png 150w,
/static/a01162aee01c597f71b1aef9576dbd38/5a46d/013Constructor.png 300w,
/static/a01162aee01c597f71b1aef9576dbd38/fc2a6/013Constructor.png 450w,
/static/a01162aee01c597f71b1aef9576dbd38/0a47e/013Constructor.png 600w,
/static/a01162aee01c597f71b1aef9576dbd38/3fca6/013Constructor.png 1112w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Spring Constructor dependency injection&lt;/u&gt;&lt;/strong&gt;
&lt;p&gt;
The Spring framework offers annotations that can inject dependencies into the class.&lt;br&gt;
Before using Spring for dependency injection, it is important to ensure the annotation-driven injection is enabled by creating a configuration class:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Configuration&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;schoolService&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArtSchool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;		
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;schoolService&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The &lt;strong&gt;@Configuration&lt;/strong&gt; annotation indicates that the class declares bean definitions. The class is therefore processed by the Spring IoC container at runtime to create the beans. 
The Bean &lt;code&gt;schoolService&lt;/code&gt; returns a new instance of &lt;code&gt;ArtSchool&lt;/code&gt;.
In the Student bean, the SchoolService is injected with the bean definition of ArtSchool. The Spring framework is now responsible for the schoolService injection, and not the Student class itself.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;schoolService &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 111.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAYAAADAQbwGAAAACXBIWXMAAAsTAAALEwEAmpwYAAADx0lEQVQ4y42U/29TVRjG91/4A4kxMUQUQUcInZSAMEWiTjGBYeSLmUQYc0DGZK50trA5u41B4gAVFyWGbyNbFAgKZisrtFvbMZ3DQVcTRWhLt+7e2+62K/bLvQ/vOXfrNjYaTvKkp+17Pnnec9/n5sTjcUBV8NhF/6VSKa40fVUeo7Si1eUo6RTksSQuuwK4YPfjokPTBbsPl7r8CMv/Z9hJwYeoqxUxJncbV9R5DrG+X6EktbocYuPYT39jaYkNH1T3YMP+HrxjcOL9Az1YVnoNlpO3eGGKJB4vgmB8CVLDakgH10CyrIJU/xpCn86F3HVmApiC5ZQHejpcUNmNNyu6sWZvFwoIunzXdRi+7dfcUb9i0zqIxoUQD+Rp2r8EUo0eofJnIbcfmwQ2nBmEbocNr5c7kF9m52J75rqq+aYGpIsSj26A8NlzEE2LIH7+MikXknkxRvY8A9l6fCrQgzwG3HMNK3Z2cq3abcOSbR0wfvfnJPBIIQHnIWTMxbBhAVeoKhfB3U8/ChzkbgoqHdhY04tNX/Ric20vte+glvsyLUvMoeEFDFUvg1CXD8GSD7HhDdzfOw+j7V9PPpTGs4N4fnM7lu6wkjsbyYqV5HDR1nZUjTtkD0VoWo+R0qcwVPkib5XfISmwcw4i1uZxoJLCf8EoDrUMktPbOEhu9zVZ8eWP/ag/fRveuxFeyGZtzNuNcGsVxDYzhluM8DaXQThnhHC+DslISAMmEokZsxwWgmyikW2xIfcL8vQMqCpykskEbailtEpSOObuPR/SNPDReIoGO4HRWIIPf0R+gFjsAfebpnMB3z2wDlWqVcfTlnHI6ExsiSNBdNzwo9DkxIe1bq6N1S4UWXqwdp8D5ylFbPl8/sy5Cc0AMqQcHkbNiZvQf3Id60xubGvsw0f1f1B6bvDfKr7p5y59/sA02KwO2c+j0hAaTg9AV9zJn/ZbFXa8a3Dg1V2dPADmEx5+JhC4nwFlBcoErDs5gLxiK94zOinb3XibYrmW9rqPL6PiqBvxWAT/3rkz47qyAl8hYKHZRS8MN326sd7kgm7rRZQdsiEsDkOSwk8IpDusP0Utb7/Kc63f/gsX27OImn7wTBuVqWtWYEQMasDiq1hdbseKkitcbK8j1xMvjLSiPtlDiUZGYPiqA4uLfqa7c2JTLcv27/we2TUYm//S0qOo2R3yzKa1AW1q9WL+livQl3TyfC8vtfH9/C2/wfz9wDSH2VseLwgKYzjS5qVs38qokXS4xYN/AlrkFHVmPGdNiqpmz/HUmkdrHwKFqNPEv+4irQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Bean injection&quot;
        title=&quot;Bean injection&quot;
        src=&quot;/static/0bf28f7acdaab07da45510e5f452a584/5a46d/013Bean.png&quot;
        srcset=&quot;/static/0bf28f7acdaab07da45510e5f452a584/1db73/013Bean.png 75w,
/static/0bf28f7acdaab07da45510e5f452a584/8a4e8/013Bean.png 150w,
/static/0bf28f7acdaab07da45510e5f452a584/5a46d/013Bean.png 300w,
/static/0bf28f7acdaab07da45510e5f452a584/fc2a6/013Bean.png 450w,
/static/0bf28f7acdaab07da45510e5f452a584/0a47e/013Bean.png 600w,
/static/0bf28f7acdaab07da45510e5f452a584/25260/013Bean.png 1113w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;The @Autowired annotation&lt;/strong&gt;
&lt;p&gt;
The &lt;Strong&gt;@Autowired&lt;/Strong&gt; annotation is used in Spring to implicitly inject dependencies into a class. 
It is not necessary to use the &lt;strong&gt;@Autowired&lt;/strong&gt; annotation if the Student class only defines a single constructor. If more than one constructor is defined, &lt;strong&gt;@Autowired&lt;/strong&gt; must be used to instruct the Spring container to inject the dependency into the class.
&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Spring setter dependency injection&lt;/u&gt;&lt;/strong&gt;
&lt;p&gt;
Rather than using the constructor of the Student class to instantiate the schoolService, Spring bean definitions can call a setter method when defining the bean.
In the Configuration class, the Student bean is updated to create a Student instance, to call the setter method, and to return the Student. 
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;	
	&lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; student &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		student&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setSchoolService&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;schoolService&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; student&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The Student class must now define the new no-argument constructor. 
If the Bean for the Student class was not defined, the dependency injection will no longer work with two constructors. Instead, the &lt;strong&gt;@Autowired&lt;/strong&gt; annotation is necessary to ensure the schoolService is injected with the Bean when a Student is instantiated:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Autowired&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SchoolService&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				
		&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;schoolService &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; schoolService&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Choosing a dependency injection technique&lt;/h4&gt;
&lt;p&gt;
The constructor and setter dependency injection achieve the same objective in slightly different ways.
The setter method can be seen as more readable against the constructor injection. Setter injections can also resolve a potential &lt;i&gt;ObjectCurrentlyInCreationException&lt;/i&gt; where a circular dependency between two Objects arise.
&lt;/p&gt;
&lt;p&gt;
The constructor injection, however, ensures that the Object is not created without the dependency injected. It may still be possible to create a Student class with an incomplete dependency if relying upon the setter injection. The constructor is also not liable to being overridden by child classes, while the setter method can be, introducing an implementation security concern.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Dependency injection is technique used to enable Objects and the services they depend upon to be created as separate entities.&lt;br&gt;
This blog has reviewed three of the most common ways of implementing dependency injection, however there are other annotations such as &lt;strong&gt;@Resource&lt;/strong&gt; and &lt;strong&gt;@Inject&lt;/strong&gt; that can achieve similar outcomes and are worth exploring in Spring.
&lt;/p&gt;
&lt;p&gt;
When using test-driven-development, or any kind of unit test, dependency injection can greatly reduce the weight and complexity of unit tests through mocking frameworks.
As well as improving code readability and reducing boilerplate code, dependency injection shifts the implementation of an application away from individual classes, and into a modular framework. &lt;br&gt;
The code sample from this blog can be found on GitHub &lt;a target=&quot;_blank&quot; href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/DependencyInjectionDemo&quot;&gt;here.&lt;/a&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Understanding Floating point precision]]></title><description><![CDATA[Understand the problems that can arise from using floating point primitives such as double and float when handling decimals and the cost-benefit that comes with BigDecimal from the java.math package.]]></description><link>https://aneesh.co.uk/understanding-floating-point-precision</link><guid isPermaLink="false">https://aneesh.co.uk/understanding-floating-point-precision</guid><pubDate>Mon, 22 Jun 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand why floating point precision does not always created expected results.&lt;br&gt;
&amp;#8226; Understand how the float and double primitives are created in the JVM.&lt;br&gt;
&amp;#8226; Use the math.BigDecimal class to resolve floating point precision inaccuracies.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Floating point primitives&lt;/h4&gt;
&lt;p&gt;
The primitive data types &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt; offer floating point values to be used for non-whole numbers. The JVM allocates floating point numbers a fixed size of memory: &lt;code&gt;float&lt;/code&gt; consumes 4 bytes (32 bits), whereas &lt;code&gt;double&lt;/code&gt; consumes 8 bytes (64 bits).&lt;br&gt;
The benefits of using floating point numbers are their speed and efficiency. The JVM is able to very quickly calculate and store the values. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Binary fractions and floating point inaccuracies&lt;/h4&gt;
&lt;p&gt;
The JVM stores floating points values as a binary representation of a fraction and exponent; floating point values are calculated using a Base 2 notation.&lt;br&gt;
In calculus, the value of 0.1 can be calculated as 10&lt;sup&gt;-1&lt;/sup&gt;. Similarly, the value of 0.55 can be calculated as 55 * 10&lt;sup&gt;-2&lt;/sup&gt;.&lt;br&gt;
The JVM calculates floating point numbers to the Base of 2. The Base 2 calculation is optimized within computers as their binary representation of 0&apos;s and 1&apos;s can be efficiently computed. The JVM is able to interpret the values without conversion and therefore enable floating point numbers to realise a benefit of efficiency with computational cost.
&lt;/p&gt;
&lt;p&gt;
The example below demonstrates how the value of 0.01 can be defined in binary code.&lt;br&gt;
The base of 2 is sequentially reduced by 1 from -1. The binary value indicates whether or not the value of Base 2 can increment the accumulative value towards 0.01.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/29cc8ba5cde5eb8862b3de139ab7dfa0/d3f96/012BinaryDec.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 97.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAACh0lEQVQ4y3VUy47TQBDMF3HkP7jxAYjPAHHjAxACceOA2BNCILRCSCwSIOW2IJGQ98t24jjvxPHbhau9PTgLO1HLk3ZPdU13tWvb7RbT6VRss9ngeDyKua4rPu7DMJSn7VhwpjaOwRHr9RqNRgOtVgvNZlOMMbXD4QDbtjEajbDb7eRwFEUCRj/3cRyLf2Z7cO2FSULr9/sYDocSlyQJanRyQ+A0TaGLft/3jS/Pc7QW3/Bjdo40i5FnuZxbLpfClnFZlqFGZGb0PE8CeJCLgbPZ7ATwU/sF3vx6jCgJSl/x2+/3kliXASQA93qYQNUEfO6jFbbBvNhnxsdyaBztRoasYbvdRhiF5vDZ5UM8r99HGPvGRyJspv4XwCAIxElAfaGrylAsy098vAlNfQZwtVoJfS4Wl4zZeTZHD3/4/RRnPx/9reFNDFlUXrHKkPqcz+cmCX1D7zM67jvpsi6toQFU3VE2VYY0BvAp/Sz23z3g4xRIsvJ6cQGkmizjiisTfbFYiDg1ExN0u11RP5lKkgLw7tc5bp87RbfLOL5j41gaPWuuXK1htSnmf2F+AbQP43/eV5cBZBNUxMzGmlqWhWWRSBne+eLi1nsLuyuGHNXBYIDxeGzEXSMrjk6VNoHJmB3kJChL6wgMfNY4N+PJazNGb2eEXdUhXzI7E/GJvByz+vAVLjpPEKellHz/AMdxZEQpvZOmkLZ2igkmk4l8SRzHLgVNwPFbXPRfFoDRFWApN8rLAJKhilsZ/m/plGTXJuX6REkNWS/9pnHxf6/XE+OVkiQVLb6+fIBn9XtmljebtXxgGae1ruk3rdPpmMKSLYvN+lHwWopN4GLpW0XHM9M8/QCrQv4ALJG09WM09H0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Floating points&quot;
        title=&quot;Floating points&quot;
        src=&quot;/static/29cc8ba5cde5eb8862b3de139ab7dfa0/5a46d/012BinaryDec.png&quot;
        srcset=&quot;/static/29cc8ba5cde5eb8862b3de139ab7dfa0/1db73/012BinaryDec.png 75w,
/static/29cc8ba5cde5eb8862b3de139ab7dfa0/8a4e8/012BinaryDec.png 150w,
/static/29cc8ba5cde5eb8862b3de139ab7dfa0/5a46d/012BinaryDec.png 300w,
/static/29cc8ba5cde5eb8862b3de139ab7dfa0/fc2a6/012BinaryDec.png 450w,
/static/29cc8ba5cde5eb8862b3de139ab7dfa0/0a47e/012BinaryDec.png 600w,
/static/29cc8ba5cde5eb8862b3de139ab7dfa0/d3f96/012BinaryDec.png 1093w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The binary code of 0001100 will continue infinitely as 0&apos;s and 1&apos;s are added in pairs. The float and double primitive values will however round the value once their memory limit is reached. Some double values can be represented by a binary fraction within a finite length of the primitive data type and therefore does not require rounding. One example may be 0.75, it would be represented as 2&lt;sup&gt;-1&lt;/sup&gt; + 2&lt;sup&gt;-2&lt;/sup&gt; = 0.5 + 0.25 = 0.75.&lt;br&gt;
As float and double primitives are not able to define the exact value of the decimal, they create unexpected results when applied to operations:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.19&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; d &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.19f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;double answer:  &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;float answer:   &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    double answer:  0.010000000000000009
    float answer:   0.010000005&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Resolving floating point precision with math.BigDecimal&lt;/h4&gt;
&lt;p&gt;
The Java math package introduces the &lt;code&gt;BigDecimal&lt;/code&gt; class to enable control over the scale and precision of decimal numbers. The following example demonstrates how BigDecimal can resolve the precision inaccuracies of floating point values:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt; e &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;0.2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt; f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;0.19&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Big decimal answer: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    Big decimal answer: 0.01&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;How BigDecimal works&lt;/h4&gt;
&lt;p&gt;
With the example of 1.23, scale refers to the number of decimal places with a base of 10 (2), and precision refers to the length of arbitrary numbers (3). BigDecimal uses the unscaled value (123) and the scale (10&lt;sup&gt;-2&lt;/sup&gt;) to create the precision value 1.23&lt;br&gt;
As BigDecimal employs Base 10 calculations for the decimals, it can precisely represent all 2 decimal placed numbers. BigDecimal is typically used within applications that compute with currencies to ensure they are correctly represented. 
&lt;/p&gt;
&lt;strong&gt;Constructors&lt;/strong&gt;
&lt;p&gt;
The BigDecimal constructor can take arguments such as BigInteger, String, int, double, float, and char to convert into a BigDecimal. When supplying a BigInteger, the constructor can also take a scale argument to build the decimal:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; a2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; a3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; a4 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2.4&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;BigInteger&lt;/span&gt; a5 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigInteger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;1234&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BigDecimal (int): &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BigDecimal (char): &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BigDecimal (double): &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BigDecimal (String): &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a4&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BigDecimal (BigInteger): &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a5&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BigDecimal (BigInteger, scale): &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BigDecimal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a5&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;BigDecimal (int):               1
BigDecimal (char):              97
BigDecimal (double):            2.399999999999999911182158029987476766109466552734375
BigDecimal (String):            2.4
BigDecimal (BigInteger):        1234
BigDecimal (BigInteger, scale): 12.34&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The example above illustrates several ways a BigDecimal can be instantiated.&lt;br&gt;
The use of double &lt;code&gt;a3&lt;/code&gt; on line 9 prints the binary value of 2.4 as a 32-bit integer. The String constructor on line 10 however prints the expected value of 2.4. Line 12 and 13 demonstrate how BigInteger can be used with a scale operation to build a decimal number.
&lt;/p&gt;
&lt;strong&gt;Good practices: String constructor&lt;/strong&gt;
&lt;p&gt;
The example above demonstrates the successful use of the String constructor with BigDecimal. The String constructor represents good practice as the BigDecimal no longer uses the binary fractions to create the value, and instead employs Base 10.
&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;Methods&lt;/strong&gt;
&lt;p&gt;
The BigDecimal class does not override operators such as &lt;code&gt;+&lt;/code&gt; or &lt;code&gt;-&lt;/code&gt; used for applying primitive data types to each other. Instead, methods such as &lt;code&gt;add()&lt;/code&gt;, &lt;code&gt;subtract()&lt;/code&gt;, &lt;code&gt;multiply()&lt;/code&gt;, and &lt;code&gt;divide()&lt;/code&gt; are implemented for operating with other BigDecimal instances.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;BigDecimal Performance&lt;/h4&gt;
&lt;p&gt;
The BigDecimal class introduces precision of decimal values by implementing Base 10 calculations. The final value, derived from the unscaled 32-bit value multiplied by 10&lt;sup&gt;-scale&lt;/sup&gt; results in a heavy performance cost to the application.&lt;br&gt;
As a result, performance costs from BigDecimal can rise up to 90 times slower than using &lt;code&gt;double&lt;/code&gt;. The decision to implement BigDecimal should strongly consider the importance of exact values to the context over speed.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The JVM uses binary functions to compute floating-point primitive values &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt;. Their design enables the values to be efficiently calculated and stored.&lt;br&gt;
The Base 2 function for deriving floating point values introduces a degree of precision loss from rounding. 
BigDecimal is a class within the math package of Java and enables decimal values to be computed against a Base 10. As a result, the decimal value created does not contain an unexpected rounded value.&lt;br&gt;
BigDecimal is a useful class implemented with applications that handle currencies. BigDecimal however introduces performance costs that are to be considered with their use.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Exploring logarithm in computer science]]></title><description><![CDATA[Explore the context of logarithms with data structures and understand why a base logarithm of 2 is implicit with Computer Science algorithms.]]></description><link>https://aneesh.co.uk/exploring-logarithm-in-computer-science</link><guid isPermaLink="false">https://aneesh.co.uk/exploring-logarithm-in-computer-science</guid><pubDate>Mon, 15 Jun 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand why logarithms are used to calculate the time complexity for certain operations and algorithms.&lt;br&gt;
&amp;#8226; Explore how binary trees are designed and used for searching.&lt;br&gt;
&amp;#8226; Understand how sorting algorithms may use binary trees.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Logarithms in computer science&lt;/h4&gt;
&lt;p&gt;
This post will explore why logarithms might appear within our calculation of time complexity. The post will then begin to explore the context for why the base of a logarithm is implied as 2 within software data structures. If you are unfamiliar with time complexities, please visit my blog where I review their importance to algorithms &lt;a target=&quot;_blank&quot; href=&quot;https://aneesh.co.uk/calculating-the-time-complexity-of-algorithms&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Logarithms apply to two different rates of growth within time complexity:&lt;br&gt;
&amp;#8226; &lt;strong&gt;log (n)&lt;/strong&gt; &lt;br&gt;
&amp;#8226; &lt;strong&gt;n log (n)&lt;/strong&gt;&lt;br&gt;
&lt;p&gt;While there is no distinguished pattern for when &lt;strong&gt;log (n)&lt;/strong&gt; and &lt;strong&gt;n log (n)&lt;/strong&gt; appear within data structures and operations, &lt;strong&gt;log (n)&lt;/strong&gt; most frequently appears when using tree data structures and &lt;strong&gt;n log (n)&lt;/strong&gt; most frequently appears when using sorting algorithms. The Big O cheat sheet, &lt;a target=&quot;blank&quot; href=&quot;https://www.bigocheatsheet.com/&quot;&gt;here&lt;/a&gt;, provides a breakdown for each occurrence.&lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Within calculus, logarithms are introduced as a means to inverse a function of exponentiation. 3 to the power of 4, also known as 3 exponent 4, is equal to 81. Consequently, logarithm 81 of base 3 is equal to 4. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACEUlEQVQ4y62T3U8aQRTF+dd9FpM+VNP43L7Wxtj4QcEoCigoYrVWBbQqK6z7NbPfgPw6s7ZEm6Yv9SQnd+Zm58y5d+/kHMfhOR4fHwnDkOF4RCg9IqNN5PSJophI5UfjMelwRJKmKhfxJ3KWZU2FNHwZ0OrWWK0uc9T6RLu+iX1eZzRMuR4IPhb3KdWOKdQvMAZWlteYTCYZp4J681uw53ZZ39jkpLnC9eEmncoatWoFw01YeLdIc2+HUvUQywtfCP7VYSBDTm/q7ByV+NopMxEGiWcipeDWCtmoHHN2dc9B28QwbSU4/LdgkqQMrHv6dg/TMXGFjyd9JSixHVflBcaDipaL67rq3Hha7gvB18CLHurbhBAZgyAgiqMsSuXQdQW9ga1ceRldTyA9F8/sIUwD23pQ34ZPDm3bzsr1PC9jp9Ph7u6OZrNJ96pLOvLpi5D598t8Lmyx9GWPWycmMS74vrvG4GSXqH+VtWYqOFazpd0kSUKxWKRcLjP/doHSToHt1gp7ly3eLH6gWqtRaZ6xfXCmxFZpbK1TWVtSo9HH85851LXrAU/VsPq+TxzHtC87HJ0eUNhfonHeoDsIsjls3wtsGZEIC//mG/6PE6TVR8hfDk3TzBb6deiS9d/U1PtQ9cVTryXwJVGgcsFT9NUIaQEZxopJttZmdOty+uBrIjczM8Pc3Byzs7P/zXw+z0/m6r37kKzo4gAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Logarithm example&quot;
        title=&quot;Logarithm example&quot;
        src=&quot;/static/921510f2d666988aff681a49382b5714/5a46d/011LogExample.png&quot;
        srcset=&quot;/static/921510f2d666988aff681a49382b5714/1db73/011LogExample.png 75w,
/static/921510f2d666988aff681a49382b5714/8a4e8/011LogExample.png 150w,
/static/921510f2d666988aff681a49382b5714/5a46d/011LogExample.png 300w,
/static/921510f2d666988aff681a49382b5714/62452/011LogExample.png 331w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Within computer science, logarithms are used to understand the depth of a binary tree. The value, n, is the number of elements within the tree, and the base, b, is set to 2. By setting the base to 2, the logarithm of n will calculate the exponent of 2 to reach n. In other words, we are asking the logarithm how many times the value n can be divided by 2 before it is equal to 2. The value we obtain is rounded up to the nearest integer.&lt;br&gt;
But why do we care about the depth of a binary tree?
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Searching within a binary tree&lt;/h4&gt;
&lt;p&gt;
Binary trees split data collections into pairs of nodes from the root node. Each node will be further split into 2 until all elements from the collection are included in the tree. The diagram below illustrates a balanced binary tree for the sorted array of: [0, 4, 6, 12, 7, 5, 9, 3]. The value of each node is identified by finding the middle value of the collection from the node above it and the start or end. Nodes that are greater than the node above it are sent to the right, and nodes that are less are sent to the left:&lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 89.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAADZElEQVQ4y3VTW28TRxT2P6IvPPBWiUogKKiBlooKpVUeQPAQQESlENKtokBQaRUBgoIoiLsIEEFArQqiwQTfEseB2DGO4wQDjm+73s3axvbGuzsfZ2YdyIWMdPbsuX1nzmVcs7M6bGsOsIlQJ26gWq1AL5ahqEXkFB1Z2aF8oYiCVkKp/B51o0r+TgyzDMIw8TqZhAuwkFUNPA7mcLpvCh0Xo2g9OYaW4yFs7xzGd9IQmo4EsLUjgO9/G0bz0SB2nhhF29kIuq7GcO3RWwRjKmyC1jUFrq7LYazZ7caaPW781D2CTb/4sIWCv/11CD8Q4ELOwTkwT/Q1+f14bATr2jxYvXMAmw4H8GAgClff0yR2/TGKjQe92NEVxDftfkHcccNBH+mdBJsP+7F2v4f0Xmw+5CObF80EyIG3HPGh80ocgRfTvGR+WSCtVOCNKOhzp3Du/hRO3k2g5/YkOi9Hceh8GNKlcfx5Ky6opzeOK/8l8Y8/g/HXOmZLhsDQVBmuWs0Rlh61WMOZewl0/D2OY9cm0H4hglN3J5FTq1jpZLNZuObm5oRg2wyWxWDULSGH4rNo6R6l0gbR3O2hkqlnR0MIRAvCXjdtWBQj4mynylwu9wmQMbYkn4U60yCXskirOWT1NOlqH60L/e3PAfJTqZkITWjo98XxaCQGrVxelOKNnMZgZIrKrn0E5bQM0G4k6x14i1Ut/2ObNIwvWp7iOu0YP6Zl82jspf38stWDJiq/oDug9ucAG0XgfdWGbzyHGwNBeCJZlCuW0NuN8oZjBfze68e/gRQlccDYyoDOqUOhrwFT8KVdVelb4guyqJdsIaBhOID8NneexTA54wBNpGSSJ+jWFnjF/d4phBIpCgYe+hO47Z7Eu3x1+VCqjT10v8jjq/3Paamnhcz52n3P4QnLmE6XsJ5eTk9vAr6IjHUH/GhqD+Dnv8aWA9INeYMYTZhRMMtrNSFzPjgms6phUUWMBaIKm5ErwjYUn2bulymWVw0h27YYKyNAxnto8kEuJOrLMt083XzyxjzbH17kO+9PgCY9vZporEWNMml0nPMS5mXxT8RXp26aOH79FdrOhBtr48RZlvO6MpkMXPSRVFWVFEWRCoXCiiSTvaRrUvJdRnqVSEnloiZ03MZjdV2XYrGY9AG2EfZzbThWWAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Logarithm example&quot;
        title=&quot;Logarithm example&quot;
        src=&quot;/static/fa62d6355bebd113ec3dd65b38ebefc0/5a46d/011BinaryTree.png&quot;
        srcset=&quot;/static/fa62d6355bebd113ec3dd65b38ebefc0/1db73/011BinaryTree.png 75w,
/static/fa62d6355bebd113ec3dd65b38ebefc0/8a4e8/011BinaryTree.png 150w,
/static/fa62d6355bebd113ec3dd65b38ebefc0/5a46d/011BinaryTree.png 300w,
/static/fa62d6355bebd113ec3dd65b38ebefc0/fc2a6/011BinaryTree.png 450w,
/static/fa62d6355bebd113ec3dd65b38ebefc0/52211/011BinaryTree.png 525w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Calculating the Big O of a binary tree search&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
If we were to search for the value of 6 within the tree, we would traverse a total of 2 levels. Level 0 is the value 5. As 7 is greater than 5, we search down the right side path. The 1st level shows the value 7. As 6 is less than 7, we now look down the left side of the node. Finally, we find the value 6 on the second level.
&lt;/p&gt;
&lt;p&gt;
When using a binary tree to find a value, we are able to identify the maximum number of operations required to find the value once we know the number of elements, n.&lt;br&gt;
In the example above, where n is equal to 8, the maximum number of moves (or levels) that need to be traversed is 3. The Big O of 3 can also be calculated with log (8) &lt;i&gt;with an implied base of 2&lt;/i&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Merge Sort: n log (n)&lt;/h4&gt;
&lt;p&gt;
Merge sort and quicksort are a popular sorting algorithm that use the divide and conquer strategy.
Merge sort performs the following actions to sort the array: [12, 7, 3, 9, 11, 10, 4, 2]:&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Step 1 (Divide): The array is split into sub arrays until they are individual elements.&lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACmklEQVQoz2VSW08TYRDt/zImQqI8YHzzH6hPKKioqIk+maho1GBC1FgSkRhBxMglWBRotwvUXqDl0oKla9l2t9vtjaXd7qW7x28vNCZMspnJ7Ddn5pwZjyzL0HUdmqa5XgdfqiOSFDFDsxjzMfDOpOGd3bfj2ZUcYikRQrlhvz+utYxlWXishGWGaTtUJBWXnsbQ0RdAZx+FUz1+ElM4fTVg+87rQZy5RqHn5TrqTafWNJ3iQqEAzzH6/1Y+khBPi1gICxhfZPHRl7W/8SUWSzEBiUwRstJqgxmGYcc8z8Ojqg6g1S2VlRDcLCCUzKMqnWxkmaIZyPAl/MnVUKgobtY8CfhkLIWOXgpnyNfRG0T3bRr0pmj/03RngsFPu+i+Q+PsDRpd/TQu3F3FvbebZFqHOscRQN2lbCXDKQGT1B7mQgz2uaP2VIYr8Np2GY9J496hCHpe/cbAmzimaa79zgZsyAp2GAkr23nkShV3QSqSBwLS+SPoLWc6rtREMltDguEhVA+h6E2SbaIkVUm+QiQzyVIIYJaXcGs4gYsPVzH0JW0Xb5MGlwcj6BtaR5HoZFG+/24L525SOE8oj8z9dTQjTa6Qd139ATzwpsDm8vC0yNk0FAVZkYezfYderlyEeFhri16VdMQzeaQORMhNw85ZUmQ4ix2DYk1zlmK2LA1ltMw68aqrl46WUSclVs5w78ywKQINF8xwm6ukVrLjggUY3athIsDhQFThixbteaitCriqho0MuUfGWc4UEX83L4MRmpiPCM72CaafXIIFG9ypIEfOyfP5+zJej/7Cj8U1jEwsI7WTwMj4MnxLIXyY9GNsKoBoLIaB5zMY/erH5GwQj4bnEY9vILASxgvvAkLhKJ69/4lv0/P4B6yFV5vFqCo4AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Merge sort step 0&quot;
        title=&quot;Merge sort step 0&quot;
        src=&quot;/static/9f79c3d0ac08368509ba2cfcadeae140/5a46d/011MergeSort0.png&quot;
        srcset=&quot;/static/9f79c3d0ac08368509ba2cfcadeae140/1db73/011MergeSort0.png 75w,
/static/9f79c3d0ac08368509ba2cfcadeae140/8a4e8/011MergeSort0.png 150w,
/static/9f79c3d0ac08368509ba2cfcadeae140/5a46d/011MergeSort0.png 300w,
/static/9f79c3d0ac08368509ba2cfcadeae140/fc2a6/011MergeSort0.png 450w,
/static/9f79c3d0ac08368509ba2cfcadeae140/0a47e/011MergeSort0.png 600w,
/static/9f79c3d0ac08368509ba2cfcadeae140/d30ee/011MergeSort0.png 980w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
Step 2 (Conquer): Each sub array is sequentially sorted and returned to the array above it.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;First the elements are sorted into pairs:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 58.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACfElEQVQoz22SyU8TcRTH+0fpwQRvJl68eTEeDGK4YWLiiShEg4ILhMUoSkIaREICNICBlEiQRZDYAg6lpWVpijTQcSi0HbpMZ/s4nSmQqi95mfze9/t98zaXoihomnbuquVg4piOrlsxVbXd0EuYUcYMK6ZVaEvuMowzwoVNfE/Q0Bui/kOQB2/WqesQuN+5zsO3Aep7gjT1hRF20zbXNCu1Ll3Xy4CJbjhowCIPz8bpnYzR5dmlY3iHzpEd3o1GcXv3GPt2QFzKOT3opq098/OEZ1aq2Pz7t/8xXTcqyjvTuLRyQs2aR9tgmJoWP7ebfFQ3+7hr+Yw/cS6a9h1S+9Jvc6pbVrj3YoWu4S308tz/qVDY1xn3pRiYjjO0mMQrZDmUL6rat8b22S8zMCfRZ7U+tCCyuqdVVOnSSy2qCkrUD1szIIWcLUubsPMVMy7YY1CTcYztBYjOwVHEyVCQ4KeH3MoYei7jtFzacVGM8rv1BonGy0jd1eTFGNL7GhKPLyG130TNHJGeaEN8WoX45IqNqcUip+vTiC3XkCytEg+Vt6w5JR8loyT2VzCKefstywnisSWKuZPyEjTEA4Hk4QamppYv0dIdx0im487lllounaFcyJAzimQNlYKh2cSTQoqi9U0VnHuTs8fkLVxWs+SVrB1LZUQUi5/XCqRlqbzlgzDp1usowiSn/XWcjjwiN/maTM8dlNVxUhZWDM+T6b5FfqqdrKcR2V2Lfhgh1XwVLTiNKkxw8rwKjn/hCq8tE3A3El76wsZwJ8HxHoLefjYGX7G5OMW6u4GIb97COgjNeAh5PxEc7WZ7Y41A/zMiP2aJLM8Q+NjEdlDgDyEEXj7ps+L9AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Merge sort step 1&quot;
        title=&quot;Merge sort step 1&quot;
        src=&quot;/static/5e0aa0b60ca4684b8ff3e480dd8e6675/5a46d/011MergeSort1.png&quot;
        srcset=&quot;/static/5e0aa0b60ca4684b8ff3e480dd8e6675/1db73/011MergeSort1.png 75w,
/static/5e0aa0b60ca4684b8ff3e480dd8e6675/8a4e8/011MergeSort1.png 150w,
/static/5e0aa0b60ca4684b8ff3e480dd8e6675/5a46d/011MergeSort1.png 300w,
/static/5e0aa0b60ca4684b8ff3e480dd8e6675/fc2a6/011MergeSort1.png 450w,
/static/5e0aa0b60ca4684b8ff3e480dd8e6675/0a47e/011MergeSort1.png 600w,
/static/5e0aa0b60ca4684b8ff3e480dd8e6675/712f7/011MergeSort1.png 1181w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The pairs are sorted into collections of 4:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 58.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACcUlEQVQoz3WSyU9TcRDH+1d58CBHl4NL4pJ4woMXiIkn4wE0GEMMe8FEFiNCMBqhYZU1xrpAowilC7RpMWwt1q609LVv//j62j7k4CSTmfnNfOc3v+9vbKIooiiKpbKhoFMW1TiTkaSyqmZOq+Q0ZFk+hS2pTdOqBScyvRylYcDPg14f9+we6jrc1HducL/Hy8M+P09eb+Hdzpi1un4aa1NVtZLQ0bWy7/6V473zkMHZHZ47wnSNhrGPhemd2GZobofxr7/Zj4tlnIExsRW1GlbFjDXB8CT+L6WaIuo/0+mVUY2GSrlElsg4mkh3XCbZcZVE+xXihj3eWDAZLRGTW5slYb9Bsus6KcOme26RmXqGVupxMqFi3SJGg8RWhoktdpFzvaO46UTNZ6y8kksibMyRdPYTm20h820YKRI8NaVN1XQ0uYgY/k7SM0M24jMfm953m2fSvs/gyfjRxB5iyMVxaJl0xGNOnErvIqxPI6yOowrZ8pNLCekwRLz1EonHZ0j11iL92SXZd8eME903kXNpjmbaSTytIdF0ltTAXTRjRQreJRItF0h2XkM82KxwqFQ4LORQ0lE0qVDeMqmInIqgiYK1Hko2jpKJGc3kk+8RC1as6xo2RdXMha0Sb6oRVH+wREnJVSTRylfr5WLBwsli0bQ25cBHru08onuG/HA9gqMR4UMLuYFaxJ8TZNsuIm05OX5xG2G+k7zjEfmhOtRogGxzDYp/CXl9kqPmcwape9gCay48rxoIrCziG7Xjn3qJf24E79tWAssLeAYbCa5+wTfWzdancTbn3+Cf7Cfkc+MdaSb44zNB10fTD/k9/AUYZWHLLgNKMAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Merge sort step 2&quot;
        title=&quot;Merge sort step 2&quot;
        src=&quot;/static/36cda3c380dd5216ce53a43c928e60fb/5a46d/011MergeSort2.png&quot;
        srcset=&quot;/static/36cda3c380dd5216ce53a43c928e60fb/1db73/011MergeSort2.png 75w,
/static/36cda3c380dd5216ce53a43c928e60fb/8a4e8/011MergeSort2.png 150w,
/static/36cda3c380dd5216ce53a43c928e60fb/5a46d/011MergeSort2.png 300w,
/static/36cda3c380dd5216ce53a43c928e60fb/fc2a6/011MergeSort2.png 450w,
/static/36cda3c380dd5216ce53a43c928e60fb/0a47e/011MergeSort2.png 600w,
/static/36cda3c380dd5216ce53a43c928e60fb/c83ae/011MergeSort2.png 1180w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And finally the two collections are returned to the final array at the top:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 58.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACbUlEQVQoz11SXUsUURie/xREF1F3dVEUCV0VFV7URRB1F4R9SRliagTiB2GoUbHpWm1ooomU+ZGsmuO6mSgq2rqz68zuzuycOR/zdDwzO6iHeZhzzvM+77zP+45GCAFjbB8oOKBAJTwu4FEWgPvqrsIzSg9pGTQhBA4v+1c/zJ47sLpuwWqvhtl2FVZHNaxX12H13Ib57i7c1WQQ7PsHtBrnPLz34YtgT9ZmUfreDWekDTu9NTDjtcjFHyH/6Snsbx2wJ96DGuuBTrBAGyJKWFlC+JAP/NCyCabumdpzZXWPE1In9hXnh5VGCQX1YPbVIt94XmFXIvP8DIrJRCSyZvqx0yS55gvIN1ch//IizP46CEbVZ8IKWSQgW3/gJD/DHuuEPRWDuzgKXtqNeF7Mozw3CHv8DUrDrco62Vw8UKXG5VB8SuD+nUQ5NQbv30pgd2cN7vIkvE1d9ZblNuT5J9yVadBs0D/ulmQBX+DIIXKnEFjem7GXWUG24SyMB0eRa7kMN7OKXOs1ZGuOwGiqAi0YsBINyD4+DuPhMRiS4x6BM/8VxrNTyDWeA9nQwx6ywDIvF8F2tyG8ctBT4oDmNmUVdmSHWZkgRvY7aoPkuXQYxAhoTP64e0nV5CqQBx5OUNKKYzJJha/EU1KOdJS46q2xrRSKDadBZuUwum7Cjt2Dk6hHsf0KyEwcBcl5qVGUWi/BGWiC3Xcf9usb4NtpFOpOgupDoHMJWE9OAPl1aEvJScx31mBpfAgLH15A/9gOfaAHv9/WY+nHoOLS02NYiDUjNdKLxYFu6PEWLC8kMd9Vi/TUKNITw2q/rM/hP7o2Y6GmmQlfAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Merge sort step 3&quot;
        title=&quot;Merge sort step 3&quot;
        src=&quot;/static/9916bbd315eab3d505790ea3b15cbb46/5a46d/011MergeSort3.png&quot;
        srcset=&quot;/static/9916bbd315eab3d505790ea3b15cbb46/1db73/011MergeSort3.png 75w,
/static/9916bbd315eab3d505790ea3b15cbb46/8a4e8/011MergeSort3.png 150w,
/static/9916bbd315eab3d505790ea3b15cbb46/5a46d/011MergeSort3.png 300w,
/static/9916bbd315eab3d505790ea3b15cbb46/fc2a6/011MergeSort3.png 450w,
/static/9916bbd315eab3d505790ea3b15cbb46/0a47e/011MergeSort3.png 600w,
/static/9916bbd315eab3d505790ea3b15cbb46/712f7/011MergeSort3.png 1181w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Calculating the Big O of a merge sort algorithm&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
The number of operations required to sort a binary tree is defined within step 2 of the merge sort.
the total operations is equal to the number of elements that are sorted at each level multiplied by the number of levels within the tree.&lt;br&gt;
The diagram below illustrates the number of operations performed to sort the sub arrays at each level. Each sub-array except [3, 9] is re-sorted, resulting in a total of 22 operations to be performed:
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAADcklEQVQ4y12TXUxbZRjHufXWS2NisouZeLGrGTXELDEmOi/MTLaLbYofm4n7SJxRs00DW0ZUtmW6ishQqQqbTrNlgMBoYWPgYINiJ9QWSltKB6Vfpz2lX6f09Jyf7zmFBn2TN895nuf//s/zf97nrVFVleoulSgDqtjFXAYlFUVJLKHEQhUrxygqBTOvarqJN86VhNV1Ha/XSw0bS9dMU1xyI39/APnrPUhNL5I4/SzSmVqkxudJndtJSsTlKx+hriY2DppkxgoEAhVCI6CXVTNYTseRbRZWOo4gt79HsmUfcuvrJC/VEWmrI/37p+RGfqacTxsHRR1aldDv92+q0CArrVFSssQKEtn1mFF3uYI3YynD35TTTHHafwnLhSypX48LmbuJnXuJjGU3tB+lJORvyNLWCuSGrcR/OkTyhwOkO4+h9V0EOY62jqoSaqUi+YlrSNdPkb16gjsdB3mldQfhsGedTvxUySHf+Ey0YC9xy2so375Bw5e1NDvaqwrXCfVqIKEXTDn/ZEI0hezkN1qhlauYpK6QKykUS3k6opMMpP3/JxSUawpycJJc1FtNqvEAxeVZ8VG5LE1g8g9nSC3cr8aUuCCLBERy06WUhVyp7W3S9dvxiTGxz9nJhqaJNe5APvUMXuu7+DNRZOthUqefY1mM0dS1TygmV5DOvky44Wkct1rMPgZ8vkqFCdcAGdtFem9/xbahD5FXY0hjlykMtVDff5Kddz9HW5whM9xG981Gtva8QzKbpDTVxWhPA1tu1LGQj7EcDFFjMCcFf1FYSdiUXumXmDJyoqNBLUdYLZgVGCOzLHxXMVnFSOLdOLNL5k0EDMnyxG8og60iEyZpt0DYRzHiI3HvF7T8KvJ0H7rws+5h1L/+YG3+HjwYoBxysWpvRg/Pkx35UbBNEVh8SE2qeS+3zmzH1Xue4PuP03/1A1asR4kfewLJdonYyW1ELuwiatlD9xe1OC+8iu38C8x+sx/nia38feVjlo4/xd3v9uMJLIgeaipPDh7mrL+LscU/ecT2JqGYmD9zBjXRuymhLUw2E+Ex+0GaZjrZMnSIy8FBdo3Wc8RtxbE4xqP2txhwj1Mz5/XRPXGbUed9HnhmuT5uwzU7h8cXxC18w3q8ftxz89x0jDDpmjbxDmG7Ju/QL7bT46FzvI+evl7+BTtBEVJ5ocSnAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Merge maximum operations&quot;
        title=&quot;Merge maximum operations&quot;
        src=&quot;/static/2b42b1ee89f35aed051527affd87b5c0/5a46d/011MergeSortOps.png&quot;
        srcset=&quot;/static/2b42b1ee89f35aed051527affd87b5c0/1db73/011MergeSortOps.png 75w,
/static/2b42b1ee89f35aed051527affd87b5c0/8a4e8/011MergeSortOps.png 150w,
/static/2b42b1ee89f35aed051527affd87b5c0/5a46d/011MergeSortOps.png 300w,
/static/2b42b1ee89f35aed051527affd87b5c0/fc2a6/011MergeSortOps.png 450w,
/static/2b42b1ee89f35aed051527affd87b5c0/0a47e/011MergeSortOps.png 600w,
/static/2b42b1ee89f35aed051527affd87b5c0/c6ff8/011MergeSortOps.png 1088w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
If sub-array [3, 9] required sorting from the 3rd level, a total of 24 operations would be performed. The Big O for the algorithm is therefore equal to 24, but how can the Big O be calculated?&lt;br&gt;
First, we can see that the maximum number of sorting operations that are performed in the tree is 8 at each level. A total of 8 sorting steps are taken at levels 1 and 2, and only 6 at the third level due to [3, 9] remaining sorted.&lt;br&gt; 
From the earlier search example, we know that the number of levels in a binary tree is equal to log (n). Therefore the total number of operations = 8 * log (8), also more generally defined as:&lt;br&gt;
&lt;strong&gt;n log (n)&lt;/strong&gt;. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Logarithms are used within data structures to understand the time complexity for binary tree operations and sorting algorithms. By implying a base of 2, we are able to understand how many times a value (number of elements) can be divided by 2, and therefore the depth of its binary tree.&lt;br&gt;
Popular sorting algorithms such as QuickSort and Merge sort implement divide and conquer strategies that split the data into a logarithmic number of levels before implementing the sorting phase.&lt;br&gt;
By understanding how logarithms are used within data structures and algorithms, we are better positioned to understand how time complexity can accrue and where it can be optimized.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Collecting a map in the Stream API]]></title><description><![CDATA[Use the Stream API from Java 8 to navigate and transform a Map collection.]]></description><link>https://aneesh.co.uk/collecting-a-map-in-the-stream-api</link><guid isPermaLink="false">https://aneesh.co.uk/collecting-a-map-in-the-stream-api</guid><pubDate>Mon, 08 Jun 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Understand the advantages of using the Stream API.&lt;br&gt;
&amp;#8226; Transform a Map into another Map using a stream.&lt;br&gt;
&amp;#8226; Explore the 3 overloaded &lt;code language=&quot;java&quot;&gt;toMap()&lt;/code&gt; methods of the Collector class.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to the Stream API&lt;/h4&gt;
&lt;p&gt;
The Stream API creates a pipeline to process and transform data in bulk operations. Stream introduces functional programming capabilities to Java where data is collectively resolved. 
&lt;/p&gt;
&lt;p&gt;
Stream operations are categorised as either &lt;strong&gt;terminal&lt;/strong&gt; or &lt;strong&gt;intermediate&lt;/strong&gt;:
&lt;p&gt;• &lt;strong&gt;Intermediate operations&lt;/strong&gt; will listen to elements being passed within the stream and will perform a function on the data. Together, intermediate operations form a pipeline for which the data is processed.&lt;br&gt;
• &lt;strong&gt;Terminal operations&lt;/strong&gt; will invoke the intermediate operations of a stream to return a single result.&lt;br&gt;
All streams &lt;u&gt;must&lt;/u&gt; contain a single terminal operation and zero or more intermediate operations.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Stream API features&lt;/h4&gt;
&lt;p&gt;
The Stream API introduces 4 features for processing data in bulk operations:&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Short circuiting&lt;/strong&gt; allows an infinite size data source be processed within finite boundaries. The following methods demonstrate short circuiting behaviour:&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;limit(n)&lt;/code&gt; is an intermediate operation to define the number (n) of elements to process.&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;findFirst&lt;/code&gt; and &lt;code language=&quot;java&quot;&gt;matchAny&lt;/code&gt; operations are terminal and return the value once a condition is satisfied.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Map Reduce&lt;/strong&gt; enables a data source can be transformed and aggregated within the stream:&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;map()&lt;/code&gt; is an intermediate operation to transform the data into a new stream.&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;filter()&lt;/code&gt; is an intermediate operation to remove elements after passing them through a boolean condition.&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;reduce()&lt;/code&gt; is a terminal operation to evaluate all elements of the stream into a single element.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Parallel operations&lt;/strong&gt; can be obtained through the Stream API without the complexity of defining threads and operations:&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;parallel()&lt;/code&gt; is an intermediate operation to utilise the multiple cores that are available.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Lazy evaluation&lt;/strong&gt; describes each stream as intermediate operations are only used when a terminal operation is called. The stream pipeline will accumulate further streams from intermediate operations and traverse through them only if a terminal operation is called.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;&lt;strong&gt;Obtaining elements from a Map&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;
Each Map instance crated has three key collections that can be derived from the key-value pairs:&lt;br&gt;
&amp;#8226; entrySet() returns the key-value pairs as a Set of Map.Entry casted to the key and value types.&lt;br&gt;
&amp;#8226; keySet() returns the keys in a Set casted to the key type.&lt;br&gt;
&amp;#8226; values() returns the values as a Collection casted to the value type.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;&lt;strong&gt;Transforming a Map&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;
The &lt;code language=&quot;java&quot;&gt;Collectors.toMap()&lt;/code&gt; method is a terminal operation that will pass the stream into a Map. The &lt;code language=&quot;java&quot;&gt;toMap()&lt;/code&gt; method is overloaded three ways to support different aggregations of the Map. To demonstrate how the &lt;code language=&quot;java&quot;&gt;toMap()&lt;/code&gt; method can be used, the below Map of United States Presidents and their age at inauguration will be used:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; presidentMap &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Washington&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Adams&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;61&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Jefferson&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Madison&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Monroe&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;58&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Q. Adams&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Jackson&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;61&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Van Buren&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Harrison&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        presidentMap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Tyler&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code language=&quot;java&quot;&gt;&lt;strong&gt;toMap(Function key, Function value)&lt;/strong&gt;&lt;/code&gt; takes two arguments that will transform the Collection into a new Map. Functions are used to determine the key and values to be added. A lambda expression can be used to obtain the entry set from the Map: &lt;code language=&quot;java&quot;&gt;entryInstance -&gt; entryInstance.getKey()&lt;/code&gt; will obtain the key from each entry. Similarly, &lt;code language=&quot;java&quot;&gt;entryInstance -&gt; entryInstance.getValue()&lt;/code&gt; can be used to obtain the entry value. It is possible to pass the entryInstance into a method that accepts and returns an int to perform business logic. In the below example, a function is used to double the value and assign it into the Map:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; collectedByKey &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; presidentMap
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;entrySet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;entryInstance &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; entryInstance&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entryInstance &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; entryInstance&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    {
        Madison=114,
        Harrison=136,
        Adams=122,
        Jefferson=114,
        Monroe=116
        Q. Adams=114,
        Van Buren=108,
        Jackson=122,
        Tyler=102,
        Washington=114
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;code language=&quot;java&quot;&gt;&lt;strong&gt;toMap(Function key, Function value, BinaryOperator merge)&lt;/strong&gt;&lt;/code&gt; takes a third argument to merge the values of the Map into a new value. The &lt;code language=&quot;java&quot;&gt;BinaryOperator&lt;/code&gt; functional interface will take two arguments of the same type and return the same type back. In the below example, a new Map is returned where the age (Integer) is the key and the president (String) is the value. As more than one president may map to each key, the BinaryOperator will concatenate the presidents as a value. 
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; accumulateValues &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; presidentMap
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;entrySet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;entry &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; entry&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entry &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; entry&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;presidentOne&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; presidentTwo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; presidentOne &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; presidentTwo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    {
        51=Tyler,
        68=Harrison,
        54=Van Buren,
        57=Madison, Jefferson, Q. Adams, Washington,
        58=Monroe,
        61=Adams, Jackson
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
&lt;code language=&quot;java&quot;&gt;&lt;strong&gt;toMap(Function key, Function value, BinaryOperator merge, Supplier mapSupplier)&lt;/strong&gt;&lt;/code&gt; takes a fourth argument, a Supplier, to create a new Map. In this instance, the Supplier creates a LinkedHashMap to maintain the values after they are sorted on line 4. Other implementations of the Map Interface can be used such as a HashTable to ensure concurrent access. In the below example, the presidents are sorted based upon their values and placed into a new LinkedHashMap to maintain the insertion order of each entry.
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; sortedByValue2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; presidentMap
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;entrySet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;comparingByValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Entry&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Entry&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;LinkedHashMap&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    {
        Tyler=51,
        Van Buren=54,
        Madison=57,
        Jefferson=57,
        Q. Adams=57,
        Washington=57,
        Monroe=58,
        Adams=61,
        Jackson=61,
        Harrison=68
    }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The Stream API introduces a new paradigm of functional programming that Java did not have prior to Java 8. The use of intermediate operations allow data to be aggregated and transformed before being collected into a new data structure. &lt;br&gt;
The &lt;code language=&quot;java&quot;&gt;Collectors.toMap()&lt;/code&gt; method can been overloaded with 3 implementations to provide different levels of Map transformations.
The source code from the example in this blog can be found on GitHub &lt;a target=&quot;_blank&quot; href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/StreamMap&quot;&gt;here.&lt;/a&gt;
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Exploring the String Class]]></title><description><![CDATA[Understand how the JVM manages String Objects in memory and at runtime. Review the functionality of the StringBuffer and StringBuilder class.]]></description><link>https://aneesh.co.uk/exploring-the-string-class</link><guid isPermaLink="false">https://aneesh.co.uk/exploring-the-string-class</guid><pubDate>Mon, 01 Jun 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; String literals are stored within a dedicated area of the heap area.&lt;br&gt;
&amp;#8226; String instantiation can determine how it can be equated to other Strings.&lt;br&gt;
&amp;#8226; The StringBuffer and StringBuilder classes offer methods that allow a sequence of characters to be handled as a mutable Object.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Introduction to java.lang.String&lt;/h4&gt;
&lt;p&gt;
The String class is used in Java to create an immutable sequence of unicode characters; each String Object is immutable and final, and therefore thread-safe. The following code sample demonstrates how a String can be instantiated with the &lt;code language=&quot;java&quot;&gt;new&lt;/code&gt; operator or as a literal:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringOne &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The memory allocation of String Objects is determined by how they are instantiated. The &lt;code language=&quot;java&quot;&gt;new&lt;/code&gt; operator will assign the String into the heap memory, however, a literal String will be stored into the &lt;i&gt;String pool&lt;/i&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;String pool&lt;/strong&gt;&lt;br&gt;
The String pool is a dedicated area within the heap memory for storing String Objects.  Before a String literal is created, the JVM will scan the String pool to check if the String value already exists; if the String already exists within the String pool, the String literal will point to the String pool Object.&lt;br&gt;
The String pool is demonstrated using the below example:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringOne &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringThree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&amp;#8226; When &lt;code language=&quot;java&quot;&gt;stringOne&lt;/code&gt; is created, the String Object is created in the heap space.&lt;br&gt;
&amp;#8226; When &lt;code language=&quot;java&quot;&gt;stringTwo&lt;/code&gt; is created, the String is assigned to the String pool.&lt;br&gt;
&amp;#8226; When &lt;code language=&quot;java&quot;&gt;stringThree&lt;/code&gt; is created, the literal will point the existing String in the String pool.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/14f9ee7235a48a0ff53b235c89301dad/ee9b6/009StringPool.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 62.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAACWklEQVQ4y2VT23ISQRDlh/kWn1L6og8pFRIVSChNJJfSBJBUEcW4kEDccNssy2UX9g5smGP3mE2BTtWpudT0OWe6exJBEMDzvCf4vr+xdxwH4/FYYjQayXkymTydEYRt2xHf63Q6yQT+GUKIjT0LPDw8yLXrulAUBbVaDVEUybur1UrQnYj3JJRMtFoteaFarULXdUynU6nMipZlgV/AgwIlOJCJeR17IMGIzyQhL0zTlMHsJgxDCQ7gJ8YiA8OAeneHy8tLjEjQdT0IKSQ2CReLBebzuQQT8RwPJmWny+USi3lI8OHYUywXIfmK1rIkokdjyQQlEvV6XeaGn0oJlm6ZiJ2zax6aMUOrP0N74EG9d/Bbs3FL+2ZnIhw3iHzfw5gdcgAHM7iqnDN2HT/ZI2UKwH6xj5e5H9h6e46tVBEvdit4ni4hddwW1YYehb5Lhib/V3l9WJS7gJQt28enioH3pyrShWts52t4/fEK6c8N7JUNcaHoke85lEPzLyGXP8Z663C/zWZUddNG7qyDvaKGzJc2do6aSB028O6khZ2TO1H5dR8FHjlcJ4xnbou4JTzPx5ie3dMMGZwu3ODNgSIdPnt1gO3970gVWqJypUmHknA4HELTNPR6PRjUGlxpzmdc/TAgUstB5qyHvZKO7HkfWVqnj26RL+v4cKaL8k+NHD4SDgYDqKqKbrcrwSRc6RgsQslGoXSNzLGC3GkD2RMF+a9NHF30cfitK25Unb7NivszmeCqcqVjMAn3FJ+zW15blgmT8jkcjmTlGbxmMYJwHP7LLv3ldvIPE/PB546mZecAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;String pool diagram&quot;
        title=&quot;String pool diagram&quot;
        src=&quot;/static/14f9ee7235a48a0ff53b235c89301dad/5a46d/009StringPool.png&quot;
        srcset=&quot;/static/14f9ee7235a48a0ff53b235c89301dad/1db73/009StringPool.png 75w,
/static/14f9ee7235a48a0ff53b235c89301dad/8a4e8/009StringPool.png 150w,
/static/14f9ee7235a48a0ff53b235c89301dad/5a46d/009StringPool.png 300w,
/static/14f9ee7235a48a0ff53b235c89301dad/fc2a6/009StringPool.png 450w,
/static/14f9ee7235a48a0ff53b235c89301dad/0a47e/009StringPool.png 600w,
/static/14f9ee7235a48a0ff53b235c89301dad/ee9b6/009StringPool.png 1041w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The immutable nature of a String Object means new Objects are created when the value of String is updated. If &lt;code language=&quot;java&quot;&gt;stringTwo&lt;/code&gt; and &lt;code language=&quot;java&quot;&gt;stringThree&lt;/code&gt; were assigned to different values, the JVM will create two new Objects providing the new values are not found in the String pool. The String pool will retain the memory address of the value &quot;World&quot;, however it will remain unreferenced in the pool:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringThree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    stringTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    stringThree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Gosling&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8df6d5eb2563444e82f2e13eb9075639/2e694/009StringPoolUpdate.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 62.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAACZUlEQVQ4y2VTa1PaQBTlD/Nr+vJDZ1rHoh21rVpftYKKdupYapWH1hhaEhMgCY8CIRA35HTPIqjtzpzJ7mb33HvuuZsIggC+70/R7/cfrbvdLjzPU3BdV30bjcZ0TyLudDqC5yqVSjKBf0Ycx4/WDBBFkZr3ej0Ui0WcnZ1BCKHOjkajWJ4RXMtAyYSmaepALpdDtVpFu91WkRmx1WqBCjjkRQhJfHt7q/5F0UgRyvAx2RhMEXLSbDbVZWYzGAwUSECJtm3Lfwwi55aJQuEcnT8thMNgKkqeFXc8yUQYhhgOh48wGSRlHaNI4MZ2kL808D1/jQvdhm64qNhtmNVWPAxDwXMqQ1lIlEolVRtKlQVW2U4k1+t19IMhdr7VMLdZwrPUHp6m9vF84RAvJFKftLigu2IQ+HA9SUiZvEwwCmvGOrHIzN62LdxUXaxkDawcmljc1fBmo4BXKzmkNgtYzPyOj34YIvB78BrN/11+OCi51+vCtOpYSmsSOubWz7Gc0THz9hAzC1nM72hxNqeLoO9LhY0xoXLrDg8H22VCuLh7hfntC0VIspfvjtUeMzwpVkXf795nOCHil1KZ2XgN6bQDu+ZhTcplZgvbJbxePcXs2imW09dY3jOmhA0SOo4Dy7JgmqYygC3Deo7dD+HUa/CaHax+sbF+7GIp8wuprQs8mduTpmQxu3EZ759c35tSq9VQLpdhGIYC24ZOT0DnadLXvIXtYxObRzrWDq7w/nMeH9JFfDz4GV+VLfls2LdeMkFX6fQEJGGTcp/Zjl+AfL+ysamAihzHldnwbUs4TizrPH3LfwHkS7qhDqc2rgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;String pool diagram&quot;
        title=&quot;String pool diagram&quot;
        src=&quot;/static/8df6d5eb2563444e82f2e13eb9075639/5a46d/009StringPoolUpdate.png&quot;
        srcset=&quot;/static/8df6d5eb2563444e82f2e13eb9075639/1db73/009StringPoolUpdate.png 75w,
/static/8df6d5eb2563444e82f2e13eb9075639/8a4e8/009StringPoolUpdate.png 150w,
/static/8df6d5eb2563444e82f2e13eb9075639/5a46d/009StringPoolUpdate.png 300w,
/static/8df6d5eb2563444e82f2e13eb9075639/fc2a6/009StringPoolUpdate.png 450w,
/static/8df6d5eb2563444e82f2e13eb9075639/0a47e/009StringPoolUpdate.png 600w,
/static/8df6d5eb2563444e82f2e13eb9075639/2e694/009StringPoolUpdate.png 1048w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Interning Strings&lt;/h4&gt;
&lt;p&gt;
&lt;code language=&quot;java&quot;&gt;intern()&lt;/code&gt; is a public method of the String class that is used to assign a String Object into the String pool. All literal instantiations of a String object call the intern() method by default. When a String is created with the &lt;code language=&quot;java&quot;&gt;new&lt;/code&gt; operator, the &lt;code language=&quot;java&quot;&gt;intern()&lt;/code&gt; method is not called and the String is only stored in the heap area. Calling &lt;code language=&quot;java&quot;&gt;intern()&lt;/code&gt; on a String Object that is instantiated with the &lt;code language=&quot;java&quot;&gt;new&lt;/code&gt; operator will provide &lt;strong&gt;additional&lt;/strong&gt; storage of the String into the String pool. The reference to the String object will now point to the String pool. As a result, twoString can be similarly compared using the &lt;code language=&quot;java&quot;&gt;==&lt;/code&gt; operator as opposed to the &lt;code language=&quot;java&quot;&gt;.equals()&lt;/code&gt; method.&lt;br&gt;
The below example demonstrates the use of &lt;code language=&quot;java&quot;&gt;intern()&lt;/code&gt;: &lt;br&gt;
&amp;#8226; stringOne is created once: in heap memory&lt;br&gt;
&amp;#8226; stringTwo is created twice: in the heap and String pool.&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringOne &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;intern&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/71c106a93a1fb1f07e73a60c1633dfa4/25c1c/009StringOp.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 62.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAACNElEQVQ4y3VTaW8SURTl7/JvjEa/uqBtU2NabRWtUiNd3D4UFKQNHbYO2wzLMAvbg3e85zVDoEaSy1vCPefc8w6J8XiMMAxXFUWRWYMgMOtwOITjOOj1emat1WpoNpvodrtwXZd3ut/vK9/3Ua1Wkwnc+SyXy43zZDJZ7ZVSKBQKyOfzmE6n8bWWveJZgJMJMlcqFZTLZcNMhbwjo/wAnU4H8/ncFAFJMBqNjFqql7NeLBaKE0lfMuF5HmzbNqAEYgNHpdLZbGaaWSQgGcccDAbI5XKmR/q1kCn2GECjWWujnQAEpBJhNWORmXuuBGu322adTmdCqrGQrw2FsRFUSmYqiVXxTPOpfDjso9W0cVkqot9zMI7ClYdaL/8FJACVrH+oPH6UjjtC1R7gpuuj3h5JeWhIyZ32/EhFQuC6dwBp/LoF5hEkVhMZ7+35DZ68yePe4yPcf5bBo60sHqSOsfPJ0ud5W0WhLwr7m4D0cD02VDwTHwl4eGbj4Gsbe9k6dj6U8DJzjb0vNeyfNvXJhS0jCyBjQ+8YF8uyjF9xsAnGcemNH4R4na1i/6SBV58t7H68Quowh+33RexmKgLYUGEMuJ5DxodgfMXbHA7gCInj9rCVLiKVvkTq3R88T5fwcPsMTw9+4cWRpY9/1m4B10f+3z+F0aEVhWsH3wsdqSa+/bbxo9iWfQunF3V9VWmp5UIx7MkEjWdDXHyY9TOLYwe+B9+TWI2GGxUGnl6ouaI9MmHyLwLXycsXOePVAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;String operator diagram&quot;
        title=&quot;String operator diagram&quot;
        src=&quot;/static/71c106a93a1fb1f07e73a60c1633dfa4/5a46d/009StringOp.png&quot;
        srcset=&quot;/static/71c106a93a1fb1f07e73a60c1633dfa4/1db73/009StringOp.png 75w,
/static/71c106a93a1fb1f07e73a60c1633dfa4/8a4e8/009StringOp.png 150w,
/static/71c106a93a1fb1f07e73a60c1633dfa4/5a46d/009StringOp.png 300w,
/static/71c106a93a1fb1f07e73a60c1633dfa4/fc2a6/009StringOp.png 450w,
/static/71c106a93a1fb1f07e73a60c1633dfa4/0a47e/009StringOp.png 600w,
/static/71c106a93a1fb1f07e73a60c1633dfa4/25c1c/009StringOp.png 1047w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Comparing Strings&lt;/h4&gt;
&lt;p&gt;
String Objects can be compared with either &lt;code language=&quot;java&quot;&gt;==&lt;/code&gt; or &lt;code language=&quot;java&quot;&gt;.equals()&lt;/code&gt;. &lt;code language=&quot;java&quot;&gt;==&lt;/code&gt; is a reference comparator and will compare the memory locations of the two Strings. &lt;code language=&quot;java&quot;&gt;.equals()&lt;/code&gt; is a content comparator and will compare the value of the two Strings.&lt;br&gt;
By using the &lt;code language=&quot;java&quot;&gt;intern()&lt;/code&gt; method, the String Object will evaluate to true when using &lt;code language=&quot;java&quot;&gt;==&lt;/code&gt; to compare references with a String literal of the same value. While the &lt;code language=&quot;java&quot;&gt;==&lt;/code&gt; method is a more time efficient comparator, the equality of two String Objects is dependent upon their location reference and not their content. As a result, two Strings with the same value may not evaluate to true if one was created in the heap area. 
The code sample below demonstrates how different String Objects can be compared using their memory address and value:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringOne &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; stringThree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;intern&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringOne &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; stringTwo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;//false&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringOne &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; stringThree&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;//true&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringOne&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringTwo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;//true&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringOne&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringThree&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;//true&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringTwo &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; stringThree&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;//false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;StringBuffer and StringBuilder&lt;/h4&gt;
&lt;p&gt;
The StringBuffer and StringBuilder classes provide support to using Strings as mutable Objects. StringBuffer and StringBuilder Objects are created within the heap area.
&lt;/p&gt;
&lt;p&gt;
The StringBuffer and StringBuilder classes differ by their thread safety. StringBuffer Objects are accessed through synchronized methods and can therefore only be accessed by a single thread at a single time. The StringBuilder Object is not thread-safe and can be accessed simultaneously by different threads. The StringBuilder class is often preferred due to its latency performance over StringBuffer.
&lt;/p&gt;
&lt;p&gt;
Both StringBuffer and StringBuilder Objects provide functions that enable a String to be transformed and accessed in memory:&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;append(String)&lt;/code&gt; to concatenate String Objects onto the StringBuffer/Builder&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;insert(int, String)&lt;/code&gt; to insert the specified String within the StringBuffer/Builder at the &lt;code language=&quot;java&quot;&gt;int&lt;/code&gt; position.&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;reverse()&lt;/code&gt; to reverse a StringBuffer/Builder.&lt;br&gt;
&amp;#8226; &lt;code language=&quot;java&quot;&gt;delete(int, int)&lt;/code&gt; to delete the characters from the start and end index defined by the two arguments.&lt;br&gt;
An additional benefit to using the StringBuffer and StringBuilder aligns to their resemblance of the builder pattern to enhance code readability:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringArg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    
        &lt;span class=&quot;token class-name&quot;&gt;StringBuilder&lt;/span&gt; sb &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        sb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stringArg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;//add argument to sb&lt;/span&gt;
        sb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;token comment&quot;&gt;//add &quot;__&quot; at third position&lt;/span&gt;
        sb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;deleteCharAt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;//remove the final char&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; sb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;//return sb as a String object&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The String class is uniquely handled by the JVM with an optimized pipeline and storage. The immutability of a String means new Objects are created when the String value is modified. A String Object can be instantiated into the heap area which effectively alters how the String Object is equated to other String Objects. Lastly, the JVM offers StringBuffer and StringBuilder classes to provide functionality for String Objects as a mutable Object.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Connecting MySQL to a Spring cloud task]]></title><description><![CDATA[Run an asynchronous task with Spring Cloud Task and store the values from the output into a MySQL database.]]></description><link>https://aneesh.co.uk/connecting-mysql-to-a-spring-cloud-task</link><guid isPermaLink="false">https://aneesh.co.uk/connecting-mysql-to-a-spring-cloud-task</guid><pubDate>Mon, 25 May 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Use Spring Cloud Task to create a microservice that executes an asynchronous task.&lt;br&gt;
&amp;#8226; Connect a Spring Cloud Task to a MySQL database to store runtime execution data.&lt;br&gt;
&amp;#8226; Review the information that can be obtained by default from the Spring Cloud Task execution.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is an asynchronous task?&lt;/h4&gt;
&lt;p&gt;
An asynchronous task is a parallel function that is executed separately from the main application thread. Asynchronous tasks are designed to be stateless and have a distinct start and end time. Without asynchronous tasks, microservices may wait upon other microservices to finish executing their task before they resume processing. As a result, application performance can risk a latency build-up due to a single task execution.
&lt;p&gt;Spring Cloud Task enables asynchronous tasks to be performed within a Spring Boot application.
One example of an asynchronous task may involve messaging. Rather than a microservice depending on feedback that a message has been processed and executed, the message can be sent to a bus using Spring Cloud Task. As a result, the dependency of message processing is decoupled from the microservice and the bus can be independently scaled to process messages with event-driven computing.&lt;/p&gt;
&lt;p&gt;In this post, Spring Cloud Task will be used to execute simple business logic. The runtime execution of the task generates a log of default fields that can be persisted in a MySQL database.&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Creating a Spring Cloud Task&lt;/h4&gt;
&lt;p&gt;
A Spring Cloud Task can be created through the Spring initializer. The dependencies of a MySQL Driver and JDBC API are also necessary for connecting with a MySQL Database:&lt;br&gt;
    &amp;#8226; Spring Cloud Task.&lt;br&gt;
    &amp;#8226; MySQL Driver.&lt;br&gt;
    &amp;#8226; JDBC API.&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;	&amp;lt;dependency&gt;
        &amp;lt;groupId&gt;org.springframework.cloud&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;spring-cloud-starter-task&amp;lt;/artifactId&gt;
    &amp;lt;/dependency&gt;

    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;mysql&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;mysql-connector-java&amp;lt;/artifactId&gt;
    &amp;lt;/dependency&gt;
			
    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;org.springframework.boot&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;spring-boot-starter-jdbc&amp;lt;/artifactId&gt;
    &amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The main class is annotated with &lt;strong&gt;@EnableTask&lt;/strong&gt; to bootstrap the functionality of Spring Cloud Task. By default, the annotation will equip the microservice with a log of data from the runtime execution, known as the TaskRepository, stored in-memory.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token annotation punctuation&quot;&gt;@SpringBootApplication&lt;/span&gt;
    &lt;span class=&quot;token annotation punctuation&quot;&gt;@EnableTask&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AdditionApplication&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;SpringApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;AdditionApplication&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
A sub-class that implements CommandLineRunner is used to execute the business logic.&lt;br&gt; 
The CommandLineRunner interface is one of two interfaces (along with ApplicationRunner) that allows methods to be run once a Spring Boot application has started. The CommandLineRunner interface provides access to the arguments passed into the application by overriding the &lt;code class=&quot;java&quot;&gt;run(String... args)&lt;/code&gt; method.&lt;br&gt;
If the microservice has multiple implementations of CommandLineRunner, the classes can be ordered with the &lt;strong&gt;@Order&lt;/strong&gt; annotation. 
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Concatenator&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CommandLineRunner&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

			&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;token class-name&quot;&gt;StringBuilder&lt;/span&gt; concatenation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; argument &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
					concatenation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; concatenation&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;argument&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
				&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Concatenated arguments: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; concatenation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;No arguments made to spring task&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;	
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;		
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
A bean is added to the main class to ensure the sub-class and the business logic is processed by Spring at runtime.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Bean&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Concatenator&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createConcatenator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Concatenator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Creating a MySQL database&lt;/h4&gt;
&lt;p&gt;
A database named &apos;springCloudTask&apos; is created within the MySQL CLI using the following command:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;mysql&gt; create database springCloudTask&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The database exists without any tables and can be validated with the following commands:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;mysql&gt; use springCloudTask
Database changed
mysql&gt; show tables;
Empty set (0.07 sec)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
When the Spring Cloud Task is connected to the database, it will automatically create and populate tables if they do not exist.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Connecting the Spring Cloud Task with the MySQL database&lt;/h4&gt;
&lt;p&gt;
Before the Spring Cloud Task can be run, the &lt;strong&gt;application.properties&lt;/strong&gt; file must be updated to reference the newly created database:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;spring.application.name=Concatenate Task
logging.level.org.springframework.cloud.task=DEBUG
spring.datasource.url=jdbc:mysql://localhost:3306/springcloudtask?useSSL=FALSE
spring.datasource.username={ENTER USERNAME}
spring.datasource.password={ENTER PASSWORD}&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
Line 1 will provide a name to the application for debugging.&lt;br&gt;
Line 2 will provide additional debugging into the log.&lt;br&gt;
Line 3 will connect to the database through the port number.&lt;br&gt;
Lines 4 and 5 are to be updated with the appropriate credentials for database connection.
&lt;/p&gt;
&lt;p&gt;
Before the Spring Cloud Task is executed, update the run configurations to include arguments that are to be concatenated by the business logic. For example, pass in the arguments &quot;run my spring boot app&quot;.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Task execution&lt;/h4&gt;
&lt;p&gt;
After running the Spring Cloud Task, the following lines are printed amongst many others to the console:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Initializing task schema for mysql database&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Creating: TaskExecution{executionId=0, ..., taskName=&apos;Concatenate Task&apos;, startTime= {...}, endTime=null, exitMessage=&apos;null&apos;,..., arguments=[run, my, spring, boot, app]}
Concatenated arguments: runmyspringbootapp&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The Spring Cloud Task has executed and shown the start and end time of the task along with the arguments that have been processed.&lt;br&gt;
The execution has also created 5 new tables in the database, found through the MySQL CLI:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;mysql&gt; show tables;

+---------------------------+
| Tables_in_springcloudtask |
+---------------------------+
| task_execution            |
| task_execution_params     |
| task_lock                 |
| task_seq                  |
| task_task_batch           |
+---------------------------+
5 rows in set (0.16 sec)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The tables show the following information:&lt;br&gt;
&amp;#8226; &lt;strong&gt;task_execution&lt;/strong&gt;: key details about when the task runtime such as id, start time, end time, exit code and exit message.&lt;br&gt;
&amp;#8226; &lt;strong&gt;task_execution_params&lt;/strong&gt;: log of the parameters passed into the Spring Cloud Task.&lt;br&gt;
&amp;#8226; &lt;strong&gt;task_lock&lt;/strong&gt;: stores information when Spring Cloud Task restrictions are applied.&lt;br&gt;
&amp;#8226; &lt;strong&gt;task_seq&lt;/strong&gt;: an incremental Id is stored and obtained as a reference for the next execution.&lt;br&gt;
&amp;#8226; &lt;strong&gt;task_task_batch&lt;/strong&gt;: links the task execution with the batch execution.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;strong&gt;task_execution&lt;/strong&gt; and &lt;strong&gt;task_execution_params&lt;/strong&gt; tables are most relevant to this example as they provide details of the runtime execution. The tables can be viewed from the workbench or the CLI:
&lt;/p&gt;
&lt;p&gt;
Task execution table:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 26.666666666666668%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABBElEQVQY002QiW6CQBRF+f+fa1OUHWZTFLARqASK4OmTtKbJnMy8JZOT61XnE1prVKGw1mLkbYyh73qs0WRpJrMC6xzjOPI9TXTtlSiMtllZlkzSG4aBeZ7xzlXDssJ8X4T1xTTNxHFKmhZoY1HGEcXywelMc/lktwtRyuAOR5IkFzKubY+nXE03QntbaYc/HlIv7CLFPjLE+YEoP+KHCuUayqbn7SMhyg6kqiRIrOxqTpcbXlXVPEAsHy/kcF8WsiynyJ8RWInFoYS6bmjblmAfS0QOZ599u9l+3Qa8wla/hss/w3Wrg1gLhkQM93L7oUYfLpR1x7ufkRTHbfY09wO1mf8AUdVzRGSe27AAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Workbench task execution&quot;
        title=&quot;Workbench task execution&quot;
        src=&quot;/static/6f2a43af9c02cce36491c6d6d3369e37/5a46d/008TaskExecution.png&quot;
        srcset=&quot;/static/6f2a43af9c02cce36491c6d6d3369e37/1db73/008TaskExecution.png 75w,
/static/6f2a43af9c02cce36491c6d6d3369e37/8a4e8/008TaskExecution.png 150w,
/static/6f2a43af9c02cce36491c6d6d3369e37/5a46d/008TaskExecution.png 300w,
/static/6f2a43af9c02cce36491c6d6d3369e37/e45a9/008TaskExecution.png 364w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 176px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACHUlEQVQoz32SiU7bQBBA/f8/UamqhCqVNoKKmxJCyGVMSGLHkMtOHCCnIZev2H5dByq1lHakp5nd2Tl2dqXlfMa1opA+O0e/bdA1mpyms+zv7WN0LRKJ45hfMri3yOdl5CsF256iVWtcZrPkC1eEUYzkOC6Dhwd0Xccwu5hGh1arQ+OugdW/f00Y4XkerusxHj1Sqaq0OwZmpy3ibtFUlZubKvP5AsnqmqiajmkaaDUVra7RbhvUVA2j3UHTNEolmfHEJo7WdFpt/CBKyhCG4R/dB0GANBo8otbqoqsGinxFtVpBVXWKxRL9Xp9Ws0mhUGKxXG2C7i3rNeH7Itkzh+F0yeTZ2TB+WjGduUI7jOylYLXx23OfhRszFnvPy5C5EzF7B0kp19neOePoLMfW512+7ByS2rvg41aKTK7K/nGWD5++oRsT5i5kczf0Rj7TRcRoFjJ+gxSEsBZjWItbJBcJ4xcS2w9f9hO/F8S4guR8ol3/Zf0WqamrHB2dUipesre7K+wD0ucXpFIpKkqZbDbD9vZXhuPpZkYV8cUcb/3vGQaivCdaSXDcQFRebwIS2xe+5AGWji/siHWYdPh/JLXe4jgtk5erHP/IcZIpcp4Xszu5pKTowi7z/TBDs2ez8EBW6vQnPvYyYjIP/0IyrTFlVfzBRo/Stc51rUlZMyiIwIYxRL3rkpM1rNGKZyemZQ4ZPK03j5IkSPTv/ARPYC2sefqCvQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Workbench task execution&quot;
        title=&quot;Workbench task execution&quot;
        src=&quot;/static/645ca850b7040f85fb5fd240deef71e3/06437/008TaskExecution2.png&quot;
        srcset=&quot;/static/645ca850b7040f85fb5fd240deef71e3/1db73/008TaskExecution2.png 75w,
/static/645ca850b7040f85fb5fd240deef71e3/8a4e8/008TaskExecution2.png 150w,
/static/645ca850b7040f85fb5fd240deef71e3/06437/008TaskExecution2.png 176w&quot;
        sizes=&quot;(max-width: 176px) 100vw, 176px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
Task execution parameters table:
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 237px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 46.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABZklEQVQoz4VSDU+DMBTk//8qTczm5hczUQfolk1bPga4UgsMaM/2TbOZLfGRl5TecX3H1ZNSwlW1LXFzc4uHex/zeYDLi0v4/iP82SOmkymi1zeEYQStNYKXJ4zHU6xWK0yuJxiNxniZhzD28ZIkQdM0SNMEbh3HMTiPkWUZdVEUyNIUjDFa17VCzDm9O24S779xWNd18IIgQNs2JLLrevxX7vAgCNF2+gQzBvB+LUdLBp7XKKVGXg1nukf5ZcBSgdnzEp8KKI64GzGg3ml4VVWR4PqdoVItugF2UoNd/7fdRA6TskYYLcCT3AoMR7jBoM1BkDGOutnR2A446cEQT2wFhXVnw8vykvYc5jjaHAlGCwaWN2TrvOWBbK75J66ufTxF3Fr+5WqyrFprWQhBgqv1B6S1PNh/3dkTT7o3hClV2ysSoSjFntsfOGRZKUWC+WZDsf/kdSbf/V6tpL17U4hKHqI9qm9nFbBrhgFTgAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Workbench task parameters&quot;
        title=&quot;Workbench task parameters&quot;
        src=&quot;/static/b1dc980a93faaf9a37a12ae945b4bdde/008e2/008TaskParams.png&quot;
        srcset=&quot;/static/b1dc980a93faaf9a37a12ae945b4bdde/1db73/008TaskParams.png 75w,
/static/b1dc980a93faaf9a37a12ae945b4bdde/8a4e8/008TaskParams.png 150w,
/static/b1dc980a93faaf9a37a12ae945b4bdde/008e2/008TaskParams.png 237w&quot;
        sizes=&quot;(max-width: 237px) 100vw, 237px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Spring Cloud Tasks enable tasks with a set start and end time to be decoupled within a microservices architecture. With just a few dependencies and lines in the application.properties file, task execution can be logged into a persistent database.&lt;br&gt;
The Spring Cloud Task can be taken further by deploying it into a Maven repository where it can be called upon by other microservices. &lt;br&gt;
The code sample from this blog can be found on GitHub &lt;a target=&quot;_blank&quot; href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/SpringCloudTask&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Implementing chaining with a hash table]]></title><description><![CDATA[Create a hash table that uses chaining to resolve hash collisions and rehashing to enforce an optimal load factor.]]></description><link>https://aneesh.co.uk/implementing-chaining-with-a-hash-table</link><guid isPermaLink="false">https://aneesh.co.uk/implementing-chaining-with-a-hash-table</guid><pubDate>Mon, 18 May 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Write functions to enable a hash table to resolve collisions with chaining.&lt;br&gt;
&amp;#8226; Write a rehash function to recreate a hash table with a smaller load factor.&lt;br&gt;
&amp;#8226; Use variables such as load factor, hash function and buckets to influence hash table performance.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Chaining in a hash table&lt;/h4&gt;
&lt;p&gt;
Hash collisions occur within a hash table or hash map when multiple keys have the same output from a hash function. 
Chaining, also known as closed addressing, is a technique used to store the value of a key when a hash collision occurs.
If you are unfamiliar with chaining, please visit my blog where I explore hash collisions and solutions &lt;a target=&quot;_blank&quot; href=&quot;https://aneesh.co.uk/resolving-hash-collisions&quot;&gt;&lt;u&gt;here&lt;/u&gt;&lt;/a&gt;.&lt;br&gt;
In this post, I will implement chaining with a hash table. The hash table will resolve collisions that occur within a bucket by storing the collided values into a LinkedList.
&lt;/p&gt;
&lt;h4&gt;Implementing chaining&lt;/h4&gt;
&lt;p&gt;
The hash table design will consist of two classes:&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;ChainedHash&lt;/code&gt;: the hash table class.&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;EntryLinkedList&lt;/code&gt;: the LinkedList Object appended to each bucket of the ChainedHash instance.&lt;br&gt;
The diagram below illustrates how the two classes will be used together. The numbers inside the EntryLinkedList Objects represent the values from their key:&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 111.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAYAAADAQbwGAAAACXBIWXMAABcRAAAXEQHKJvM/AAACfklEQVQ4y52U23PSQBjF4///4h+gvvjgOI46dlp5UCHc0ypQAoFSCCHh0nIpLeUSkiU57rc0sZSWUjOzbNhkf3O+822O1O12sVgsxJjNZphMJphOp5jP5+L/jsFoD9//DYDEx6tIJCJJBKDLcRwM+n1opTJM08QeF6Ofm5ubECjLsiSNx2PxtM9hjXodqXicQ1W+4m/s9n3/4RBAvj8ERqNRSbq+vhYbPM+D2WohnUzh9LQk1s5MDXI+gv74InzH970ngbFY7J/C5XIJo9kUwHxBFWuxXBzvDl/DvKhv1fskMFDoui5ahrGhUG3k8f34M1o9C0vHhVKJI1X8Acddhh4+qfD29hZNrlDJZFDgQI9bmCym8VF+A71nYDK1caB8wdfEe9jO4nkgHYGmriOVSKBSLq/byFyhxvNWokkuc8TaXh7S2SNgNp1GRdO2PXuph4wx4SEp1O4UhkcE/l7HZstDgwNPFAW1avVReb7/AoXCw6aOhCzj959TwSjU8jhIf0Bn0NpQvBMYKuQe0jk8zmZRVNclK+VjfJLfwrzU9wcGCoOmJOnTK5ceNOA/SqY5UFitVELAPUX7dTko2bZt6Praw5KqPtbV/bp8/9PrdDoo5HKi9B0pIwYPit3AIE3ISwrddruNXq8ngjYI4GDcrTEKFP5eCDw6OtqMr+Ci0k+UrPCTzmad52StVsP5+bmYKYD5PaP3+Pf/uMLA9NVqBcu0UOIRppU02EtbNIy8psNPCU8Kr66uGFUzGAx2A70VD9qOAaX2C7mzDEbDkQASiFKdAzAajSj62XA4JFueA/rQrTqSjUNkqj95o7qwLEuUSeVTmXTPG8ho5s82gH8B0Wg5w5Ch04UAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Chained hash table diagram&quot;
        title=&quot;Chained hash table diagram&quot;
        src=&quot;/static/58ae7ce74e9f750a741ce07b6a3574e0/5a46d/007_chainedDiagram.png&quot;
        srcset=&quot;/static/58ae7ce74e9f750a741ce07b6a3574e0/1db73/007_chainedDiagram.png 75w,
/static/58ae7ce74e9f750a741ce07b6a3574e0/8a4e8/007_chainedDiagram.png 150w,
/static/58ae7ce74e9f750a741ce07b6a3574e0/5a46d/007_chainedDiagram.png 300w,
/static/58ae7ce74e9f750a741ce07b6a3574e0/fc2a6/007_chainedDiagram.png 450w,
/static/58ae7ce74e9f750a741ce07b6a3574e0/0a47e/007_chainedDiagram.png 600w,
/static/58ae7ce74e9f750a741ce07b6a3574e0/e619b/007_chainedDiagram.png 787w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
The ChainedHash class will use the following methods to implement chaining:&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;put(key, value)&lt;/code&gt; to place key-value pairs in the hash table.&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;hashFunction(key)&lt;/code&gt; to calculate the hash value of the key.&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;validateLoadFactor()&lt;/code&gt; to ensure the load factor of the hash table is monitored and adjusted as new entries are made.
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The buckets within the ChainedHash class will be an array of EntryLinkedList Objects. Each EntryListObject will contain the following variables:&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;String key&lt;/code&gt; to store the key.&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;int value&lt;/code&gt; to store the value.&lt;br&gt;
&amp;#8226; &lt;code class=&quot;java&quot;&gt;EntryLinkedList nextListEntry&lt;/code&gt; to link additional key-value pairs to the bucket.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Class: EntryLinkedList&lt;/h5&gt;
&lt;p&gt;
The EntryLinkedList constructor will instantiate the key, value and nextListEntry.
There are getters and setters for the value and nextListEntry, and a getter for the key.&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt; nextListEntry&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        
    &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;key &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nextListEntry &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Class: ChainedHash&lt;/h5&gt;
&lt;p&gt;
The ChainedHash class instantiates its buckets from the constructor. The load factor for the hash table will be evaluated against a static constant: &lt;code class=&quot;java&quot;&gt;MAX_LOAD_FACTOR&lt;/code&gt;. The MAX_LOAD_FACTOR represents the upper-bound value of the load factor. The constructor will take an argument to define the initial number of buckets and it will instantiate all buckets to null.
&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; loadFactor&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; hashTableBuckets&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; numberOfBuckets&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; numberOfEntries &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; MAX_LOAD_FACTOR &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.75&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ChainedHash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; bucketsToCreate&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        numberOfBuckets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bucketsToCreate&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTableBuckets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;numberOfBuckets&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; numberOfBuckets&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            hashTableBuckets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
The &lt;code class=&quot;java&quot;&gt;hashFunction(String key)&lt;/code&gt; method is used to calculate the hash value of the key passed in. The hash function will sum the ascii values of the String (lines 4 to 6) and return the modulo of the sum against the number of buckets (line 9 and 11):&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;hashFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; asciiValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            asciiValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; asciiValue &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; hashValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;asciiValue &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; numberOfBuckets&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; hashValue&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
The &lt;code class=&quot;java&quot;&gt;validateLoadFactor()&lt;/code&gt; method is called within the put(key, value) method to ensure the load factor of the hash table is not greater than the MAX_LOAD_FACTOR constant. If the load factor is greater than MAX_LOAD_FACTOR, the numberOfBuckets static value is doubled (line 4) and the hash table is replaced by calling the &lt;code class=&quot;java&quot;&gt;rehash()&lt;/code&gt; method (line 6).
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;validateLoadFactor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;calculateLoadFactor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; MAX_LOAD_FACTOR&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Current load factor: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculateLoadFactor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            numberOfBuckets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numberOfBuckets &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Buckets raised to: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; numberOfBuckets &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; buckets.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;rehash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;p&gt;&lt;/p&gt;
The rehash method will recreate the hash table using the updated number of buckets in 4 steps:&lt;br&gt;
1. On line 3, a temporary EntryLinkedList array is created as a replica of the existing array.&lt;br&gt;
2. On line 5, the existing EntryLinkedList is replaced by a new array with the new number of buckets.&lt;br&gt;
3. On line 6, the &lt;code class=&quot;java&quot;&gt;numberOfEntries&lt;/code&gt; static value is reset to ensure the new load factor is correctly recalculated.&lt;br&gt;
4. From line 7, the new hash table is created by looping through the previous entries in the &lt;code class=&quot;java&quot;&gt;temp&lt;/code&gt; array and appending them into the new EntryLinkedList.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Note, the hash function is also adjusted to the new &lt;code class=&quot;java&quot;&gt;numberOfEntries&lt;/code&gt; value and will therefore compute new index positions for each key into the new EntryLinkedList array. 
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rehash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; temp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hashTableBuckets&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        hashTableBuckets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;numberOfBuckets&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        numberOfEntries &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; temp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

                &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNextListEntry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNextListEntry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNextListEntry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
                    temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNextListEntry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;p&gt;
The diagram below illustrates the change made to the hash table following a rehash as it doubles in size and realigns each key-value pair.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 125.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAYAAAAxFw7TAAAACXBIWXMAABcRAAAXEQHKJvM/AAADb0lEQVQ4y41V2Y7iVhDlW+cb8gvRvOUHoiQj9csk6iRSJukkms501B0Ws8ww0Cw20KzdA8YYvGLj5UyVwQ7GtJIrlbjY5XPrVNWpm1uv19jtdtA2GyiKgu12C8dxInNdN2Mb8td1Pdqf+pqmiRw/9DwPrVYLxXyewF2kV5jsVqsVGvU6+j0J5xbj5CzLQhiGeBgMIJQEGOYWQiuPzvhj5gPDMFApC+i0u7AcG6XmLTRzfTg2jJgmgP1eD/l8ERvdxl+131HpFODuPIzmPTjuNvqIqdYqFdy3JMzVFX549woL9SkL6Ps+ekSjVBSgGzbyzRs0B3VyXuGn2wusNDn6aEN5rpTLFKGItanhuvYGy/UiSU0KUBJFFAlQ07e4qf+Jqni3dwvCiEEMWKtW0en0oGgqfiteQjkHGFMul0qwbYci+wRls/i3LAdAziFTFrsibErDx0EFhqUlfmnKkkSAQtQCtmOSWQlYDLg+RNjtdKL//s7fMzh0QgYwpvyH8AvK3dtnACtotyXIGxU//3NBxZmli2LbdrThU0vFAjWrkzjEQPGvqqoHyl0E9GypLqgT3DTlGJCdCvkCLHtLrTLA43KciZAB3x9RPs1xQpnXeDTCfbOBrePh7/fX+CAVM4BclCpFGAN6vpd6nwLkdXUnoiHNE8WdUl4sFhGg+H8i5PXFV2/R6u+b2A+CyDEIwjM5lGC5Nuq9Mkxbz7bNXtg+vvz6jmTmJQ5BGKai4GnEEbbbPaqugsubC1LK/JyWY8p9lOpUkOkD2ZAhcf+whmF7LBnM5wtUBIEi7FPbrPCr8Po8YLxcatTug4zpXEN3pOK7qwFevBQwk/c+Co0vjrBHMo38aQY+W5TwiN7wycBV8Yka/BOsrXd4j4gyD4cu5dB2HdSlMnRrcz6H8UnHwKdVXEaAAgH2oeoargo/Hmk+fB6QK+uz+Wk6siz/d9vwFXAKeC5S1jtT5uHAqnoW8PHxMXLkSFmGx8aXDjvFgMvlElXKIQNyVWUacxkt3zeaqNc+YDabYTweYzgcYjKZgA/iPQPHFWWlsJbbNGCfFBmv336TXAEBCYFHX24wltAdNmAaZnQtHkfId8gpdY6CZ+C+DEGmgLk37y7x/fW3KNG0ZsvTVVooFJK9SD03nU4xouHBDHg/IYv2k/0vG7Ma0M35GcZEZII4/6AAAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Rehash diagram&quot;
        title=&quot;Rehash diagram&quot;
        src=&quot;/static/15339330b8b7cf414f959adca1bb817b/5a46d/007_Rehash.png&quot;
        srcset=&quot;/static/15339330b8b7cf414f959adca1bb817b/1db73/007_Rehash.png 75w,
/static/15339330b8b7cf414f959adca1bb817b/8a4e8/007_Rehash.png 150w,
/static/15339330b8b7cf414f959adca1bb817b/5a46d/007_Rehash.png 300w,
/static/15339330b8b7cf414f959adca1bb817b/fc2a6/007_Rehash.png 450w,
/static/15339330b8b7cf414f959adca1bb817b/0a47e/007_Rehash.png 600w,
/static/15339330b8b7cf414f959adca1bb817b/f680b/007_Rehash.png 1108w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
The &lt;code class=&quot;java&quot;&gt;put(key, value)&lt;/code&gt; method is used to add a new key-value pair into the hash table. The put method consists of 4 key steps:&lt;br&gt;
1. The &lt;code class=&quot;java&quot;&gt;hashFunction(key)&lt;/code&gt; method is used on line 2 to calculate the hash value for the key.&lt;br&gt;
2. On line 5, the load factor is verified against the MAX_LOAD_FACTOR. &lt;br&gt;
3. On line 7, the put method checks if the existing hashFunction value already has an EntryLinkedList Object in the array.
If the array position is null, the put method will create a new EntryLinkedList and populate the first position with the value (line 9).&lt;br&gt; 
4. If there already exists an EntryLinkedList in the bucket, the put method will loop over the EntryLinkedList array (line 14) to either replace the value of the existing key (line 20) or append the value into the next position on the EntryLinkedList (line 23).
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; key&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; hashValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;hashFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        numberOfEntries&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token function&quot;&gt;validateLoadFactor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hashTableBuckets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;hashValue&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

            hashTableBuckets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;hashValue&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt; hashValueLinkedList &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hashTableBuckets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;hashValue&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hashValueLinkedList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNextLinkedValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;hashValueLinkedList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                hashValueLinkedList &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hashValueLinkedList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNextLinkedValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hashValueLinkedList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                hashValueLinkedList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token class-name&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;numberOfCollisions&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
                hashValueLinkedList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setNextListEntry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h5&gt;Using the hash table from the main method&lt;/h5&gt;
&lt;p&gt;
The main method will begin by instantiating a ChainedHash instance with 8 buckets. The instance will then call the put method followed by the &lt;code class=&quot;java&quot;&gt;printEntryLinkedList()&lt;/code&gt; method which will print each entry of the hash table to the console.&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;ChainedHash&lt;/span&gt; hashTable &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ChainedHash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;First entry (Alex, 47)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alex&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nOne more entry (George, 80)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;George&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nOne more entry (Phillip, 19)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Phillip&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nOne more entry (Eliza, 97)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Eliza&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;97&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nOne more entry (John, 90)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nOne more entry (Lin, 16)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Lin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nOne more entry (Miranda, 84)..&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Miranda&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;84&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        hashTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printEntryLinkedList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\nTotal collisions: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; numberOfCollisions&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h5&gt;Output review&lt;/h5&gt;
&lt;p&gt;
The output includes many debugging lines such as the hash table output and a prompt of what action is being taken for the explanation.
&lt;/p&gt;
&lt;p&gt;
The main method adds key-value pairs as expected up to the third put call (line 11 main method). The key-value pair of (Phillip, 19) maps into index position 2 from the hash function. As a result of the collision, the value of Phillip is appended to the EntryLinkedList instance with (Alex, 47) on line 10 in the output.
&lt;/p&gt;
&lt;p&gt;
When the 7th entry is made on line 28 in the main method (Miranda, 84), the load factor of the hash table is calculated as 0.875 (line 31 in the output). The load factor exceeds the 0.75 MAX_LOAD_FACTOR constant and the hash table is doubled in size to 16 buckets (line 32 in the output). Each entry of the existing hash table is recalculated in the rehash.&lt;/p&gt;
&lt;p&gt;
The final output of the hash table is shown from lines 33 to 39 in the output. Each index position for the key-value pairs are recalculated and almost all entries move to a different bucket. The new hash table reduces the likelihood of collisions as there are more buckets. The only collision that occurred from the original 8-bucket ChainedHash has been reduced to separate buckets.
&lt;/p&gt;
&lt;p&gt;
The efficiency of the hash table can be evaluated against the number of collisions that are created (as a result of the hash function) and the number of times a rehash is performed. Collisions  retain constant time access within the hash table, however rehashing will create a performance cost as each entry must be recalculated and entered.  
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;First entry (Alex, 47).. 
[hash value=&apos;2&apos;, key=&apos;Alex&apos;, value=47], null

One more entry (George, 80)..
[hash value=&apos;1&apos;, key=&apos;George&apos;, value=80], null
[hash value=&apos;2&apos;, key=&apos;Alex&apos;, value=47], null

One more entry (Phillip, 19)..
[hash value=&apos;1&apos;, key=&apos;George&apos;, value=80], null
[hash value=&apos;2&apos;, key=&apos;Alex&apos;, value=47], [hash value=&apos;2&apos;, key=&apos;Phillip&apos;, value=19], null

One more entry (Eliza, 97)..
[hash value=&apos;1&apos;, key=&apos;George&apos;, value=80], null
[hash value=&apos;2&apos;, key=&apos;Alex&apos;, value=47], [hash value=&apos;2&apos;, key=&apos;Phillip&apos;, value=19], null
[hash value=&apos;5&apos;, key=&apos;Eliza&apos;, value=97], null

One more entry (John, 90)..
[hash value=&apos;1&apos;, key=&apos;George&apos;, value=80], null
[hash value=&apos;2&apos;, key=&apos;Alex&apos;, value=47], [hash value=&apos;2&apos;, key=&apos;Phillip&apos;, value=19], null
[hash value=&apos;5&apos;, key=&apos;Eliza&apos;, value=97], null
[hash value=&apos;7&apos;, key=&apos;John&apos;, value=90], null

One more entry (Lin, 16)..
[hash value=&apos;1&apos;, key=&apos;George&apos;, value=80], null
[hash value=&apos;2&apos;, key=&apos;Alex&apos;, value=47], [hash value=&apos;2&apos;, key=&apos;Phillip&apos;, value=19], null
[hash value=&apos;3&apos;, key=&apos;Lin&apos;, value=16], null
[hash value=&apos;5&apos;, key=&apos;Eliza&apos;, value=97], null
[hash value=&apos;7&apos;, key=&apos;John&apos;, value=90], null

One more entry (Miranda, 84)..
Load factor is too high: 0.875
Buckets raised to: 16 buckets.
[hash value=&apos;2&apos;, key=&apos;Phillip&apos;, value=19], null
[hash value=&apos;3&apos;, key=&apos;Lin&apos;, value=16], null
[hash value=&apos;12&apos;, key=&apos;Miranda&apos;, value=84], null
[hash value=&apos;5&apos;, key=&apos;Eliza&apos;, value=97], null
[hash value=&apos;9&apos;, key=&apos;George&apos;, value=80], null
[hash value=&apos;10&apos;, key=&apos;Alex&apos;, value=47], null
[hash value=&apos;15&apos;, key=&apos;John&apos;, value=90], null

Total collisions: 1&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The implementation of chaining in a hash table can be achieved with just a few methods that cover the load factor evaluation, rehash, and put. By experimenting with the hash function, MAX_LOAD_FACTOR and the number of buckets from instantiation, the performance of the hash table can be altered to reduce the number of rehashes required, the bunching of values in buckets and collision frequency.&lt;br&gt;
Feel free to clone the source code from this blog on &lt;a target=&quot;_blank&quot; href=&quot;https://github.com/4neesh/DeveloperBlogDemos/tree/master/ChainingDemo&quot;&gt;&lt;u&gt;GitHub&lt;/u&gt;&lt;/a&gt; to test the impact of more entries on the performance of the hash table.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How the JVM manages memory]]></title><description><![CDATA[Explore the different types of garbage collectors and how the JVM uses them to manage memory.]]></description><link>https://aneesh.co.uk/how-the-jvm-manages-memory</link><guid isPermaLink="false">https://aneesh.co.uk/how-the-jvm-manages-memory</guid><pubDate>Mon, 11 May 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Memory management plays a vital role in the performance of an application.&lt;br&gt;
&amp;#8226; Java uses a garbage collector to automatically manage memory on behalf of the developer.&lt;br&gt;
&amp;#8226; There are six different types of garbage collectors.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Automated memory collection in Java&lt;/h4&gt;
&lt;p&gt;
Memory management is the process of assigning memory to Objects created by an application and releasing the memory when the Object is no longer required.&lt;br&gt;
In languages such as C++ and C, the developer will need to manually allocate Objects memory using methods such as &lt;code class=&quot;java&quot;&gt;malloc()&lt;/code&gt; and &lt;code class=&quot;java&quot;&gt;calloc()&lt;/code&gt;. If the developer does not remember to release the Object from the memory, a &lt;i&gt;Memory leak&lt;/i&gt; could occur.&lt;br&gt;
&lt;p&gt;
&lt;strong&gt;&lt;i&gt;Memory leak: when an Object continues to consume memory even after it is needed or when an Object stored in memory cannot be accessed by the programme.&lt;/i&gt;&lt;/strong&gt; 
&lt;/p&gt;
Memory management can be completed with many different strategies to ensure the application efficiently frees and allocates memory during execution. Memory management techniques used within applications are primarily concerned with the performance requirements of the following factors:&lt;br&gt;
&amp;#8226; &lt;strong&gt;Throughput&lt;/strong&gt;: how quickly memory can be freed from Objects that are no longer required.&lt;br&gt;
&amp;#8226; &lt;strong&gt;Stop the world events&lt;/strong&gt;: a period when the application is completely stopped to allow memory to be freed and assigned to all live Objects in the application.&lt;br&gt;
&amp;#8226; &lt;strong&gt;Memory fragmentation&lt;/strong&gt;: when memory is unevenly distributed across a memory block, leaving gaps of unfilled memory slots between Objects. There is a cost-benefit consideration that may arise between the performance gain of leaving memory fragmented against the cost of memory defragmentation to the application runtime.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Garbage collectors&lt;/h4&gt;
&lt;p&gt;
Java makes use of garbage collectors which promise the developer that Objects later referenced within the application will not be deleted from memory. Without such as promise, it is possible that a separate thread may obtain access to an Object reference and remove its memory allocation thus resulting in a null pointer when the Object is referenced from a different thread.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;The different types of garbage collectors&lt;/h4&gt;
&lt;p&gt;
There are 6 different types of garbage collectors that can be selectively used together. The different types of collectors allow an application to efficiently manage memory with the relative importance of throughput, memory fragmentation, and stop the world events implemented by design. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Do nothing garbage collector&lt;/h5&gt;
&lt;p&gt;
Do nothing collectors will only guarantee that live Objects are kept alive.
&lt;/p&gt;
&lt;h5&gt;Reference counting garbage collector&lt;/h5&gt;
&lt;p&gt;
Reference counting resolves the problem of having circular references within our code. As the garbage collector is responsible for collecting dead (unreferenced) Objects, a circular reference, where two Objects reference each other, can create a condition whereby neither Object will be collected. Circular referencing exists in the example below:
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;One&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Two&lt;/span&gt; two&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
     
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setTwo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Two&lt;/span&gt; two&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;two &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; two&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Two&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;One&lt;/span&gt; one&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
     
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setOne&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;One&lt;/span&gt; one&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;one &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; one&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;//one instance of each class is created&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;One&lt;/span&gt; instanceOne &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;One&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Two&lt;/span&gt; instanceTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Two&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
        &lt;span class=&quot;token comment&quot;&gt;// A circular reference is made between the two classes&lt;/span&gt;
        instanceOne&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setTwo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instanceTwo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        instanceTwo&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setOne&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instanceOne&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
 
        &lt;span class=&quot;token comment&quot;&gt;// Although the classes are now null, their reference to each other&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// remains in the other&apos;s method, therefore preventing collection&lt;/span&gt;
        instanceOne &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        instanceTwo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
Reference counting will resolve the problem of circular references; each Object will own a counter that is incremented and decremented as they are referenced and dereferenced. As a result, the counter assigned to each Object will allow the garbage collector to identify Objects that are truly referenced by the root set.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;i&gt;Root set: the initial set of Objects from which the reachability of all the other Objects can be derived upon.&lt;/i&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Mark and sweep garbage collector&lt;/h5&gt;
&lt;p&gt;
Mark and sweep performs three individual phases in garbage collection:&lt;br&gt;
&amp;#8226; &lt;strong&gt;Mark&lt;/strong&gt;: identify Objects that are currently in use.&lt;br&gt;
&amp;#8226; &lt;strong&gt;Sweep&lt;/strong&gt;: remove unused Objects.&lt;br&gt;
&amp;#8226; &lt;strong&gt;Compact&lt;/strong&gt;: remove memory fragmentation that may occur from the sweep phase.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mark phase&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
During the mark phase, the Objects that are reachable are identified by following the course of the root set.&lt;br&gt;
In the image below, the identified Objects within the memory are marked as &apos;live&apos;, Objects that reference each other without a root set reference will not be marked as live.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/56f2109c3dccfaad452c956494e368e6/02cd5/006Mark.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 69.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAABcRAAAXEQHKJvM/AAACaUlEQVQ4y4VTPY/TQBDNf6Gj4m9ASUNJgURBRYHQlTRUiOYamksHukiXIJ2EdCAEQgTlQi524uTO+b5L/JEv20lsJ3Ycx37sbuLojCIYebW74923b97MJEajEVzXxXw+x2Kx2M22bUOSJMiyjMFggOFwCFVV2do0zdhZOjuOg+l0igR17LMwDFGpVsHzPARBQKVSQZHjUCqV4Pv+3jvUn6BMqAUEAOTzlh57bb3aXgrC2CXP87BcLllUiP9i/h0gNcOaot5pQR9raNy0UZc64EQBM8vE1LHQJHu132fhy6qC+nULruNiZpuYrxysyGMMMNg+9UY4wbPj1/iZPsPDDwe4c/wYd1NPcJhO4n76Je6lnuJ95hifM6c4zCTxIPUCXz9+wkHqLZ7n3pGYEWfYM/q4EMuololmvQZ+tUv4USug1e3gu3DO1iNtDEPToI4HyNV5KJKMSksEf32FcB3EAUM/wNy04a1WbO+aJGF+yMIKPB+LmR3TjPqo3p673OhPQ3YWNr6cd1Ft9jGbGTAmk20uAmi6Bn+93jxGxpiwo9kPgoD4/b9zsgFUFQnJTAHcpYwwWLHDkdG6WkeABEjXdQIS7i2xHWC71cBRugD+SoHvOYSlyQqXDkVRGGDESiPaUdMdE2fyBUzbYnKEtxnWxEscneRQafQZQ4cciOqMzrctAqwOO3h0+gpiswZFVuIMZalLQi4iX74h4s5JmLMdQzomRFMaumEYrPVYvZI6/f0ti5oosi6yLIv5aYISRH4UawZ6Ayumx/8swP5ziWw2C6HMoVziWZ/+a9C+5kg/c9uepvvIR+d8Po8/40wR1Im93ucAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Mark phase diagram&quot;
        title=&quot;Mark phase diagram&quot;
        src=&quot;/static/56f2109c3dccfaad452c956494e368e6/5a46d/006Mark.png&quot;
        srcset=&quot;/static/56f2109c3dccfaad452c956494e368e6/1db73/006Mark.png 75w,
/static/56f2109c3dccfaad452c956494e368e6/8a4e8/006Mark.png 150w,
/static/56f2109c3dccfaad452c956494e368e6/5a46d/006Mark.png 300w,
/static/56f2109c3dccfaad452c956494e368e6/fc2a6/006Mark.png 450w,
/static/56f2109c3dccfaad452c956494e368e6/0a47e/006Mark.png 600w,
/static/56f2109c3dccfaad452c956494e368e6/02cd5/006Mark.png 821w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;Sweep phase&lt;/strong&gt;
&lt;p&gt;
During the sweep phase, the Objects that were not marked as &apos;live&apos; are removed from the heap memory. As a result, the memory block contains only live Objects.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/1f9fe0881146bbcc72f73b5660e72f4a/3cd52/006Sweep.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAACMUlEQVQ4y41Ty27TQBTNP7BmlU/hA2DJgg0SS5bs2KMu2aFKqBJCWURWk0BAVQVBoC5KikjqNnUTlNBHnMSv8SO248bxzMHjR0JCF1xpPJ6ZO+eee+6dgq7rCIIAvu8vx2w2w3g8xnA4hCzLUFU1mUejERRFged5f/sz7u+6Li2Xy8UC37zNJsoEbfEYrXYLoiji9KyD43jd7/c3XVnyYYzWarViIUZGtpEchcE8YXirRQzzeXa+WAeMoohWK5VigdPPrXlxil/yBXRDR138hl2xgUbnELpvY//3EdoDCbZpQdM07J0cYOq7IJ7NJPMqAUwZxoA0A3z6+SXq0gFUXcO9yjPc3XmIJx+2ILQ+4c72A7xqVhC6AdRYx/vV5ziXB/ja+8Ee722BRZTW3r9bZ+gQO0sgtalmwlA0qBMFZKTixg9WZ8SCaZogusGc+D82KghCqiGxfMzDCIQQLKJoKYxhElCW8vd8D3lwrjfL50T8TMNqtViwbRtv6h2cnF9C1xRQSpcseIB8zQPngHyPpmD/Ana7XbwWDnE1NlfO8eB+vEdvA1zrmdRWgJIkYVv4iU5viKljwTBIwswwDFiWtbzIwVwvbbFH+y/wpXcERzfBVqqnVeaAO7vfcT2xlgzZKh0eOS2Y4yDv2Ua3ibNBD/L1EIqqMt5GMYG0KJzF248SROkyY2gkDDcHTz8MwwQw9G7gTV3Yjs39Gb/DAUulUto2nj/HYhHhfyyv8ObTy1P+A0WbwP3JngUlAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Sweep phase diagram&quot;
        title=&quot;Sweep phase diagram&quot;
        src=&quot;/static/1f9fe0881146bbcc72f73b5660e72f4a/5a46d/006Sweep.png&quot;
        srcset=&quot;/static/1f9fe0881146bbcc72f73b5660e72f4a/1db73/006Sweep.png 75w,
/static/1f9fe0881146bbcc72f73b5660e72f4a/8a4e8/006Sweep.png 150w,
/static/1f9fe0881146bbcc72f73b5660e72f4a/5a46d/006Sweep.png 300w,
/static/1f9fe0881146bbcc72f73b5660e72f4a/fc2a6/006Sweep.png 450w,
/static/1f9fe0881146bbcc72f73b5660e72f4a/0a47e/006Sweep.png 600w,
/static/1f9fe0881146bbcc72f73b5660e72f4a/3cd52/006Sweep.png 857w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;strong&gt;Compact phase&lt;/strong&gt;
&lt;p&gt;
The current memory block contains a fragmented distribution of Objects. During the compact phase, the physical memory address of each Object is changed. The Objects are rearranged in the memory block so that they are aligned next to each other. If a compact phase was not executed, Objects would need to fit within the fragmented spaces of the memory block if they are to be stored. Following the compact phase, the memory block has an open space to add Objects as they are assigned to the heap, thus allowing the full memory block to be utilised.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8677fe00b4b8f3369e575fd7b2800095/3cd52/006Compact.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAACHElEQVQ4y41TXWsTQRTNTxGC4FP9QwqK0Fd/gc/9A74IxQdr0NjmCxoVC4qgBAmktDHbTX1om8Rkk93NfuZjP2aOc2ez67YoesPN7MzcPXvOmTsFXdexWq2wWCxkLpdLOI6D0WiEyWQCTdOypLXxeAzLsmTd5h1Oz57nsXK5XCzQ4s0IohBqX8VJ9xSd42N0v3fRU3o4PRXzTgfz+TxfzuUf56xerxcLAhmbBYCJh3hTwjflMf4VsjKOY1atVosF3/eznfZVD6/a7/D27CsOOkdoG+cof3uPQ/ULLl0NURBivV4jCIJ8chqjKGKNRkMw9D1BLKHz+NNT3C4/xN3X29h6s407ew+w9fIRbu3dw5PPuwjcJXTDgGmaWQr5nM5BgLJms3md4XxmSNmuYcGempgNJ5hrOrSrn1i6vrRFMCF5WYo5Z4xJybVaLfHQtBYIo1iaHYtNU4xc/GIWw7Lt//ZQAjrihReHPXT7Axj6FEywICnEJgxD2SLpoaXJOMvPrwOqqornBy1cDHX5MdonT1J5ecD8mNFL4jegoih4tt9B73wEz7VhCNMpKW4CEjOK+x928LHfhqNLa9IvJH1IgLv7LQzGSbOSh6kcaoe0idM1iiOlhbOLHxgOhpjOpvKUhU2sUqkUC8SAPDxRLuE6lmSXtgQVpjcpDxh44tp5PmxbKpKAYmSlUilpG2+xFvL+fiUy/5IJ+B9OOZX8C0kdw6FV0zGyAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Compact phase diagram&quot;
        title=&quot;Compact phase diagram&quot;
        src=&quot;/static/8677fe00b4b8f3369e575fd7b2800095/5a46d/006Compact.png&quot;
        srcset=&quot;/static/8677fe00b4b8f3369e575fd7b2800095/1db73/006Compact.png 75w,
/static/8677fe00b4b8f3369e575fd7b2800095/8a4e8/006Compact.png 150w,
/static/8677fe00b4b8f3369e575fd7b2800095/5a46d/006Compact.png 300w,
/static/8677fe00b4b8f3369e575fd7b2800095/fc2a6/006Compact.png 450w,
/static/8677fe00b4b8f3369e575fd7b2800095/0a47e/006Compact.png 600w,
/static/8677fe00b4b8f3369e575fd7b2800095/3cd52/006Compact.png 857w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h5&gt;Copying garbage collector&lt;/h5&gt;
&lt;p&gt;
The copying garbage collector uses two memory blocks: &apos;fromSpace&apos; and &apos;toSpace&apos;. During each garbage cycle, Objects will be stored in one of the blocks; the block used for storage is known as the &apos;fromSpace&apos; block. When the &apos;fromSpace&apos; block becomes full, a mark and sweep will be performed by the garbage collector, moving the live Objects into the &apos;toSpace&apos; memory block. The &apos;fromSpace&apos; memory block will now be empty and will be referenced as the &apos;toSpace&apos; memory block for the next collection cycle.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0c6824987ace8be4f4167f9a3d522f21/f2793/006Copying.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 77.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABcRAAAXEQHKJvM/AAACZ0lEQVQ4y4VUSY7TQBT1DbgIYs8tOAIbDsBRWLBg0wgJaBALskBBCBGJTeZOdzpxHDJ0O4OnOLZjZ3AS2/WoX3FaCaHhW19WVf16//2pJI8LuERRxJrNJgqFAkqlEorFIur1OiqVitjL5/PIZrNir9vtIpfLoVwuo1qtotPpYC+S4zgCMEkStlqtQPgTewLDMDAejzGdTuH7PoJ5AE3TEIYhyG42m8GyLNi2jeVyKcAYY5Bc1/XSBdt78WwHiqKg2WhgeKPyQ5wIAff7faiqis1mcwqYsERccwIPlV91WIYJzdCRly8wmlmI4khcoG+9XgtW9Ce2i8VC/I8AoyQWgN/UMh6ePUXm7Ue8ePcKD86e4NGHZ7DmDnaOmQh1NBpB13XBcDAYiHTwtB0wTFKGvofGTRvrVYhZ4ENbTHHRaSBO4l1YuwJiu92eqGDoODvA1BbbzRbLYHGUL39XN3EB7C8JPRBpHsy8mmKg3ta4ZQLHdTGZTJCyFiGaPMR9zXiud7m8RyV3anrvv7aQySksXPrQeTGCILhjREoO2B/MDtkenkkt+dp7/eknCvXRvbFYKUNip81thNv1kcNDUKnXaXkvzwv4/ENhC9+BaVqCETUsKYFRc5OswhUef3mO70oRw+4tX4dHwCLkWq3qla86GJs+xSAqGMfxke69UzO/yZzj8voKXT5u1DaHDAWgrhtuGlmSTsuJ7vd5I7PLcpWpt7dMHQwYj0CcUcvtbSQ+p8Fdz/xHqNeo4jTr7XZbNDc9KPRg0JqmRWq1WkX+ECi805s8BPlfym3kXq8n80fjSGl/OBzK3IH8GyvjaiFP34mnAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Copying diagram&quot;
        title=&quot;Copying diagram&quot;
        src=&quot;/static/0c6824987ace8be4f4167f9a3d522f21/5a46d/006Copying.png&quot;
        srcset=&quot;/static/0c6824987ace8be4f4167f9a3d522f21/1db73/006Copying.png 75w,
/static/0c6824987ace8be4f4167f9a3d522f21/8a4e8/006Copying.png 150w,
/static/0c6824987ace8be4f4167f9a3d522f21/5a46d/006Copying.png 300w,
/static/0c6824987ace8be4f4167f9a3d522f21/fc2a6/006Copying.png 450w,
/static/0c6824987ace8be4f4167f9a3d522f21/0a47e/006Copying.png 600w,
/static/0c6824987ace8be4f4167f9a3d522f21/f2793/006Copying.png 743w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Incremental garbage collector&lt;/h5&gt;
&lt;p&gt;
An incremental garbage collector does not look at all the Objects during a collection; the cost of the collection is therefore reduced. Similar to the copying garbage collector, the incremental collector will use different memory blocks to divide the Objects that are to be reviewed based upon certain characteristics.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Generational garbage collector&lt;/h5&gt;
&lt;p&gt;
A generational garbage collector is a form of incremental garbage collector. Generational collectors aim to improve the efficiency of a mark and sweep cycle by dividing Objects into sets (generations) according to when they were last used. As an Object survives a mark and sweep cycle, they are promoted into an &apos;older&apos; generation which are reviewed less frequently for collection. Different generational garbage collectors will implement rules that determine when an Object is promoted and when generations are reviewed by the cycle. Once an Object is moved into the older generation, the younger generation is cleared to allow new Objects to be assigned memory. 
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;How the Java Virtual Machine uses garbage collection&lt;/h5&gt;
&lt;p&gt;
The JVM uses generational, copying, and mark and sweep garbage collectors strategies. The JVM garbage collector consists of two generations: young and old. There are two types of garbage collections: &lt;strong&gt;minor&lt;/strong&gt; collections that occur within the young generation, and &lt;strong&gt;full&lt;/strong&gt; collections that occur across both generations.&lt;br&gt;
The young generation has 3 different memory spaces within it: &lt;br&gt;
&amp;#8226; &lt;strong&gt;Eden space&lt;/strong&gt;: where new Objects are directly allocated memory.&lt;br&gt;
&amp;#8226; &lt;strong&gt;Survivor space 1 and 2&lt;/strong&gt;: live Objects are assigned into one of the spaces following a minor collection. The two spaces are used for copying garbage collection and memory defragmentation.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Garbage collection cycles within the JVM&lt;/strong&gt;&lt;br&gt;
The example below shows the movement of Objects within a JVM garbage collection:&lt;br&gt;
1. New Objects are assigned to the Eden space upon creation.&lt;br&gt;
2. When the Eden space is full, a minor collection is run using a mark and sweep. &lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e603bf1e2efff48a6787a9a501af1c57/4d383/006Cycle1P1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABmElEQVQoz4VSy0rDUBTM3/lDLty4dieIKxeuBBeufeBGEEHcFUpBIm3atGma96u5aZpHx8zRlC4EbxhOcnPvnDlzjrZareA4DhjX6zWKopA4Ho8Fs9kMhmFgPp9jOp3KO2MURdhsNlBKyZ0eWpqmQkD0a7fb7ZMsl0shZXRdF7ZtS6yqCn8tTQh+Hx4iETP/t4IgQFmWaJoGdV2LCEIrcoU8zlCXHZnrwDRNKYvqkiRBGIYSCVax3W6FiN9MTBGMvQjt8esdR3fHuB7cI1h5CMIAcRyLT57nwbIs8YvEvJRlGfI839vEs23bin+i8PXjDSdnp7h/fkChCpFPFfSKhFRKQioiGf2kh6yE/3RdF8VMJoSfgyHOry5w+XILlXYZk1gys6u+70szojjae9aDBIdE7LYQJnGChbOEGdhQnZ9UQfTd9BwXKlnLHgn6Cgj6R3Cf3/suH47LYeRKK4WRb6CtG7GA5ZfdZSpikn4WSSqE/YY6GE6Ce03Xed0zcTN4gmPZGA6HGI1GPzZ0TVosFuLpZDIRcKa/AeKZ9VusDplfAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Eden space full&quot;
        title=&quot;Eden space full&quot;
        src=&quot;/static/e603bf1e2efff48a6787a9a501af1c57/5a46d/006Cycle1P1.png&quot;
        srcset=&quot;/static/e603bf1e2efff48a6787a9a501af1c57/1db73/006Cycle1P1.png 75w,
/static/e603bf1e2efff48a6787a9a501af1c57/8a4e8/006Cycle1P1.png 150w,
/static/e603bf1e2efff48a6787a9a501af1c57/5a46d/006Cycle1P1.png 300w,
/static/e603bf1e2efff48a6787a9a501af1c57/fc2a6/006Cycle1P1.png 450w,
/static/e603bf1e2efff48a6787a9a501af1c57/0a47e/006Cycle1P1.png 600w,
/static/e603bf1e2efff48a6787a9a501af1c57/4d383/006Cycle1P1.png 1195w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
3. Objects that survive the mark and sweep are assigned into one of the survivor spaces. Below, 
the marked Objects from the Eden space are moved into the Survivor 1 memory block.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a1058f35346ea5b03ccc47c6e648ffee/4d383/006Cycle1P2.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABfklEQVQoz4VSSUoEQRDsX/oPf+Af/IZ48OpN8CIiNLiMONM9vdj7vkzvYUdCDS0IFgRZlWRGRkWV5vs+PM8TRFGEtm0FQRBgt9vheDzCMAyJtm3jcDjANE24rit1TdMIVJ9WliWKokBVVZJQi3nbseG4jhDt93uEYSh7Dk/TFH8tbRiGXwkWsvG/RVV5nmOeZ4zjKFEIqS7LMiHquk7UZkmKD8/Ao6nj2XpHlMSimASs6fsedV1LjoJ45oBpmqCRiN7RSyaKtWjuRlw/3eDi7gqX99f4ckwUWS7DaAuJCNrEeDqdzsQayWiwZVlSTOkkrrICZZShTHJ5IKojAQezno/DHu5pEa8thHEcS4KF6ioESTx/fX3+ghXpagsVUgkVbUGys0IWKR/pA1WQUNngBz7yKEVd1ajWvGregkT0lrc7v/KyLJJgJLbrNTERVinaukGwDlG3oBD1DwkO0+ibmsqoIJOHHpgW3H4+4MV4w7flQNd1+eSO42DrPz8/zz/c5/nQ8zjjwAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Live objects into survivor 1&quot;
        title=&quot;Live objects into survivor 1&quot;
        src=&quot;/static/a1058f35346ea5b03ccc47c6e648ffee/5a46d/006Cycle1P2.png&quot;
        srcset=&quot;/static/a1058f35346ea5b03ccc47c6e648ffee/1db73/006Cycle1P2.png 75w,
/static/a1058f35346ea5b03ccc47c6e648ffee/8a4e8/006Cycle1P2.png 150w,
/static/a1058f35346ea5b03ccc47c6e648ffee/5a46d/006Cycle1P2.png 300w,
/static/a1058f35346ea5b03ccc47c6e648ffee/fc2a6/006Cycle1P2.png 450w,
/static/a1058f35346ea5b03ccc47c6e648ffee/0a47e/006Cycle1P2.png 600w,
/static/a1058f35346ea5b03ccc47c6e648ffee/4d383/006Cycle1P2.png 1195w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
4. When the Eden space becomes full again, the minor collection will be performed on the Eden and Survivor 1 space. The Objects that survive the collection are moved into the survivor space 2.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/76a187a05a01e86ff350ee05bbb01a7e/187fa/006Cycle2P1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABvklEQVQoz4VSuW7bQBBll8/yB6TxN6R0H+Qz1NswkC6uUqdJERsuAx8CAsuRRHJ5iOdyeUqijhe+CSi4CJAFHnZnsDPz3sxYURRhtVoJsixD13WCOI4xm83gui5s25bb8zwsl0s4joMgCORf27aCMc6qqgpFUaAsS2y3W4yHfs/3JJCJFosFkiSRN4trrfGvY/V9fzKOx6N8jIfA/x2yMsbgcDhgt9vJLQnJpKormKrEZrMRpjrXmMcKP/0XOKGC0YUwZgL+IYm6rsXHN5WxwH6/h5XlOe5/3OLu23dsujWargX6Ayb3Nzi7vsDk5hK+7SLXuRRjIFvUNI2AvvV6fUpsZXGCy89X+DD5CBUNjW5a6ELDdxQCW+H58Qnz+VzYkRV7Stv3fYFSChwsZUvCNElx/uUT3l29x7P3Cp1kMKWB4zpwh88cQBAGyPO/DMmEjCid4JtSTwyLofIsdvA7ckUuJVFKmqYIw1DWx6QaTVWjHvxkwuCxl0xC0OZgrN2bKdNBcNpvz4tWyBoj7aA8FmQSDmXcQ4LFLC5jT/39VirSObKgD/sjvr7e4sH+hVD5mE6nsthMRhXjnnL5af8B5/fxaOuOe3UAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Mark sweep survivor 1&quot;
        title=&quot;Mark sweep survivor 1&quot;
        src=&quot;/static/76a187a05a01e86ff350ee05bbb01a7e/5a46d/006Cycle2P1.png&quot;
        srcset=&quot;/static/76a187a05a01e86ff350ee05bbb01a7e/1db73/006Cycle2P1.png 75w,
/static/76a187a05a01e86ff350ee05bbb01a7e/8a4e8/006Cycle2P1.png 150w,
/static/76a187a05a01e86ff350ee05bbb01a7e/5a46d/006Cycle2P1.png 300w,
/static/76a187a05a01e86ff350ee05bbb01a7e/fc2a6/006Cycle2P1.png 450w,
/static/76a187a05a01e86ff350ee05bbb01a7e/0a47e/006Cycle2P1.png 600w,
/static/76a187a05a01e86ff350ee05bbb01a7e/187fa/006Cycle2P1.png 1194w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The Objects are compressed in survivor space 2. They have a number assigned to them which represents the number of cycles they have survived.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/6458eb82ac7cef66eb7a6267d8ddbf35/187fa/006Cycle2P2.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABlElEQVQoz4VSy0rDUBDNb/hNbl37C279Avdu/QhdiqIgglCKC1sQaYWkTVrTNs07N71N2hxzRm6oIHjhMJNh5twzJ9daLBbwfV8QBAHKshQsl0uMRiNMp1M4jiPR8zzYto3JZIL5fC59SimBmbOyLEMcx0jTVArmsD6bzWTQEK1WK8l5eRRF+OtYVVX9KrCRg/8dqkqSBPv9HnVdSxRCKqNCEmmtRWkYhoiTGGEUdmCdBOzZbrfI81y2oCB+84LdbgeLRPSOXrLARlUqROuWdNUSBa0dQYQsSTtbSFQUhYD5ZrPpiC2S0SfXdaWZ0gnbd9H7HOBp/IrHUR9e4EMVSvwz3hLMaRHXFsL1ei0FKmQhzTNANzi+PsfR1QnO7i9xenuBF2cAXZTQbQ8VcXWCOck6hfTF+EgfspZQqw3uPnt4GPehoxx1UgoZ7aAtHD4EiUjOzbq/3DSNFEw8PO/hBEEeo2xX5ibGPwox75CgUou+GckS65+oq3a1rQZ2DW7Gz3hzPvDlzjAcDuWR07tD//n4+f0NrZv2ort6sKoAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Copy into survivor 2&quot;
        title=&quot;Copy into survivor 2&quot;
        src=&quot;/static/6458eb82ac7cef66eb7a6267d8ddbf35/5a46d/006Cycle2P2.png&quot;
        srcset=&quot;/static/6458eb82ac7cef66eb7a6267d8ddbf35/1db73/006Cycle2P2.png 75w,
/static/6458eb82ac7cef66eb7a6267d8ddbf35/8a4e8/006Cycle2P2.png 150w,
/static/6458eb82ac7cef66eb7a6267d8ddbf35/5a46d/006Cycle2P2.png 300w,
/static/6458eb82ac7cef66eb7a6267d8ddbf35/fc2a6/006Cycle2P2.png 450w,
/static/6458eb82ac7cef66eb7a6267d8ddbf35/0a47e/006Cycle2P2.png 600w,
/static/6458eb82ac7cef66eb7a6267d8ddbf35/187fa/006Cycle2P2.png 1194w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
5. The JVM can define a number of cycles an Object must survive before it is eligible to be promoted into the old generation. In the example below, 3 cycles are used as the promotion value. When the survivor and eden space are full, Objects that meet the criteria for promotion are moved into the old generation. The old generation Objects will no longer be reviewed by the minor collection.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/77d3de88644a45ff3b384a575799215d/4d383/006Cycle3P1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABwUlEQVQoz4VSu47TQBT1F+038Bms+AFqmu1YOhpKtBLFVlQIrbZYilQgKqi2iciStRLHjuM4dvyI3484hzl3GVdIjHU0d2Z8z5xz7xi+72O73cLzPOz3e1RVJdjtdpjP57BtG8vlUub1ei3xarXCZrNBWZYjdJ6RZRnSNBU0TQM9uO+6riQ+ES0QBIHEFBDHMf41jK7rJDjxO53kRyb+b1AVRQzDgL7vZRbCw+GAOi/R5pUo5DqKIiRpIuQa3Ncu2rZFURTigoK45gXH4xFGFEd49fU9Xn+5Qlc1KMpCDiNVz0DVMQr3giw5CCnPOOd5LuAldV2PxAaLf359gbN3z2EHG5HdH3u4voeZ+Ru/zAdMzRmc0ENVlFJTx3FGsEnkoG0hNAMHLz5f4uL6LRaxi6asMTQ9zm/f4NmHl/g0ucXV3Ud8s+7RFjUalURFtE4wJtmoMFGSvdDHYrdGWmRig6QT8ycmjz+Qx8pmkglZXuRSJybrWmoirtkYo//bZen08NRp3TE9HiIbQR6LZb5bXkoSXVMNKjX4GMW/Iu76TmKi6ZS1Vr1LxX3z+B331gye42I6ncKyLOlwGIbjO+Ue138AI7bv7IJWVnEAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Mark sweep survivor 2&quot;
        title=&quot;Mark sweep survivor 2&quot;
        src=&quot;/static/77d3de88644a45ff3b384a575799215d/5a46d/006Cycle3P1.png&quot;
        srcset=&quot;/static/77d3de88644a45ff3b384a575799215d/1db73/006Cycle3P1.png 75w,
/static/77d3de88644a45ff3b384a575799215d/8a4e8/006Cycle3P1.png 150w,
/static/77d3de88644a45ff3b384a575799215d/5a46d/006Cycle3P1.png 300w,
/static/77d3de88644a45ff3b384a575799215d/fc2a6/006Cycle3P1.png 450w,
/static/77d3de88644a45ff3b384a575799215d/0a47e/006Cycle3P1.png 600w,
/static/77d3de88644a45ff3b384a575799215d/4d383/006Cycle3P1.png 1195w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/cc6ba051239c80759ee97dc9743b48ec/187fa/006Cycle3P2.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABvElEQVQoz4VSyYoUURCsX/Bv/AqP+juCRz9APc3dU48exIPiwQ1UGOhBbWaquvbqrn3fu8IXidUoCCYkyQtexouMl9rhcEAQBPB9H1EUoW1byePxiN1uB8uysN/vpTqOA8MwYJomPM+Te03TSK59WlmWyPNcksAaxB3Xgeu5sGwbuq4jDEMhpYA0TfGv0MZx/AvgRTb+L6iKIk6nE6ZpkiqERVEgyzIh6vsePKdJCjv08dMzsPP3+Gxu4cYBkjjB0k3AtKCsKpmCgoZhkAfmeYZGInpHLwnkZQGMC+6/fAjt8V3ceXIPD149wuvrDzhaHi4+bvDi6h3Gtkf1m7TrujOxRjIabCuf6OF8mgGl/q3xFU+/bfD8+g30Wx36zS2qrMCzLxtcfLpEHIRwXVeSFnFsIYzjWAAqJFDXNapavZzk6JISTVKoz1FNUYhMeSahpu76TiyiOpKdFdKz1Uf6wDFISgIv8OEfAhRxikZhxAfV2A+9NJNkJSI5P+b8y8uyCMDK/DO+JyaiKkNbNzKJECsSCln3kEmlGn1bJbOuKS+PAzAv2Ny8x9X+B3zbxXa7lcXmZ6z+cze5/Dz/AnlC8fJO7mc8AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Copy into survivor 1 and old&quot;
        title=&quot;Copy into survivor 1 and old&quot;
        src=&quot;/static/cc6ba051239c80759ee97dc9743b48ec/5a46d/006Cycle3P2.png&quot;
        srcset=&quot;/static/cc6ba051239c80759ee97dc9743b48ec/1db73/006Cycle3P2.png 75w,
/static/cc6ba051239c80759ee97dc9743b48ec/8a4e8/006Cycle3P2.png 150w,
/static/cc6ba051239c80759ee97dc9743b48ec/5a46d/006Cycle3P2.png 300w,
/static/cc6ba051239c80759ee97dc9743b48ec/fc2a6/006Cycle3P2.png 450w,
/static/cc6ba051239c80759ee97dc9743b48ec/0a47e/006Cycle3P2.png 600w,
/static/cc6ba051239c80759ee97dc9743b48ec/187fa/006Cycle3P2.png 1194w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
6. When the old generation eventually becomes full, a &lt;strong&gt;full&lt;/strong&gt; garbage collection is run. The full collection will scan both generations, perform a mark and sweep and remove dead Objects from the young and old generations. 
&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
The JVM garbage collector assumes that Objects either live for a short time, or they live &lt;i&gt;forever&lt;/i&gt;. By dividing the memory blocks into generations, the memory management system can utilise minor collections to identify and remove short-lived Objects while only using stop the world events sparingly during full collections of the old and young generation.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Garbage collectors introduce different types of mechanisms for allocating and freeing memory within an application.
There are many variables that are involved with garbage collection that require consideration. The many types of implementations also allow the garbage collector to leverage different styles to efficiently manage memory. The JVM garbage collector process covered in this blog post is the default serial garbage collector. The multi-threaded capability of Java can enable concurrent garbage collectors to minimise the use of stop the world events. There are three other collectors that can be used in Java which offer various advantages and drawbacks for application behaviour: G1, parallel, and CMS.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Resolving hash collisions]]></title><description><![CDATA[Understand how hash functions are used with hash collections and the different strategies for resolving hash collisions.]]></description><link>https://aneesh.co.uk/resolving-hash-collisions</link><guid isPermaLink="false">https://aneesh.co.uk/resolving-hash-collisions</guid><pubDate>Mon, 04 May 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Hash tables and hash maps are efficient data structures to be used for data that requires dictionary operations.&lt;br&gt;
&amp;#8226; Collisions occur when a hash function computes to the same value for different keys.&lt;br&gt;
&amp;#8226; Collisions can be resolved with open addressing and closed addressing techniques.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What are hash functions and why do we use them?&lt;/h4&gt;
&lt;p&gt;
Hash functions are used to map data of any arbitrary size into a fixed length. Java uses hash functions in hash maps and hash tables as key-value data structures.&lt;br&gt;
Hash map and hash table are both efficient data structures to use for dictionary operations (insert, search, delete). There are just a few differences between hash map and hash table:&lt;br&gt;
&amp;#8226; Hash map data is not synchronised, hash table data is synchronised.&lt;br&gt;
&amp;#8226; Due to synchronisation, hash map operations can be performed faster than hash table.&lt;br&gt;
&amp;#8226; Hash map allows one null key and any null values, hash table does not allow any null keys or values.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
As the differences between a hash maps and hash tables are negligible, the remainder of this post will use hash tables for consistency. The context of hash functions are equally as applicable to hash maps.&lt;br&gt;
Hash tables store data in a key-value pair that use a fast-access location known as a &quot;bucket&quot; for storing the value. Buckets are stored in an array; when an entry is made to a hash table, the position in which the value is stored within the array is calculated through the key of the entry. Hash tables can be designed for &quot;direct-addressing&quot; where each key is an integer and is directly used as the index position for where the corresponding value is stored in the array. Direct-addressing can create a problem as the size of the array used for storing keys can become excessively large. The number of keys stored within the array may be relatively small thus meaning the majority of the array space (and computer memory) assigned to the hash table is not utilised.&lt;br&gt;
Hash functions provide a solution to direct-addressing by reducing the array size for storage, but keeping an appropriate number of open buckets to store values. Each entry will use the key as an input to a hash function which will produce the index position to the bucket that the value is stored in.&lt;br&gt;
&lt;p&gt;The hash function is defined by the &lt;code class=&quot;java&quot;&gt;public int hashCode()&lt;/code&gt; method of the key. The example below demonstrates how a key-value pair of peoples names and age {String, Integer} can be placed within a hash table; H({key}) is read as &quot;hash of {key}&quot; and describes the use of the hash function to obtain the index position.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 65.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAACmUlEQVQ4y01TS2tTURC+v08Quq8bNyK48ZVCFRS1KFLsQmmhikjpQgXRha6KC1HUapImNzdNm6ZNzLN5P26Sk5v7OJ8zc03jIZOTzDnznW++mTFi6aL68KWgdnaL6uO3glirYyteoxHbSFUqFVUul9VwOBT/dDpVp6enKpFIqFwux2YfHR3h5OTkmvFmJ4eF27u4uWHh0moCF1aiqLUVeE09gEBweHiIeDyOwWAg/kCTfzRBsVhEKpVCNpv1u90u2u32ktHtDXR/6OjuQOm+PaHfE+37HKLJoEulki4UCme753maF58RU02g7HeDIECr1YoYG+/SuL5uYfm5hQdbB8Qyjt39dsjQDeD7Pmq1Gur1OmzbDhnSe54fyJO+7zG4x4CNRiNiPH1r4fKTBO692kdkI4XF+7/mgHSXJEMmkxEjHcVPGQggfUiGvnYcRwCbzWbE2HyfJv1MLG+msLK1jytrMfzOzAAD0kuDAsQoRWEcLi3fkwlL5M8BH28nsbgSw411E1efmVi49QNfzaZcdihlh0ComiJ+MplkAIFyXF8YdjodTT4WNkx569MB7rxM4+F2BquvD4ipib1s50wr13UxHo9BxQAHhSwDhHXh9H2ukcfMheHdF3GcX/6Ji4+ipOUezi19x+dYPUzH1XSpBdM0hWE0GpXd/ceOFOG24mrPUy5WOyg1FOodhXJjiEy+hb6tEPiesPnfuEC8c9qhqZmYc4bOZIzZch26ELioVSsoFP6wPuj1eqBpkbQpANVqVcC4haZTVxgyIEsiGnL1ZmtKeg0puEGB3Hc8GRzIzBiEp4GC5D8/wmy50RmQ4+k8YhwfH4PmUl5mozGSkaLJQD6f5/kU43s0EeKj2T07oxhNje9xj1qWFfkLcLCyWdkyfxAAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;hash table visualisation&quot;
        title=&quot;hash table visualisation&quot;
        src=&quot;/static/fd3f25bb6e4e1528088737911d9115b0/5a46d/005HashTable.png&quot;
        srcset=&quot;/static/fd3f25bb6e4e1528088737911d9115b0/1db73/005HashTable.png 75w,
/static/fd3f25bb6e4e1528088737911d9115b0/8a4e8/005HashTable.png 150w,
/static/fd3f25bb6e4e1528088737911d9115b0/5a46d/005HashTable.png 300w,
/static/fd3f25bb6e4e1528088737911d9115b0/fc2a6/005HashTable.png 450w,
/static/fd3f25bb6e4e1528088737911d9115b0/0a47e/005HashTable.png 600w,
/static/fd3f25bb6e4e1528088737911d9115b0/0b6f4/005HashTable.png 984w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
 In other implementations of the hash table, the key and value can be completely different types of Objects. 
&lt;/p&gt;
&lt;strong&gt;The benefit of using a hash table&lt;/strong&gt;
&lt;br&gt;
&lt;p&gt;
Hash table resolves the time-complexity problem for searching, inserting, or deleting a value within an array when the index position is unknown. If an array has 100 integers, a for-each loop would result in up to 100 searches, and a time-complexity of O(n) - learn more on the time complexity &lt;a target=&quot;_blank&quot; href = &quot;https://aneesh.co.uk/calculating-the-time-complexity-of-algorithms&quot;&gt;here&lt;/a&gt;. Hash tables support the direct searching of a value based upon its key. As a result, the hash table can pinpoint the index of the value instantly, regardless of the number of elements within the hash table. Hash tables support dictionary operations with a constant time O(1).
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Hash function collisions&lt;/h4&gt;
&lt;p&gt;
The example above does not explain how the hash function actually derived the Integer value from the String. The below image shows the formula within the hash function:
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABcRAAAXEQHKJvM/AAABwElEQVQoz11SPY/aQBD1T02TKkXa/INIaSJdd20qFIU2xUWnu6spQgABNhjDnQGDDdj4A3+/2zecrehGGs3uvtm3M29Wm5prdP7M8eO3gV/3JvwgRBTFOBwO2Gw2cBwHu91Oou8HiOJEIs+Ox6PknU4n7Pd7de5D+6c7+PT9Lz7f9PHldoAgLlBWwEElW5YlF7bbLZZLSz12RqEwkhJbrVYwTVOIdV0XUq1zp+PD1x4+frt69/EZtLKq4bouhsOhXMzzXM6TtEBO1jdLkgRlWbZ7rTfa4OeDje6Tjc79M4YL/0qoygzDUNomcZamqOpaHqqU1/XVSdas6dolCd+46zaymqaiqqraZK6LohAsy7I2r3Fimqc0KlQ1aZYjTelZS3JUGKujRq7riej/t/feUtWFFgQBzuczFouFaDUYDKRV2m7vYjwei47z+RzWcintRgq3bRue50nkD6ALISfISU0mE5mcYcwUuYmLqnbruJjNZop0gtFohBd7LVNmu4ZhYDqdot/vy33+Bp5rXNDiOJbRs+LG+A34IC+zEu4pRdMeO4uiSNY0IWTZ1IWb904tiZGUopOswTgAYozNoPiFXgEDxfHQn2IjtwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Hash function&quot;
        title=&quot;Hash function&quot;
        src=&quot;/static/e1eb1a32d4d2c03249dee4eb4617b64f/5a46d/005HashTable2.png&quot;
        srcset=&quot;/static/e1eb1a32d4d2c03249dee4eb4617b64f/1db73/005HashTable2.png 75w,
/static/e1eb1a32d4d2c03249dee4eb4617b64f/8a4e8/005HashTable2.png 150w,
/static/e1eb1a32d4d2c03249dee4eb4617b64f/5a46d/005HashTable2.png 300w,
/static/e1eb1a32d4d2c03249dee4eb4617b64f/fc2a6/005HashTable2.png 450w,
/static/e1eb1a32d4d2c03249dee4eb4617b64f/0a47e/005HashTable2.png 600w,
/static/e1eb1a32d4d2c03249dee4eb4617b64f/20f89/005HashTable2.png 1213w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
A collision will occur when the hash function computes two different keys into the same index position of the array.&lt;br&gt;
If a fourth entry with a key-value pair of: {&quot;Phillip&quot; , 19} was added to the hash table, the index position would equal 2 if we follow the same hash function:&lt;br&gt;
1. &quot;Phillip&quot; ASCII value = 722&lt;br&gt;
2. 722 / 8 = 90&lt;br&gt;
3. 90 % 8 = 2&lt;br&gt;
The addition of a new key, &quot;Phillip&quot;, will create a collision with &quot;Alex&quot;, which is also stored at index 2. Without a collision resolution, {&quot;Alex&quot; , 47} will be replaced by the key-value pair {&quot;Phillip&quot; , 19}.
&lt;/p&gt;
&lt;p&gt;
As the hash table is populated with more entries, the potential for collisions increase.&lt;br&gt;
&lt;strong&gt;Load factor&lt;/strong&gt; is an important statistic used with hash tables, defined as the &lt;u&gt;number of entries divided by the number of buckets&lt;/u&gt;. A hash table can implement &lt;strong&gt;dynamic resizing&lt;/strong&gt; where the array will be doubled in size when the load factor has reached a certain value. In practice, a load factor of 75% is deemed the upper-bound limit of populated buckets within the hash table array before it is resized. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Resolving collisions&lt;/h4&gt;
&lt;p&gt;
There are two strategies used for resolving collisions:&lt;br&gt;
&amp;#8226; &lt;strong&gt;Open addressing&lt;/strong&gt;: finding alternative locations for storing the collided value, ensuring only one value is stored per bucket.&lt;br&gt;
&amp;#8226; &lt;strong&gt;Closed addressing&lt;/strong&gt; (also known as chaining): storing an arbitrary number of values per bucket by using a separate data structure from within the bucket.&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Open addressing&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;Linear probing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
Linear probing will re-direct the index to point to the next available bucket in the hash table. For the case of &quot;Phillip&quot;, which otherwise would fall into bucket 2, the next available bucket would be bucket 3. As a result, the value of &quot;Phillip&quot; will be stored in bucket 3. If another key is added to the hash table with a hash function of 2, the value will instead be stored in bucket 4. 
&lt;/p&gt;
&lt;u&gt;Cluster effects&lt;/u&gt;
&lt;p&gt;
Clustering in hash tables refers to the degree that items are &quot;bunched&quot; when stored. A high degree of clustering is likely to increase the chances of collisions, and as a result, can result in less efficient storage of data. &lt;br&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAABcRAAAXEQHKJvM/AAABH0lEQVQoz62SyY6FQAhF6/9/0IULdeE8z1OUl0OCeXand01yA1TBhaJweZ7Luq4yz7Msy/KAs2mapGka6bpO2rZV+2ccsDNi3DiOglzXJcdxPDjPUwk9z5MoiiQIAvF9X/q+11juDfgIeY4ABE0lqmADigHsqqq0U7oEdV0/2jh41UNI8LZtqodhUCI6ROOnaarJhqIoHk0TCPmOYGuXJApAat3Y7KxzsyEiF3/f99+E/yEvwvu+X0ConCSJIgxD/Rh+8694JbQZMq/vZ9pTWassy3QcaOZma2SwTXkRWhA+v4VPMrOlK2bGGWuCX5al+ty/fplVsefRwXdlCE2zNmYDOreiECEUc8zFEuI41sp0A0gy2BlF8RkBsfjkWv4HbVpNg60gXA4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Clustering visualisation&quot;
        title=&quot;Clustering visualisation&quot;
        src=&quot;/static/045112a4b9f3594217c1772ba2f87da8/5a46d/005Cluster.png&quot;
        srcset=&quot;/static/045112a4b9f3594217c1772ba2f87da8/1db73/005Cluster.png 75w,
/static/045112a4b9f3594217c1772ba2f87da8/8a4e8/005Cluster.png 150w,
/static/045112a4b9f3594217c1772ba2f87da8/5a46d/005Cluster.png 300w,
/static/045112a4b9f3594217c1772ba2f87da8/fc2a6/005Cluster.png 450w,
/static/045112a4b9f3594217c1772ba2f87da8/0a47e/005Cluster.png 600w,
/static/045112a4b9f3594217c1772ba2f87da8/02cd5/005Cluster.png 821w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Linear probing can create high amounts of clustering within an array as buckets become populated near each other. Quadratic and double-hash probing algorithms aim to reduce the possibility of high clustering within an array.&lt;/p&gt;
&lt;/p&gt;
&lt;strong&gt;Quadratic probing&lt;/strong&gt;
&lt;p&gt;
Quadratic probing resolves collisions by using a quadratic equation to compute the new index away from the original hash function index. The following indices would be checked sequentially for the example of &quot;Phillip&quot; (also assuming a much larger size array):&lt;br&gt;
&amp;#8226; 7 + (1*1) = index 8. &lt;br&gt;
&amp;#8226; 7 + (2*2) = index 11. &lt;br&gt;
&amp;#8226; 7 + (3*3) = index 16. &lt;br&gt;
&amp;#8226; 7 + (4*4) = index 23. &lt;br&gt;
&amp;#8226; 7 + (5*5) = index 32. &lt;br&gt;
&amp;#8226; 7 + (6*6) = index 43. &lt;br&gt;
The quadratic probing collision resolution will result in a less dense cluster of values than linear probing.
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 38.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcRAAAXEQHKJvM/AAABsUlEQVQoz3WSy4vTUBTG87/4H7kQBMGdi1FwoS50Vu50wIVbF4KCylBsR4soOjLIQBelram2ZJK201fSNE1rbdLSNMnk5723uvD1weF+59zXdx7abDZjuVwShAFBELBarXBdl9FohOd5OI6Dbdv4vs9kMlH+cDhkOp0ShuFfpsVxzJ9wRg6lUolarUa1WqVcLmNaFnpdR9d1KpWKiv0L2jqKtiyFszgl2kSKZ4LLz9I0JUkSiDOSOCH56Us10WZDLAyxLfeyLEOTzgM9x7ncZV40P7DxQ3aOH/Jc8O/ulG6/x8T1uPTpPsedz/j2WGQwwhap+yL+ZWhx/vAuA1EG+bgmxb2pHnFx7zpPX+8LJWfsPrq35QJJmuCNx1y4c4UnuWcqJmu9WCwUf/fxPbcf723PCuVaLOV/W3DasDC+Nmk0G5waLV69LHBinhCIi4WDAkdvD7l146ZqzC/IB67tXKVW2tZTlkeTef8PUoWcgPV6rXzZ3Xw+z1gons/nFItFDMP4vSky0Ol0aLVaytrttlplrN/v0+v1lJmmqcZnMBhQr9dVt7vdrhojS0yAPC/v/gBSxEZCok+8fQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Quadratic probing&quot;
        title=&quot;Quadratic probing&quot;
        src=&quot;/static/97fa82d4fb39806f675af71ef4cd71ea/5a46d/005Quadratic.png&quot;
        srcset=&quot;/static/97fa82d4fb39806f675af71ef4cd71ea/1db73/005Quadratic.png 75w,
/static/97fa82d4fb39806f675af71ef4cd71ea/8a4e8/005Quadratic.png 150w,
/static/97fa82d4fb39806f675af71ef4cd71ea/5a46d/005Quadratic.png 300w,
/static/97fa82d4fb39806f675af71ef4cd71ea/fc2a6/005Quadratic.png 450w,
/static/97fa82d4fb39806f675af71ef4cd71ea/0a47e/005Quadratic.png 600w,
/static/97fa82d4fb39806f675af71ef4cd71ea/02cd5/005Quadratic.png 821w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;strong&gt;Double-hash probing&lt;/strong&gt;
&lt;p&gt;
Double-hash probing resolves collisions by applying a secondary hash to the computed index to create a new random index. A second hash function can be defined and the subsequent index will be computed upon by alternating between hash functions until an empty bucket is found. 
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 40%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABcRAAAXEQHKJvM/AAAB1UlEQVQoz1WSy48SQRDG+f+8Gm8aLxuNJh7Vva2buBfjQS9efCycvJAQjdGYFfYiypvZRcABZpgBFoZ5McMwIJj8nO6o0U46VfVV15euqi81mUwIggDP81gsFti2jWVZMhbWms9xHEfidmLd37jjOni+j59caZPaefI2FUUR/55VsKRRraOcKRSLRSbGiNbZObVajcH3HkpToVQqUa/WGOnGf7Xb3ZZUHK2wYh/VMdlEsbR9y8R3PXzPp20Pmdgz+ePmhcrUtgj9AGM2pqy3WK9iTHfKYHHB7kdCyBaenb/hzslTiGHvwyMef3lNaHl4c4fL7w44UUvMrBmXcvs0hh1iO+Cj+pUr7w/ZuEueN95y+/QJ/ITUbrdjoGu8ePWSzWbDweEDHh4dyRZ6vR5Xr1/jc9K6qqrcuHWTT/m8zB2nj7l7/x6O7ZAvFMifFiSeCsNQOt9aLTmn6XRKLpejUqmQzWaplMu0220URWHQ75NOp+l0OmQyGbSBRrPZpFqtEietyxn+WUq4DNE07e+ARVE/IRBHqMA0Temv12vq9TrLaCnj8Xgsty6Xsk0IhWzELwUofCERIYXRaISu65JM4IJc5FzXpdvtSkzIzDAMhsOhrBHS+gVXMD38uabkmgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Double-hash probing&quot;
        title=&quot;Double-hash probing&quot;
        src=&quot;/static/d5beade3e49bd8e9772724f4f9f02574/5a46d/005DoubleHash.png&quot;
        srcset=&quot;/static/d5beade3e49bd8e9772724f4f9f02574/1db73/005DoubleHash.png 75w,
/static/d5beade3e49bd8e9772724f4f9f02574/8a4e8/005DoubleHash.png 150w,
/static/d5beade3e49bd8e9772724f4f9f02574/5a46d/005DoubleHash.png 300w,
/static/d5beade3e49bd8e9772724f4f9f02574/fc2a6/005DoubleHash.png 450w,
/static/d5beade3e49bd8e9772724f4f9f02574/0a47e/005DoubleHash.png 600w,
/static/d5beade3e49bd8e9772724f4f9f02574/02cd5/005DoubleHash.png 821w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
Open addressing can enable collisions to be resolved with loose clustering to reduce the chance of repeated collisions. The use of quadratic and double-hashing algorithms, however, increases the compute time of the hash function. When a search operation is performed on a hash table, the computational cost will accumulate through the additional probing algorithms and the degree of clustering within the hash table. Closed addressing offers a resolution to collision hashing which avoids the need for expensive computations to find new index positions.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Closed addressing&lt;/h5&gt;
&lt;p&gt;
Closed addressing offers a more direct approach to collision resolution; rather than finding a new bucket to store the value, each bucket contains a linked list that can be appended upon.&lt;br&gt;
When a hash function identifies a new collision, the value will be placed in the next available position within the linked list of the bucket.&lt;br&gt;
When a search is conducted upon the hash table, the hash function will identify the bucket and list position of the respective value. The addition and retrieval of a value within a linked list is performed at constant time O(1), therefore closed addressing supports the efficient storage of values to the hash table.
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABcRAAAXEQHKJvM/AAABvElEQVQ4y31T25KCMAzl/z/OR2/jiCKUQgvlDoKS7YmWkR13M5MJpMnJSdJ6Ukqq65qKoqCyLBeNooiEEGzDMKTb7UZxHLNPKUVVVS05sF3X0fF4JK/ve/omSEbA+Xxm3e125Ps+7fd7LvBNQM5rmoZ/8jyn5/PJlSCw8zzTNE00jiP7XHEwgmRZxhYdOhIeEkE7SaTVhIwxpLXmbyjaS9OUW4WFL/s4hyIeoEEQkNe2LVccp8dSEQXACnO63+/U9QODPR4PLujYoQOXMwwDj4YZAjBVGc8AIMzCBgIENkm1ZRhRbsFwlpuS20Oes8i7Xq8vQNANY83MIKgMATud5SQSbb8H9iNZJBkDOMHslxmiZRyeLyEz+ARsmppMUVp/RVgeFoTlBaGkzNp5fq7iMWdmaGxV/ypISL0KQDEAFIVhQDDBCMJYrWIdwzdgaxcwUd30drD3FSDsYNvGwF0SYqq6s3b8G1BIRbujb5egEMIBUAf8SiTb8kiRSOhyk3aJ6h038/YheFUerkddN/ZpRcsF/U+MKXhR32JxZ73T6USHw4Gf1na7Xd7tb4UfFxdxeH7uCeIMfly5zWZDP8Lh4tn02ctdAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Closed addressing&quot;
        title=&quot;Closed addressing&quot;
        src=&quot;/static/8b8d22834e8a1116df3e9d4e93e9ca91/5a46d/005Closed.png&quot;
        srcset=&quot;/static/8b8d22834e8a1116df3e9d4e93e9ca91/1db73/005Closed.png 75w,
/static/8b8d22834e8a1116df3e9d4e93e9ca91/8a4e8/005Closed.png 150w,
/static/8b8d22834e8a1116df3e9d4e93e9ca91/5a46d/005Closed.png 300w,
/static/8b8d22834e8a1116df3e9d4e93e9ca91/fc2a6/005Closed.png 450w,
/static/8b8d22834e8a1116df3e9d4e93e9ca91/0a47e/005Closed.png 600w,
/static/8b8d22834e8a1116df3e9d4e93e9ca91/a8e5b/005Closed.png 711w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Hash functions are algorithms that are used to convert a key into an arbitrary integer. Hash functions enable the hash table to have an efficient computer memory consumption by defining the boundaries of possible index positions in which a value can be stored. Collision resolution techniques are important to understand as they support the use of hash functions by maintaining the integrity and performance of the hash table.&lt;br&gt;
By understanding the differences between open and closed addressing, a hash table can be designed to store collided hash values in an optimised manner. 
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Packaging and logging a Java application]]></title><description><![CDATA[Create JAR files to package your Java application and implement logging to provide transparency into runtime activity.]]></description><link>https://aneesh.co.uk/packaging-and-logging-a-java-application</link><guid isPermaLink="false">https://aneesh.co.uk/packaging-and-logging-a-java-application</guid><pubDate>Mon, 27 Apr 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Maven-assembly-plugin enables us to package Maven dependencies into an executable JAR file.&lt;br&gt;
&amp;#8226; We can use Log4J to debug and understand the runtime context of our application as it is run from a server.&lt;br&gt;
&amp;#8226; There are many ways that we can configure the format and layout of our logs to enhance readability.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;Running a Java application with a JAR&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;I have recently been developing applications that perform small and simple tasks to eliminate the repetitive work from my daily activity.
The applications are packaged into an executable JAR file and run on a rota through a batch job.&lt;br&gt;
Each time I create an application, I repeatedly deploy a similar project structure that allows me to include dependencies within the JAR and external logging of the runtime. The JAR is designed to be run from a server and will provide debugging and context in the form of logging to the runtime execution.&lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
We will begin by running a simple Java class from the command line:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from your Java application&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
Once we have navigated to the directory of the class, we can compile and execute the main thread with the following lines:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;javac Example.java

java Example
Hello from your Java application.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
We can achieve the same result if we package our class into an executable JAR and run it from the command line.&lt;br&gt;
Using IntelliJ, you can package your application with the following steps:&lt;br&gt;&lt;br&gt;
1. Select &lt;strong&gt;File&lt;/strong&gt; &gt; &lt;strong&gt;Project Structure&lt;/strong&gt;&lt;br&gt;
2. Select &lt;strong&gt;Artifacts&lt;/strong&gt; &gt; &lt;strong&gt;+&lt;/strong&gt;&lt;br&gt;
3. Select &lt;strong&gt;JAR&lt;/strong&gt; &gt; &lt;strong&gt;From modules with dependencies&lt;/strong&gt;&lt;br&gt;
4. Select &lt;strong&gt;Example Class (or class with the main thread)&lt;/strong&gt; &gt; &lt;strong&gt;OK&lt;/strong&gt; &gt; &lt;strong&gt;OK&lt;/strong&gt;.&lt;br&gt;
5. Select &lt;strong&gt;Build&lt;/strong&gt; &gt; &lt;strong&gt;Build Artifacts...&lt;/strong&gt; &gt; &lt;strong&gt;Build&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
IntelliJ will create a new &lt;strong&gt;Out&lt;/strong&gt; directory in your project structure that will contain the JAR file.&lt;br&gt;
IntelliJ will also create a new folder and file in the &lt;strong&gt;src&lt;/strong&gt; folder: &lt;strong&gt;src/META-INF/MANIFEST.MF&lt;/strong&gt; &lt;br&gt;
The Manifest file contains information about the project structure; the JAR will reference the Manifest file to locate the class with the main thread, thus making the JAR executable.&lt;br&gt;
If we return to the command line, we can now run our class through the JAR file that has been created in the &lt;strong&gt;out/artifacts/{project name}_jar/&lt;/strong&gt; directory:&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;java -jar example.jar
Hello from your Java application.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Injecting dependencies into a JAR file&lt;/h4&gt;
&lt;p&gt;
Our current JAR file will compile the classes and run the main thread as per the Manifest file.&lt;br&gt;
I will now introduce a new Class that uses the &lt;strong&gt;jackson.databind&lt;/strong&gt; dependency to convert a JSON file to and from a POJO.&lt;br&gt;
The purpose of this example is to introduce a dependency into our application; the main thread will create a POJO and return the &apos;firstName&apos; property as per the JSON file.&lt;br&gt;
The pom.xml will include a new dependency:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependencies&gt;
    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;com.fasterxml.jackson.core&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;jackson-databind&amp;lt;/artifactId&gt;
        &amp;lt;version&gt;2.11.0.rc1&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;
&amp;lt;/dependencies&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;p&gt;
The main method is also updated to use the dependency:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;ObjectMapper&lt;/span&gt; mapper &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ObjectMapper&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt; student &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mapper&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;readValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;data/student_a.json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Student&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;First name: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; student&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getFirstName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we attempt to rebuild and run our JAR file with the new dependencies, we will receive the following error message from the command line:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;no main manifest attribute&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;p&gt;
The error arises as the JAR file has not been generated through Maven and does not reference the dependencies.&lt;br&gt;
To resolve the problem, we can use &lt;strong&gt;maven-assembly-plugin&lt;/strong&gt; from within our pom.xml file to include the dependencies and reference the location of the Manifest file:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;plugin&gt;
    &amp;lt;artifactId&gt;maven-assembly-plugin&amp;lt;/artifactId&gt;
    &amp;lt;configuration&gt;
        &amp;lt;descriptorRefs&gt;
            &amp;lt;descriptorRef&gt;jar-with-dependencies&amp;lt;/descriptorRef&gt;
        &amp;lt;/descriptorRefs&gt;
        &amp;lt;archive&gt;
            &amp;lt;manifestFile&gt;src/main/java/META-INF/MANIFEST.MF&amp;lt;/manifestFile&gt;
        &amp;lt;/archive&gt;
    &amp;lt;/configuration&gt;
    &amp;lt;executions&gt;
        &amp;lt;execution&gt;
            &amp;lt;phase&gt;package&amp;lt;/phase&gt;
            &amp;lt;goals&gt;
                &amp;lt;goal&gt;single&amp;lt;/goal&gt;
            &amp;lt;/goals&gt;
        &amp;lt;/execution&gt;
    &amp;lt;/executions&gt;
&amp;lt;/plugin&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
Within &lt;strong&gt;maven-assembly-plugin&lt;/strong&gt;, there are a few key values that set the configuration of the JAR file creation:&lt;br&gt;
We define &lt;strong&gt;descriptorRef&lt;/strong&gt; to the value &apos;&lt;strong&gt;jar-with-dependencies&lt;/strong&gt;&apos;.&lt;br&gt;
The configuration of the &lt;strong&gt;descriptorRef&lt;/strong&gt; will instruct Maven to create a JAR that contains the unpacked dependencies. 
The &lt;strong&gt;descriptorRef&lt;/strong&gt; will subsequently append &apos;jar-with-dependencies&apos; to our JAR filename.&lt;br&gt;
The &lt;strong&gt;execution&lt;/strong&gt; block ensures the &lt;strong&gt;maven-assembly-plugin&lt;/strong&gt; is included during the Maven build lifecycle. Therefore, the JAR file will be updated upon each build.&lt;br&gt;
The Manifest file is defined within the &lt;strong&gt;manifestFile&lt;/strong&gt; property of the archive configuration; the JAR can now find and use the Manifest file properties.&lt;br&gt;
Once we have configured our pom.xml file, we can build our new JAR by selecting &lt;strong&gt;Maven&lt;/strong&gt; &gt; &lt;strong&gt;Install&lt;/strong&gt; in the Maven Lifecycle menu on the right side of the IDE.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Maven Install&lt;/strong&gt; will build a new JAR in the &lt;strong&gt;target&lt;/strong&gt; directory with the dependencies included.&lt;br&gt;
Before we run the new JAR file, it is important to remember that any relative file locations you may specify in the application (such as a properties file) must also be
moved relative to where the JAR file is located, otherwise the JAR will not be able to resolve the location.&lt;br&gt;
If we navigate to the target directory at the command line, we can run the JAR file to successfully include the dependencies and print the result:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;java -jar example-1.0-SNAPSHOT-jar-with-dependencies.jar
First name: Alice&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Introducing logging for our packaged application&lt;/h4&gt;
&lt;p&gt;
Running our packaged application with Maven dependencies is the first step to creating simple and transparent applications, however in a production environment we may find some problems...&lt;br&gt;
&amp;#8226; What happens if the application throws an exception?&lt;br&gt;
&amp;#8226; How can we verify that the application is working as expected?&lt;br&gt;
&amp;#8226; How can we debug our application if any unexpected results occur?&lt;br&gt;
The concerns outlined above can be resolved with logging which will provide us with a view into the runtime activity of our application.&lt;br&gt; 
Logging will create a persisted document from the application that can be referenced for debugging or runtime information. The log file can be generated at custom intervals such as each day, each hour, or each execution.&lt;br&gt;
&lt;/p&gt;
&lt;strong&gt;Log4J&lt;/strong&gt;
&lt;p&gt;
Log4J is a popular package that we can implement into our application to provide a detailed logging context.&lt;br&gt;
We will register Log4J to our application and append records to it throughout the life of the application. Each record will include information or debugging context from the application.&lt;br&gt;
We can format the log files to be conveniently stored alongside the JAR file in a separate directory, named with the time and date for ease of access. 
Log4J will be included in our application with the following Maven dependency in our pom.xml file:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;dependency&gt;
    &amp;lt;groupId&gt;log4j&amp;lt;/groupId&gt;
    &amp;lt;artifactId&gt;log4j&amp;lt;/artifactId&gt;
    &amp;lt;version&gt;1.2.17&amp;lt;/version&gt;
&amp;lt;/dependency&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The runtime configuration for the log is defined in the properties file &lt;strong&gt;log4j.properties&lt;/strong&gt;. 
Properties may include details such as where we log our files to and the format of the records:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;conf&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-conf line-numbers&quot;&gt;&lt;code class=&quot;language-conf&quot;&gt;log4j.rootLogger=DEBUG, consoleAppender, fileAppender

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n

log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender.File=log/${current.date.time}_Application.log&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The above file makes use of the Log4J interfaces to define and create two different logs.&lt;br&gt;
On line 1, the &lt;strong&gt;rootLogger&lt;/strong&gt; defines variables that we will use to assign different logs.&lt;br&gt;
From line 3 onwards, we use the &lt;strong&gt;log4j.appender&lt;/strong&gt; interface to create and design the layout of our logs:&lt;br&gt;
On lines 3 to 5, we are defining a pattern layout for a log to the console.&lt;br&gt;
On lines 7 to 10, we are creating a &lt;strong&gt;File&lt;/strong&gt; for each log. The format for the log is also defined and the creation of the log will append the current date and time of the machine to the filename.
&lt;/p&gt;
&lt;p&gt;
Once we have the properties configured, we can implement Log4J in our main class with a new static method and two lines to instantiate the logger and reference the properties file:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;SimpleDateFormat&lt;/span&gt; dateFormat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SimpleDateFormat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;yyMMdd-hhmmss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;current.date.time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dateFormat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Logger&lt;/span&gt; logger &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Logger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;PropertyConfigurator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;log4j.properties&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The &lt;code class=&quot;java&quot;&gt;static&lt;/code&gt; block at the top of the main class will set the system property we define as &lt;strong&gt;current.date.time&lt;/strong&gt;.&lt;br&gt;
You may recognise the system property from the properties file as it is used to append the date and time for log filenames.&lt;br&gt;
On line 6, we create an instance of the logger. As we may create many different loggers in our application, we name our logger after the class. 
Using the class name helps us to avoid name duplication and also provides descriptive detail in the log file for which logger within the application is being used.&lt;br&gt;
On line 10 we use the &lt;code class=&quot;java&quot;&gt;PropertyConfigurator&lt;/code&gt; which allows our logger to be configured by an external file.
&lt;/p&gt;
&lt;p&gt;
Now that the logger is successfully configured to the application, we can begin writing logger records that will provide information and debugging context to our log file:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt; 
 &lt;span class=&quot;token comment&quot;&gt;//inside the main method&lt;/span&gt;

logger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Starting the application&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

 &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

 &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;IOException&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            logger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;fatal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Unable to find the json file&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;p&gt;
The output of our log file from a successful application run will appear as:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[main]  INFO    Application     - Starting the application&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
However, if an exception is thrown, we may see more detail:&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;[main]  INFO    Application     - Starting the application
[main]  FATAL   Application     - Unable to find the JSON file&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Packaging and logging are both frequent utilities to implement in our applications; they are two important foundations to any new application we build to enable transparency and ease of use to the application.&lt;br&gt;
The example we have used has only touched upon the various message types and formatting that is available with Log4J. I would encourage you to explore Log4J functionality 
 &lt;a target = &quot;_blank&quot; href=&quot;https://logging.apache.org/log4j/2.x/manual/index.html&quot;&gt;here&lt;/a&gt; to enhance your logging to be succinct and effective.&lt;br&gt;
If you would like to implement logging across your application with enhanced control and structure, please visit my blog on aspect-orientated-programming &lt;a target = &quot;_blank&quot; href=&quot;https://aneesh.co.uk/implementing-aop-in-a-spring-application&quot;&gt;here&lt;/a&gt;.&lt;br&gt;
Finally, you can find the source code from this blog via GitHub &lt;a target = &quot;_blank&quot; href=&quot;https://github.com/4neesh/JavaToolBoilerplate&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Implementing AOP in a Spring Application]]></title><description><![CDATA[Apply aspect-orientated programming to a Spring application to support cross-cutting behaviours in a clean and modular design.]]></description><link>https://aneesh.co.uk/implementing-aop-in-a-spring-application</link><guid isPermaLink="false">https://aneesh.co.uk/implementing-aop-in-a-spring-application</guid><pubDate>Mon, 20 Apr 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; AOP enables us to separate cross-cutting concerns from within our application.&lt;br&gt;
&amp;#8226; Pointcut expressions enable fine-grained AOP implementations.&lt;br&gt;
&amp;#8226; Spring AOP makes use of light-weight AspectJ AOP classes and methods.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is AOP?&lt;/h4&gt;
&lt;p&gt;
AOP enables us to apply additional functionality to our application to separate cross-cutting concerns without updating the business logic source code.&lt;br&gt;
A cross cutting concern, for example, may include security, logging, or analytics.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
AOP introduces several keywords during its best-practice.&lt;br&gt;
We will use the below example to describe a simple business logic application.&lt;br&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;playGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Game has started.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
The method &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; is known as the &lt;strong&gt;Joinpoint&lt;/strong&gt;. The Joinpoint is an exact point within the execution of an application that can be defined on the thread. An example of a Joinpoint can be calling a constructor or method, or throwing an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;.&lt;br&gt;
We are able to define different Joinpoints within our application by using &lt;strong&gt;Pointcut expressions&lt;/strong&gt;. A Pointcut expression is a predicate that can match different Joinpoints. We define Pointcut expressions to pin-point certain Joinpoints within the application that we would like to apply our cross-cutting concerns to. If we defined a Pointcut expression for the above example, it would define the package, class name and method signature exactly.&lt;br&gt; In AOP, &lt;strong&gt;Advice&lt;/strong&gt; is the action that is taken when a Pointcut expression is satisfied. Both the Pointcut expressions and the Advice are defined together within an &lt;strong&gt;Aspect&lt;/strong&gt; class. The Aspect class is a handy way Spring can identify if, and where, to find possible AOP implementations to be applied in conjunction with a Joinpoint. &lt;br&gt;You may be wondering when, in relation to the Joinpoint execution, that the Advice method is called. The exact position in relation to the Joinpoint can be defined by the of 5 different types of Advice, illustrated below:
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e616d0996f02a5a3213c52da56f0ca58/e2e58/003Flow.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 114.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAXCAYAAAALHW+jAAAACXBIWXMAABcRAAAXEQHKJvM/AAACo0lEQVQ4y5VV2a6iQBD1/z/CVx98UBO3uO/RiZqMDzoZnQeVOy4guAEq0DV9WkFA752ZTsoGqjjUOVXVRjabjabrOp1OJ3Y+n8m10+m+G/qZ9If5/cHYkwUMSZKiEX6h0X0xCi2HPzmbNlk2o5vFXgOey8LPfr+PRji6AGT3Ra5hmRebRr/29H22p/4PlYyLRY/YsAmHqqrRCE/ZA/R/0n9rcGDHYQGwUOzXgO6lu88+zm8/9FfAMGV3X6uXANA/Zegm6TfnsX/IJqfsCBPPH9c+CYKAeGjb9qdFnK/1t899TJ6AiqJo8XickskkKxaL1O/3aTQaUb1ep0KhQLlcjlQuYbVapUwmQ51OhxA3HA7vrXXP+gl4OBy01WpFmqYx3kd0PB5xjZ4SOw+ilXwkWVbEPfzwVSoVD5CbAOT+oIZemxiGR8fVDYvLQrfbTUxHq9Xy0xaAu93uCchf9IpiWRaNx2NKp9NUq9VENqAPuviAaZoUi8UIUsHX7XatdrtNk8nktW2wMJfIgsuBORX3yBp2vV6Fr1QqCerw8d1CnCzL7ynjBWSBTGGGYdLlchFgAAUIihaeZaEhDgdULZFIsFQqRYvFgpbLpaDXaDR4dSu0VGzq9b5RPp+nwWAgKu4WBboGqgxA3jqgx1BBZAbhQQFV5UKTcrh5Eri0v5wUgKBtQOXd8C82wcZGAmCBD0KSlz7MZrPEjaGSCAYgqgZa5XKZpK0uKt5sNmk2m0Ee0fDCJ0mehh4gNOQvs16vJ/oLk4L2QCFgSw6IbEAZ94iDxjAkMZ1OrUfmd8rQa71eM+i13W5pPp8HKC63RvB45jTdieGtAp1fGzv8F+CdOPxg/a2YFD7a3v0FBI4v/6T4X7Rsh4Y/tZeD97//AsIH6SeZvbTNH8zs1JJRPegSAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Advice in AOP&quot;
        title=&quot;Advice in AOP&quot;
        src=&quot;/static/e616d0996f02a5a3213c52da56f0ca58/5a46d/003Flow.png&quot;
        srcset=&quot;/static/e616d0996f02a5a3213c52da56f0ca58/1db73/003Flow.png 75w,
/static/e616d0996f02a5a3213c52da56f0ca58/8a4e8/003Flow.png 150w,
/static/e616d0996f02a5a3213c52da56f0ca58/5a46d/003Flow.png 300w,
/static/e616d0996f02a5a3213c52da56f0ca58/fc2a6/003Flow.png 450w,
/static/e616d0996f02a5a3213c52da56f0ca58/0a47e/003Flow.png 600w,
/static/e616d0996f02a5a3213c52da56f0ca58/e2e58/003Flow.png 1069w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;• &lt;strong&gt;@Before&lt;/strong&gt; is called before &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; is executed. &lt;br&gt;
• &lt;strong&gt;@After&lt;/strong&gt; is called after &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; is executed.&lt;br&gt;
• &lt;strong&gt;@Around&lt;/strong&gt; is called both before and after &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; is executed.&lt;br&gt;
• &lt;strong&gt;@AfterReturning&lt;/strong&gt; is called after &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; is executed and returns a value.&lt;br&gt;
• &lt;strong&gt;@AfterThrowing&lt;/strong&gt; is called after &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; is executed, and if it throws an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;Implementing AOP step by step&lt;/h4&gt;
&lt;p&gt;
AOP requires two dependencies in our Spring project, we will define them in our POM.xml file:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;    &amp;lt;dependencies&gt;
        &amp;lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-context --&gt;
        &amp;lt;dependency&gt;
            &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;
            &amp;lt;artifactId&gt;spring-context&amp;lt;/artifactId&gt;
            &amp;lt;version&gt;${spring-version}&amp;lt;/version&gt;
        &amp;lt;/dependency&gt;
        &amp;lt;!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --&gt;
        &amp;lt;dependency&gt;
            &amp;lt;groupId&gt;org.aspectj&amp;lt;/groupId&gt;
            &amp;lt;artifactId&gt;aspectjweaver&amp;lt;/artifactId&gt;
            &amp;lt;version&gt;1.9.5&amp;lt;/version&gt;
        &amp;lt;/dependency&gt;
    &amp;lt;/dependencies&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
We will be using the &lt;code class=&quot;java&quot;&gt;spring-context&lt;/code&gt; dependency for supporting Spring components and &lt;code class=&quot;java&quot;&gt;aspectj&lt;/code&gt; for supporting the Aspect class and Advice implementations.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;AnnotationConfigApplicationContext&lt;/span&gt; context
		&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AnnotationConfigApplicationContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SpringConfig&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt; footballGame &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getBean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;footballGame&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		footballGame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;playGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		context&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
We will begin by instructing the main thread to call a Joinpoint called &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;AnnotationConfigApplicationContext&lt;/strong&gt; is used to import the Spring Configuration class that defines the package to scan for Spring components.&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;Context&lt;/code&gt; will be used to obtain a Spring bean to which it will later be used to call &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
The Spring configuration class exists as below:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Configuration&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@EnableAspectJAutoProxy&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@ComponentScan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;com.aneesh.aopdemo&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SpringConfig&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The &lt;strong&gt;@Configuration&lt;/strong&gt; annotation defines the class as a configuration class.&lt;br&gt;
&lt;strong&gt;@EnableAspectJAutoProxy&lt;/strong&gt; enables support for handling Aspect classes.&lt;br&gt;
&lt;strong&gt;@ComponentScan(&quot;com.aneesh.aopdemo&quot;)&lt;/strong&gt; directs the Spring application on where to search for components.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
Business logic is processed in &lt;code class=&quot;java&quot;&gt;public class FootballGame&lt;/code&gt;. This class will begin with a single void method that prints &quot;Game has started.&quot;.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Component&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;playGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Game has started.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The above class includes the &lt;strong&gt;@Component&lt;/strong&gt; annotation to ensure it is scanned by &lt;code class=&quot;java&quot;&gt;SpringConfig&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
We will now build the Aspect class that will hold the Advice and Pointcut expressions to our business logic:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Aspect&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@Component&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; footballAspect &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

	
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The &lt;strong&gt;@Aspect&lt;/strong&gt; annotation informs Spring that there may be Advice within this class to process.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Defining Pointcut expressions&lt;/h5&gt;
&lt;p&gt;
Before we define the Advice, we need to understand which Joinpoint(s) we would like the Advice to be called upon.&lt;br&gt;
When applying our Pointcut expressions, we use the term &quot;execution&quot;. &quot;execution&quot; is the primary method of implementing Pointcut expressions in the following structure: &lt;br&gt;
&lt;code class=&quot;java&quot;&gt;&quot;execution([optional]{access modifier} {return type} {package} {class} {method name} {arguments}&quot;)&lt;/code&gt;&lt;br&gt;
By default, the access modifier is &apos;Public&apos;, therefore it can be left undefined from the expression.&lt;br&gt;
For each of the other input values, you can specify custom values or leave it open with an asterisk (*). &lt;br&gt;
&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;java&quot;&gt;&quot;execution(* com.aneesh.aopdemo.* .* () )&quot;&lt;/code&gt;&lt;br&gt;
Scan all public access modifiers, all return types for all classes within the com.aneesh.aopdemo package with all method names and no arguments&lt;br&gt;&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;&quot;execution(* com.aneesh.aopdemo.FootballGame .* () )&quot;&lt;/code&gt;&lt;br&gt;
Scan all public access modifiers, all return types for only the FootballGame class within the com.aneesh.aopdemo package with all method names and no arguments&lt;br&gt;&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;&quot;execution(* com.aneesh.aopdemo.FootballGame .playGame () )&quot;&lt;/code&gt;&lt;br&gt;
Scan all public access modifiers, all return types for only the FootballGame class within the com.aneesh.aopdemo package with the method name &quot;playGame&quot; and no arguments&lt;br&gt;&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;&quot;execution(* com.aneesh.aopdemo.FootballGame .playGame (..) )&quot;&lt;/code&gt;&lt;br&gt;
Scan all public access modifiers, all return types for only the FootballGame class within the com.aneesh.aopdemo package with the method name &quot;playGame&quot; and all possible arguments&lt;br&gt;&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;&quot;execution(private void com.aneesh.aopdemo.FootballGame .playGame (..) )&quot;&lt;/code&gt;&lt;br&gt;
Scan all private access modifiers, void return types for only the FootballGame class within the com.aneesh.aopdemo package with the method name &quot;playGame&quot; and all possible arguments&lt;br&gt;&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;&quot;execution(public void com.aneesh.aopdemo.FootballGame .playGame (String, int) )&quot;&lt;/code&gt;&lt;br&gt;
Scan all public access modifiers, void return types for only the FootballGame class within the com.aneesh.aopdemo package with the method name &quot;playGame&quot; and arguments of String followed by int&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;
The examples above illustrate how specific or generic a Pointcut expression can be.&lt;br&gt;
The granularity of Pointcut expressions enable us to define explicitly which Joinpoint(s) we want to be scanned.&lt;br&gt;
We can then use the 5 Advice types to provide further granularity to specify where in the Joinpoint we would like to implement the Advice.&lt;br&gt;
The below example will implement before and after Advice on our &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt; Joinpoint:
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Using @Before and @After&lt;/h5&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Aspect&lt;/span&gt;
&lt;span class=&quot;token annotation punctuation&quot;&gt;@Component&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; footballAspect &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;token annotation punctuation&quot;&gt;@Before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;execution( void com.aneesh.aop.* .playGame (..))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;beforeAspect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		
		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Make sure players warm up before game.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token annotation punctuation&quot;&gt;@After&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;execution( void com.aneesh.aop.* .playGame (..))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;afterAspect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Make sure players warm down after a game.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The outcome produced from running the application:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Make sure players warm up before game.
Game has started.
Maintain hydration after the game.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The application has run the Before and After methods in their respective order as expected. As a result, the application has performed some additional behaviours beyond the business source code.
&lt;/p&gt;
&lt;p&gt;
The &lt;strong&gt;@Around&lt;/strong&gt;, &lt;strong&gt;@AfterReturning&lt;/strong&gt;, and &lt;strong&gt;@AfterThrowing&lt;/strong&gt; Advice are implemented slightly differently, introducing a few more variables.&lt;br&gt;
To illustrate the annotations, we will introduce a new method, &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt;, that returns the number of players on the pitch if it is equal to 22, otherwise it will throw an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;.&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;class FootballGame&lt;/code&gt; has been updated (below) and the main class will now call &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; instead of &lt;code class=&quot;java&quot;&gt;playGame()&lt;/code&gt;.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	
	&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; playersOnPitch &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;playGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Game has started.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;countPlayers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;playersOnPitch &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;FootballGame Class has successfully got 22 players.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; playersOnPitch&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;FootballGame Class has not got 22 players and will throw Exception.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
			&lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;FootballGame class Exception: Incorrect number of players.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;	

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		
		&lt;span class=&quot;token class-name&quot;&gt;AnnotationConfigApplicationContext&lt;/span&gt; context
		&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AnnotationConfigApplicationContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SpringConfig&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt; footballGame &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getBean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;footballGame&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FootballGame&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Main method has counted players value of: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; footballGame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;countPlayers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Exception&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Main method has caught exception: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

		context&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h5&gt;Using @Around&lt;/h5&gt;
&lt;p&gt;
We will create new Advice methods in the Aspect class to process the information from the new Joinpoint. We will include around the execution, and upon returning an &lt;code class=&quot;java&quot;&gt;int&lt;/code&gt; or &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@Around&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;execution(  int com.aneesh.aop.* .countPlayers (..))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;aroundAspect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ProceedingJoinPoint&lt;/span&gt; proceedingJoinPoint&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Throwable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Around: Count the players on the pitch...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; proceedingJoinPoint&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;proceed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Around: Count is done.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
On line 1 we use the &lt;strong&gt;@Around&lt;/strong&gt; type Advice. The Around Advice is very different to Before or After as it is intercepting the method call from the main method to &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt;.
You will see on line 2 that the method returns an &lt;code class=&quot;java&quot;&gt;Object&lt;/code&gt;.&lt;br&gt;
The business logic expects the call to &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; to return an &lt;code class=&quot;java&quot;&gt;int&lt;/code&gt;. The Around Advice will intercept that method, but it now has the responsibility to retain the business logic and return an &lt;code class=&quot;java&quot;&gt;int&lt;/code&gt;.&lt;br&gt;
The process of receiving, processing, and returning the int is achieved in 3 key sections: {line 4}, {line 6}, and {lines 8 &amp; 9}.&lt;br&gt;
Line 4 will be the business logic processed &lt;u&gt;before&lt;/u&gt; &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; is called. &lt;br&gt;
Line 6 will use the &lt;strong&gt;ProceedingJoinPoint&lt;/strong&gt; to execute the actual &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; method.&lt;br&gt;
ProceedingJoinPoint acts as the connection between the Advice and the &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; Joinpoint. By calling &lt;code class=&quot;java&quot;&gt;.proceed()&lt;/code&gt;, we are telling the Advice to continue with executing the Joinpoint in the application.&lt;br&gt;
&lt;code class=&quot;java&quot;&gt;proceedingJoinPoint.proceed()&lt;/code&gt; is a unique method to the &lt;strong&gt;@Around&lt;/strong&gt; Advice as it sits on both sides of the Joinpoint.&lt;br&gt;
By default, the &lt;code class=&quot;java&quot;&gt;.proceed()&lt;/code&gt; method will return an &lt;code class=&quot;java&quot;&gt;Object&lt;/code&gt;. We have defined an &lt;code class=&quot;java&quot;&gt;Object&lt;/code&gt; as the result on line 6, however this can be wrapped in an &lt;code class=&quot;java&quot;&gt;Integer&lt;/code&gt; and returned.&lt;br&gt;
On lines 8 and 9, we process the business logic &lt;u&gt;after&lt;/u&gt; the method has returned from &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt;. The &lt;code class=&quot;java&quot;&gt;int&lt;/code&gt; is then sent to the main application as it would have expected to be delivered had the Advice not been used. In this example, the int is packaged as an &lt;code class=&quot;java&quot;&gt;Object&lt;/code&gt;.
&lt;/p&gt;
&lt;br&gt;
&lt;h5&gt;Using @AfterReturning&lt;/h5&gt;
&lt;p&gt;
The below example uses &lt;strong&gt;@AfterReturning&lt;/strong&gt;:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@AfterReturning&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointcut &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;execution( int com.aneesh.aop.* .countPlayers (..))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; returning&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;result&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;afterReturningAspect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;AfterReturning has the result: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
On Line 1 we use the &lt;strong&gt;@AfterReturning&lt;/strong&gt; Advice to process information after &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; has returned an &lt;code class=&quot;java&quot;&gt;Object&lt;/code&gt; (otherwise an &lt;code class=&quot;java&quot;&gt;int&lt;/code&gt;).&lt;br&gt;
The argument to the Advice, however, does not only consist of a Pointcut expression. We define the Pointcut to the variable &quot;pointcut&quot;, then we also define a &lt;code class=&quot;java&quot;&gt;String&lt;/code&gt; called &quot;return&quot; to the &quot;returning&quot; property.&lt;br&gt;
The value of &quot;returning&quot; is used in the Advice method. The &quot;returning&quot; value is equal to what is returned from &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt;. The &quot;returning&quot; value is later back to the main method after the Advice.&lt;br&gt;
We could use the result from &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; in the Advice and return it to the application by adjusting the method signature (on line 2) and returning an int at the end of the method (line 5), however this example will only return void.&lt;br&gt;
On line 2 we can see the method will take an &lt;code class=&quot;java&quot;&gt;int&lt;/code&gt; argument which comes directly from &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt;. The business logic is processed on line 4. &lt;br&gt;
The diagram below illustrates the movement of Objects between the &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; and the Advice, before being returned to the main method:
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/cff125b50d91fd8fc151db2409ca33fb/350de/003AfterReturning.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 111.99999999999999%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAYAAADAQbwGAAAACXBIWXMAABcRAAAXEQHKJvM/AAACrUlEQVQ4y5VUXVPiQBDk//8PX33zwVKp8gpPERWRA8EDYw4kBAL5IN8J9KWHW0SvUNyqqbC62zvT0z2lKI7R1n00ei5qHRsv4xDfXavVSr739/cohWEI7vVxgIEZIM2WcuA7kee5AN7d3REwks3USeBH2frFLzL5uBRgvV5Hyfd9hGEAP4gQxSnSNCkilYgLOrIsQxAESJJkJ+g7QNd15cJ8Poeu65hMJhiNRhgMBrBtG8PhEHyUZ/YC5GHHcYTQi4sLNBoN4aJaraLdbuPy8lIeY7YKcBeHG0DLstBqtfDw8CCAj4+PuLq6EkAC93o9KX3XWi6Xb4Ce54GdVrzxYpalSIqMyBsjiiLwnLrMrPhlZvzy3laXQwGYzDzMHR95GmM4dvCrZyErflOnpIRca5oG0zSFX/LMyrifTqcCWqvV1oBcIyvC3Fu/5Bd0tV+8TUk8zGYdHR2hXC7j5OREgnSQmsPDQ3l0kyHX3EsQxDkWnovX0RiOn7IDoknfDzYckvPT01NUKhVcX1/L30kLy3+XoTGLsAiX0F+eUflZhWa8WTCJQ8lgsVhgNptJ09iwTqcjZStJMeMN4NSJ4QXpBmRixwjjJTQzQ7VlFkAWms2mRL/fFxB2n3vq9T9AlrwI12WlWY7uwEe9Y6D8owr9dSZKIAiD2bJ0JXjuWfLNzc2blzUjgOWu7ZUvVzDtRLrd73ULLvO9ps0/YQfIswT1rok/Y1dGAzNs9m1oo0D2PLPt5U+nDQWrSHVdR3gh6YZhQHt+FstRNixvLy8TUL3+9PQbZ2dnOD4+li7SinQJpaG43mva8AI9TNEyqDMK9/z8XMDZVaXDvQC3u6W8TTtxyvD/asQpL38M9ZhwyI0y+7bptxugpslXi5oscQYS+fb29tOgxnYFLUiwg4MD/AXk2IX3T5kD5AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;AfterReturning Advice flow&quot;
        title=&quot;AfterReturning Advice flow&quot;
        src=&quot;/static/cff125b50d91fd8fc151db2409ca33fb/5a46d/003AfterReturning.png&quot;
        srcset=&quot;/static/cff125b50d91fd8fc151db2409ca33fb/1db73/003AfterReturning.png 75w,
/static/cff125b50d91fd8fc151db2409ca33fb/8a4e8/003AfterReturning.png 150w,
/static/cff125b50d91fd8fc151db2409ca33fb/5a46d/003AfterReturning.png 300w,
/static/cff125b50d91fd8fc151db2409ca33fb/fc2a6/003AfterReturning.png 450w,
/static/cff125b50d91fd8fc151db2409ca33fb/0a47e/003AfterReturning.png 600w,
/static/cff125b50d91fd8fc151db2409ca33fb/350de/003AfterReturning.png 998w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The result from processing the AfterReturning Advice would be:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Around: Count the players on the pitch...
FootballGame Class has successfully got 22 players.
Around: Count is done.
AfterReturning has the result: 22
Main method has counted players value of: 22&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br&gt;
&lt;h5&gt;Using @AfterThrowing&lt;/h5&gt;
&lt;p&gt;
The current application does not throw an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;, but if we were to change the &lt;code class=&quot;java&quot;&gt;playersOnPitch&lt;/code&gt; value in &lt;code class=&quot;java&quot;&gt;class FootballGame&lt;/code&gt;, &lt;code class=&quot;java&quot;&gt;countPlayers()&lt;/code&gt; would throw an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;.&lt;br&gt;
The example below demonstrates how an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; would be handled with Advice.
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@AfterThrowing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pointcut &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;execution( int com.aneesh.aop.* .countPlayers (..))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
				 throwing &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;thrownExpression&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;afterThrowingAspect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Throwable&lt;/span&gt; thrownExpression&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;AfterThrowing has received a message: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; thrownExpression&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;p&gt;On line 2 we have defined the Advice in a similar pattern to the &lt;strong&gt;AfterReturning&lt;/strong&gt; Advice. Instead of using the &quot;returning&quot; parameter, we are using &quot;throwing&quot;, and we pass the &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; into the Advice method as a Throwable on line 3.&lt;br&gt;
On line 5, we simply process the message of the &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; before allowing the &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; to be sent back to the main method.&lt;br&gt;
In real business logic, we may log the &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; or transform it before sending it back. &lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
If we change the number of players so an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; is thrown, the &lt;strong&gt;AfterThrowing&lt;/strong&gt; Advice would return:
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre class=&quot;language-text&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Around: Count the players on the pitch...
FootballGame Class has not got 22 players and will throw Exception.
AfterThrowing has received a message: FootballGame class Exception: Incorrect number of players.
Main method has caught exception: FootballGame class Exception: Incorrect number of players.&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
Notice that the second Around Advice has not been called as an &lt;code class=&quot;java&quot;&gt;Object&lt;/code&gt; was not returned to the Advice. Instead, the thread has thrown an &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt;.&lt;br&gt;
The &lt;strong&gt;AfterThrowing&lt;/strong&gt; Advice is acknowledged when the &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; is thrown and run before the &lt;code class=&quot;java&quot;&gt;Exception&lt;/code&gt; is sent back to the main method.
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
Spring provides simple and effective tools that enable us to implement AOP into our applications. &lt;br&gt;
By defining Pointcut expressions, we are able to specify exact Joinpoints where we would like to add additional processing. We use Advice types to define when we would like the processing to happen, and as a result, we are able to delicately add additional functionality to our application.&lt;br&gt;
By using AOP with object-orientated programming, we are able to drastically improve the maintainability of our source code by grouping additional features and behaviours of our application into modular sections.&lt;br&gt;
Furthermore, we are able to cross boundaries that are defined by packages and classes within our application to provide shared behaviour and functionality that can, incredibly, be defined by just a single method.
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;&lt;small style=&quot;float: right;&quot; &gt;Picture: Rio De Janeiro, Brazil by &lt;a target=&quot;_blank&quot; href=&quot;https://unsplash.com/@phaelnogueira&quot;&gt;Raphael Nogueira&lt;/small&gt;&lt;/a&gt;&lt;br&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Calculating the time complexity of algorithms]]></title><description><![CDATA[Understand why the Big O is an important notation to reference when designing optimized algorithms.]]></description><link>https://aneesh.co.uk/calculating-the-time-complexity-of-algorithms</link><guid isPermaLink="false">https://aneesh.co.uk/calculating-the-time-complexity-of-algorithms</guid><pubDate>Mon, 13 Apr 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
&amp;#8226; Time complexity is used to express the efficiency of an algorithm.&lt;br&gt;
&amp;#8226; The Big O is a measure that defines the type of CPU growth an algorithm has as the input elements scale upwards.&lt;br&gt;
&amp;#8226; The Big O of an algorithm is defined by the operation that is least time efficient.&lt;br&gt;
&lt;br&gt;
&lt;h4&gt;What is time complexity?&lt;/h4&gt;
&lt;p&gt;
The term ‘time complexity’ is used to describe the efficiency of an algorithm with respect to its number of input elements. Defining the efficiency of an algorithm can be difficult while accounting for variables such as memory, disk, and network usage.&lt;br&gt;
The Big O Notation is used to describe time complexity for the upper-bound runtime; the Big O is expressed for an algorithm&apos;s CPU usage alone, therefore discarding the other variables from consideration.
&lt;/p&gt;
&lt;p&gt;
The Big O is part of a family of asymptotic notations which, include Big Theta Θ and Big Omega Ω.&lt;br&gt;
The notations define the upper and lower asymptotic bounds for an algorithm. The Big O is only concerned with the approximate worst-case complexity of an algorithm; in other words, the guaranteed time in which an algorithm will complete.
&lt;/p&gt;
&lt;p&gt;
Big O defines how the resources required (from the operations performed) in the algorithm scale up as the input elements to the algorithm grow.&lt;br&gt;
The Big O does not reflect the performance of an algorithm. The performance of an algorithm would capture how much time or memory is consumed when the algorithm is executed.&lt;br&gt;
The graph below illustrates the 5 classifications an algorithm can have:
&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c47db9832a8feaa77350c753753b8a57/977f7/002Graph.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 78.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAABcRAAAXEQHKJvM/AAADbUlEQVQ4y22UXWzTVhTH3QQYDKEiHob42sRYN9qmAaUS7zyAxPa+PexhE+IFNlTQGO1QES2hlI+2UivxwNsqwdMeJiFNmihVpA0KEgh1Ke3Sj5AP0zaJHTtx7NiJff3nXNeUT0u/nOsb35/OuT7XgqHrsGo11Ci6qgqnVILlYxJ1wiVW59VXUUVNVmFZFofVyKHrel6wacCv4ugo5nfsQLa1Ffm9ewGfLJEilgmXzzXz+WZg925YXd/Bdb31jP/Ytp0X6qbpCeWBASQFATlikQCh++MUsUwwf56jHdyM4uAhMLMC1xO6qNfrlKEvrA4Pew8aa9dCb2gACIlw/TGCAe9/fq99+wWk6H6oA1/DtfTVDFeEtAf8Thsa8hYowSCqFG1C9rNhDSTiMUiy7/dA7T8AubMFpcEjntB9U+jW60gpCnLR6IowEIBFsUxwMRr8MjcEoR/7CtKldsRPhvDg2OfI9h2Gbb6ToWvbmMpmsdTb6y2USMj3SuLlBfwMN65B+ocmxM+GETv6JSaOt+C/E03IkJDa450MSRjPZLB44YK3WCahQbHky5zN66D+3IJHp0JIdLQi2xnG7JkwljubUR46AmZ9IMO5fB5LPT1eZrzkIskcLvtkPZTTISjRdqjd+5HqbMPCr20o/NYO5ZcwlP5v3n8pcBw8WVjAi+5ub8/kNUGUuGzXRqhdYRSjEWRIMnMyjExHBEoXya61Qb69FVYiRH1YJt5sG3rLvHH0wUFoJBIJs2kT1HP7qLwIEsf3IXmaWmSoFeadz1Cfa4ShrsPDpwKm53fSSmM1Q8dxXvehNTLiNW8htA3SmQN4fjYCsb8ZlT8/BZKNsMsB1GzKnAmwdGpsSUBF2QVFydCRMxljDjRNe31Sli5dxj9bBEz1bcfTmxuQ/FfAC1Gg/RUwKa7wOPURYv9/jPHZRvyVaMSd6Qjizx7jwf0JNjMzjXQ6nRfoUPN0WXzsLrs18iMbn/iJ3Zs8wf6438FGY13s99h5dnO8hw2PXWTX7/axy/eust6xAXbu76vsSuwGm5p5xhKJhFOp0BF03ZxAdeNDV25Rw9TkPJKzi4SI9Dwxl4VjvP28/7WBv4dFgT45Ik2IVcMgKmK1WhFNUxdLZUUsSDlRLhZWUCRRkguiVimLhmmIelX3IlXIyZJjmbKcfAkdTMeMFDoFnQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Big O categorization&quot;
        title=&quot;Big O categorization&quot;
        src=&quot;/static/c47db9832a8feaa77350c753753b8a57/5a46d/002Graph.png&quot;
        srcset=&quot;/static/c47db9832a8feaa77350c753753b8a57/1db73/002Graph.png 75w,
/static/c47db9832a8feaa77350c753753b8a57/8a4e8/002Graph.png 150w,
/static/c47db9832a8feaa77350c753753b8a57/5a46d/002Graph.png 300w,
/static/c47db9832a8feaa77350c753753b8a57/fc2a6/002Graph.png 450w,
/static/c47db9832a8feaa77350c753753b8a57/977f7/002Graph.png 547w&quot;
        sizes=&quot;(max-width: 300px) 100vw, 300px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;• &lt;span style=&quot;background-color: rgb(0,176,80)&quot;&gt;Constant time O(1)&lt;/span&gt;&lt;br&gt;&lt;br&gt;
• &lt;span style=&quot;background-color: rgb(146,208,80)&quot;&gt;Logarithmic time O(log n)&lt;/span&gt;&lt;br&gt;&lt;br&gt;
• &lt;span style=&quot;background-color: #FFFF00&quot;&gt;Linear time O(n)&lt;/span&gt;&lt;br&gt;&lt;br&gt;
• &lt;span style=&quot;background-color: rgb(237,125,49)&quot;&gt;N logarithmic time O(n log (n))&lt;/span&gt;&lt;br&gt;&lt;br&gt;
• &lt;span style=&quot;background-color: rgb(255,0,0)&quot;&gt;Polynomial time O(n²) / Exponential time O(2&lt;sup&gt;n&lt;/sup&gt;)&lt;/span&gt;&lt;br&gt;&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Defining the Big O of each operation&lt;/h4&gt;
&lt;p&gt;
&lt;p&gt;We are able to identify the Big O classification of each operation with the help of the Big O Cheat Sheet found &lt;a target=&quot;_blank&quot; href=&quot;https://www.bigocheatsheet.com/&quot;&gt;here&lt;/a&gt;&lt;br&gt;
Below are two examples of algorithms that capture a Big O classification themselves from a single operation:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;background-color: rgb(0,176,80)&quot;&gt;Constant time O(1)&lt;/span&gt; translates to ‘Order of 1’. The resources required to complete the operation will not change as the input size grows. The below example illustrates an O(1) operation:&lt;br&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; values&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;span style=&quot;background-color: #FFFF00&quot;&gt;Linear time O(n)&lt;/span&gt;, defines the linear growth of algorithm complexity in-line with the growth of elements. The below example illustrates an operation of O(n)&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;got the value: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
The remaining classifications follow a pattern where the resources required will increase at the respective growth of n.&lt;br&gt;
For example, typical sorting algorithms have a Big O notation of (n log (n)). The sorting algorithm would experience a resource requirement growth of (n log (n)) as the input elements increase by n. 
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Calculating the Big O of an algorithm&lt;/h4&gt;
&lt;p&gt;
As algorithms will usually consist of multiple operations, we will use the example below to understand how we consider multiple operations into a Big O calculation:&lt;br&gt;
&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;java&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber 0&quot; class=&quot;language-java line-numbers&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculateMaxValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; values&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; max &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; max&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            max &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Max Value is: &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; max&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
Line 2 is used to initialise a variable. As this line is not influenced by the size of input &lt;code class=&quot;java&quot;&gt;values&lt;/code&gt;, line 2 has a Big O of O(1). &lt;br&gt;
From lines 3 to 7, the function will loop through each member of &lt;code class=&quot;java&quot;&gt;values&lt;/code&gt; and identify if it is the maximum value of the List.&lt;br&gt;
If &lt;code class=&quot;java&quot;&gt;values&lt;/code&gt; = {1,2,3,6,3}, the number of elements, n, will be 5. &lt;br&gt;
The code block will run a total of 5 times in order to validate that the largest value is found within the list.&lt;br&gt;
It will run for &lt;code class=&quot;java&quot;&gt;int i&lt;/code&gt; = 0,1,2,3, and 4 as &lt;code class=&quot;java&quot;&gt;values.size()&lt;/code&gt; = 5. A total of 5 operations for an input size of 5 means the code block has a Big O of O(n).&lt;br&gt;
We can run through the code block again to support the Big O classification. If &lt;code class=&quot;java&quot;&gt;values&lt;/code&gt; = {1,2,3}, then &lt;code class=&quot;java&quot;&gt;values.size()&lt;/code&gt; will equal 3, and the number of operations ran will also be 3 (checking values of 1, 2, and 3).&lt;br&gt;
Lastly, line 8 is used to print the result of the algorithm. This line is not influenced by the number of input values, therefore it is constant time and has a Big O of O(1).
&lt;/p&gt;
&lt;p&gt;
As we account for the three operations within the algorithm, we have accumulated a Big O of: O(1) + O(n) + O(1).&lt;br&gt;
The accumulation can be summarised into: 2 O(1) + O(n).&lt;br&gt;
One defining characteristic of the Big O is that it is only defined by the &lt;u&gt;rate&lt;/u&gt; of growth. Big O will not define how tightly an algorithm fits within the classification, but it will define the type of classification it falls within.&lt;/p&gt;
&lt;p&gt;
As we understand that it is only the type of growth we will consider, we can remove the constants from the accumulated Big O value.&lt;br&gt;
As a result, 2 O(1) + O(n) is reduced to: O(1) + O(n).&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The final reduction made to our Big O calculation involves releasing the non-dominant terms, in other words, an algorithm is only as efficient as its least efficient operation.&lt;br&gt;
Working from the least-efficient classification of exponential growth, back to constant growth, we can identify the category the algorithm belongs as the classification that is satisfied first.&lt;br&gt;&lt;br&gt;
&amp;#8226; Exponential/Polynomial time: &amp;#10008;&lt;br&gt;
&amp;#8226; N logarithmic time: &amp;#10008;&lt;br&gt;
&amp;#8226; Linear time: &amp;#10003;&lt;br&gt;
&amp;#8226; Logarithmic time: &amp;#10008;&lt;br&gt;
&amp;#8226; Constant time: &amp;#10003;
&lt;/p&gt;
&lt;p&gt;
We can see from the above checklist, the algorithm satisfies the linear time complexity first (from top-down).&lt;br&gt;
It later satisfies constant time, however this is discounted as it is not the least efficient classification. As a result, the algorithm has a Big O Notation of constant time: O(n).&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
The Big O notation enables us to better understand how the individual operations within an algorithm can contribute to its upper-bound CPU consumption.&lt;br&gt;
By breaking down an algorithm and understanding how the resource consumption of each operation will scale with the growth of input elements, we are better able to identify where possible inefficiencies lie.&lt;br&gt;
Calculating the Big O will encourage you to explore and utilise the various data structures and methods available to create CPU-optimised algorithms.&lt;br&gt;
The time complexity of our algorithms provide a measurable and reflective purpose for us to review how well each operation within our algorithms can scale with input elements.
&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Hello World - My first blog]]></title><description><![CDATA[My first blog post. How I found my passion for software engineering and what I aim to achieve with my blog.]]></description><link>https://aneesh.co.uk/hello-world-my-first-blog</link><guid isPermaLink="false">https://aneesh.co.uk/hello-world-my-first-blog</guid><pubDate>Mon, 06 Apr 2020 16:34:00 GMT</pubDate><content:encoded>&lt;br&gt;
&lt;p&gt;
It was roughly 5 years ago when I wrote my first line of code. I had attended a job interview for an internship when I was asked about writing macros to reflect upon my experience with Microsoft Excel. At that point, I had no experience to reflect upon. That evening I wrote my first macro to change the colour of a cell to red with a keyboard shortcut.&lt;br&gt;
This experience taught me that the advent of high-level programming languages could provide a more intuitive experience writing code than I could have thought.
&lt;/p&gt;
&lt;/p&gt;
Fast-forward to the present day and I am starting a blog to document the areas of technology that I explore, find interesting, and am challenged by on a daily basis.&lt;br&gt;
My experience with writing a dissertation, where I had to introduce the technologies involved (Big Data and Blockchain) allowed me to appreciate the process and value of understanding a technical concept to its core. My blog will support me as I use the Feynman technique to learn and write bite-sized posts on how and why the technologies I use work.
&lt;/p&gt;
&lt;p&gt;
My expectations for this blog are that it will surround all areas of Java, Spring, and algorithms.&lt;br&gt;
Despite this, the deluge of questions that arrive each day from my search for answers will no doubt broaden the context.&lt;br&gt;
Nevertheless, I am creating this website for my enjoyment and with the hope that you, the reader, may also benefit from one of the many challenges I overcome as an engineer.
&lt;/P&gt;
&lt;p&gt;
Enjoy!
&lt;/p&gt;</content:encoded></item></channel></rss>