<?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 Udara Chinthaka on Medium]]></title>
        <description><![CDATA[Stories by Udara Chinthaka on Medium]]></description>
        <link>https://medium.com/@udarachinthaka135?source=rss-8433707b13fd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xSFbUCbs6VnsbR_4r4v9Sw.png</url>
            <title>Stories by Udara Chinthaka on Medium</title>
            <link>https://medium.com/@udarachinthaka135?source=rss-8433707b13fd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 18 May 2026 06:31:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@udarachinthaka135/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[WEB & REST API]]></title>
            <link>https://medium.com/@udarachinthaka135/web-rest-api-1d432b0fafd1?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d432b0fafd1</guid>
            <category><![CDATA[statelessness]]></category>
            <category><![CDATA[rest-principle]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[webapi]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sat, 17 Aug 2024 09:51:31 GMT</pubDate>
            <atom:updated>2024-08-17T09:51:31.042Z</atom:updated>
            <content:encoded><![CDATA[<p>let’s explore API</p><p>API stands for Application Programming Interface, a standardized mechanism for enabling interaction between software components. For instance, the weather data displayed on your mobile device is typically sourced through an API. Services such as Weatherstack provide real-time meteorological data accessible via API requests. An API specifies the methods and data types for facilitating communication between software systems. In the technical world, there are many kinds of API.</p><p>Here’s a detailed technical overview of how weather data is retrieved and displayed on a mobile device through an API call:</p><ol><li><strong>Application Initialization</strong>: The user launches the weather application or utilizes an integrated weather service on the mobile device.</li><li><strong>API Request</strong>: The application constructs and sends an API request to the weather service, querying for the current weather conditions at the user’s location.</li><li><strong>API Response</strong>: The weather service processes the request and returns a response containing the weather data for the specified location.</li><li><strong>Data Presentation</strong>: The application processes the API response and renders the weather information in a user-friendly format on the device’s interface.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hpdoB7ciGtx8h_Bnm2RTww.png" /><figcaption>API call for retrieve weather info</figcaption></figure><h3>What is the Web API?</h3><p>Again, it’s same as I explained API. Here I will go through another example.</p><p>In simple terms, Web API is like a bridge that allows to communicate with different software components with each other <strong>over the internet</strong>. Just imagine below real-world example.</p><p>Imagine you want to order a pizza using a food delivery app on your phone. Here is how a web API helps make that happen:</p><ol><li><strong>Placing an Order</strong>: You open the food delivery app and choose your pizza.</li><li><strong>Sending a Request</strong>: When you press the “Order” button, the app sends a request to the pizza restaurant’s online system. This request is like a message saying, “Please prepare a pizza with these specifications for this delivery address.”</li><li><strong>Restaurant’s Response</strong>: The pizza restaurant’s system receives the request, prepares the pizza, and sends back a response to the app. This response might include details like the order confirmation, estimated delivery time, and tracking information.</li><li><strong>Showing the Details</strong>: The app then displays this information to you, letting you know that your pizza is being made and giving you updates about the delivery.</li></ol><p>In above example, your mobile app and restaurant system are the two different software components. So, the bridge will be the API call that carries your order details to the restaurant.</p><p>There are many kinds of bridges (APIs). Below are the main APIs that are using in the worldwide application.</p><p>- <strong>REST APIs</strong> — This API stands for HTTP methods like put, post get and delete etc. these methods are stateless which means API will return all the required information needed to fulfill request.</p><p>- <strong>SOAP APIs</strong> — Simple Object Access Protocol APIs uses XML based message protocol. This is mainly using in ERP level bank applications.</p><p>- <strong>Private APIs</strong> — The APIs use only within an organization like HR system connect to payroll system.</p><p>- <strong>Open APIs</strong> — These are available for any developer to use like Google map API, Twitter API etc.</p><p>- <strong>Streaming APIs</strong> — continues flow of real time data. Twitter, Facebook streaming APIs are the example for this.</p><p>- <strong>GraphQL APIs</strong> — one of best alternative for REST APIs. It provides exactly what client expect from the server.</p><h3>What is the REST API?</h3><p>REST stands for representational state transfer. In technical world, this type of APIs introduces as RESTful API, RESTful web API. <strong>It is a type of Web API.</strong></p><p>There are different methods, functions that client can access server data using http protocol. Put, get, delete post are the main functions that are using in rest APIs. This provides a flexible and lightweight way to integrate between client and server applications.</p><p>Specially, response for the REST APIs calls is plain text. No graphical rendering to the client side. This is calling statelessness which is main feature of REST API.</p><p>There are six main principles in REST API as below.</p><h3>Client server decoupling</h3><p>Server and client applications must be independent. Client application is directly representing the presentation layer and UI while sever or backend application representing the business logic and process the requests from the client.</p><h3>Statelessness</h3><p>Statelessness means each API requests should contain all the data for processing the requests like API token, headers etc. REST APIs do not require to store any server-side sessions. The REST API ensures that each request is self-sufficient and not dependent on server-side session management.</p><h3>Catchability</h3><p>This principle is mainly used where the same response should be expected from the client. There are some certain scenarios where we can use catchability.</p><p>o Cacheable response.</p><p>o HTTP caching headers</p><p>o Caching request and sub requests.</p><p>The main goal is doing the caching is to improve the performance, reduce the load and improve the efficient of data retrieval.</p><h3>Layered system architecture</h3><p>This is a system design architect where we can implement with the RESTful APIs. There are few main layers as below.</p><ul><li>Presentation layer: this is the client-side application where the client or user interact with UI.</li><li>API layer: handle the API request and response.</li><li>Service layer (business logic): handle the business logics, authentication and data validation etc.</li><li>Data access layer: manage the data revival and storage including perform CRUD operations.</li><li>Database layer: The actual database or data store where the persistent data resides.</li></ul><p>This is one of most important principle in REST APIs and it has many benefits like modularity, reusability, scalability, flexibility, code readability, testability etc.</p><h3>Code on demand</h3><p>This is an optional principle in REST API. Servers can extend the client’s functionality by transferring executable code (e.g., JavaScript).</p><h3>Uniform interface –</h3><p>Below points are the main breakdown of uniform interface.</p><p><strong>1. Identify the resource</strong></p><ul><li>You must use unique URI (uniform resource identifier) to identify the exact resource (user/423). This means that the structure of URIs is consistent across the API, making it predictable and easier to use.</li></ul><p><strong>2. Manipulation of resources through representations.</strong></p><ul><li>Use the http standards communication methods like put, post with the help of unique URI.</li></ul><p><strong>3. Self-descriptive messages</strong></p><ul><li>Each message (request or response) includes enough information for the client or server to understand how to process it. For example, HTTP headers provide metadata about the request or response, like content type or caching directives.</li></ul><p><strong>4. Hypermedia as the engine of the application (HATEOAS)</strong></p><ul><li>HATEOAS is a constraint where the API provides hypermedia links within responses. These links guide clients on how to interact with the API further, allowing clients to navigate the API dynamically based on the responses they receive.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i2MQ0WNT4KlG0HzRXuP6Eg.gif" /><figcaption>REST API (Source: begintechnicalwriter)</figcaption></figure><h3>Key Differences Of WEB &amp; REST API</h3><ul><li><strong>Scope</strong>: “Web API” is a broad term encompassing any API accessed via the web. “REST API” refers specifically to APIs that follow REST architectural principles.</li><li><strong>Design Constraints</strong>: REST APIs adhere to specific constraints and principles (e.g., statelessness, uniform interface), while web APIs may not follow these constraints and can vary widely in design and implementation.</li><li><strong>Use Cases</strong>: REST APIs are commonly used for web and mobile applications due to their simplicity and ease of integration. Web APIs can be used for various purposes and may not always follow REST principles.</li></ul><p>In summary, while all REST APIs are web APIs, not all web APIs are RESTful (like SOAP request). REST is a subset of web APIs that follows a specific set of guidelines and principles designed to standardize and simplify web interactions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d432b0fafd1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Jump Search Algorithm]]></title>
            <link>https://medium.com/@udarachinthaka135/jump-search-algorithm-488c0f6e293d?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/488c0f6e293d</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[jump-search]]></category>
            <category><![CDATA[linear-search]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Tue, 06 Aug 2024 21:31:51 GMT</pubDate>
            <atom:updated>2024-08-06T21:35:20.142Z</atom:updated>
            <content:encoded><![CDATA[<p>It is one of quite simple algorithms to understand but complex than the linear search. The explanation for jump search is very simple as below. Before proceeding further, you should have little knowledge about java programming like conditional loops, return, method signatures and arrays etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B90hx9K1r5p4DJDA6YRnIg.png" /><figcaption>Jump Search</figcaption></figure><p>Jump search is an algorithm used for searching in a sorted array. It is faster than linear search but simpler than the binary search. It is designed to jump a fix number of steps and perform a linear search withing a small array segment. This can be used where you want to apply simplicity of linear search and efficiency of binary search.</p><p>Here are the main steps that we can see in the jump search.</p><p>1. Get the array length and calculate the fixed step.</p><p>2. Start at the beginning and jump ahead then check if the value we are looking for is inside the array.</p><p>3. One we found the array segment (less than or equal to target element) perform linear search within specific array segment.</p><p>4. Once we found the target element return it or if the element does not exist return minus value.</p><p>Let’s checkout simple code block in java. Below is the complete code that we are going to go through. Do not worry, I will explain each line of code later this blog. Just check the below code and scroll down.</p><pre>public class JumpSearch {<br><br>    public static int jumpSearch(int[] arr, int x) {<br>        int n = arr.length;<br><br>        // Set block size to be jumped<br>        int step = (int) Math.floor(Math.sqrt(n)); // step will be 3<br><br>        /*<br>         * Math.floor - will round number to the nearest integer<br>         * Math.sqrt - will return square root of a value<br>         * Math.min - will return minimum value<br>         */<br><br>        // Set the block where element is present (if it is present)<br>        int previouseValue = 0;<br>        for (int minStep = Math.min(step, n) - 1; arr[minStep] &lt; x; minStep = Math.min(step, n) - 1) {<br>            <br>            previouseValue = step;<br>            step += (int) Math.floor(Math.sqrt(n));<br><br>            if (previouseValue &gt;= n)<br>                return -1;<br>        }<br><br>        // Doing a linear search for x in block<br>        // beginning with previouseValue.<br>        while (arr[previouseValue] &lt; x) {<br>            previouseValue++;<br><br>            // If we reached next block or end of array, element is not present.<br>            if (previouseValue == Math.min(step, n))<br>                return -1;<br>        }<br><br>        // If element is found in the array<br>        if (arr[previouseValue] == x)<br>            return previouseValue;<br><br>        return -1;<br>    }<br><br>    // Driver program to test function<br>    public static void main(String[] args) {<br>        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };<br>        int x = 6;<br><br>        // Find the index of &#39;x&#39; using Jump Search<br>        int index = jumpSearch(arr, x);<br><br>        // Print the index where &#39;x&#39; is located<br>        System.out.println(&quot;Number &quot; + x + &quot; is at index &quot; + index);<br>    }<br>}<br><br></pre><p>Let’s examine the main method in detail.</p><p>We have an array called <strong>‘arr’</strong> that keeps sorted list of numbers. <strong>X</strong> is the target value that we are going the find the index. Once we called the <strong>jumpSearch</strong> method, it will return the index where the target value is in. If not, it will <strong>return -1</strong>. Finally, output will be printed on the console.</p><p>Let’s move to the jumpSearch method. In there we are accepting array and target value as parameter.</p><pre>public static int jumpSearch(int[] arr, int x) {<br>        int n = arr.length; // get the array lenght<br><br>        // Set block size to be jumped<br>        int step = (int) Math.floor(Math.sqrt(n)); // step will be 3<br><br>        /*<br>         * Math.floor - will round number to the nearest integer<br>         * Math.sqrt - will return square root of a value<br>         * Math.min - will return minimum value<br>         */</pre><p><strong>int n: </strong>store the array length. In our case, length is 10.</p><p><strong>int step:</strong> store the block size to be jumped through the array.</p><p>Hope you are aware about the Math functions. If not, please check the comment in above code segment.</p><p>Let’s get a clear idea about the for loop,</p><pre>// Set the block where element is present (if it is present)<br>        int previouseValue = 0;<br>        for (int minStep = Math.min(step, n) - 1; arr[minStep] &lt; x; minStep = Math.min(step, n) - 1) {<br>            <br>            previouseValue = step;<br>            step += (int) Math.floor(Math.sqrt(n));<br><br>            if (previouseValue &gt;= n)<br>                return -1;<br>        }</pre><p><strong>minStep:</strong> will be 2. (step =3 and n = 10, therefore Math.min will return 3. so, after -1 it will be 2)</p><p><strong>previouseValue: </strong>will be 3</p><p><strong>step:</strong> will be 6 (this is because addition assignment operator).</p><p>Finally, there is a condition that checks <strong>previouseValue</strong> is greater than or equal to <strong>n</strong>. Surely, this is false because n is 10 and <strong>previouseValue</strong> is 3. So, as you know, for loop keep recurring until its condition gets false. Check the below figure which helps you to get an idea.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0BTl6uLl014J5IYsMwTrKw.jpeg" /><figcaption>values withing the loop</figcaption></figure><p>Loop will be executing for the second time and the loop condition will be false.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0zcfkqWEeN69CNnLCyXK8w.png" /><figcaption>arr[minStep] and target value</figcaption></figure><p>The second execution in the loop, condition will be false as it checks <strong>minStep</strong> value in the array is less than out <strong>target value (x)</strong>. So, condition will be false as in the above image.</p><p>It is clear now value of both <strong>arr[minStep] and X is 6</strong>. Therefore, <strong><em>condition will be false as it checks arr[minStep] less than X</em></strong>. So program will exit from the for loop and will start the while loop.</p><pre>   // Doing a linear search for x in block<br>        // beginning with previouseValue.<br>        while (arr[previouseValue] &lt; x) {<br>            previouseValue++;<br><br>            // If we reached next block or end of array, element is not present.<br>            if (previouseValue == Math.min(step, n))<br>                return -1;<br>        }</pre><p>While loop will be performed below steps.</p><p><strong>Step 01: first execution of while loop</strong></p><p>The value of <strong>previouseValue is 3.</strong></p><blockquote><strong>arr[previouseValue] = 4</strong></blockquote><blockquote><strong>X = 6</strong></blockquote><p>arr[previouseValue] &lt; X is true. Therefore, 1 will be added to previouseValue. Then value of <strong>previouseValue is 4.</strong></p><p>Next, if condition will be false as min function returns 6 and previouseValue is 4.</p><p><strong>Step 02: second execution of while loop</strong></p><p>The value of <strong>previouseValue is 4.</strong></p><blockquote><strong>arr[previouseValue] = 5</strong></blockquote><blockquote><strong>X = 6</strong></blockquote><p>arr[previouseValue] &lt; X is true. Therefore, 1 will be added to previouseValue. Hence value of<strong> previouseValue is 5</strong>.</p><p>Next, if condition will be false as min function returns 6 and previouseValue is 5.</p><p><strong>Step 03: third execution of while loop</strong></p><p>The value of <strong>previouseValue is 5.</strong></p><blockquote><strong>arr[previouseValue] = 6</strong></blockquote><blockquote><strong>X = 6</strong></blockquote><p>arr[previouseValue] &lt; X is false now as both values are equal. Therefore, program will exit from the while loop and move into next code lines where we get if condition as below.</p><pre>// If element is found in the array<br>        if (arr[previouseValue] == x)<br>            return previouseValue;</pre><p>Look at the above if condition carefully, we already know that X is our target and 6 is the value of X.</p><p>Check the few lines above, you will find the value of arr[previouseValue]. It is 6. So finally, our target value exists at index 5.</p><p>So, final output will be ‘Number 6 is at index 5’.</p><p><strong>Jump search</strong> is most effective when dealing with large, sorted arrays where binary search might be too complex to implement or when you want a balance between simple linear search and more complex algorithms like binary search.</p><p>In summary, jump search is a practical algorithm that can offer a good balance between the simplicity of linear search and the efficiency of binary search, especially when working with sorted data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=488c0f6e293d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Core Concepts in NodeJS]]></title>
            <link>https://medium.com/@udarachinthaka135/core-concepts-in-nodejs-cbba1aff0e5e?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbba1aff0e5e</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[call-stack]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Thu, 01 Aug 2024 23:23:22 GMT</pubDate>
            <atom:updated>2024-08-01T23:23:22.818Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s learn call stack, callback queue event loop, async and sync..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*pmnWE2pEfFtLbsne1hizSw.png" /><figcaption>Core concepts</figcaption></figure><p>In this article, we will delve into several core concepts of Node.js, including:</p><ul><li>Asynchronous Programming</li><li>Synchronous Programming</li><li>Call Stack</li><li>Callback Queue</li><li>Event Loop</li></ul><p>Whether you are a developer or an IT student, you are likely familiar with terms such as “stack,” “queue,” and “loop” from the context of data structures and algorithms. These concepts are not only fundamental to computer science but also play a crucial role in application development. Let’s explore these concepts in the context of NodeJS.</p><h3>Why is This Important?</h3><p>When developing web applications, it is essential to understand various components, such as functions, APIs, and architectural layers. Equally important is a deep understanding of application performance and event behavior. A solid grasp of the “call stack” is vital for optimizing application performance.</p><p>The call stack is a powerful tool for managing function calls and improving application efficiency. Each call stack can operate independently, handling simultaneous function calls until the program terminates. Understanding how the call stack functions allow developers to write more efficient and responsive code.</p><p>Before diving into the call stack, will have a quick overview about async and sync programming.</p><h3>Synchronous Programming</h3><p><strong>Synchronous programming</strong> means that tasks are executed one after another. Each task must complete before the next one starts. This model is straightforward but can lead to blocking behavior, where the program must wait for each operation to finish before continuing.</p><ul><li>Will be blocked the code.</li><li>Sequential execution</li></ul><p>Good For:</p><p>Tasks where each operation must complete before the next begins, and blocking behavior is acceptable or desired.</p><h3>Asynchronous Programming</h3><p><strong>Asynchronous programming</strong> allows tasks to be executed independently of the main program flow. This means that tasks can start and finish at different times without blocking the execution of other tasks. Asynchronous operations are often handled through callbacks, promises, or async/await syntax.</p><ul><li>Non-blocking.</li><li>Concurrent execution</li></ul><p>Good For:</p><p>Tasks like I/O operations, network requests, or any situation where you want to avoid blocking the main thread and improve performance and responsiveness.</p><p>Hey, Stay focused, we are diving into deeper.. 💡</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*jp83pVlv5TzeknpLPsUw3w.gif" /></figure><h3><strong>What is Call Stack?</strong></h3><p>In simple terms, the call stack is a mechanism used by interpreters, such as JavaScript or Python, to manage script execution and keep track of the currently running functions.</p><p>To dive a bit deeper, the call stack operates on the Last In, First Out (LIFO) principle, similar to the stack structure used in data structures and algorithms (DSA). When a program begins execution, the script may call functions. Each function call is pushed onto the top of the call stack, where it is executed. Once the function completes its execution, it is removed, or “popped,” from the stack by the interpreter.</p><p>The mechanism becomes more complex when considering Web API level executions. In such cases, understanding the “callback queue” and “event loop” is crucial for grasping how asynchronous (non-blocking) execution works.</p><p>To fully understand these concepts, it will be helpful to explore some simple examples. Before diving into those, it’s important to grasp a few foundational concepts.</p><p>Let’s take below code lines.</p><pre>console.log(&#39;START&#39;);<br>setTimeout(()=&gt;{<br>console.log(&#39;1 second timer&#39;);<br>},1000);<br>console.log(&#39;END&#39;);</pre><p><strong>Output:</strong><br><em>START<br>END<br>1 second timer</em></p><p>How this is possible? It is a simple answer that setTimeout function has one second delay therefore, program will take 1 second to print the ‘1 second timer’ text. Specially, java script interpreter is keep running the code and print last log line while waiting for the 1 second. This is called <strong>NON-BLOCKING</strong> in NodeJS.</p><p><strong>Line 01 </strong>print ‘START’ as it is a synchronous code.</p><p><strong>Line 02</strong> will start setTimeout function and wait for one second meanwhile program will be moving on for the next lines of code as this is an asynchronous function.</p><p><strong>Line 05</strong> prints ‘END’ as it is a synchronous code.</p><p>After once second, program will print as ‘1 second timer’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*b62n03yktC0eFzJEOwuvow.png" /><figcaption>simple program for async/sync</figcaption></figure><p>As I explained above, this is one of simplest examples that we can give for non-blocking program. Now, let’s move deeper and see how this is going to handle by the java script interpreter. You will get idea for call stack, node API, callback queue, event loop etc. I will use below simple program from here onwards.</p><p>Look at below code lines, how simple that is.</p><pre>console.log(&#39;program started&#39;);<br><br>setTimeout(()=&gt;{<br>console.log(&#39;1 second passed&#39;);<br>},1000);<br><br>console.log(&#39;Hi UDARA&#39;);<br><br>setTimeout(()=&gt;{<br>console.log(&#39;2 seconds passed&#39;);<br>},2000);<br><br>console.log(&#39;program ended&#39;);<br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*bYLHLJt-N8o5N9Azf8lMPQ.png" /><figcaption>output of above program</figcaption></figure><p><strong>Step 01:</strong></p><p>Now let’s see what happens behind the above code. Firstly, the main function will be added to the call stack and then the <strong>“program started”</strong> string will be printed on the console and the function will be removed from the stack.</p><p>Have a look on below image. Do not forget, all the methods are in the program will be loaded into call stack.</p><pre>console.log(&#39;program started&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lwDhR_M_d5uZY7vq03hVaw.png" /><figcaption>print first line</figcaption></figure><p>As you can see, first log() function is in the call stack and it is done the duty as ‘program started’ is already printed in the output. Hence, log() function will be popped off from the call stack and next function will be loaded.</p><p><strong>Step 02:</strong></p><p>Next, the first <strong>setTimeout</strong> function will be called which must wait for one second to be executed. therefore, it will be moved into node API and next function will be loaded into the call stack.</p><pre>console.log(&#39;1 second passed&#39;);<br>},1000);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NjXmPx2zuvEkxnB19t14Ug.png" /><figcaption>move first setTimeout into node API</figcaption></figure><p><strong>Step 03</strong></p><p>First setTimeout function will wait for one second and program will be executing. As a result, “Hi UDARA” text will be printed which is next log function’s task.</p><pre>console.log(&#39;Hi UDARA&#39;);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B_Ji9Vl2zoy7eLUXBnc8zg.png" /><figcaption>print second synchronous code line</figcaption></figure><p><strong>Step 04</strong></p><p>As next step, second setTimeout function will be loaded into call stack and move to Node API as it is having 2 seconds waiting time.</p><pre>setTimeout(()=&gt;{<br>console.log(&#39;2 seconds passed&#39;);<br>},2000);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eMC8TDKzcPcFSE2HerpOxg.png" /><figcaption>move second setTimeout function into node API</figcaption></figure><p>Now you can guess the next step. It will be last log function. You will see ‘program ended’ text in the console. Log and main functions will be popped off from the call stack.</p><p><strong>Step 05</strong></p><p>All the log functions has been finished except setTimeout functions.Hence, Call back queue and event loop are going to start their task resulting first time out function is moved into call back queue which was waiting for one second to be executed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D_cIfAe21eL-tE9qqIZqQw.png" /><figcaption>move first setTimeout function into callback queue</figcaption></figure><p><strong>Step 06</strong></p><p>Event loop is always looking into call stack and if the call stack is empty, setTimeout function will be loaded into the call stack as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*irlCXAoxiViCWCjPc-Xl5g.png" /><figcaption>print ‘1 second passed’</figcaption></figure><p>Once first setTimeout function moved to call stack, second function will move into call back queue, and it will be picked by the event loop then move into the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ruAb4K6XYPvqSTNs5fS1fw.png" /><figcaption>print ‘2 seconds passed’</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8L_gARaROBAvT_cplbaP-w.png" /><figcaption>execution done</figcaption></figure><h3>Let’s have an overview</h3><ol><li><strong>Call Stack</strong>: The call stack is a data structure that keeps track of function calls in a JavaScript application. When a function is called, it gets pushed onto the stack, and when the function returns, it is popped off. In Node.js, the call stack helps manage the execution of synchronous code.</li><li><strong>Node API</strong>: The Node API (or Node.js API) refers to the set of built-in libraries and modules provided by Node.js for performing various tasks such as file operations, networking, and more. These APIs are designed to handle asynchronous operations and interact with the underlying system.</li><li><strong>Event Loop</strong>: The event loop is a fundamental part of Node.js’s architecture. It continuously checks the call stack and the callback queue to determine if there are any functions ready to be executed. The event loop allows Node.js to perform non-blocking I/O operations by processing events and callbacks asynchronously.</li><li><strong>Callback Queue</strong>: The callback queue (or task queue) is where functions that are ready to be executed asynchronously are placed. When the call stack is empty, the event loop takes functions from the callback queue and pushes them onto the call stack to be executed.</li></ol><p>In summary, the call stack manages synchronous execution, while the event loop and callback queue handle asynchronous operations in Node.js.</p><p>Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbba1aff0e5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Binary Search Algorithm]]></title>
            <link>https://medium.com/@udarachinthaka135/binary-search-algorithm-f35c91dddc3c?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/f35c91dddc3c</guid>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[binary-search]]></category>
            <category><![CDATA[algorith]]></category>
            <category><![CDATA[udara-chinthak]]></category>
            <category><![CDATA[time-complexity]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sat, 27 Jul 2024 21:10:23 GMT</pubDate>
            <atom:updated>2024-07-27T21:14:44.662Z</atom:updated>
            <content:encoded><![CDATA[<p>Binary search is one of the most efficient search algorithms used in sorted array.</p><p>The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). This works by repeatedly dividing the search interval in half until the target value is found or the interval is empty. The main task is finding a target value in a sorted array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xrmYSMKgnCRuizPgewyN_A.jpeg" /></figure><p>Without further delay, we will jump into a code example with clear explanations</p><p>I will explain the algorithm using example in Java. Before moving to the complete code, we will look into the main method.</p><pre> public static void main(String[] args) {<br>        // create sorted array<br>        int arr[] = { 2, 4, 6, 8, 10, 12, 14 };<br>        // get the array lenght<br>        int lenght = arr.length;<br>        // set the target value for search<br>        int target = 12;<br>        // call searchIndex method and assign the return result<br>        int result = searchIndex(arr, 0, lenght - 1, target);<br>        // show the result based on the return result<br>        if (result == -1) {<br>            System.out.println(&quot;Unable to fine the given element&quot;);<br>        } else {<br>            System.out.println(&quot;Given element is present at index &quot; + result);<br>        }<br>    }</pre><p>Here we have an array named in arr which is having seven numbers. Hence, length will be 7.</p><p>Number 12 is the one which we are going to find out from the array. So, our target value will be 12.</p><p>In the code, we are passing <strong><em>array, low value, high value and target value</em></strong> into <strong>searchIndex</strong> method then method will return the index where our target value is located else it will return the -1 if the target value is not found.</p><pre>int result = searchIndex(arr, 0, lenght - 1, target);</pre><p>Before moving into the searchIndex method, we should have clear idea on above code line. Here, we are passing 4 parameters into searchIndex method.</p><h3>parameters of searchIndex Method</h3><blockquote><strong>arr</strong>: array name (need to perform the search).</blockquote><blockquote><strong>0 :</strong> lowest index of the array (first index is always 0 in array).</blockquote><blockquote><strong>length -1: </strong>this is to set the highest index. (Array length is 7. In our case, largest index is 6 since counting from 0).</blockquote><blockquote>t<strong>arget:</strong> pass the value to find out the index which we are looking for.</blockquote><p>Let’s move to the searchIndex method. As per the below method, we are accepting the four parameters. Hope there is no doubt about it.</p><pre>public static int searchIndex(int arr[], int low, int high, int target) {<br>        while (high &gt;= low) {<br>            // setup the mid value<br>            int mid = low + (high - low) / 2;<br>            // if the mid is equal to target value, then return particular index<br>            if (arr[mid] == target) {<br>                return mid;<br>            }<br>            // If x greater, ignore left half<br>            if (arr[mid] &lt; target) {<br>                low = mid + 1;<br>            } else {<br>                // If x is smaller, ignore right half<br>                high = mid - 1;<br>            }<br>        }<br>        // If the programme reach here, it means element is not exist<br>        return -1;<br>    }</pre><p>At first, we are setting up the mid index.</p><pre>int mid = low + (high - low) / 2;</pre><blockquote>mid = 0 + (6–0) / 2</blockquote><blockquote>mid = 3</blockquote><p>As explained earlier this algorithm repeatedly dividing the search interval in half, therefore we are using <strong>while</strong> <strong>loop</strong>.</p><pre>// if the mid is equal to target value, then return particular index<br>            if (arr[mid] == target) {<br>                return mid;<br>            }</pre><p>Here is one of my favorite parts in the binary search. As soon as set the mid index, it is checking the array mid index whether is it equal to our target value or not. If it is match, then will simply return the mid index which is holding our target value. This will reduce searching time significantly and making easier to reach out the target value.</p><p>In our case, <strong><em>arr[mid] = 8</em></strong> do not forget that mid is equal to 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jTdc57GTsqr78c9AdGk6hQ.png" /><figcaption>array with low mid and high</figcaption></figure><p>Let’s move to the next part</p><pre> // If target greater, set new low value<br>            if (arr[mid] &lt; target) {<br>                low = mid + 1;<br>            }</pre><p>Now you can understand what we are doing in above code bloc. It is a simple process. if the middle value of array is less than our target value (12) then will set the low value as mid + 1.</p><p>It means, algorithm will ignore the left part of the array since those values are less than our target value (12). <em>(note: we cannot do this in unsorted array).</em></p><blockquote>low = mid + 1 (0 = 3 + 1)</blockquote><blockquote>Now low value is 4.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Og5GQ0evtjwDfNf3-HiF_w.png" /><figcaption>array with new low value</figcaption></figure><p>In our case, below part will not be executed because our target value (12) is greater than array middle value.</p><pre>        else {<br>                // If target is smaller, set new high value<br>                high = mid - 1;<br>             }</pre><p>Therefore, program will repeat the same process again until our target value is found. Loop condition still true. So, it will calculate the middle value again.</p><pre>while (high &gt;= low) {<br>            // setup the mid value<br>            int mid = low + (high - low) / 2;</pre><blockquote>mid = low + (high — low) / 2</blockquote><blockquote>mid = 4 + (6–4) / 2</blockquote><blockquote>mid = 5</blockquote><p>Now, mid value is 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8XHMXiyTVDWVDWz0LS87XA.png" /><figcaption>array with new mid value</figcaption></figure><p>So look at the below code part which is going to execute in next millisecond.</p><pre>// if the mid is equal to target value, then return particular index<br>            if (arr[mid] == target) {<br>                return mid;<br>            }</pre><p>As per the above figure, our target value (12) is located at our mid index. Therefore, mid will be returned by the program.</p><p>At last, result variable value will be 5 and output will be “Given element is present at index 5”</p><pre>int result = searchIndex(arr, 0, lenght - 1, target);<br>        // show the result based on the return result<br>        if (result == -1) {<br>            System.out.println(&quot;Unable to fine the given element&quot;);<br>        } else {<br>            System.out.println(&quot;Given element is present at index &quot; + result);<br>        }</pre><p>Have a rest and check below key points</p><h3>Below are the key points of the Binary Search</h3><p>1. <strong>Initialization:</strong> Start with two pointers, one at the beginning (low) and one at the end (high) of the array.</p><p>2. <strong>Calculate Middle:</strong> Compute the middle index mid of the current range:<br> mid=low + (high-low) / 2</p><p>3. <strong>Comparison:</strong> Compare the target value with the middle element of the array:</p><ul><li>If the target value equals the middle element, the search is successful, and the index of the middle element is returned.</li><li>If the target value is less than the middle element, adjust the high pointer to mid - 1 to search in the left half.</li><li>If the target value is greater than the middle element, adjust the low pointer to mid + 1 to search in the right half.</li></ul><p>4. <strong>Repeat:</strong> Continue the process of recalculating the middle and adjusting the pointers until the low pointer exceeds the high pointer or the target value is found.</p><p>5. <strong>Termination:</strong> If the target value is not found by the time low exceeds high, the target is not present in the array.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f35c91dddc3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linear Search Algorithm]]></title>
            <link>https://medium.com/@udarachinthaka135/algorithm-part-01-d38722cd5e6e?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/d38722cd5e6e</guid>
            <category><![CDATA[linear-search-algorithm]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[index]]></category>
            <category><![CDATA[linear]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sun, 07 Jul 2024 21:50:33 GMT</pubDate>
            <atom:updated>2024-07-07T21:52:55.401Z</atom:updated>
            <content:encoded><![CDATA[<p>How the linear search is performing?</p><p>We can introduce Linear search algorithm as one of the most elementary searching techniques. Operating in a sequential manner, it systematically traverses through each element of a given list when tasked with finding a specified value. Beginning from the first element and progressing linearly to the end, it diligently inspects each element until the desired value is found.</p><p>Now, let’s delineate the step-by-step procedure of linear search utilizing the following array.</p><p><strong>Consider value of N is 3. As per the below array, our matching condition will be array[2]=3</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AJ-FnaacTGv9n4hbj4E6hw.jpeg" /><figcaption>unsorted array</figcaption></figure><p>We are going to search <strong>3</strong>. Based on the above given array, it’s located in index 2. Below are the steps how linear search is looking for 3.</p><h3>Step 01</h3><p>In the first case, algorithm is looking for the index 0 where the value is 8. So that will not make our condition true.</p><h3>Step 02</h3><p>Therefore, Algorithm will be switched to the first index where the value is 15. Step two is also not match with our condition. Do not forget that we are looking for the number <strong>3</strong>.</p><h3>Step 03</h3><p>Algorithm will continue the search in second index where the condition is matched. Please check the below steps so that you can have a clear picture of all the steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aa3Xv59G8zzGKvrMm_dojw.jpeg" /><figcaption>all steps</figcaption></figure><h3>Java Code</h3><p>below code will be doing</p><ul><li>Pass the array and target value into <strong>linearSearch </strong>method.</li><li>Get the array length.</li><li>Traverse the array element using a for loop until find the target value.</li><li>Return the index if found.</li><li>Return minus one if there is no target value.</li></ul><p>Code should be written within the <strong>LinearSearch</strong> class</p><pre>public static void main(String[] args) {<br>        int array[] = { 8, 15, 3, 20, 7, 2 }; // array<br>        int n = 3; // target value<br><br>        int result = linearSearch(array, n); // stores the result<br>        System.out.println(&quot;Value found in index: &quot; + result); // prints the result<br>    }</pre><pre> public static int linearSearch(int array[], int target) {<br>        int len = array.length; // stores the length of the array<br>        // check if the traget value is matching with the array values<br>        for (int i = 0; i &lt; len; i++) {<br>            if (array[i] == target) {<br>                return i; // returns the index<br>            }<br>        }<br>        return -1; // if value not found<br>    }</pre><p>Let’s have a look into another example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L2WyCTXFXqqx2jPff_ZEJg.png" /><figcaption>unsorted array</figcaption></figure><p>Based on the above array</p><p>Number of index = 5 (counting from zero).</p><p>Array length = 6 (total number of element in the array).</p><p>Remember our t<strong>arget value is 12</strong> which is at index two. That means, simply we are going to find out whether number twelve is exist or not in the above array. If exist, return the index of target value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z1LS5CyLWH94cPrBaDdkDQ.png" /><figcaption>step 01</figcaption></figure><p>45 is the zeroth index value and it does not match with our target value which is twelve. As the next step looping to next iterator. As I mentioned earlier, it traverses indexes linearly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BFs6fY4lYf3R1bkI8_BzBw.png" /><figcaption>step 02</figcaption></figure><p>Actually this point is the first iteration where zero becomes one. Check the our condition. It is still not matched. 78 is the value of index one. So we are looking for number twelve. Let’s go ahead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJ7MRsAXBGL2kl0I83TPzQ.png" /><figcaption>step 03</figcaption></figure><p>This is the point we were looking for. See the array after second iteration. We found the number twelve at the index two. Now the program should return the index where target value is exist. Let’s see the coding example.</p><pre>class LinearSearch {<br>    public static int findTargetValue(int arr[], int targetValue) {<br>        int n = arr.length; // get array lenght<br>        for (int i = 0; i &lt; n; i++) {<br>            if (arr[i] == targetValue) // check the condition<br>            {<br>                return i; // return the found index<br>            }<br>        }<br>        return -1;<br>    }<br><br>    public static void main(String[] args) {<br>        int arr[] = { 45, 78, 12, 6, 89, 24 };<br>        int targetValue = arr[2]; // 2nd value is twelve <br><br>        int result = findTargetValue(arr, targetValue); // pass array and target value into method<br><br>        if (result != -1) {<br>            System.out.println(&quot;Target value is found at index &quot; + result + &quot;.&quot;);<br>        } else {<br>            System.out.println(  &quot;Target value is not found.&quot;);<br>        }<br>    }<br>}</pre><p>Hope you enjoy the blog and got some knowledge about linear search.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d38722cd5e6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Operators]]></title>
            <link>https://medium.com/@udarachinthaka135/java-operators-6196224805c6?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/6196224805c6</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[operators]]></category>
            <category><![CDATA[loop]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sun, 07 Jul 2024 01:12:37 GMT</pubDate>
            <atom:updated>2024-07-07T01:12:37.854Z</atom:updated>
            <content:encoded><![CDATA[<h3>ජාවා operators (1 කොටස)</h3><p>අද කතා කරන්න යන්නේ ජාවා operators ගැන. කවුරුත් දන්නවා operators නැතුව programming කරන්නනම් බැරි බව. Operators කියන්නේ java වල විතරක් නෙවෙයි ඕනිම පරිගනක භාෂාවක අත්‍යවශ්‍ය අංගයක්. අපි කෙලින්ම note එකට යමු. ජාවා වල operators කොටස් කිහිපයකටම බෙදෙනවා. අපි බලමු ඒ මොනවද කියලා.</p><p>Category</p><p>Operators</p><p>Arithmetic</p><p>+, -, *, %, ++, — , /,</p><p>Logical operator</p><p>&amp;&amp;, ||, !</p><p>Relational operator</p><p>&lt;, &gt;, &lt;=, &gt;=, ==, !=</p><p>Assignment operator</p><p>=, +=. -=, *=, /=, %=, &amp;=, ^=, |=, &lt;&lt;=, &gt;&gt;=, &gt;&gt;&gt;=</p><p>Shit operator</p><p>&lt;&lt;, &gt;&gt;, &gt;&gt;&gt;</p><p>Ternary operator</p><p>?, :</p><p>Bitwise operator</p><p>&amp;, ^, |</p><p>මේ විදිහට ජාවා operators වර්ග කරනවා. අද බලපොරොත්තු වෙන්නේ programming වලදී ගොඩක්ම භාවිතාවන operators වර්ග කීපයක් ගැන.</p><p><strong>Arithmetic operator.</strong></p><p>මේ arithmetical operators ඕනි වෙන්නේ ගණිතමය සංයුතියක් තියන programming part එකක් coding කිරීමේදියි. මම මෙහෙම දෙයක් කරන්නම් තේරුම් ගන්න ලේසි විදිහකට මේ operators ටික table එකකට උදාහරණත් එක්කම පෙන්නලා දෙන්නම්. මේකට ඕනිවෙනවා integer type එකේ අගයන් දෙකක්. මම int X = 10 ලෙසත් int Y=5 ලෙසත් ගන්නම්. දැන් මේ table එක හොඳට අධ්‍යයන කරන්න.</p><p>Operator</p><p>විස්තරය</p><p>උදාහරණය</p><p><strong>+</strong> (Addition)</p><p>අගයන් දෙකක් එකතු කිරීමට මෙම operator එක භාවිතා කරනවා.</p><p>X+Y = 15</p><p><strong>- </strong>( Subtraction)</p><p>අගයන් දෙකක් අඩු කිරීමට මෙම operator එක භාවිතා කරනවා.</p><p>X-Y=5</p><p><strong>* </strong>(Multiplication)</p><p>අගයන් ගුණ කිරීම සඳහා මෙම operator එක භාවිතා කරනවා.</p><p>X*Y=50</p><p><strong>/</strong> (Division)</p><p>අගයන් බෙදීම සඳහා මෙම operator එක භාවිතා කරනවා.</p><p>X/Y=2</p><p><strong>%</strong> (Module)</p><p>යම් අගයක් තවත් අගයකින් බෙදා ඉතිරිය ලබාගැනීමට මෙමෙ operator එක භාවිතා කරනවා.</p><p>X%Y=0</p><p><strong>++ </strong>(increment)</p><p>මේ operator එකෙන් කරන්නේ තියන අගයට එකක් එකතු කරන එක. **</p><p>X++ = 11</p><p><strong>— </strong>(Decrement)</p><p>මේ operator එකෙන් කරන්නේ තියන අගයෙන් එකක් අඩු කරන එක. **</p><p>X — = 9</p><p>අපි දැන් මේ operators භවිතා කරලා sample program එකක් බලමු.</p><p>public class BasicOperatorsOne {</p><p>public static void main(String[] args) {</p><p>int x = 10;</p><p>int y = 5;</p><p>System.out.println(x+y);</p><p>}</p><p>}</p><p><strong>Output: 15</strong></p><p>මේ program එක code කරලා run කරලා බලන්න. අනිත් operators add කරලත් (x-y, x%y, x/y ආදී වශයෙන්) run කරල බලන්න.</p><p>** මේ increment සහ decrement operators වල කොටස් දෙකක් තියනවා.</p><p><strong>Increment :</strong></p><ul><li>Post increment operator (10++) (අගයට පසු post operator භාවිතා වේ.)</li><li>pre increment operator (++10) (අගයට පෙර pre operator භාවිතා වේ.)</li></ul><p>public class BasicOperatorsOne {</p><p>public static void main(String[] args) {</p><p>int x = 10;</p><p>int y = 5;</p><p>System.out.println(++x); //try y++, x++, ++x</p><p>}</p><p>}</p><p><strong>Output: 11</strong></p><p>උඩ දීලා තියන program එකට <strong>(post increment)</strong> x++ හා y++ දාල run කරලා බැලුවොත් පිලිතුරේ වෙනසක් නැතිවෙයි. දැන් අපි බලමු එකට හේතුව මොකක්ද කියලා. Post increment operators යම් අගයකට එකතු කරහම එක වැඩ කරන්නේ දෙවෙනි වතාවට ඒ operator එක තියන line එක run වෙනකොටයි. මේකටත් අපි උදාහරණයක් බලමු.</p><pre>public class BasicOperatorsOne {<br>public static void main(String[] args) {<br>for(int i =0; i &gt; 10 ; i++)<br>System.out.println(i);<br> }<br>}</pre><p><strong>Output: 0,1,2,3,4,5,6,7,8,9</strong></p><p>මේ program එකේ තියෙන්නෙ for loop එකක්. දැන් (i++) කියල තියන operator එකට අවධානය යොමු කරන්න. මේ Program එක run කරලා බැලුවොත් out put එක විදිහට 0,1,2,3,4,5,6,7,8,9 පිලිවලින් print වෙයි. මේ program එකෙන් බලගන්න පුලුවන් post increment එක භවිතාකරලා එකින් එක වැඩි වෙලා දීලා තියන condition (i&gt;10) එක false වෙනකල් වැඩකරලා තියන හැටි. ඉතින් මේකෙන් තේරුම් ගන්න ඕනි දේ තමයි pre increment operator (++x) භාවිතා කරහම පලවෙනි පාර program එක run වෙනකොටම එකක් එකතු වෙන බවත්. Post increment operator (x++) භවිතා කරහම දෙවෙනි වතාවට post increment operator එක තියන line එක run වෙනකොට එකක් එකතු වෙන බවත්.</p><p><strong>Decrement:</strong></p><ul><li>Post decrement operator (10 — ) (අගයට පසු operator භාවිතා වේ.)</li><li>Pre decrement operator ( — 10) (අගයට පෙර operator භාවිතා වේ.)</li></ul><pre>public class BasicOperatorsOne {<br>public static void main(String[] args) {<br>int x = 10;<br>int y = 5;<br>System.out.println( - y); //try y - , x - <br>System.out.println( - x);<br>  }<br>}</pre><p><strong>Output: 4, 9</strong></p><p>මේකෙත් කලින් විස්තර කරපු operator එක වගේමයි. වෙනස තියෙන්නේ මේක decrement එක වීමයි. <strong>Post decrement operator</strong> එක තේරුම් ගන්නනම් යට දීල තියන program එක run කරල බලන්න.</p><pre>public class BasicOperatorsOne {<br>public static void main(String[] args) {<br>for(int i =10; i &gt; 0 ; i - )<br>System.out.println(i);<br>  }<br>}</pre><p><strong>Output: 10,9,8,7,6,5,4,3,2,1</strong></p><p>මේ image ටික බලලා for loop එකත් increment operator එකත් වැඩකරන ආකාරය පිලිබද අදහසක් ගන්න. පලවෙනියටම i අගය 0 නිසා output එක 0 වෙනවා. දෙවෙනි පාර compile වෙනකොට i අගය 1 වෙනවා. ඒක වෙන්නේ, පලවෙනිපාර 0 ලෙස output එක ආවට පස්සේ ඊලගට compile වෙන්න පටන් ගන්නේ (i++) increment operator එක ලග ඉදන්. ඒ වෙනකොට i ගේ අගය බින්දුවයි increment එක මගින් බින්දුවට එකක් එකතුවෙනව එතකොට i ගෙ අගය එකක් වෙනවා. ඊටපස්සේ අපි දීල තියන condition එක check කරනවා (i&lt;10). එක හරිනම් ආයෙත් i ගෙ අගය output එකක් විදිහට දෙනවා. මේ රටාවටම අපි දීලා තියන condition එක වරදිනකන්ම (false) output දෙනවා.</p><p>post decrement operator එකට example විදිහට දීලා තියන program එකෙත් වෙන්නේ මේ සිද්ධියමයි. වෙනස වෙන්නේ decrement නිසා 10 ඉදන් 1 ට print වෙනඑකයි.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*rjAp2lhwVavRZXs4.PNG" /><figcaption>step 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*llDtbIRWrJtUWPfW.PNG" /><figcaption>step 2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*hLLTPsh6tlKf3V6t.PNG" /><figcaption>step 3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*tYzqQh9XK4t1YZhO.PNG" /><figcaption>step 4</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*R7eFc70HcU2IzX1l.PNG" /><figcaption>step 5</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*rnZ7UvmEeUDzvj_G.PNG" /><figcaption>step 6</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*OvIh5bomUBQNzrtS.PNG" /><figcaption>step 7</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*nX6-cJycjl6Oubtq.PNG" /><figcaption>step 8</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*auXXcIHFUsjmJLlG.PNG" /><figcaption>step 9</figcaption></figure><p>අවුලක් තියනවනම් comment කරන්න. ඊලග කොටසින් හම්බෙමු එහෙනම්… ස්තූතියි ඈ.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6196224805c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Class variable and instance variable in Java]]></title>
            <link>https://medium.com/@udarachinthaka135/class-variable-and-instance-variable-in-java-e4017e67980e?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4017e67980e</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[instance-variables]]></category>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[class-variables]]></category>
            <category><![CDATA[static-variable]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sat, 06 Jul 2024 00:33:42 GMT</pubDate>
            <atom:updated>2024-07-06T00:33:42.558Z</atom:updated>
            <content:encoded><![CDATA[<h3>Class variables (Static variable)</h3><p>Class variables are variables declared within a class, outside any method, with the static keyword.</p><p>Static key word එක භාවිතා කරලා, class එකක් ඇතුලේ, method එකකින් පිටතදී declare හා initialize කරන variables වලට අපි කියනවා class variables එහෙමත් නැත්නම් satatic variables කියලා. Static key word එක use කරලා variable එකක හැදුවොත් ඒක ඒ class එක ඇතුලේ තියන සියලුම method වලට access කරන්න පුලුවන්. ඒ වගේම variable එකේ අගයත් වෙනස් කරන්න පුලුවන් එනවා. අපි මේකටත් example program එකක් බලමු.</p><pre>public class StaticVariableDemo {<br>  public static String myClassVar=&quot;this is static variabale&quot;;<br>  public static void main(String args[]){<br>    StaticVariableDemo obj = new StaticVariableDemo();<br>    StaticVariableDemo obj2 = new StaticVariableDemo();<br>    //All three will display &quot;class or static variable&quot;<br>    System.out.println(obj.myClassVar);<br>    System.out.println(obj2.myClassVar);<br>    //changing the value of static variable using obj2<br>    obj.myClassVar = &quot;Changed Value&quot;;<br>    //All three will display &quot;Changed Text&quot;<br>    System.out.println(obj.myClassVar);<br>    System.out.println(obj2.myClassVar);<br>  }<br>}</pre><p>output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/0*75pzkc8IiKoZgYx0.PNG" /></figure><p>හරි අපි බලමු මේ program එකත් මොකද මේකෙන් වෙන්නේ කියලා.</p><p>public static String myClassVar=”this is static variabale”;</p><p>මේ line එකේ තියන myClassVar word එක තමා class variable එකේ එහෙමත් නැත්නම් static variable එකේ name එක වෙන්නේ. Main method එක ඇතුලේ මොකද කරලා තියෙන්නෙ කියලා බලමු අපි. StaticVariableDemo කියන class එකෙන් object දෙකක් හදලා තියනවා “obj” සහ “obj2” කියන නම්වලින්. ඊට පස්සේ කරලා තියෙන්නේ අර class variable එක object දෙකම භාවිතා කරලා print කරගන්නවා. Out put එක විදිහට “this is static variabale” දෙපාරක් print වෙනවා.</p><p>obj.myClassVar = “Changed Value”;</p><p>මේ line එකෙන් කරලා තියෙන්නේ, පලවෙනි object name එක භාවිතා කරලා static variable එකට “change value” කියන text එක assign කරන එක. ඊට පස්සෙත් කලින් වගේම object දෙක භවිතාකරලා class variable එක print කරගන්නවා. එතකොට output එක විදිහට ලැබෙන්නේ Changed Value කියන text එකම දෙපාරක් print වෙලා.</p><p>මේකට හේතුව තම මෙ variable එක static key word එක සමග declare කර තිබීම. මේ class එක ඇතුලේ වගෙම වෙනත් class තුලත් access කරන්න පුලුවන් හැබැයි මේ static variable එක තියන class එක extend කරන්න ඕනි.</p><h3>Instance variable (Non-static variable)</h3><p>Instance variables are variables within a class but outside any method.</p><p>මේ variable type එකත් උඩින්කිව්ව එක වගේමයි හැබැයි මෙතන static key word එකක් නෑ. Declare කරන්නෙත් initialize කරන්නෙත් methods වලින් පිට හැබැයි class එකක් ඇතුලේ. මේ variables class එකේ ඕනිම method එකක් ඇතුලෙ access කරන්න පුලුවන්. මේ instance variable වල විශේෂ ලක්ෂණයක් තමයි, අපි variable එක වෙන තැනකදී access කරනකොට කලින් එකෙ copy එකක් තමා access වෙන්නේ. අපි sample program එකකුත් එක්ක බලමු එතකොට හොදටම තෙරෙයි.</p><pre>public class InstanceVariable {<br>  String myInstanceVar = &quot;instance variable&quot;;<br>    public static void main(String args[]) {<br><br>      InstanceVariable object1 = new InstanceVariable();<br>      InstanceVariable object2 = new InstanceVariable();<br><br>      System.out.println(object1.myInstanceVar);<br>      System.out.println(object2.myInstanceVar);<br><br>      object1.myInstanceVar = &quot;java programmer&quot;;<br><br>      System.out.println(object1.myInstanceVar);<br>      System.out.println(object2.myInstanceVar);<br>  }<br>}</pre><p>output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*97P8d-GPtMhwzERf.PNG" /></figure><p>String myInstanceVar = “instance variable”;</p><p>මේකත් අර විදිහමයි හැබැයි static key word එක නෑ. මෙකෙත් object දෙකක් තියනවා. Object1, object2 කියලා. පලවෙනි පාර object දෙක භාවිතා කරලා myInstanceVar කියල variable එක print කරනකොට print වෙන්නෙ “instance variable” කියලා.</p><p>object1.myInstanceVar = “java programmer”;</p><p>මේ line එකේදී object1 එකෙන් variable value එක java programmer කියල change කරනවා. ඊට පස්සෙත් උඩින් වගේම object දෙක use කරලා myInstanceVar කියන variable එක print කරනවා. මෙතනදී, object1 එකට අදාලව print line එකේ output එක වෙන්නේ “java programmer” කියන එක. Object2 එකට අදාල print line එකේ output එක වෙන්නේ කලින් එකමයි ඒ කියන්නෙ instance variable කියන එක. Class variable එකේදී මේ variable දෙකම වෙනස් වුනා ඒත් මේකේ එහෙම නෑ, ඒක තමා මේ variable type දෙක අතරේ තියන ප්‍රධානම වෙනස. මේ ගෙන මම ගොඩක් කියන්න යන්නෙ නෑ මොකද local variable ගෙන කියනකොටත් අපි මේ ගෙන කතා කරා. හරි එහෙනම් දැනට නවතිනවා මේ programs අරගෙන compile කරලා බලන්න.</p><p>ස්තූතියි….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*LElVFgphCbQRb76v5grYzA.png" /><figcaption>Image Source: Medium (Sineth)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4017e67980e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Variables]]></title>
            <link>https://medium.com/@udarachinthaka135/java-variables-51b616b91cae?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/51b616b91cae</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[local-variable]]></category>
            <category><![CDATA[static]]></category>
            <category><![CDATA[instance]]></category>
            <category><![CDATA[variables]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sat, 06 Jul 2024 00:28:21 GMT</pubDate>
            <atom:updated>2024-07-06T00:28:21.775Z</atom:updated>
            <content:encoded><![CDATA[<h3>මොනවද මේ variable කියන්නේ හා ඒවගේ කොටස් මොනවද ?</h3><p>මේකනම් ගොඩක් වැදගත් ලිපියක්. එහෙමයි කියලා අනිත් ඒවා වැඩක් නෑ කියනවා නෙවෙයි. මේ ලිපියෙන් කියන්නෙ java වල programming part වලට ගොඩක්ම වැදගත් වෙන තොරතුරු ටිකක්. Normally කියනවනම් variable එකක් කියන්නේ කුමක් හෝ data type එකක data එකක් store කරගන්න ram එකේ හදගන්න space එකක්. අපි බලමු variable එකක් ලියන්නෙ කොහොමද කියලා.</p><p>Variable declaration</p><p>int i;</p><p>initializing variable</p><p>i = 10;</p><p>variable එකක් ලිවීමෙදී අපි මේ methods අනුගමනය කලයුතුයි. Variable declaration කියන එකෙන් කරන්නෙ variable එකේ data type එකයි, variable name එකයි දෙන එක. Variable initialize කිරීමේදී අපි කරන්නේ declare කරගත්ත variable එකට අගයක් assign කරන එක. මේ වැඩ දෙකම අපිට එක් line එකකදී කරන්න පුලුවන් මේ විදිහට.</p><p>int i = 10;</p><p>හරි අපි දැන් බලමු මොනවද java වල තියන variable types. ප්‍රධාන වශයෙන් කොටස් තුනක් තියනවා.</p><ul><li>Local variable.</li><li>Class variable (static variable).</li><li>Instance variable (Non-static variable).</li></ul><h4>Local variable</h4><p>Variables defined inside methods, constructors or blocks are called local variables.</p><p>Local variable කියන්නේ ඇත්තටම class එකක method එකක් හෝ constructor එකක් ඇතුලේ declare හා initialize කරන variables වලට. ඒ variable එක ඕනේම data type එකකින් යුක්ත විය හැකියි. ඒ variables method එකෙන් බාහිරව access කිරීමට හැකියාවක් නෑ. ඒ වගේම තමා ඒ method එකෙන් පිටතදී මේ type එකේ variables වල අගයන් වෙනස් කරන්නත් බෑ. අපි උදාහරණයක් අරගෙනම බලමු.</p><pre>public class LocalVariableText {<br>  public String myVar=&quot;instance variable&quot;;<br>    public void myMethod(){<br>    // local variable<br>    String myVar = &quot;Inside Method, local variable&quot;;<br>    System.out.println(myVar);<br>    }<br>  public static void main (String args[]){<br>    // Creating object<br>    LocalVariableText obj = new LocalVariableText();<br>    obj.myMethod();<br>    System.out.println(obj.myVar);<br>  }<br>}</pre><p>හරි අපි දැන් අපි බලමු මේ code එකෙන් මොකද වෙන්නේ කියලා. මේ program එකේ තියන class එකේ name එක LocalVariableText කියන එක , method name එක වෙන්නේ myMethod කියන එක. ඊට අමතරව main method එකත් තියනවා.</p><p>public String myVar = “instance variable”;</p><p>මේ line එකේ තියෙන්නේ string type එකේ instance variable එකක්. ඒ variable එක භාවිතා කරලා තියෙන්නෙ program එක පැහැදිලිවෙන්න. හරි දැන් අපි බලමු main method එක ඇතුලේ මොකද වෙන්නේ කියලා. Main method එක ගැන කියන්න දෙයක් නැනේ. එකත් අව්ල් වගේනම් blog එකේ තියනවා බලාගන්න පුලුවන්. පලවෙනියටම අපි කරලා තියෙන්නේ class එකෙන් object එකක් හදගන්න එක. Object name එක තමයි “obj” කියන්නේ. එතනට ඔයාට ඕනේ name එකක් දාගන්න පුලුවන්. ඊලගට කරලා තියෙන්නේ object එක මගින් method එකට call කරනවා. දැන් myMethod() එකෙ body එක බලන්න. එකේ තියනවා string type එකේ myVar කියන variable එක. “Inside Method, local variable” කියන text එක ඒ variable එකට assign කරලා තියනවා.</p><p>String myVar = “Inside Method, local variable”;</p><p>මෙතන තියන variable එක තමා local variable එක වෙන්නේ. ඒකට හේතුව තමා මේ method එක තියෙන්නේ myMethod කියන method එක ඇතුලේ නිසා. මේ local variable එකේ scope එක එහෙමත් නැත්නම් access කලහැකි පරාසය වෙන්නේ මේ method එක ඇතුලත විතරයි.</p><p>System.out.println(myVar);</p><p>මේ විදියට ඊලගට අපි local variable එක print කරගන්නවා. Output එක වෙන්නේ “Inside Method, local variable” කියන text එක. මොකද myVar variable එකේ value එක ඒක නිසා. ඊලගට instance variable කියලත් මේ program එකේදි print වෙනවා. හොදට main method එක බලන්න්න.</p><p>System.out.println(obj.myVar);</p><p>මේ line එකත් main method එකේ අවසානෙට තියනවා. එකත් class object එක මගින් myVar කියන variable එක තමා access කරලා තියෙන්නේ. හොදට බලන්න myMethod() එක ඇතුලේ තියන string variable එකත් ඒකට උඩින්, method එකෙන් පිට තියන string variable එකත් එකම name එකෙන් තමා තියෙන්නේ.</p><p>System.out.println(obj.myVar)</p><p>main method එකෙ අවසානෙට තියන මේ line එකේ myVar variable එකට assign කරලා තියෙන්නේ “instance variable” කියන text එක. ඒ නිසා value එක විදිහට print වෙන්නේ “instance variable” කියලා. ඒකට හේතුව තමා local variable එකේ scope එක method එක ඇතුලට විතරක් සීමා වෙන නිසා. අනිත් හෙතුව තමා instance variable එකේ scope එක සම්පූර්ණ class එක පුරාවටම තියන නිසා. මේ හේතු හින්දා තමා එකම නමින් variable දෙකම තිබ්බත් print වෙන අගයන් වෙනස් වෙන්නේ. මෙතනදී print වෙන්නේ local variable එක නෙවෙයි. Program එක අරගෙන run කරලා බලන්න.</p><p>output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/0*Bv7fnrKZHetm2e82.PNG" /></figure><p>Class variable හා instance variable වලින් හම්බෙමු එහෙනම්. ස්තූතියි….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Hbauw6DGI4CKbA5gRCQyQ.jpeg" /><figcaption>Image Source: BoardInfinity</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51b616b91cae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Operators]]></title>
            <link>https://medium.com/@udarachinthaka135/java-operators-affe0ee68062?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/affe0ee68062</guid>
            <category><![CDATA[logic]]></category>
            <category><![CDATA[operators]]></category>
            <category><![CDATA[relational]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sat, 06 Jul 2024 00:22:34 GMT</pubDate>
            <atom:updated>2024-07-06T00:22:34.293Z</atom:updated>
            <content:encoded><![CDATA[<h3>operators (operator part 3)</h3><h3>Relational operators</h3><p>අද කතකාරන්න යන්නේ <strong>relational</strong> හා <strong>ternary operators</strong> ගෙන. මේ <strong>relational operators</strong> වල ප්‍රධාන කාර්ය වෙන්නේ numbers දෙකක් එහෙමත් නැත්නම් අගයන් දෙකක් සංසන්දනය (compare) කරලා Boolean අගයක් (true or false) return කරන එක. ජාවා වල මේ type එකේ operators හයක් තියනවා.</p><p>&lt; less than ( x &lt; y , x කුඩයි y ට වඩා).</p><p>&gt; greater than ( x &gt; y , x විශාලයි y ට වඩා).</p><p>&lt;= less than or equal ( x &lt;= y , x කුඩයි හෝ සමානයි y ට).</p><p>&gt;= greater than or equal ( x &gt;= y , x විශාලයි හෝ සමානයි y ට).</p><p>== equal ( x == y , x සමානයි y ට).</p><p>!= not equal ( x != y , x අසමානයි y ට).</p><p>මේ operators ටික නම් බැලූ බැල්මටම ලේසි තේරුම් ගන්න. මේ operators හයටම අපි එක program එකක් බලමු. ඊට පස්සෙ ටිකක් කතාකරමු.</p><pre>public static void main(String[] args) {<br>int x = 10;<br>int y = 15;<br>  System.out.println(&quot;x less than y :&quot; + (x &lt; y));<br>  System.out.println(&quot;x greater than y :&quot; + (x &gt; y));<br>  System.out.println(&quot;x less than or equal to y :&quot; + (x &lt;= y));<br>  System.out.println(&quot;x greater than or equal to y :&quot; + (x &gt;= y));<br>  System.out.println(&quot;x equal to y :&quot; + (x == y));<br>  System.out.println(&quot;x not equal to y :&quot; + (x != y));<br>}</pre><p>ඔයාලට පෙන්නව මේ program එකේ මම x වල අගය විදිහට දීල තියෙන්නේ 10 ඒ වගේම y වල අගය දීල තියෙන්නේ 15. අපි මේක relational operators භාවිතාකරලා කියනවනම් මෙහෙමයි ( x &lt; y ) x කුඩයි y ට වඩා. මේක තමා එතකොට හරි condition එක. මම දීල තියන program එකේ මේ condition එක භාවිතාකරලා තියෙන්නේ පලවෙනි print line එකේ.</p><p>“ System.out.println(“x less than y :” + (x &lt; y)); “</p><p>බැලූ බැල්මට එහෙම පෙනුනට මේ program එකේ condition තුනක් හරි. අපි බලමු ඒ මොනවද කියලා.</p><p>1. System.out.println(“x less than y :” + (x &lt; y));</p><p>2. System.out.println(“x not equal to y :” + (x != y));</p><p>3. System.out.println(“x less than or equal to y :” + (x &lt;= y));</p><p>මේ line තුනම හොදට බලන්න. එක, x කුඩයි y ට වඩා. දෙක, x අසමානයි y ට. තුන, x <strong>කුඩයි</strong> හෝ <strong>සමානයි</strong> y ට. එහෙනම් මේ condition තුනම හරි. මෙන්න output එක.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/0*EgNolgRa7y2Unl2n.PNG" /></figure><p>මේ program එක run කරල බලන්න උඩින් කියපු condition තුන තියන lines වලට අදාලව ture කියල එයි ඉතුරු ඔක්කොම lines වලට අදාලව false print වෙයි. අපි තවත් program එකක් if else use කරලා බලමු තේරුම්ගන්න ලේසි වෙන්න.</p><pre>public static void main(String[] args) {<br>int x = 10;<br>int y = 15;<br>  if (x == y) {<br>    System.out.println(&quot;X equal to Y.&quot;);<br>  } else if (x &gt; y) {<br>    System.out.println(&quot;X greater than Y.&quot;);<br>  } else if (x &lt;= y) {<br>    System.out.println(&quot;X less than or equal to Y&quot;);<br>  } else if (x &gt;= y) {<br>    System.out.println(&quot;X greater than or equal to Y&quot;);<br>  } else if (x != y) {<br>    System.out.println(&quot;X not equal to Y&quot;);<br>  } else if (x &lt; y) {<br>    System.out.println(&quot;X less than Y.&quot;);<br>  }<br>}</pre><p><strong>If else</strong> දන්නැත්නම් ඒකට අවුල්ක් ගන්න එපා. මේකෙන් කරන්නේ if , else if එකේ වරහන් ඇතුලේ තියන condition එක check කරලා output එකක් දෙන එක. මේක වැඩකරන්නේ මෙහෙමයි. පලවෙනියටම if එක ඇතුලෙ තියන condition එක check කරනවා. ඒක වැරදිනම් ඊලගට තියන else if එක ඇතුලට ගිහින් එකේ තියන condition එක check කරනවා. එකත් වැරදිනම් ඊලගට තියන else if එකේ condition එක check කරනවා. හැබැයි if එකෙ condition එක හරිනම් ඒක ඇතුලට ගිහින් එකේ තියන “X equal to Y.” කියන line එක print කරනවා. එතනින් ඉවරයි. එක condition එකක් වැරදිනම් විතරයි අනිත් එකට යන්නේ. Output එක මෙන්න.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/0*FER5MrJQHTnbOJ-_.PNG" /></figure><p>ඉතින් මේ program එකේ තුන් වෙනියට තියන condition එක බලන්න. ඒක තමා පලවෙනියටම true වෙන condition එක. ඉතින් ඒ line එක print කරලා program එක නතර වෙනවා. Program එක compile කරලා බලන්න. If else ගෙන වැඩිය කතාකරන්නේ නෑ ඒක වෙනම අපි කතා කරමු.</p><h3>Ternary Operator</h3><p>අපි ternary operator විදියට operators දෙකක් හදුනගත්තා පලවෙනි lesson එකේදී. ( ? , : ) මේ operator දෙක තමා java වල ternary operator වෙන්නේ. දැන් අපි බලමු ඒවා කොහොමද භාවිතා වෙන්නේ කියලා. මේ operator දෙකම අපිට if else වෙනුවට භාවිතා කරන්නපුලුවන්. අපි බලමු කොහොමද ඒක වෙන්නේ කියලා. If else වැඩකරනහැටි උඩ විස්තරේදී පොඩ්ඩක් දැන ගත්ත නිසා මේ program එක බලන්න.</p><pre>public static void main(String[] args) {<br>int number = -5;<br>  if (number &gt; 0) {<br>    System.out.println(number + &quot; is positive&quot;);<br>  } else {<br>    System.out.println(number + &quot; is not positive&quot;);<br>  }<br>}</pre><p>int variable එකක් තියනවා <strong>number</strong> කියලා අපි එකට <strong>-5</strong> කියන අගය සමාන කරලා තියනවා. If condition එක ඇතුලෙදි number variable එකේ අගය 0 ට වඩා වැඩීනම් ඒක positive අගයක් ලෙසත්, එහෙම නැත්නම් ඒ අගය positive නොවන අගයක් ලෙසත් print කරන්න කියල තමා මේ program එකෙන් code කරලා තියෙන්නේ. දැන් අපි බලමු ternary operators භාවිතාකරලා කොහොමද මේ program එක code කරන්නේ කියලා.</p><pre>public static void main(String[] args) {<br>int number = -5;<br>String result;<br>result = (number &gt; 0) ? &quot;positive&quot; : &quot;not positive&quot;;<br>  System.out.println(number + &quot; is &quot; + result);<br>}</pre><p>මේ program එකේත් උඩින් එකේ තිබුන int variable එකයි, තව string type එකේ variable එකක් තියනවා result කියන name එකෙන්. මේ String කියන්නේ character values store කරලා තියගන්න පුලුවන් data type එකක් කියල දැනට මතක තියාගන්න. මේ program එක කියවන්නේ මෙහෙමයි.</p><p><strong>number</strong> කියන variable එකෙ තියන අගය බිංදුවට වඩා විශාලයිනම් <strong>result</strong> කියන String variable එකට සමාන කරන්නලු “positive” කියලා. එහෙම නැත්නම් සමාන කරන්නලු “not positive” කියලා. මේ program එක අරගෙන run කරලා බලන්න. If else එක භාවිතා කරල කරපුදේමයි මේකෙනුත් කරල හියෙන්නේ. “?” (question mark) එකෙන් if condition එක ඇතුලෙදි කරන දේත් “:” (colon) එකෙන් else part එකත් සම්පූර්න වෙනවා. “for each” loop එකට එහෙමත් මේ operator එක භාවිතා වෙනවා.</p><p>System.out.println(number + “ is “ + result);</p><p>මේ line එකේදී කරලා තියෙන්නේ number කියන variable එකයි, “is” කියන word එකයි, result කියන variable එකයි print කරනඑක. Plus mark (+) එකෙන් කරලා තියෙන්නේ මේ අගයන්,ඒ වගේම is කියන word එක එකිනෙකට සම්බන්ධ කරනඑක. Variable එකක් print කර ගන්න ඕනිනම් double quotes (“”) භාවිතා නොකර නිකම්ම print කරගන්නවා මේ විදිහට.</p><p>System.out.println(number);</p><p>Variable දෙකක් හෝ ඊට වඩා print කරගන්න ඕනිනම් මෙහෙමයි.</p><p>System.out.println(number + result);</p><p>// System.out.println(number + result + number);</p><p>හැබැයි අපිට වෙන මොකක් හරි වචනයක්, වාක්‍යක් මේ අතරට දාගන්න ඕනිනම් වරහන් භාවිතා කරන්න වෙනවා. මේ වගේ.</p><p>System.out.println(number + “ is “ + result);</p><p>ඔන්න ඒ ටිකත් එතනදිම කිව්වා එහෙනම් තවත් පාඩමකින් හම්බෙමු.</p><p>ස්තූතියී…..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Mv6AdBpdOYmhJ9JYQTQHQ.png" /><figcaption>Java operators (Image Source: logicmojo)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=affe0ee68062" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Operators]]></title>
            <link>https://medium.com/@udarachinthaka135/java-operators-bbcb70495568?source=rss-8433707b13fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbcb70495568</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[logical]]></category>
            <category><![CDATA[operators]]></category>
            <dc:creator><![CDATA[Udara Chinthaka]]></dc:creator>
            <pubDate>Sat, 06 Jul 2024 00:16:34 GMT</pubDate>
            <atom:updated>2024-07-06T00:16:34.262Z</atom:updated>
            <content:encoded><![CDATA[<h3>ජාවා operators (2 කොටස)</h3><h3>අද කතාකරන්න යන්නේ logical operators ගැන.</h3><p>&amp;&amp; = logical AND operator</p><p>|| = logical OR operator</p><p>! = logical NOT operator</p><p>ජාවා වලදී ප්‍රධානවශයෙන් logical operators තුනක් භාවිතාවෙනවා. මේ operators තුනම බොහෝ දුරට භවිතාවෙන්නේ conditions check කිරීමේදීයි.</p><p><strong>අපි &amp;&amp; (AND) operator එක අරන් බලමු.</strong></p><p>මේකෙන් කරන්නේ condition දෙකක් පරීක්ෂා කරනඑකයි. අපි check කරන condition දෙකෙන් එකක් හරි false නම් අපිට උත්තරේ දෙන්නේ false කියලා. අපි මේකට උදාහරණයක් බලමු.</p><pre>public static void main(String[] args) {<br>int x=10;<br>int y=5;<br>int z=15;<br>  System.out.println(y&lt;x &amp;&amp; x&gt;z);<br>  System.out.println(y&lt;x &amp;&amp; z&gt;x);<br>}</pre><p>මේ program එක බලමු.</p><p><strong>X අගය 10 යි, Y අගය 5යි, Z අගය 15 යි.</strong></p><p>ඊට පස්සේ එක line එකක් print කරනවා. ඒකෙදී condition දෙකක් check කරනවා &amp;&amp; (AND) operator එක මගින්. පලවෙනි condition එක වෙන්නේ Y කුඩයි X ට වඩා <strong>( Y&lt;X )</strong> ඒ condition එකනම් හරි. AND (&amp;&amp;) operator එක තියන නිසා ඊලග condition එකත් check කරනවා. දෙවෙනි condition එක වෙන්නේ <strong>( X&gt;Z )</strong> X විශාලයි Z ට වඩා. ඒකනම් වැරදියී මොකද X අගය 10 යි, Z අගය 15 යි එහෙනම් X විශාල වෙන්න විදිහක් නෑ.</p><p>එහෙනම් පලවෙනි print line එකේ output එක වෙන්නේ FALSE. හේතුව තමා condition දෙකෙන් එකක් වැරදි වීම.</p><p>දෙවෙනි print line එකත් හොදට බලන්න. එකේ condition දෙකම හරි ඒ කියන්නේ output එක TRUE.</p><p>මේ program එකත් run කරලා බලන්න.</p><pre>public static void main(String[] args) {<br>int x = 10;<br>int y = 5;<br>int z = 15;<br>  if(x &lt; z &amp;&amp; x &lt; y){<br>    System.out.println(&quot;Both conditions are true&quot;);<br>  }else{<br>    System.out.println(&quot;FALSE&quot;);<br>  }<br>}</pre><p><strong>ඊලගට අපි || (OR) operator එක බලමු.</strong></p><p>මේ operator එකෙන් කරන්නෙත් condition දෙකක් check කරන එක. Check කරන condition දෙකෙන් එකක් හරි හරිනම් output එක වෙන්නේ TRUE. අපි කලින් ගත්ත program එකම අරගෙන බලමු.</p><pre>public static void main(String[] args) {<br>int x = 10;<br>int y = 5;<br>int z = 15;<br>  System.out.println(y &lt; x || x &gt; z);<br>  System.out.println(y &gt; x || z &lt; x);<br>}</pre><p>පලවෙනි print line එක කියවන්නේ මෙහෙමයි <strong>“y කුඩයි x ට වඩා හෝ x විශාලයි z ට වඩා”</strong>. ඉතින් මේ condition දෙකෙන් දෙවෙනි condition එක වැරදි නමුත් එක condition එකක් හරි නිසා මේකේ output එක වෙන්නේ TRUE. මොකද මේකේ හෝ කියලා තියනවා පලවෙනි එක නැත්නම් දෙවෙනි එක. දෙවෙනි print line එකේ condition දෙකම වැරදි ඒ නිසා output එක වෙන්නේ FALSE.</p><p>මේ program එකත් check කරලා බලන්න.</p><pre>public static void main(String[] args) {<br>int x = 10;<br>int y = 5;<br>int z = 15;<br>  if(x&lt;z || x&lt;y){<br>    System.out.println(&quot;TRUE&quot;);<br>  }else {<br>    System.out.println(&quot;FALSE&quot;);<br>  }<br>}</pre><p><strong>! (NOT) operator</strong></p><p>අගයන් දෙකක් එකිනෙකට අසමානයි නම් ඒ අසමානතාවය අගයන් දෙක සංසන්දනය කර එය සත්‍ය හෝ අසත්‍ය බව පෙන්නුම් කිරීමට මේ operator එක භාවිතා කරනවා.</p><pre>public static void main(String[] args) {<br>int x,y,z;<br>x=10;<br>y=5;<br>z=15;<br>  System.out.println(x&gt;z);<br>  System.out.println(!(x&gt;z));<br>}</pre><p>මේ program එක අපි බලමු. කලින් program වල තිබුනු අගයන්මයි මේ program එකෙත් තියෙන්නේ. මේකේ පලවෙනි print line එකේ දීල තියන condition එක false. මොකද x අගය z අගයට වඩා කුඩා නිසා. දෙවෙනි condition එකත් ඒ විදිහමයි. ඒ උනාට ඒකට අපි ! (not) operator එක භාවිතා කරලාතියනවා. ඒ නිසා එකේ output එක true වෙනවා. හේතුව තමයි ! (not) operator එක මගින් දීලාතියන condition එක මොකක් උනත් ඒක අනිත්පැත්ත හරවනවා. True එකක් තිබුනොත් false කරනවා. False එකක් තිබුනොත් true කරනවා. අපි තවත් program එකක් බලමු.</p><pre>public static void main(String[] args) {<br>int x, y, z;<br>x = 10;<br>y = 5;<br>z = 15;<br>    System.out.println(x &gt; z);<br>    System.out.println(!(x &gt; z));<br>  if (z &gt; x &amp;&amp; z &gt; y) {<br>    System.out.println(&quot;Z is the largest value.&quot;);<br>  }<br>  if (x == y || y == z) {<br>    System.out.println(&quot;There are same values.&quot;);<br>  } else {<br>    System.out.println(&quot;There are no similar numbers.&quot;);<br>  }<br>  if (!(x &lt; y)) {<br>    System.out.println(&quot;x is greater than y. &quot;);<br>  }<br>}</pre><p><em>මේ program එක code කරලා run කරල බලන්න. Values වගේම operators මාරුකරලා run කරලත් බලන්න.</em></p><p>Login system එකක database එකෙන් ගන්න user name, password check කරනකොට මේ &amp;&amp; (AND) operator එක භාවිතා වෙනවා. ඒ වගේම තමයි system එකක user level පරීක්ෂා කරනකොට ! (NOT) operator එක භවිතාවෙනවා. ඒ වගේම තමා condition කිහිපයක් භවිතාවෙන තැනක || (OR) operator භවිතා වෙනවා. උදාහරණයක් විදිහට ගත්තොත් system එකක එකම feature එකක් user level දෙකකට පොදුයිනම් ඒ user level දෙක check කිරීමට මේ || (OR) operator එක භාවිතා කරන්න පුලුවන්.</p><ul><li>මේ logical operators නිතරම condition එකක් check කරලා එලියට දෙන්නේ Boolean අගයක්, ඒ කියන්නේ true හෝ false කියන දෙකෙන් එකක්.</li><li>· ! (not operator) මේ operator එක සමහර තැන්වලදී unary operator එකක් විදිහටත් හදුන්වනවා. අපි ඒ ගෙන වෙනම කතාකරමු.</li></ul><p>එහෙනම් ස්තූතියි…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Mv6AdBpdOYmhJ9JYQTQHQ.png" /><figcaption>Java operators (image source: LogicMojo)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbcb70495568" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>