<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Roshni Silva on Medium]]></title>
        <description><![CDATA[Stories by Roshni Silva on Medium]]></description>
        <link>https://medium.com/@roshnisilva?source=rss-1b7234a91810------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9o7NfgfFRHuHXhtf6sd5HA@2x.jpeg</url>
            <title>Stories by Roshni Silva on Medium</title>
            <link>https://medium.com/@roshnisilva?source=rss-1b7234a91810------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:55:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@roshnisilva/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Prototype Design Pattern…]]></title>
            <link>https://roshnisilva.medium.com/prototype-design-pattern-140bc2575aff?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/140bc2575aff</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Thu, 30 Dec 2021 12:54:00 GMT</pubDate>
            <atom:updated>2021-12-30T12:54:00.436Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oXCGRqhXkW2R15D3uYY8qg.gif" /></figure><p>The prototype design pattern is categorized under the creational design pattern. This pattern is not a very popular one but it is a kind of useful design pattern and there are some situations where creating new objects can be very expensive. In other words when object creation is a costly operation and time-consuming. Therefore this prototype pattern provides the best ways of creating an object and this pattern is carrying a concept is to copy an existing object instead of creating a new object from the scratch. The already existing object that we created for the very first time acts as the prototype and the newly cloned instance is a copy of the original instance but we can do changes to the newly cloned instance whenever needed. <em>We can tell this pattern is used to save costly resources and processing time of a program</em>.</p><p>So, now we are aware of if there is a scenario where the creation of object process is expensive or there are a lot of things to do when creating objects or we have to create many more objects from a class and where each object is exactly the same to each other or which have to have small differences, we can use this prototype pattern. When we implement this prototype pattern, we use the “<strong>clone()</strong>” method which is one of the best available ways to create an object from existing objects. In this cloning process, what it does is, it will create an instance from a class one time and place them in registers. later whenever the new creation request is received, it will get an already existing object from the registry and clone it and give that to the client. When we deal with cloning, as developers we need to make sure in your architecture you need to have a <strong>Shallow Copy</strong> or <strong>Deep Copy</strong>.</p><p>What are Shallow Copy and Deep Copy?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*oX3ieiKJhJogKQIALqw8jQ.png" /></figure><ul><li>Shallow Copy — In here, an instance is created by simply copying the data of all variables of the original object. If data is a reference type, only the reference is copied but not the reference instance itself. Hence, original and clone both refer to the same memory location and due to this, it will create ambiguity and runtime errors dangling pointer. A shallow copy is faster.</li><li>Deep Copy — In here, an instance is created by copying data of all variables including its reference and allocating similar memory resources with the same value to the object. A deep copy is comparatively slow.</li></ul><p>❗️❗️❗️Key Note: Shallow copy is quite dangerous sometimes 😨because you get the object to another means you copy some references to your new object and if you modify those references and that could affect the original object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*C7BiDwgVKqQ7NH0TVx97aA.png" /></figure><p>Prototype Design Pattern Usecase:</p><p>Now consider the below scenario and let&#39;s try to implement a prototype pattern for the given scenario.</p><p><em>Assume as a programmer, you asked to develop a system for a pizza shop to keep records of pizza details and order details, and imagine in that shop we have different types of pizzas. (in this scenario I only go with Non-Vegetarian Pizza</em><strong><em> </em></strong><em>and Vegetarian Pizza</em><strong><em> </em></strong><em>types but as you know there are many more types available 😉) Now think if we don’t use a prototype pattern for this scenario then we may need to create a new pizza object each time whenever a client requests a pizza. Since we use the prototype pattern, we don’t need to create instances of Pizza every time🤩.</em></p><p>We have two types of Pizza as <strong><em>NonVegPizza </em></strong>and <strong><em>VegPizza</em></strong>. Both are subclasses of a class called <strong><em>Pizza </em></strong>and<strong><em> </em></strong>we have <strong><em>PizzaRegistry </em></strong>class which is used for the creation and storage of NonVegetarianPizza and VegetarianPizza pizza types in HashMap. so whenever clients request the pizza, they can pass on their preferred pizza type that they want and get a clone of pre-created instances of pizza types.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ehbx12VU_qxr9gZ2z6AdNQ.jpeg" /><figcaption>UML representation of the Prototype Design Pattern scenario</figcaption></figure><p>Let’s look at the implementation of the scenario.</p><p><em>Pizza </em>class is an abstract class and since we need to override clone() method, this class implements the Cloneable interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be87d13eab1a35835f4caf5419cd7af3/href">https://medium.com/media/be87d13eab1a35835f4caf5419cd7af3/href</a></iframe><p><em>NonVegPizza </em>and <em>VegPizza </em>extend from the Pizza class as below. Here NonVegPizza class has its own attribute and <em>oderPizza() </em>method is overridden in both classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b86361be40a195598dbc22939e3460c4/href">https://medium.com/media/b86361be40a195598dbc22939e3460c4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0354bf9ed2683fbb4d0088ec1a9b51f7/href">https://medium.com/media/0354bf9ed2683fbb4d0088ec1a9b51f7/href</a></iframe><p>Also, I implemented an enum called PizzaType and it stores the types of pizza available. In this scenario, I have only 2 types.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/146234a428df81a0b109e77b1f01721b/href">https://medium.com/media/146234a428df81a0b109e77b1f01721b/href</a></iframe><p>Next is to create PizzaRegistry class and that is the place where you create the object (initial)one-time using a new keyword.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df1d2bae924e315796c5127c3f22c77b/href">https://medium.com/media/df1d2bae924e315796c5127c3f22c77b/href</a></iframe><p>As you can see this <em>PizzaRegistry</em> class has a constructor and it will call the <em>initialize()</em> method and inside that, I directly create 2 different instances from both pizza types and I have implemented HashMap with their class type to hold the objects that are initially created. <em>getPizza()</em> method is there return pizza type object and inside that method, I have called <em>clone()</em> method and that will return cloned object. Since the <em>clone()</em> method returns the object type, we need to do perform casting into the type we want.</p><p>As a final step, we have our main class which is called as <em>MainApp </em>and refer to the below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3b9ec327d9e726f3dd4fcf4f85d4be0/href">https://medium.com/media/a3b9ec327d9e726f3dd4fcf4f85d4be0/href</a></iframe><p>Ready to see the output of the above scenario 🧐.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*cqs68g8cQiA7V1F-iRg7XA.png" /></figure><p>Click on the below GitHub link to see the full source code of the above scenario.</p><p><a href="https://github.com/Roshni-Romeshika/Learning-with-Krish/tree/main/Design%20Patterns/src/prototypepattern">Learning-with-Krish/Design Patterns/src/prototypepattern at main · Roshni-Romeshika/Learning-with-Krish</a></p><p>Ta-Daaa 😎So here you come to the end of the Prototype pattern discussion.</p><p><strong>References:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fzw8kbrEbYxU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dzw8kbrEbYxU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fzw8kbrEbYxU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/dccd1addf6234e28be9f17a12d8eb05a/href">https://medium.com/media/dccd1addf6234e28be9f17a12d8eb05a/href</a></iframe><ul><li><a href="https://www.geeksforgeeks.org/difference-between-shallow-and-deep-copy-of-a-class/#:~:text=Shallow%20Copy%20stores%20the%20references,copies%20of%20the%20object&#39;s%20value.&amp;text=Deep%20copy%20doesn&#39;t%20reflect,the%20references%20to%20the%20objects">https://www.geeksforgeeks.org/difference-between-shallow-and-deep-copy-of-a-class/#:~:text=Shallow%20Copy%20stores%20the%20references,copies%20of%20the%20object&#39;s%20value.&amp;text=Deep%20copy%20doesn&#39;t%20reflect,the%20references%20to%20the%20objects</a>.</li></ul><p><a href="https://www.geeksforgeeks.org/prototype-design-pattern/">Prototype Design Pattern - GeeksforGeeks</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=140bc2575aff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Factory Method Design Pattern…]]></title>
            <link>https://roshnisilva.medium.com/factory-method-design-pattern-f8d6f78a5beb?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8d6f78a5beb</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Fri, 17 Dec 2021 10:43:51 GMT</pubDate>
            <atom:updated>2021-12-17T10:46:15.463Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*2GW9GXpYVk9-HByQZpTKiw.gif" /></figure><p>Factory Method Pattern or Factory Pattern is categorized under creational pattern and this factory pattern just defines an interface or an abstract class for creating an object but subclasses decide which class to instantiate. Hence, subclasses of the factory method are responsible to create instances of the class. The factory method is allowed creating objects without exposing the instantiation logic to the client.</p><p>***The factory method pattern is also known as <strong><em>Virtual Constructor</em></strong>.***</p><p>The factory method pattern is totally opposite side of Singleton. When you come up with a factory pattern make sure that you have to pass arguments to the constructor, and it must switch the instance that returns depending on the parameters which come in.</p><h3>When to use factory method pattern?</h3><p>This pattern can be used in the following situations such as,</p><ul><li>A class doesn’t aware of what subclasses will be required to create.</li><li>A class needs that its subclasses specify the objects to be created.</li><li>The parent class chooses the creation of objects to its subclasses.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*P5TVWONNDqr6MaA6viWRqA.jpeg" /><figcaption>Factory Pattern Structure</figcaption></figure><p>Factory Method Usecase:</p><p><em>Assume there is a small-scale restaurant that provides only chicken food items and they have introduced mainly 2 food combinations offers namely ‘Basic Combo Offer’ and ‘Super Combo Offer’ for this Christmas season. These combo offers include several combo items. Customers can able to choose combo offers based on their preferences and they will get combo items according to the combo offer.</em></p><p>Now let’s try to understand the implementation of this scenario according to the factory method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*0ERCxmd2skXqHhbxJCDwxw.png" /><figcaption>Implementation of ComboItem abstract class</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*5GLZFnmTmTAPkspBlyZGDA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*GR-YrF07uwCodMxAsEC0cA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*GeXgeglvDHLAREkH32WKpw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*u5tljcK3pPGJjm6tly60UA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jel-kdgUWC2dUsHHKhFtwQ.png" /></figure><p>BigBossBuger, FrenchFries, ChickenFriedRice, ChilliChicken and Soup classes extend from ComboItem abstract class. All those classes are represented the <strong>IS-A relationship</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vvbOGT_EBvAynBDURL39eA.png" /></figure><p>ComboOfferFactory class is also an abstract class it creates ArrayList to store ComboItems and those items are included in different combo offers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*UW8rYkisBqha2dC5o_58vg.png" /></figure><p>ComboOfferName is an Enum that stores types of combo offers. In this scenario it has two types named “BASIC” and “SUPER”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QKrABTDGX56rRQ1fYGlMMw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z2Y7C5SpMEa00rkGKEmdag.png" /></figure><p>‘BasicComboOffer’ and ‘SuperComboOffer’ classes extend from the ‘ComboOfferFactory’ abstract class. when ‘createComboOfer()’ calls it will create related combo items for each combo offer type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0NArATf9oTCQyogzUodMTA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SUWuOgzcECV7JL4lhSKDzw.png" /><figcaption>Implementation of the Application class</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WxbElFQeRYAiSZq6f6kowA.png" /><figcaption>Sample Output for Factory Method</figcaption></figure><p>According to the above output, we got combo items related to the ‘BASIC’ combo offer type whereas the second line of the output gave combo items that are included in the ‘SUPER’ combo offer type.</p><p>When comparing with singleton pattern, factory pattern is somewhat difficult and complicated to implement as it may have several subclasses to be implemented so there can be lots of coding when going with factory method pattern but this factory method pattern uses abstract class or interface to hide the details of implementation and also it makes the code more robust, loosely coupled and easy to extend.</p><p>So here you come to the end of the Factory Method pattern discussion. I’ll meet you guys with another story. 😎</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8d6f78a5beb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Singleton Design Pattern…]]></title>
            <link>https://roshnisilva.medium.com/singleton-design-pattern-891fda57acde?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/891fda57acde</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Thu, 16 Dec 2021 08:54:26 GMT</pubDate>
            <atom:updated>2021-12-16T16:18:48.218Z</atom:updated>
            <content:encoded><![CDATA[<p>According to the Gang of Four, the singleton design pattern is categorized under creational pattern and that provides the best ways of object creation. This pattern is mostly used design pattern in Java. When it comes to object creation of this pattern, it only allows one instance per container. In the case of Java, it creates a single instance of a class per JVM and it provides a global point of access to that instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*IAfC7iSrh6CdgKW85RDSzQ.gif" /></figure><p>Now you might think about why we need only one instance of a class or why we need to restrict the limit of object creations to only one, this frequently guarantees that there is access control to resources, for example, database connection or socket. Most of the time, some programmers use this singleton design pattern without having a good understanding of where or when to apply this pattern. Therefore, make sure not to overuse this pattern as it is kind of really hard to do unit testing due to it being tightly coupled with a singleton instance.</p><p>As a programmer, when we implement a singleton pattern,</p><ul><li>we must make the constructor of the singleton class ‘private’ in order to restrict the instantiation of the class from other classes,</li><li>we must create a static variable for instance and that should not take any arguments.</li><li>we must implement a static method to provide a global access point for the singleton object.</li></ul><p>Apart from that, we can use the approaches such as the ‘Lazy Initialization Method’(Instances are created only when required.), ‘Early Initialization Method’ (Instances are created at load time), and ‘Double-Checking Singleton’ to make thread-safe singleton using the ‘Synchronized’ keyword when we implement the singleton pattern.</p><p>Singleton Usecase:</p><p><em>Now assume, there is a small counseling office where each counseling session is taken through an online video call and only one session handler is there to handle every session. However, this person can only have one calling session at a time which means that 2/ more clients cannot have to call a session with the session handler at the same time.</em></p><p>Now let’s try to come up with the implementation of the singleton pattern which guarantees that only a single session handler can be created.</p><p>CounselingManager class —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uFKbRtQn3g5Z2hkhTFo2Tg.png" /></figure><p>CounselingManagerApplication class —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xi6qSM4n9GM9GUfMmCqHCQ.png" /></figure><p>Output —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*LUhqcxdYeLY5QWSDd6VS4Q.png" /></figure><p>According to the above output, we are clear that though we have created two instances from CounselingManager class, it gave us the same instance. The implementation of the CounselingManager class included getCounselingManager() method and that checks for an instance of CounselingManager. When implementing CounselingManager class, to ensure thread safety of the implementation, it used the ‘double-checking singleton’ approach which helps to prevent edge cases such as two or more threads may creating an instance.</p><p>Most of the developers use this ‘Synchronized’ keyword instead of ‘static’ in a method-level declaration which leads to getting a performance hit due to blocking everything. Therefore remember to use the ‘Synchronized’ keyword in the correct place in order to block the exact code segment that you want to block rather than blocking everything.</p><p>Overuse of singleton leads to giving some problems to your implementation and difficulty of unit testing can be marked as drawbacks of Singleton whereas saving memory due to creation of a single instance and easy maintenance due single point of access to a particular instance can be marked as advantages of Singleton pattern.</p><p>So here you come to the end of the Singleton pattern discussion. and I hope you all are clear somewhat of Singleton.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*iTrwlcVR9upfRqKcZhcB3Q.png" /><figcaption>I will discuss about Factory Method Pattern in the next story.</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=891fda57acde" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Design Patterns]]></title>
            <link>https://roshnisilva.medium.com/introduction-to-design-patterns-878d6d2be4a0?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/878d6d2be4a0</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Wed, 15 Dec 2021 12:21:45 GMT</pubDate>
            <atom:updated>2021-12-15T12:27:36.850Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YbACulkYiS_jNoLYJAR2pA.jpeg" /></figure><p>The idea of design patterns was introduced by <em>Erich Gamma</em>, <em>Richard Helm</em>, <em>Ralph Johnson</em>, and <em>John Vlissides </em>(Gang of Four). They distributed a book called “<strong><em>Design Pattern-Elements of Reusable Object-Oriented Software</em></strong>” in 1994 which prompted the development of design pattern ideas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*v5hG-fzAO-IGniMrdLfBIQ.jpeg" /></figure><h3>What is Design Pattern?</h3><p>Designing object-oriented software is hard and designing reusable object-oriented software is even harder. To accomplish best practices in object-oriented programming, software engineers use Design Patterns. It is not a completed design that can be changed straightforwardly into code. It is a layout for how to tackle an issue that can be utilized in a wide range of circumstances. In the simplest way, we can describe design patterns as a common solution given for most of the problems which programmers faced during the development process. These solutions were acquired by experimentation by various programming engineers over a significant considerable period. According to the Gand of Four (<em>Erich Gamma</em>, <em>Richard Helm</em>, <em>Ralph Johnson</em>, and <em>John Vlissides</em>), design patterns are principally dependent on the accompanying standards of object-oriented design.</p><ul><li><strong><em>program to an interface, not an implementation.</em></strong></li><li><strong><em>favor object composition over inheritance.</em></strong></li></ul><p>According to the book called ‘<em>Design Pattern-Elements of Reusable Object-Oriented Software’, </em>Gang of Four has mentioned that in general, a pattern has 4 essential elements which are ‘The pattern name’, ‘The problem’, ‘The solution and ‘The consequences ’</p><p><strong>Usage of Design Patterns</strong></p><p>If we go through the main usage of the design patterns is to have a common way of implementing the flow of the program and also using these design patterns, a programmer has a chance to speed up the development process by giving tested, proven development paradigms. Most people just see how to apply specific programming design strategies to specific issues. These procedures are hard to apply to a more extensive scope of issues. design patterns give general solutions, reported in a layout/format that doesn’t need points of interest attached to a specific issue. Design patterns are reusable and it provides transparency to the application design and further, improve code readability. Consequently, learning these design patterns assists unpracticed programmers with building their code in a very much organized manner.</p><p>Types of Design Patterns</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*oWnAusXJ12YwcOIBiCZhPA.png" /></figure><p>According to the book, ‘<em>Design Pattern-Elements of Reusable Object-Oriented Software’ </em>which was published by Gang of Four, design patterns are categorized into 3 different categories named as below.</p><ol><li><strong>Creational Pattern</strong></li><li><strong>Structural Pattern</strong></li><li><strong>Behavioral Pattern</strong></li></ol><p><strong>Creational Pattern — </strong>These design patterns are involved with the way of creating the objects. It allows creating objects without uncovering the creation logic therefore, we can give the type of object which we need and it will give us that object. Thus, it avoids instantiating objects straightforwardly, using the ‘new’ keyword. These creational design patterns provide more flexibility and increase the reusability of code.</p><p>examples:</p><ul><li><em>Singleton Design Pattern</em></li><li><em>Factory Design Pattern</em></li><li><em>Abstract Factory Design Pattern</em></li><li><em>Builder Design Pattern</em></li><li><em>Prototype Design Pattern</em></li></ul><p><strong>Structural Pattern</strong>— These design patterns are involved to define the composition and structure of classes and objects and this uses the concept of inheritance for the procedure of composing interfaces and by using this type of design pattern, it helps to provide flexibility to classes and objects.</p><p>examples:</p><ul><li><em>Adapter Design Pattern</em></li><li><em>Composite Design Pattern</em></li><li><em>Proxy Design Pattern</em></li><li><em>Fly Weight Design Pattern</em></li><li><em>Facade Design Pattern</em></li><li><em>Bridge Design Pattern</em></li><li><em>Decorator Design Pattern</em></li></ul><p><strong>Behavioral Pattern</strong> — This pattern is involved in communication (eg: how communication happens, when objects interact with each other), interaction and assignment of responsibilities between objects.</p><p>examples:</p><ul><li><em>Template Design Pattern</em></li><li><em>MediatorDesign Pattern</em></li><li><em>Chain of Responsibility Design Pattern</em></li><li><em>Observer Design Pattern</em></li><li><em>Strategy Design Pattern</em></li><li><em>Command Design Pattern</em></li><li><em>State Design Pattern</em></li><li><em>Visitor Design Pattern</em></li><li><em>Iterator Design Pattern</em></li><li><em>Interpreter Design Pattern</em></li><li><em>Memento Design Pattern</em></li></ul><p>From the upcoming stories, I will talk about some of the design patterns. GoodBye for now…..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qyzdIvNA4KMnI0M8RwLixA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=878d6d2be4a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Importance of Date and Time in the programming world.]]></title>
            <link>https://roshnisilva.medium.com/importance-of-date-and-time-in-the-programming-world-2819ccd26325?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/2819ccd26325</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Sat, 11 Dec 2021 09:38:11 GMT</pubDate>
            <atom:updated>2021-12-11T09:38:11.021Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Hbr0dsGQsD77zsGfZIfHg.jpeg" /></figure><p>Date and Time, are just two different words. sometimes you might feel like this but most of the things in this world are totally dependent on a date and time. For me, now it&#39;s the 10th of December 2021 and here the time is 4.38 pm. So does that mean each and everyone on this globe has the same time?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*twafQths85zQ23Dw6QHVBw.png" /></figure><p>No………Not even the time, but some humans in the world almost passed to 10th December why because as far as we know this Earth is globe shape and its rotates around its axis and revolves around the sun. Due to this rotation fact, we can understand that the sun can’t face everywhere on the earth at the same time. So some of the people in the world enjoy their day time while some enjoy their the night.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*rngtlr5-OHKCAFsBJ1PhIg.gif" /></figure><p>Now for a moment let’s imagine if the entire world has a one time zone. So what will happen now? for some places it would be noon, for some it would be early morning and for some, it would be evening. Since the different places of the earth enter and exit daylight at different times, so different time zones were introduced. These time zones are measured from a starting point centered at England’s Greenwich Observatory and this point is called Greenwich Meridian or Greenwich Mean Time (GMT) and according to this time zone, each is given an offset which is either positive or negative value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hwZ_afKq5_JWVTSKG63aCA.png" /><figcaption>Time Zones : <a href="https://en.wikipedia.org/wiki/File:Standard_Time_Zones_of_the_World_(October_2015).svg">https://en.wikipedia.org/wiki/File:Standard_Time_Zones_of_the_World_(October_2015).svg</a></figcaption></figure><p>So what is this UTC? UTC is standard for Universal Time Coordinated and this is not a time zone but the primary time standard used to represent time zone. The time of each and every country is calculated based on this.</p><p><em>Let’s consider Sri Lanks as the example: we use IST (Indian Standard Time) and our time is 5 hours and 30 minutes ahead of UTC so we can represent our time as UTC + 5.30.</em></p><p>Before jumping into the programming world, we need to know one more fact which is DST.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*autCXxiySZcM1djtG8Enuw.jpeg" /></figure><p><strong>DST</strong> is standard for Daylight Saving Time, It is the practice of advancing clocks (by 1 hour)during the summer month, therefore, darkness comes at a later clock time and backward by one hour during the autumn.</p><p>As a programmer, having an understanding of how data and time work in different places in the world is really important when we handle date and time-related data.</p><p><em>You are in Sri Lanka. Now just assume a scenario where you have to implement a system to keep track of the arrival and departure times of employees to a global company and you implemented that system and deployed it into the cloud but you keep all the date and time records according to your time zone. This will create a problem if the client is from a different region such as the US wants to see records according to his time zone, but your application represents the records in Sri Lankan time. </em>Now<em> </em>you are in a big mess 🤯…</p><p>Now you can feel how important is to handle dates and times in a correct manner in the programming world. If you don’t handle date and time in a correct manner you will be ended up with large issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*eSQbKipwaInGAyoKIITxHw.gif" /></figure><p>In a programming world, we have lots of programming languages but via this story, I take you guys through only JAVA and Java.time, java.sql, java.text and java.util packages contain lots of classes to represent date and time. Let’s see what are some classes available in java to handle date and time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*381-Cv96q3Qkl7tlydWycg.png" /></figure><p>ARE YOU READY TO FOLLOW?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*EB_wbdVopkiHJgPaiX0KnA.png" /><figcaption>let’s go one by one</figcaption></figure><h3><strong><em>Java Classical Date/Time API</em></strong></h3><blockquote><strong><em>java.time.LocalDate class </em></strong>— <em>represents the date and time in java and this provides constructors and methods to deal with date and time. we can use this to represent time according to the relevant time zone.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iVd8Gnuvwa-aK9TVCC70gw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21Hvhwh-LP19MKiA31BkNg.png" /></figure><blockquote><strong><em>java.sql.Date class </em></strong>— <em>This is only capable of representing the date in YYYY-MM-DD format in java and it inherits java.util.Date class. Since it represents the date that can be sorted in the database, JDBC is used this mostly.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ri5zKlOU60H6oiVWLNxLBQ.png" /></figure><blockquote><strong><em>java.util.Calendar class</em></strong> — <em>This class provides methods to converting date between a specific instant in time and set the calendar fields and this class is also an abstract class that inherits object class and implements the comparable interface. There are lots of pre-defined methods in this class and </em>getInstance()<em> method is used to get the instance of the calendar according to the current time zone.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*evsGjCc6YMK-nfzwXwoVrw.png" /></figure><blockquote><strong><em>java.util.TimeZone class</em></strong> — <em>Object class is inherited by this class. This TimeZone class is used to represent a time zone offset and also this helps to figure out daylight saving. This class also contains some of the pre-defined methods and method </em>getDisplayName()<em> is used to get the default time zone.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZaBtM2lbQvIeYcidZRcM4g.png" /></figure><h3>Java 8 Date/Time API</h3><blockquote><strong><em>java.time LocalDate class</em></strong> — <em>If a programmer uses this class, this provides a date with a default format of YYYY-MM-DD. This class also inherits an object class and an immutable class. We can use LocalTime object without the timezone information. Out of many methods available in this class </em>now()<em>, the method is used to get the current date.</em></blockquote><blockquote><strong><em>java.time LocalTime class</em></strong><em> — This class has the ability to specify a specific time of the day without any timezone information. this class includes several pre-defined methods out of these </em>now()<em> is used to get the current time. This LocalTime class is an immutable class and it represents the time with the default format of hour-minute-second.</em></blockquote><blockquote><strong><em>java.time LocalTime class</em></strong><em> — This class represents a local date and time both without any information about the timezone. This class is somewhat a combination of java 8 LocalTime and LocalDate classes and it represented date-time, with the default format as YYYY-MM-DD dd-HH-mm-ss.zzz. This is an immutable and inherits object class. </em>now()<em> method in this class is used to get the current date and time.</em></blockquote><blockquote><strong><em>java.time ZoneDateTime class</em></strong><em> — This represents date-time with a timezone. This class is used to store date and time fields to a precision of nanoseconds and timezone with zone offset. This is mostly used to handle ambiguous local date-time.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kclsjmKYJ7M6n24VXqEmfA.png" /></figure><p>It’s not really simple as you can think when you working with date and time. As a programmer, when you deal with date and time, REMEMBER to choose the right date and time class based on your requirement and make sure you are aware of the data format that you want to keep for the date and time. Lastly, I want you guys to always be aware of time zones and when you should use these time zones while handling dates and times.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*OCsr9cHV1FFwt-1q-cmvSA.jpeg" /><figcaption>See you Later!!!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2819ccd26325" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do you know how computers deal with floating-point numbers?]]></title>
            <link>https://roshnisilva.medium.com/do-you-know-how-computers-deal-with-floating-point-numbers-d23bec8cf953?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/d23bec8cf953</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Wed, 08 Dec 2021 14:28:19 GMT</pubDate>
            <atom:updated>2021-12-08T14:44:04.282Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever thought about how computers gonna dealing with floating-point numbers? As a software engineer, lead software engineer, or programmer, it&#39;s really important to have a good understanding of fundamentals which is a base to building a good career. There are some edge cases where the computer gives some unexpected results to even to your small calculations. These floating-point numbers also lead to giving you an unexpected result for your calculations so it&#39;s really important to know about how computers handle these numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*LFU3yRHQTJNpovbmz4mjlg.jpeg" /></figure><p>In computers, there is a standard called IEEE 754, which is used to represent the floating-point numbers in computers. It takes a floating-point number and divides it into 3 components.</p><ol><li>Sign</li><li>Exponent</li><li>Mantissa</li></ol><p>According to this standard, floating-point numbers are represented in a single or double or in long double precision.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*t6h5ytklocHIO9XSDeYPZA.png" /><figcaption>Representation of Single, Double and Long Double</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*XOd7FDsewQ6WCstU3oa0nw.png" /><figcaption>IEEE 754 Representation for Single Precision</figcaption></figure><p>Let’s take a 9.1 and convert this into IEEE 754 standard and see actually what is the value computer accept as 9.1. In order to convert this decimal value into IEEE 754 format computer follows several steps as below.</p><blockquote><strong>Convert the given floating-point number into a binary representation.</strong></blockquote><blockquote><strong>Convert the binary representation into scientific notation.</strong></blockquote><blockquote><strong>Write the scientific notation in an IEEE 754 format.</strong></blockquote><p>Let’s walk through one by one.</p><p><strong>Convert the given floating-point number into a binary representation.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*Wmjx53PzABXTiokVcOZh0g.png" /></figure><p>According to the above image. We have divided 9.1 into 2 parts which are whole and decimal. After we convert the whole part (9) into binary, the result is <strong>1001 </strong>and after converting 0.1 into binary, the result is <strong>00011001100110011….</strong></p><p>Finally, we have <strong>1001.00011001100110011… </strong>as our binary representation of 9.1</p><p><strong>Convert the binary representation into scientific notation.</strong></p><p>Once we get the binary representation then we can convert it into scientific notation. Now have a look at below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*wJhLrqTqrZ8MJV5VlHe71Q.png" /><figcaption>Scientific notation of 9.1</figcaption></figure><p><strong>Write the scientific notation in an IEEE 754 format.</strong></p><p>The first bit is SIGN, it says if the value is positive then the sign bit becomes 0. Else if the value is negative the sign bit becomes 1. In our case, our value is a positive value so the sign bit becomes 0.</p><p>Then we have EXPONENT, which has 8 bits to represent the exponent component and it has a range -128 to +127. This +127 is called “<strong><em>Exponent Bias</em></strong>” so whenever we have a positive / plus number for exponent we add that into 127 and get the sum in a binary format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*ppVBpGt4tJU97wlSHecz8Q.png" /></figure><p>In our case, we have 2³ where the exponent is positive. So, we add that into 127. Now we have 130. Then we have to convert this 130 into binary format to find the exponent value for 9.1. After we convert it into binary the result is <strong>10000010</strong> which is our exponent value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*5icfIzT5gSteFwNBg-MaCA.png" /><figcaption>The binary value of 130</figcaption></figure><p>Finally, we have MANTISSA, from the scientific notation we can ignore 1 and take the rest as mantissa (23 bits). So now our mantissa for 9.1 is <strong>00100011001100110011001</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*2qpAQb8KgsEoMu2loGDgew.png" /></figure><p>Now our Final IEEE 754 representation for 9.1 is <strong>01000001000100011001100110011001</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*fH6k2PNgRobp6Y6zmE7QcA.png" /></figure><p>But if you go to any calculator which implemented this IEEE 754 standard you won’t get above mentioned answer. Instead, you get <strong>01000001000100011001100110011010 </strong>because in IEEE there is the rule when calculating the mantissa it always checks the 24th bit (if available). If the 24th bit is ‘1’ then it must add it to the 23rd bit in order to round the value. On the other hand, if the 24th bit is ‘0’ then no worries 😏.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*P2vnMHxwqN0-PEOeGkkLlw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*zzhEBfNM470GhcyVJDZTGQ.png" /><figcaption>The way of rounding Mantissa</figcaption></figure><p>Original IEEE 754 Representation for 9.1 is 010000010001000110011001100110<strong>01</strong></p><p>IEEE 754 Calculator Value for 9.1 is 010000010001000110011001100110<strong>10</strong></p><p>There is a difference between the above 2 IEEE 754 representations in the last two digits and we called this ‘<strong><em>Floating Point Rounding Problem in Computer</em></strong>’.</p><p>Now let’s try to convert this IEEE 754 format value back to decimal. What do you think? Do you think that we get our original value (9.1)? The answer is ‘<strong>NOOO</strong>’ you won’t get 9.1 as your answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*IjMf9VhRbHK9ZMbpY2HGOA.png" /></figure><p>Let’s see how this happens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*GoN6C--suT3TwdO2DhwgmQ.png" /></figure><p>As you can see our final value is 9.10000038 but not 9.1. We have an additional value due to the rounding occurring in IEEE 754 standard. So these kinds of problems lead to unexpected results in certain scenarios. As the best practice, we should not use float or double for sensitive data calculations so always make sure to use an appropriate datatype when you do programming.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V5iSJ32c8OtNjzOhvLDHMw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d23bec8cf953" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Division by ZERO is Undefined]]></title>
            <link>https://roshnisilva.medium.com/division-by-zero-is-undefined-8d5d13bbbe8f?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d5d13bbbe8f</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Fri, 03 Dec 2021 14:44:12 GMT</pubDate>
            <atom:updated>2021-12-03T14:44:12.687Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*kyi95DtiPQDMY_IKcI93PA.png" /></figure><p>I hope you all are aware of what is meant by division 😉 and you all know that dividing any given number by 0 is unfortunately impossible but some would think that any number divided by 0 is infinite (<em>∞</em>). <em>Example: 10/0 = ∞, 5/0 = ∞</em></p><p>When it comes to computers, it doesn’t accept that any given number called <em>X </em>divided by 0 is infinite<em>. </em>So the problem is why the computer doesn’t accept this? Let’s try to figure it out.</p><p>We all are know how to perform with division and computer also uses their way to perform division. Now assume the computer wants to divide a given number. what it does is simply subtract the given number until it meets 0.</p><p>For example: Let’s try to with 20/4 &amp; 8/2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*AEMXvisV9Sfz4mO-LSiijw.png" /><figcaption>Example: 20/4</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*qZcE1VhcY836ApFllMRQnw.png" /><figcaption>Example: 8/2</figcaption></figure><p>To come up with an answer to the example 20/4, the computer must subtract 4 from 20, for 5 times to meet 0 also for the example 8/2, the computer must subtract 2 from 8, for 4 times to meet 0.</p><p>In the same way, when a computer performs multiplication, it does addition. <em>For example, will take 4*5 &amp; 2*4.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*w6_rqEDm5NdtWTycEAWbEQ.png" /></figure><p>Now according to this, we are clear, If I take 20/4, the answer is 5. On the other way round, 5*4 is equal to 20 which means if dividend divided by divisor is equal to an answer called ‘y’. On the other hand, the answer called ‘y’ into divisor is equal to the dividend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*Byo4pUBNc8mmF9KLm26cMA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*aVRCG3EgpyGN4dFm0IYGBQ.png" /></figure><p><strong>Let’s back to divide by ZERO</strong></p><p>If we take a look at below,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*wlALPRLmPuXde8Axi0_mCw.png" /></figure><p>According to the mathematical concept that we discussed, 10 divided by 0 is <em>∞ </em>and 1 divided by 0 is also <em>∞. </em>On the other way round, <em>∞ * </em>0 = 10 and <em>∞ </em>* 0 is 1 so this simply means 1 = 10 which is not equal.</p><p>Therefore, any given number divided by 0 is not considered infinite(<em>∞</em>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*jakqWQwLuscBUTm8aMvIdA.png" /></figure><p>Let’s have a look at why dividing by ZERO is undefined.</p><p>Now consider we want to divide 1 by 0, let’s divide 1 by positive decimal numbers and negative decimal numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*ZfSdWW8DBhKbckZG3gmZUw.png" /></figure><p>As we can see, when the number gets near zero and if it is positive then the result is a large value. When it comes to negative then the result takes a very small value. Let’s try to understand this by using a graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4fMUSzNvrP0Uo5R32EV3Ng.png" /></figure><p>According to the above graph, we can see a &amp; b ends going in different directions and c &amp; d ends going in different directions as they never going to meet this is undefinable. Therefore we say any number divided by 0 is undefined. So, this is how we ended up “<strong>Division by ZERO is Undefined</strong>.”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d5d13bbbe8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Are you curious to know what’s inside JVM???]]></title>
            <link>https://roshnisilva.medium.com/are-you-curious-to-know-whats-inside-jvm-f9cfc781556d?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9cfc781556d</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Tue, 30 Nov 2021 11:28:51 GMT</pubDate>
            <atom:updated>2021-11-30T11:34:52.199Z</atom:updated>
            <content:encoded><![CDATA[<p>From the last story I walked you guys through the basics of JVM but today I would like to take you guys inside to JVM.</p><p>So, guys are you ready to go inside JVM??</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*SH0_3SCDWIeVjErUUHpmXg.png" /></figure><p>This storyline started with ‘once upon a time, someone in the world succussed to open up this JVM, which cannot be found in the reality but he could able to find three main components inside this JVM’. Now let’s talk a bit more about the main components. Below is the simple scenario which tells about 3 tasks involved (load, store and execute) with main 3 components in a very simple manner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aSjKTZmZP37q3Tu46gFS_g.png" /></figure><p>Now we have ClassLoader, Memory Area and Execution Engine. Here onwards we’ll look into each and every component separately so let’s start off with ClassLoader.</p><p><strong>ClassLoader</strong></p><p>ClassLoader is there to load .class files (btye code) into JVM and this ClassLoader is mainly responsible for ‘LOADING’, ‘LINKING’ and ‘INITIALIZATION’.</p><p><em>1 LOADING</em> — The main job of loading is taking .class files and put into JVM. So, when doing this mechanism JVM reads below information</p><p>· The fully qualified name of your loaded class</p><p>· Information about variables</p><p>· Immediate parent of your loaded class</p><p>· Whether your loaded class is a class, an interface or an enum</p><p>Now let’s go with a simple scenario.</p><p>Assume now you have Student.java class and complier convert this into .class file. Now you have Student.class file. Now ClassLoader is there to load your file into JVM and now it reads all the above-mentioned information then JVM creates an object from class type (Guys please don’t confuse. Here I’m not talking about your class which is ‘Student’ but there is a special type in java called ‘Class’) and it assigns this student into that class type object and put into the heap.</p><p><em>****Note — At the very first time only JVM creates an object from the class type for each loaded java class and stores it into the heap.</em></p><p>Okay, now you might think ‘what this is the heap?’. I’ll explain to you guys what is heap please be read forward.</p><p><em>2 LINKING</em> — This can be further divided into 3 parts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*Pubcikk7IMipFGbPQlgEKw.png" /></figure><p><em>3 INITIALIZATION</em> — In this phase mainly focuses on assigning original values back to the variable as mentioned in the java code and executes static block or static method. Most importantly, JVM enforces the initialization must do before a class becomes an active use. Now let’s have a look at what are the active use cases of a class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*7qZDtkDeDZeJBo0taJVIYQ.png" /></figure><p>Guys, let’s keep a full stop to ClassLoader and move further.</p><p><strong>Memory Area</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6exzbeQMlCc1RR_V0fSkPA.png" /></figure><p>Soooo….. here we come to the Memory Area component and let’s talk about it.</p><p>According to the above diagram, the memory area has 5 different sub-areas in it. Now let me talk to you about each and every sub-area with a brief discussion.</p><p><em>1 METHOD AREA</em>- Loads class data into here and this holds information about class data such as static variables, blocks and static methods and immediate parent class name (if any) and etc.</p><p><em>2 HEAP AREA</em>- when loading classes this is the place all objects are stored.</p><p><em>Note –There is only one method area and a heap per JVM.</em></p><p><em>3 STACK</em>- In a simple term, this place keeps information of methods, local variables. Stack uses the Last-In-First-Out approach. A block of stack area we called as ‘frame’ and holds local variable information of a method call. When we call a particular method then it will create a frame and once the method execution is over it will remove a particular frame.</p><p><em>4 PC REGISTERS</em>- This holds information about the next execution (if not a native method) and each thread holds its own PC register.</p><p><em>5 NATIVE METHOD AREA</em>- This holds the information about the native methods which are written in language other languages such as C or C++.</p><p><em>Are you ready to go through the final component of JVM??</em>😎</p><p><strong>Execution Engine</strong></p><p>Execution of the bytecode happens here and it executes the bytecode line-by-line and the execution engine states the priority which class to be executed first. When we talked about sub-components of the execution engine, it has an Interpreter, JIT (Just In Time) Compiler and finally Garbage Collector.</p><p><em>1 INTERPRETER</em>– This reads bytecode then interprets them and executes line by line. Due to that this becomes slow. The downside of this interpreter is that whenever the same method is invoked multiple times, each and every time a new interpretation is needed which caused a reduction in the performance of the system. So, this is where the JIT compiler takes place.</p><p><em>2 JIT COMPILER</em>- In order to overcome the downside of an interpreter, JIT compiler runs parallel to the interpreter. It uses to identify repeated code in the program while the interpreter executes the bytecode line-by-line. JIT compiler compiles bytecode into machine code and this machine code is stored in the cache. Later these codes are used whenever the code is repeated. So, this literally leads to increase performance in java applications.</p><p><em>3 GARBAGE COLLECTOR</em>- The name itself tells it is there to collect garbage or unused objects. This checks the heap area and finds out are there any unreferenced objects. If so this will destroy those objects from the heap and clean the memory. The garbage collector follows Mark and Sweep steps when he is doing his job.</p><p>· Mark — In this step garbage collector identifies the unused objects in the heap.</p><p>· Sweep — In this step garbage collector removes identified unwanted objects from the heap area.</p><p>Guy… I hope you have got some knowledge from this story regarding the inside view of JVM. Now it’s time to come out from JVM and take note of what you have learned.👻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9cfc781556d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hey Guys!!!!! Do you really think that you know the basics about JVM??]]></title>
            <link>https://roshnisilva.medium.com/hey-guys-do-you-really-think-that-you-know-the-basics-about-jvm-31b1656c843e?source=rss-1b7234a91810------2</link>
            <guid isPermaLink="false">https://medium.com/p/31b1656c843e</guid>
            <dc:creator><![CDATA[Roshni Silva]]></dc:creator>
            <pubDate>Sun, 28 Nov 2021 10:52:22 GMT</pubDate>
            <atom:updated>2021-11-28T10:52:22.884Z</atom:updated>
            <content:encoded><![CDATA[<p>So, your answer would be either ‘YES’ or ‘NO’… No worries let’s try to figure it out also remember I am not going to discuss the entire information about JVM but let’s give a START.</p><p>Most people think that they know about Java Fundamentals including myself. Hey, guys please don’t be angry about me note that I am not talking about everyone but of course about a few or more people. When we talk about Java fundamentals, we can’t forget about JVM, JRE and JDK. So here I mainly take you guys through a bit about JVM.</p><p>Let’s take look at the below conversation which is a small part of the JAVA Software Engineer interview session.</p><p>“<em>Interviewer: …. Hey, so let me take you through some of JAVA fundamentals. Can you explain to me what is JVM?</em></p><p><em>Interviewee: JVM standard for Java Virtual Machine and it executes java byte codes further it converts compiled bytecode into machine code.”</em></p><p>I am not trying to say each and everyone would come up with an above answer but a few or more use the above answer when someone asked about JVM also I’m not trying to tell that the above answer is completely wrong but of course above answer is not fair. Here I used a term called ‘fair’ instead of wrong. Now, let’s see why I used ‘fair’. Assume someone comes and asks you to explain about your mother so what if you just tell, she is the one who prepares your meal every day so now you might feel ‘Okay this is somewhat true. Yes of course this is somewhat true but this is not a fair answer why because out of 1000 + tasks, preparing your meal is just one task that your mother does for you. If you give such an answer this is not really fair RIGHT??</p><p>As similarly, the above answer regarding JVM is just one point that you trying to show.</p><p>Ooopsss!!! I forgot to tell you. When someone answers the above mentioned JVM question, a few or more people sometimes tell that it is inside the JRE. So, guys what do you think is that really TRUE??? Let’s see. Now let me put full stop storytelling session and walk you guys through to the real flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iK1EFamgj6pjOvuhROEFNA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*YGIxUGj-i4Rg0hm0ItAecQ.png" /><figcaption><strong>???????</strong></figcaption></figure><p><strong>JVM</strong> these three letters standard for Java Virtual Machine so the name itself tells that it is a virtual machine. Now some of you might think what does it really mean. It means this is a machine that does not exist in the reality or is not available physically further you cannot go and touch or find this machine in the real environment/ in your computer. Do you ever try to download and install this so-called JVM on your computer? Obviously, the answer would be NO because this JVM is nothing that exists physically or entity-wise.</p><p>Let’s go a little bit more with virtual machines then you can easy to find what is JVM.</p><p>I hope now you have somewhat an idea about what is a virtual machine. There are two types of virtual machines which are System based Virtual Machines (SVM) and Application based Virtual Machines (AVM).</p><p>· SVM — This type of virtual machine is used one or more hardware and it creates multi environments which are totally independent from each other.</p><p>Examples: Hypervisor, XEN</p><p>· AVM — This type of virtual machine has not involved any type of hardware instead it may have software to create a platform to run the programs. Since these types of virtual machines are applications, we can consider this as a process so we can call this a Process based Virtual Machine as well.</p><p>Examples: JVM, CRL (Common Runtime Language)Now back JVM</p><h3><em>Now back to JVM</em></h3><p>So JVM is a virtual machine that does not exist in reality and fallen under Application or Process based virtual machine and this JVM is completely a specification that tells what needs to be done in order to run a java program also you need to remember when the time you install JRE (Java Runtime Environment) which is an implementation which illustrates how the things need to be done, it deploys all the code which required to create JVM. When the time we start running our Java program, it will create a JVM instance, in another word if you are not executing any program on your computer at that time remember you don’t have any JVM instance on your computer. Assume now you are running a ’n’ number of different java programs, at that time there are ’n’ number of JVM instances are available on your computer. So, the point to remember is that JVM is not static or constant on the computer. Now we are aware of at what time this JVM instance gets created and let’s see how this JVM instance gets destroyed. We have two ways of destroying this JVM instance. First, in java, we have daemon and non-daemon threads. When a java program started it creates a JVM instance and that JVM instance creates a non-daemon thread. When the time the last non-daemon thread gets dies, at that time JVM also gets destroyed. Second, the program itself has to call the system exit() method.</p><p>JVM is platform-dependent because the JVM instance takes care of reading class files and converting that into machine language which is your operating system understands.</p><p>JVM topic is kind of huge range topic so there are many things that you need to aware of JVM but above I have discussed simple and very basic points about JVM and hope to cover some more points in JVM in upcoming stories.</p><blockquote>Finally, I hope now you guys can come up with a fair answer to the question <strong>‘Can you explain about JVM?’</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31b1656c843e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>