<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Java · MangoDriod</title><link>https://md.eknath.dev/tags/java/</link><description>Java updates from MangoDriod</description><language>en-us</language><atom:link href="https://md.eknath.dev/tags/java/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Wed, 25 Nov 2026 09:45:04 +0530</lastBuildDate><item><title>Linked List in Java - Collections Framework- DSA</title><link>https://md.eknath.dev/posts/dsa/linked-list-in-java-collections/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/linked-list-in-java-collections/</guid><pubDate>Wed, 23 Feb 2022 13:02:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><category>linkedlist</category><description>Gist: A Linked list is dynamic array where a group of objects can be stored while following the insertion order and allow duplicates, the 2 types of linked list are singly and doubly linked lists, the default one is singly linked list where the elements can be traversed only in forward direction this is because unlike array here the elements are stored at random locations and only the node of each element knows the address of the next element. A simple example would be our circuit board here a point X may be connected to Y and Y connected to point Z here the insertion order is preserved and we can know the next point only by following the signals from the previous point.</description><content:encoded><![CDATA[<h3 id="gist">Gist:</h3>
<p>A Linked list is dynamic array where a group of objects can be stored while following the insertion order and allow duplicates, the 2 types of linked list are singly and doubly linked lists, the default one is singly linked list where the elements can be traversed only in forward direction this is because unlike array here the elements are stored at random locations and only the node of each element knows the address of the next element. A simple example would be our circuit board here a point X may be connected to Y and Y connected to point Z here the insertion order is preserved and we can know the next point only by following the signals from the previous point.</p>
<p>Same as the example but in Linked list each element has 2 parts in singly linked list where 1 part is the node with the address to the next node and the other which is the actual data of that element like a post letter the envelope has the address and inside the contents. but in doubly linked list there are 3 parts 2 of them are nodes which has the address of previous and next element and the other one is the actual data of that element.</p>
<p>The insertion or deletion are in constant time so compared to ArrayList the mega shifting process will be avoided.</p>
<h3 id="hierarchical-order">Hierarchical order</h3>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/linked-list-in-java-collections-1.png"></p>
<h3 id="type-of-constructors">Type of constructors</h3>
<ul>
<li>Empty argument constructor or the default constructor is the same as invoking any other object here a new linked list object will be created, do note that there is no scope for reserving memory for the number of elements as we add new element the size is maintained dynamically a new LinkedList is an empty list.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>LinkedList list <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList();
</span></span></code></pre></div><ul>
<li>Conversion of other Collections like ArrayList, Vector, etc&hellip;to a LinkedList is possible with the following constructor.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>LinkedList arraysFromVector <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList( VariableNameOfTheCollection );
</span></span><span style="display:flex;"><span><span style="color:#75715e">//here any collection will be converted into a LinkedList</span>
</span></span></code></pre></div><h3 id="important-points">Important Points</h3>
<ul>
<li>By default an empty LinkedList is created.</li>
<li>Is Dynamically Growable and Shrinkable.</li>
<li>Heterogeneous Objects are allowed.</li>
<li>There is no initial capacity</li>
<li>No load factor is applicable here.</li>
<li>Order of insertion is preserved</li>
<li>We can also use generic data-type such that the list acts like a homogenous collection.</li>
<li>Random access interface is not implemented.</li>
<li>Ideal if the operations are Insertion or deletion.</li>
</ul>
<h3 id="implementing-interfaces">Implementing Interfaces</h3>
<ul>
<li>Serializable</li>
<li>Cloneable</li>
<li>Iterable &lt;E&gt;</li>
<li>Collection &lt;E&gt;</li>
</ul>
<h3 id="methods-available-to-only-the-linkedlist-object">Methods available to only the LinkedList object.</h3>
<ul>
<li>.addFirst(Element) -&gt; adds the element in the front of the LinkedList.</li>
<li>.addLast(Element) -&gt; add an element in the last part of the LinkedList.</li>
<li>getFirst() &ndash;&gt; returns the Object at the front of the Linked List.</li>
<li>getLast() &ndash;&gt; returns the last object in the linked list.</li>
<li>removeFirst() -&gt; returns the object in the first and removes it from the linked list</li>
<li>removeLast() &ndash;&gt; returns the last objects and removes the object from the linked list.</li>
</ul>
<table>
  <thead>
      <tr>
          <th>Pros</th>
          <th>Cons</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ideal if insertion or deletion operations are more.</td>
          <td>Worst if there are lots of search operations as it needs to iterate through all the other elements.</td>
      </tr>
      <tr>
          <td>By default the elements can be traversed through forward direction.</td>
          <td>Java LinkedList class is non-synchronized.</td>
      </tr>
      <tr>
          <td>Can hold duplicate elements.</td>
          <td>Does not implement random access interface.</td>
      </tr>
      <tr>
          <td>Can hold null values.</td>
          <td></td>
      </tr>
      <tr>
          <td>The process of insertion or deletion is simple and fast.</td>
          <td></td>
      </tr>
  </tbody>
</table>
<hr>
<p><em>Note: This article was originally published on Blogger on February 23, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Chocolate Feast - Problem Solving - Hacker Rank Solution.</title><link>https://md.eknath.dev/posts/dsa/chocolate-feast-problem-solving-hackerrank/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/chocolate-feast-problem-solving-hackerrank/</guid><pubDate>Sun, 20 Feb 2022 00:04:00 +0530</pubDate><category>java</category><category>dsa</category><category>hackerrank</category><category>problem-solving</category><description>The expectation is to find the total number of chocolate one can consume by taking full advantage of the offer, Here there are 3 inputs n which holds the value of initial amount of money for buying chocolate, c is the cost price of each candy if paid by cash and m is the exchange rate for the candy.
Inputs n Initial cash to buy candy. c Cost of each candy if paid by cash. m Exchange rate for a new candy in offer. The initial count of chocolate will be the cash / cost and the wrappers in hand will be the same value of chocolate, and from there we loop through until the wrap count is less than the exchange rate, inside the loop the chocolate count will still hold the same formula as before but divided with exchange rate. The wrap count is the tricky part&amp;hellip; the wrap will be wrap / exchange rate (the no. of chocolates) + the remainder of this division (THIS IS VERY IMPORTANT) because for example if the count of wrapper is 3 and the exchange rate is 2 you can only buy 1 chocolate but the remainder 1 needs to be included or it will pop errors in the test cases.</description><content:encoded><![CDATA[<p>The expectation is to find the total number of chocolate one can consume by taking full advantage of the offer, Here there are 3 inputs <code>n</code> which holds the value of initial amount of money for buying chocolate, <code>c</code> is the cost price of each candy if paid by cash and <code>m</code> is the exchange rate for the candy.</p>
<h4 id="inputs">Inputs</h4>
<ul>
<li><strong>n</strong> Initial cash to buy candy.</li>
<li><strong>c</strong> Cost of each candy if paid by cash.</li>
<li><strong>m</strong> Exchange rate for a new candy in offer.</li>
</ul>
<p>The initial count of chocolate will be the cash / cost and the wrappers in hand will be the same value of chocolate, and from there we loop through until the wrap count is less than the exchange rate, inside the loop the chocolate count will still hold the same formula as before but divided with exchange rate. The wrap count is the tricky part&hellip; the wrap will be wrap / exchange rate (the no. of chocolates) + the remainder of this division (THIS IS VERY IMPORTANT) because for example if the count of wrapper is 3 and the exchange rate is 2 you can only buy 1 chocolate but the remainder 1 needs to be included or it will pop errors in the test cases.</p>
<h3 id="algorithm">Algorithm</h3>
<ul>
<li>declare 2 new variables chocolate and wrappers.</li>
<li>Initialize the chocolate to <code>n / c</code>.</li>
<li>Initialize the wrapper to chocolate.</li>
<li>Run a while loop while the wrapper &gt;= exchange rate (or <code>m</code>)</li>
<li>Add the value of chocolate to <code>wrapper / exchange rate</code> (or <code>m</code>)</li>
<li>Set the value of wrapper to the number of chocolates exchanged and the balance un-used wrapper.</li>
<li>return the count of chocolates consumed.</li>
</ul>
<p><strong>I Highly recommend you dont go through the below code now but try to solve it on your own to get the ability to solve problems, but im just pasting it here for those who haven&rsquo;t managed to find the solution despite many tries</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">chocolateFeast</span>(<span style="color:#66d9ef">int</span> n, <span style="color:#66d9ef">int</span> c, <span style="color:#66d9ef">int</span> m) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> cho <span style="color:#f92672">=</span> n <span style="color:#f92672">/</span> c, wrap <span style="color:#f92672">=</span> cho;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">while</span> (wrap <span style="color:#f92672">&gt;=</span> m) {
</span></span><span style="display:flex;"><span>        cho <span style="color:#f92672">+=</span> wrap <span style="color:#f92672">/</span> m;
</span></span><span style="display:flex;"><span>        wrap <span style="color:#f92672">=</span> wrap <span style="color:#f92672">/</span> m <span style="color:#f92672">+</span> wrap <span style="color:#f92672">%</span> m;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> cho;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 20, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Designer PDF Viewer - HackerRank Problems</title><link>https://md.eknath.dev/posts/dsa/designer-pdf-viewer-hackerrank-problems/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/designer-pdf-viewer-hackerrank-problems/</guid><pubDate>Sat, 12 Feb 2022 21:55:00 +0530</pubDate><category>java</category><category>dsa</category><category>hackerrank</category><category>problem-solving</category><description>Difficulty: EASY Problem : The objective here is to find the size of the highlighted area, and we are given the sizes of all the alphabets, we have to find the largest alphabet in the highlighted word and then calculate the size of the rectangle so if the tallest character is 3 then the size of the box will be 3 * number of characters given. Visual representation of the selection :</description><content:encoded><![CDATA[<h5 id="difficulty-easy">Difficulty: EASY</h5>
<h5 id="problem-">Problem :</h5>
<p>The objective here is to find the size of the highlighted area, and we are given the sizes of all the alphabets, we have to find the largest alphabet in the highlighted word and then calculate the size of the rectangle so if the tallest character is 3 then the size of the box will be 3 * number of characters given. Visual representation of the selection :</p>
<p><code>[abc]</code> <code>[def]</code> <code>[ghij]</code></p>
<h4 id="inputs">Inputs</h4>
<ol>
<li>An array with the sizes of all alphabets a-z in order.</li>
<li>A String of highlighted words.</li>
</ol>
<h4 id="important-points-to-note">Important points to note</h4>
<ul>
<li><strong>The array which holds the height of each character in ascending order</strong>
which means the arrays 0th index will have the height of a 1st index will have the height of b and so on and so forth in the end the height of z will be there so it&rsquo;s easy to locate each character.</li>
<li><strong>A String with the highlighted word.</strong>
This means we have got the characters inside the rectangle, all we have to find is the tallest one and half our problem is solved, here we need some knowledge of ASCII representation of characters—if not I recommend you go through that first and then join again.</li>
<li><strong>All the alphabets inside the String are lower case.</strong>
This means the ASCII values of the characters will start from 97 so if we decrement this value from the characters of the string we will get their index.</li>
<li><strong>Only the tallest matters to us</strong>
With the value from the array&rsquo;s index, we need only the tallest in the range of our characters inside the string, so we need only the highest value. for example, if our String is <code>&quot;abc&quot;</code> and the array&rsquo;s values are <code>{5,3,1,9..}</code> we take only 5 coz it&rsquo;s the tallest of our string <strong>which</strong> is at index 0.</li>
<li><strong>We need to return the size of the rectangle.</strong>
Once we get the tallest character then we can multiply it by the length of the given String.</li>
</ul>
<h4 id="algorithm">Algorithm</h4>
<ul>
<li>Initialize a variable tallest to zero</li>
<li>Loop through the character inside the given string</li>
<li>Inside the loop Initialize a variable index</li>
<li>Set the value of the index to the value - 97 (here value is the current character)</li>
<li>Compare the value from the ArrayList.get(index) to tallest</li>
<li>Update tallest if the value is greater.</li>
<li>Return tallest multiplied words length after the loop.</li>
</ul>
<h4 id="below-is-the-code-implementation">Below is the code implementation.</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">designerPdfViewer</span>(List <span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> h, String word) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> tallest <span style="color:#f92672">=</span> 0;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">char</span> c : word.<span style="color:#a6e22e">toCharArray</span>()) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">int</span> index <span style="color:#f92672">=</span> c <span style="color:#f92672">-</span> 97;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (h.<span style="color:#a6e22e">get</span>(index) <span style="color:#f92672">&gt;</span> tallest)
</span></span><span style="display:flex;"><span>            tallest <span style="color:#f92672">=</span> h.<span style="color:#a6e22e">get</span>(index);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> tallest <span style="color:#f92672">*</span> word.<span style="color:#a6e22e">length</span>();
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 12, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Introduction to Threads in Java</title><link>https://md.eknath.dev/posts/java/introduction-to-threads-in-java/</link><guid isPermaLink="true">https://md.eknath.dev/posts/java/introduction-to-threads-in-java/</guid><pubDate>Sat, 12 Feb 2022 12:26:00 +0530</pubDate><category>java</category><category>threads</category><category>concurrency</category><description>Threads are basically a way to run multiple tasks concurrently this can be helpful to speed up your program, usually, a Java program runs on a single thread by utilizing only one core of your processor here the other processors are just sitting idly by introducing thread java helps us to make use of these idle processors.
How to create a new thread process?
There are two ways of creating a new thread is by extending the Thread class and another way is by implementing Runnable interface either way the class must override the run method which will be called during the start method of the thread object.</description><content:encoded><![CDATA[<p>Threads are basically a way to run multiple tasks concurrently this can be helpful to speed up your program, usually, a Java program runs on a single thread by utilizing only one core of your processor here the other processors are just sitting idly by introducing thread java helps us to make use of these idle processors.</p>
<p><strong>How to create a new thread process?</strong></p>
<p>There are two ways of creating a new thread is by extending the Thread class and another way is by implementing Runnable interface either way the class must override the run method which will be called during the start method of the thread object.</p>
<p><em><strong>1. extending the Thread class</strong></em></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">extends</span> Thread {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><em><strong>2. Implementing Runnable Interface</strong></em></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">implements</span> Runnable {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>The ideal way would be by implementing the Runnable interface because this enables us to extend other required base classes if required, here the method run() will be called after the thread is started, here you may call the methods which you want to sublet or do any required process inside it, please note the run() returns nothing.</p>
<p><strong>Creating a thread Object.</strong></p>
<p>Creation of Thread object is same as creating any other object, <em>Thread &lt;name&gt; = new Thread(&lt;Object&gt;)</em> here a thread object will be created, now we can access the methods of the Thread class.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">extends</span> Thread {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
</span></span><span style="display:flex;"><span>    Main thread <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Main();
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="how-to-start-the-thread-">How to start the thread ?</h3>
<p>The thread can be started by calling the start method which is available in the thread class, for example <em>ThreadName.start()</em> here when the start method is called the process will start and eventually it will automatically call the run method and the thread will become active and start processing the methods inside the run method.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">extends</span> Thread {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
</span></span><span style="display:flex;"><span>    Main thread <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Main();
</span></span><span style="display:flex;"><span>    thread.<span style="color:#a6e22e">start</span>();
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is outside of the thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="lifecycle-of-thread">LifeCycle of Thread</h4>
<p>A thread exists only in any one of these states at a time, lets look at them one by one.</p>
<ul>
<li><strong>New</strong> as the name specifies this is a new state where the thread object is created, but none of the code inside it has started to execute yet.</li>
<li><strong>Runnable</strong> At this point the code inside this block is ready to run at any instant, once the thread scheduler flags green the code inside it will start to run one by one until there is any interruption.</li>
<li>Blocked</li>
<li>Waiting</li>
<li>Timed Waiting</li>
<li>Terminated</li>
</ul>
<hr>
<p><em>Note: This article was originally published on Blogger on February 12, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Collection Interface - Java Collections Framework - DSA</title><link>https://md.eknath.dev/posts/dsa/collection-interface-in-java-dsa/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/collection-interface-in-java-dsa/</guid><pubDate>Fri, 11 Feb 2022 17:59:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><description>Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example add() to add an element, size() to get the size and much more, below is a table of most common methods.</description><content:encoded><![CDATA[<p>Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example <code>add()</code> to add an element, <code>size()</code> to get the size and much more, below is a table of most common methods.</p>
<h4 id="hierarchy-of-the-collection-interface">Hierarchy of the Collection Interface.</h4>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/collection-interface-in-java-dsa-1.jpg"></p>
<p>The Parent of Collection Interface is <strong>Iterable</strong> Interface and the Collection is the base interface for List Interface, Set Interface and Queue Interface, the respective classes which implement either of the sub interfaces will also implement the defined methods from the Collection Interface, below are some of the commonly used methods.</p>
<h4 id="defined-methods">Defined Methods:</h4>
<table>
  <thead>
      <tr>
          <th>Method</th>
          <th>Description</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>add()</td>
          <td>This method returns a Boolean value true if it inserts the specified element in this collection.</td>
      </tr>
      <tr>
          <td>addAll()</td>
          <td>This method returns a Boolean value true if all elements were added successfully.</td>
      </tr>
      <tr>
          <td>clear()</td>
          <td>Removes all the elements of the collection.</td>
      </tr>
      <tr>
          <td>contains()</td>
          <td>returns a Boolean value if the specified object exists true else false.</td>
      </tr>
      <tr>
          <td>containsAll()</td>
          <td>returns a Boolean value if the specified Objects from the argument of collection and if all the elements exist inside the collection then it returns true else false.</td>
      </tr>
      <tr>
          <td>equals()</td>
          <td>returns true if the specified Object is equal to this current Collection.</td>
      </tr>
      <tr>
          <td>hashCode()</td>
          <td>returns the hash code value of this collection.</td>
      </tr>
      <tr>
          <td>isEmpty()</td>
          <td>returns true if this collection has no elements otherwise returns false.</td>
      </tr>
      <tr>
          <td>iterator()</td>
          <td>returns an Iterator over the elements in proper sequence.</td>
      </tr>
      <tr>
          <td>remove()</td>
          <td>Removes the first element where the specified element is found, but only the 1st element of the specified object will be removed.</td>
      </tr>
      <tr>
          <td>removeAll()</td>
          <td>same like remove() but all objects matching will be removed from this collection.</td>
      </tr>
      <tr>
          <td>removeIf()</td>
          <td>It removes all the elements of this collection that satisfy the given predicate filter.</td>
      </tr>
      <tr>
          <td>retainAll()</td>
          <td>The Objects inside the arguments collection will be kept and all other Objects will be removed from this collection.</td>
      </tr>
      <tr>
          <td>size()</td>
          <td>This returns the number of objects present inside this collection.</td>
      </tr>
      <tr>
          <td>spliterator()</td>
          <td>It returns a spliterator over the elements in this collection.</td>
      </tr>
      <tr>
          <td>toArray()</td>
          <td>It returns an array containing all the elements of this collection which are in proper sequence.</td>
      </tr>
  </tbody>
</table>
<h4 id="classes-that-implements-collection">Classes that implements Collection</h4>
<p><code>ArrayList</code> , <code>LinkedList</code> , <code>Vector</code> , <code>Stack</code> , <code>PriorityQueue</code> , <code>ArrayDeque</code> , <code>TreeSet</code> , <code>HashSet</code> , <code>LinkedHashSet</code></p>
<p>Which means all the above methods in the table can be used from the objects of these classes which implement Collection interface, along with these different collections have their own interfaces they implement depending on the underlying collections.</p>
<h4 id="sample-code">Sample Code:</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#f92672">import</span> java.util.*;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">TestJavaCollection1</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String args<span style="color:#f92672">[]</span>) {
</span></span><span style="display:flex;"><span>        ArrayList<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span> list <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span>(); <span style="color:#75715e">// Creating arraylist</span>
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ravi&#34;</span>); <span style="color:#75715e">// Adding object in arraylist</span>
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Vijay&#34;</span>);
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ravi&#34;</span>);
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ajay&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Traversing list through Iterator</span>
</span></span><span style="display:flex;"><span>        Iterator<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span> itr <span style="color:#f92672">=</span> list.<span style="color:#a6e22e">iterator</span>();
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">while</span> (itr.<span style="color:#a6e22e">hasNext</span>()) {
</span></span><span style="display:flex;"><span>            System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(itr.<span style="color:#a6e22e">next</span>());
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// output</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ravi</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Vijay</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ravi</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ajay</span>
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 11, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Array List - Collections Framework in Java - DSA</title><link>https://md.eknath.dev/posts/dsa/array-list-of-collections-in-java-dsa/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/array-list-of-collections-in-java-dsa/</guid><pubDate>Fri, 11 Feb 2022 10:15:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><category>arraylist</category><description>Gist: An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector.</description><content:encoded><![CDATA[<h3 id="gist">Gist:</h3>
<p>An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector.</p>
<h3 id="hierarchical-order">Hierarchical order</h3>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/array-list-of-collections-in-java-dsa-1.jpg"></p>
<h3 id="type-of-constructors">Type of constructors</h3>
<ul>
<li>Empty argument constructor or the default constructor is the same as invoking any other object here a new ArrayList is created with a default size of 10. Below is the most commonly used constructor by beginners and others alike.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ArrayList array <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList();
</span></span><span style="display:flex;"><span><span style="color:#75715e">//array has a capacity of 10</span>
</span></span></code></pre></div><ul>
<li>The default constructor above will allot only 10 slots but if you want the initial size to be 20 or 1000 you can do so with the following constructor this is ideal when you know the number of entries expected, compared to the default here you will save a lot of performance. since this part is important I will stretch it with an example.</li>
</ul>
<p>If you are preparing to insert 100 inputs by calling the default constructor, then every time the ArrayList reaches its capacity it will clone itself and grow to a certain capacity and this internal task will take a toll on your performance so instead if you already set the capacity while creating the array list this internal process of growing can be avoided thus saving a lot of performance and time.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ArrayList arrayOfPages <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList(380);
</span></span><span style="display:flex;"><span><span style="color:#75715e">//array has a capacity of 380</span>
</span></span></code></pre></div><ul>
<li>Conversion of other Collections like LinkedList, Vector, etc&hellip;to an ArrayList is possible with the following constructor.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ArrayList arraysFromVector <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList( VariableNameOfTheCollection );
</span></span><span style="display:flex;"><span><span style="color:#75715e">//here any collection will be converted into an ArrayList</span>
</span></span></code></pre></div><h3 id="important-points">Important Points</h3>
<ul>
<li>Is Dynamically Growable and Shrinkable.</li>
<li>Heterogeneous Objects are allowed.</li>
<li>The initial capacity is 10</li>
<li>Load Factor is 0.75</li>
<li>Order of insertion is preserved</li>
<li>We can also use generic data-type such that the array acts like a homogenous.</li>
</ul>
<h3 id="implementing-interfaces">Implementing Interfaces</h3>
<ul>
<li>Serializable</li>
<li>Cloneable</li>
<li>RandomAccess</li>
<li>Iterable &lt;E&gt;</li>
<li>Collection &lt;E&gt;</li>
<li>List &lt;E&gt;</li>
</ul>
<h3 id="commonly-used-methods-from-list-and-the-respective-base-classes">Commonly Used methods from List and the respective base classes.</h3>
<ul>
<li>.add(element) -&gt; add an element in the last.</li>
<li>.add(Index, element) -&gt; add an element at the specified index.</li>
<li>.addAll(Collection x) -&gt; adds all the elements of the collection x</li>
<li>.addAll(Index , Collection x) -&gt; adds all the elements of the collection x from the specified index.</li>
<li>.set(index,element) -&gt; change an element at the specified index.</li>
<li>.get(index) -&gt; Retrieve the element from the requested index.</li>
<li>.contains(object) -&gt; Retrieves a boolean value if the element is available..</li>
<li>.indexOf(object) -&gt; Retrieves index value if the element is available or -1.</li>
<li>.remove(object) -&gt; to remove an element at the requested position.</li>
<li>.sort() -&gt; Sorts the collection.</li>
<li>.subList(startIndex, endIndex) -&gt; returns an array list with values from the range of index.</li>
<li>.trimToSize() -&gt; The capacity of the array will be set to the size of the array to save memory.</li>
</ul>
<table>
  <thead>
      <tr>
          <th>Pros</th>
          <th>Cons</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ideal if a lot of index-based retrieval or update operations will be performed.</td>
          <td>If elements need to be <strong>added</strong> or <strong>removed</strong> at an index the entire array elements will be shifted internally.</td>
      </tr>
      <tr>
          <td>Can be traversed through both directions.</td>
          <td>Java ArrayList class is non-synchronized.</td>
      </tr>
      <tr>
          <td>Can hold duplicate elements.</td>
          <td></td>
      </tr>
      <tr>
          <td>Can take hold of null.</td>
          <td></td>
      </tr>
      <tr>
          <td>Once the load factor is met then the capacity will be increased by (current capacity *3/2)+1</td>
          <td></td>
      </tr>
  </tbody>
</table>
<hr>
<p><em>Note: This article was originally published on Blogger on February 11, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Breaking the Records - HackerRank- Problem Solving in Java</title><link>https://md.eknath.dev/posts/dsa/breaking-records-hackerrank-problem/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/breaking-records-hackerrank-problem/</guid><pubDate>Sun, 30 Jan 2022 12:36:00 +0530</pubDate><category>java</category><category>dsa</category><category>hackerrank</category><category>problem-solving</category><description>Difficulty : EASY Problem : Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.
Visual representation of the table Game Score Minimum Maximum Count Min Count Max 0 12 12 12 0 0 1 24 12 24 0 1 2 10 10 24 1 1 3 24 10 24 1 1 Output : [1 , 1] Important Points to note We calculate the record determining her performance on Game 0 which will be the value of minimum and maximum. We change the value of maximum or minimum only if her score breaks our current record of maximum or minimum. in case she breaks record then we increment the counter respectively. return the Count max and Count min in the same order as a List. Algorithm inputs received : List&amp;lt;Integer&amp;gt; score, int games Set Min and Max to scores 1st element or 0th index element. Loop through the scores from the 2nd element or the 1st index till the scores.size()-1 inside the above loop check if the value of current index is larger than maximum if so then increase the counter of the maximum and set the value of the maximum to the current value. else if statement to check if the value is smaller than current minimum if so then increase the counter of the minimum and set the value of minimum to the current value outside the loop create an array and add the two values in the order of maxcount then min count and add the list to the return statement. I Highly recommend you dont go through the below code now but try to solve it on your own to build the ability to solve problems, but im just pasting it here for those who haven&amp;rsquo;t managed to find the solution despite many tries</description><content:encoded><![CDATA[<h5 id="difficulty--easy">Difficulty : EASY</h5>
<h5 id="problem-">Problem :</h5>
<p>Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.</p>
<h4 id="visual-representation-of-the-table">Visual representation of the table</h4>
<table>
<tr>
<th>Game</th>
<th>Score</th>
<th>Minimum</th>
<th>Maximum</th>
<th>Count Min</th>
<th>Count Max</th>
</tr>
<tr>
<td>0</td>
<td>12</td>
<td>12</td>
<td>12</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>24</td>
<td>12</td>
<td>24</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>10</td>
<td>10</td>
<td>24</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>24</td>
<td>10</td>
<td>24</td>
<td>1</td>
<td>1</td>
</tr>
  <tr>
    <th colspan="2">Output :</th>
    <td colspan="4">[1 , 1]</td>
  </tr>
</table>
<h3 id="important-points-to-note">Important Points to note</h3>
<ul>
<li>We calculate the record determining her performance on Game 0 which will be the value of minimum and maximum.</li>
<li>We change the value of maximum or minimum only if her score breaks our current record of maximum or minimum.</li>
<li>in case she breaks record then we increment the counter respectively.</li>
<li>return the Count max and Count min in the same order as a List.</li>
</ul>
<h3 id="algorithm">Algorithm</h3>
<ul>
<li>inputs received : <code>List&lt;Integer&gt; score, int games</code></li>
<li>Set Min and Max to scores 1st element or 0th index element.</li>
<li>Loop through the scores from the 2nd element or the 1st index till the scores.size()-1</li>
<li>inside the above loop check if the value of current index is larger than maximum if so then increase the counter of the maximum and set the value of the maximum to the current value.</li>
<li>else if statement to check if the value is smaller than current minimum if so then increase the counter of the minimum and set the value of minimum to the current value</li>
<li>outside the loop create an array and add the two values in the order of maxcount then min count and add the list to the return statement.</li>
</ul>
<p><strong>I Highly recommend you dont go through the below code now but try to solve it on your own to build the ability to solve problems, but im just pasting it here for those who haven&rsquo;t managed to find the solution despite many tries</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">breakingTheRecords</span>(List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> score) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> currentMax <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(0);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> currentMin <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(0);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> surpassed <span style="color:#f92672">=</span> 0;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> backlogged <span style="color:#f92672">=</span> 0;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> index <span style="color:#f92672">=</span> 1; index <span style="color:#f92672">&lt;</span> score.<span style="color:#a6e22e">size</span>(); index<span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (score.<span style="color:#a6e22e">get</span>(index) <span style="color:#f92672">&gt;</span> currentMax) {
</span></span><span style="display:flex;"><span>            surpassed<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>            currentMax <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(index);
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> (score.<span style="color:#a6e22e">get</span>(index) <span style="color:#f92672">&lt;</span> currentMin) {
</span></span><span style="display:flex;"><span>            backlogged<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>            currentMin <span style="color:#f92672">=</span> score.<span style="color:#a6e22e">get</span>(index);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> result <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;&gt;</span>();
</span></span><span style="display:flex;"><span>    result.<span style="color:#a6e22e">add</span>(surpassed);
</span></span><span style="display:flex;"><span>    result.<span style="color:#a6e22e">add</span>(backlogged);
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(surpassed <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34; &#34;</span> <span style="color:#f92672">+</span> backlogged);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> result;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on January 30, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Literals of Base numbers in Java ( Octal , Hexadecimal, Decimal)</title><link>https://md.eknath.dev/posts/java/literals-in-java/</link><guid isPermaLink="true">https://md.eknath.dev/posts/java/literals-in-java/</guid><pubDate>Sat, 29 Jan 2022 18:37:00 +0530</pubDate><category>java</category><category>literals</category><category>basics</category><description>1. Overview: A literal key indicates the compiler how to interpret the value of the given data type, for numbers we can calculate the value by using Octal representation or hexadecimal representation but just typing out a hexadecimal value to an int will throw us an error because the compiler has no idea how to handle it but if we assign the java specified prefix for the required bases with some literals then the compiler will not throw us any error as it understands how to interpret the value.</description><content:encoded><![CDATA[<h3 id="1-overview">1. Overview:</h3>
<p>A literal key indicates the compiler how to interpret the value of the given data type, for numbers we can calculate the value by using Octal representation or hexadecimal representation but just typing out a hexadecimal value to an int will throw us an error because the compiler has no idea how to handle it but if we assign the java specified prefix for the required bases with some literals then the compiler will not throw us any error as it understands how to interpret the value.</p>
<table>
  <thead>
      <tr>
          <th>Base</th>
          <th>Literals</th>
          <th>Values</th>
          <th>Example</th>
          <th>Eg. Value</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>Decimal</strong></td>
          <td>none</td>
          <td>0-9</td>
          <td><em>int x = 10;</em></td>
          <td>x is 10</td>
      </tr>
      <tr>
          <td><strong>Octal</strong></td>
          <td>0 (zero) as the prefix</td>
          <td>0-7</td>
          <td><em>int x = 012;</em></td>
          <td>x is 10</td>
      </tr>
      <tr>
          <td><strong>Hexadecimal</strong></td>
          <td>0x or (zero) along with an x</td>
          <td>0-9 and a-f or A-F</td>
          <td><em>int x = 0xA;</em></td>
          <td>x is 10</td>
      </tr>
  </tbody>
</table>
<h3 id="1-binary-or-base2">1. Binary or Base(2)</h3>
<p><strong>Allowed Digits</strong> <em><strong>0 and 1</strong></em><br>
<em>int i = 0b1010;</em> and now the variable i has value 10.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> n <span style="color:#f92672">=</span> 0b1111101;
</span></span><span style="display:flex;"><span><span style="color:#75715e">// n has the value 125</span>
</span></span></code></pre></div><h3 id="2-decimal-or-base10">2. Decimal or Base(10)</h3>
<p><strong>Allowed Digits</strong> <em><strong>0 to 9</strong></em><br>
Decimal does not require any special literals to represent its base as the default base is 10, so the value is taken exactly as the raw value from the code.<br>
<em>int i = 10;</em> and now the variable i has value 10.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> n <span style="color:#f92672">=</span> 125;
</span></span><span style="display:flex;"><span><span style="color:#75715e">// n has the value 125</span>
</span></span></code></pre></div><h3 id="3-octal-or-base8">3. Octal or Base(8)</h3>
<p><strong>Allowed Digits :</strong> <em><strong>0 to 7</strong></em>
<strong>Prefix :</strong> 0(zero)
The first doubt that arises seeing the number 0-7 and calling it octal or base 8 confused me and I&rsquo;m sure there are many out there like me so here there are 8 digits including 0 so it has the name octal or base 8, to represent a number on octal in java a literal 0 (zero) must be prefixed before the octal value.
for example <em>int i = 0377;</em> and now the variable I has value 255.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> n <span style="color:#f92672">=</span> 0377;
</span></span><span style="display:flex;"><span><span style="color:#75715e">// n has the value 255</span>
</span></span></code></pre></div><h3 id="4-hexadecimal-or-base16">4. Hexadecimal or Base(16)</h3>
<p><strong>Allowed Digits :</strong> <em><strong>0 to 9 and a-f or A-F</strong></em>
<strong>Prefix :</strong> <em><strong>0x or 0X (zero) along with an x or X</strong></em>
The hexadecimal numbers can be assigned to any primitives using the literal 0(zero) and an x or X here the case is not important as the compiler takes it either way.
for example <em>int i = 0xa;</em> and now the variable i has value 10.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> n <span style="color:#f92672">=</span> 0xb;
</span></span><span style="display:flex;"><span><span style="color:#75715e">// n has the value 11</span>
</span></span></code></pre></div><h3 id="some-important-points">Some Important points</h3>
<ul>
<li>The default base is decimal.</li>
<li>The 0 is the prefix for both octal and hexadecimal but the hexadecimal needs an x after the 0
here x is case insensitive.</li>
<li>Hexadecimal characters a-z are case insensitive so a-f or A-F makes no difference to the compiler.</li>
</ul>
<hr>
<p><em>Note: This article was originally published on Blogger on January 29, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Introduction to Primitives - Java</title><link>https://md.eknath.dev/posts/java/introduction-to-primitives-java/</link><guid isPermaLink="true">https://md.eknath.dev/posts/java/introduction-to-primitives-java/</guid><pubDate>Sat, 29 Jan 2022 17:17:00 +0530</pubDate><category>java</category><category>primitives</category><category>basics</category><description>Overview: There are eight primitive data types byte, short, int, long, float, double, boolean, char. These eight data types store values as raw instead of Objects these primitives help to save memory to a great extent and simplify other processes as they are directly stored in the stack.
Data Type Size(in bits) Minimum Range Maximum Range byte 8 -128 127 short 16 -32768 32767 int 32 -2147483648 2147483647 long 64 -9223372036854775808 9223372036854775807 float 32 -3.4e38 to -1.4e-45 1.4e-45 to 3.4e38 double 64 -1.8e308 to -4.9e-324 4.9e-324 to 1.8e308 boolean 1 - - char 16 \u0000 (0) 65535 1. byte A byte has the capacity of 8 bits or 1 byte and it can store numbers between -2^7 and 2^7 -1 or simply -128 to 127. This is very useful while dealing with large arrays as they are way better than int or short arrays while considering memory efficiency and if the value is in the said range. The default value is 0</description><content:encoded><![CDATA[<h3 id="overview">Overview:</h3>
<p>There are eight primitive data types byte, short, int, long, float, double, boolean, char. These eight data types store values as raw instead of Objects these primitives help to save memory to a great extent and simplify other processes as they are directly stored in the stack.</p>
<table>
  <thead>
      <tr>
          <th>Data Type</th>
          <th>Size(in bits)</th>
          <th>Minimum Range</th>
          <th>Maximum Range</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>byte</strong></td>
          <td>8</td>
          <td>-128</td>
          <td>127</td>
      </tr>
      <tr>
          <td><strong>short</strong></td>
          <td>16</td>
          <td>-32768</td>
          <td>32767</td>
      </tr>
      <tr>
          <td><strong>int</strong></td>
          <td>32</td>
          <td>-2147483648</td>
          <td>2147483647</td>
      </tr>
      <tr>
          <td><strong>long</strong></td>
          <td>64</td>
          <td>-9223372036854775808</td>
          <td>9223372036854775807</td>
      </tr>
      <tr>
          <td><strong>float</strong></td>
          <td>32</td>
          <td>-3.4e38 to -1.4e-45</td>
          <td>1.4e-45 to 3.4e38</td>
      </tr>
      <tr>
          <td><strong>double</strong></td>
          <td>64</td>
          <td>-1.8e308 to -4.9e-324</td>
          <td>4.9e-324 to 1.8e308</td>
      </tr>
      <tr>
          <td><strong>boolean</strong></td>
          <td>1</td>
          <td>-</td>
          <td>-</td>
      </tr>
      <tr>
          <td><strong>char</strong></td>
          <td>16</td>
          <td><code>\u0000</code> (0)</td>
          <td>65535</td>
      </tr>
  </tbody>
</table>
<h3 id="1-byte">1. byte</h3>
<p>A byte has the capacity of 8 bits or 1 byte and it can store numbers between -2^7 and 2^7 -1 or simply -128 to 127.
This is very useful while dealing with large arrays as they are way better than int or short arrays while considering memory efficiency and if the value is in the said range.
<strong>The default value is 0</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">byte</span> number <span style="color:#f92672">=</span> 125;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">byte</span> num;
</span></span></code></pre></div><h3 id="2-short">2. short</h3>
<p>A short is if you want to save memory but byte is too small for your activity and int is too large then the short data type is there for your rescue which has a memory space of 16 bits or 2 bytes which can store the numbers between -2 ^15 and 2^15-1 or simply -32,768 to 32,767.
<strong>The default value is 0.</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">short</span> number <span style="color:#f92672">=</span> 125;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">short</span> num;
</span></span></code></pre></div><h3 id="3-int">3. int</h3>
<p>An int is the go-to data type for a number by the programmers as they need not worry about the range for their basic calculation for everyday problem solving, the size is 32 bits or 4 bytes and has a range of -2^31 and 2^31-1 or in simple numbers -2,147,483,648 to 2,147,483,647.
<strong>The default value is 0</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> number <span style="color:#f92672">=</span> 599999;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> num;
</span></span></code></pre></div><h3 id="4-float">4. float</h3>
<p>A float is a data type that can store decimals but can guarantee the precision only until the decimal place is 6 or less otherwise the value becomes an estimate but the size is the same as int which is 32 bits or 4 bytes but the range is 1.40239846 x 10^-45, and the largest value is 3.40282347 x 10^38.
<strong>The default value is 0.0f</strong>
<strong>float values must end with literal f</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">float</span> number <span style="color:#f92672">=</span> 3.<span style="color:#a6e22e">14129f</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">float</span> num;
</span></span></code></pre></div><h3 id="5-double">5. double</h3>
<p>A double is also a data type that can store decimals, as the name suggests the size is double of the float, which is 64 bits or 8 bytes and the range is 4.9406564584124654 x 10^-324 to 1.7976931348623157 x 10^308
<strong>The default value is 0.0d</strong>
<strong>double values must end with literal d</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">double</span> number <span style="color:#f92672">=</span> 3.<span style="color:#a6e22e">14129d</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">double</span> num;
</span></span></code></pre></div><h3 id="6-boolean">6. boolean</h3>
<p>A boolean can store only 2 values <em>true</em> and <em>false</em> which are primarily used for the control of the loops or other methods the size is just 1 bit but java stores it as 1 byte.
<strong>The default value is <em>false</em></strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">boolean</span> run <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">boolean</span> flag;
</span></span></code></pre></div><h3 id="7-char">7. char</h3>
<p>The last data type is char which is a 16-bit integer representing a Unicode-encoded character. Its range is from 0 to 65,535 which in Unicode represents &lsquo;\u0000&rsquo; to &lsquo;\uffff&rsquo;.
<strong>The default value is <em><code>\u0000</code> (null character)</em></strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">char</span> c <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;H&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">char</span> s;
</span></span></code></pre></div><h3 id="some-important-points">Some Important points</h3>
<ul>
<li>The default value is applicable only for instance variables or simply variables declared inside the class but outside the constructor.</li>
<li>String: Even though the string is declared and acts like a primitive in java it is basically a class every time we create a string an object is created with a sequence of characters in a char array.</li>
<li>Using underscore between two numbers are legal in java example <em>int k = 59_6565_231;</em> just not the beginning or end of the numbers and in case of decimals not next to them either..</li>
</ul>
<hr>
<p><em>Note: This article was originally published on Blogger on January 29, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Naming Conventions in Java</title><link>https://md.eknath.dev/posts/java/naming-conventions-in-java/</link><guid isPermaLink="true">https://md.eknath.dev/posts/java/naming-conventions-in-java/</guid><pubDate>Sat, 29 Jan 2022 16:47:00 +0530</pubDate><category>java</category><category>naming-conventions</category><category>basics</category><description>As the movies are rated G, PG, U, A depending on the age restriction and by just gazing through the rating our parents decide the movie to watch together, or a book with different labels to keep track of the sections and just by seeing them we know what they represent, the same labeling method has been introduced by the Java team to differentiate variables, methods, classes, constants etc.. and we will be going through them all here.</description><content:encoded><![CDATA[<p>As the movies are rated G, PG, U, A depending on the age restriction and by just gazing through the rating our parents decide the movie to watch together, or a book with different labels to keep track of the sections and just by seeing them we know what they represent, the same labeling method has been introduced by the Java team to differentiate variables, methods, classes, constants etc.. and we will be going through them all here.</p>
<table>
  <thead>
      <tr>
          <th>Type</th>
          <th>Naming Convention</th>
          <th>Example</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Variables</td>
          <td>Must start with a lower case and every other word capitalized</td>
          <td><em>int counter;</em></td>
      </tr>
      <tr>
          <td>Constants</td>
          <td>Must all be upper case and _ for differentiating every other word</td>
          <td><em>final int MAX_HEALTH = 100;</em></td>
      </tr>
      <tr>
          <td>Methods</td>
          <td>Must start with a lower case and every other word capitalized. can be distinguished from the variable by the () next to the method name</td>
          <td><em>void printNum(){..}</em></td>
      </tr>
      <tr>
          <td>Class</td>
          <td>Must start an upper case letter and every other word must be capitalized</td>
          <td><em>class HelloWorld{..}</em></td>
      </tr>
      <tr>
          <td>Interface</td>
          <td>Must start an upper case letter and every other word must be capitalized</td>
          <td><em>interface ActionListener{..}</em></td>
      </tr>
  </tbody>
</table>
<h3 id="1-packages">1. Packages</h3>
<p>A package must all be in lowercase letters example <em>java.awt</em> and as it represents a group of classes and interfaces in case another developer wants to import your package this naming convention will be helpful for him to proceed further without worrying about the case in the name for example <em>java.awt</em> is easier to recall than <em>jAvA.AwT</em> so it avoids unnecessary confusion.</p>
<p><strong>here is an example</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#f92672">package</span> myown.io;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> java.io.*;
</span></span></code></pre></div><h3 id="2-constants">2. Constants</h3>
<p>A constant variable must be completely upper case and to differentiate the words we can use underscores(_), there are some pre-built constants such as MAX_VALUE , MIN_VALUE in the Number&rsquo;s subclasses etc..</p>
<p><strong>here is an example</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">final</span> <span style="color:#66d9ef">int</span> WORKING_HOURS <span style="color:#f92672">=</span> 6;
</span></span></code></pre></div><h3 id="3-variables">3. Variables</h3>
<p>A variable is a reference of an object or a value of any datatype and the name must start with lowercase and every other word must be capitalized and this method is called camel casing for example <em>int ageOfPet = 1;</em> or if its only one word then <em>int age = 10;</em> so anytime you see this naming convention then assume its a variable except with an exception that we will see in the methods.</p>
<p><strong>here is an example</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> age <span style="color:#f92672">=</span> 6;
</span></span><span style="display:flex;"><span>age <span style="color:#f92672">=</span> 12;
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(age);
</span></span></code></pre></div><h3 id="4-methods">4. Methods</h3>
<p>The naming convention is the same as the variable, must start with the lowercase and every other word must be capitalized or in other words must be a camel case word but I know you are asking in your mind &ldquo;<em>if both methods and variables have the same naming convention whats the whole point?&rdquo;</em> Well, the thing is methods always have brackets next to them example <em>sum(5+5)</em> or <em>public static void main(String[] args)</em> so as you can see even though they have the same naming convention they both can be easily differentiated.</p>
<p><strong>here is an example</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">printNum</span>(<span style="color:#66d9ef">int</span> a)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(a);
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="5-class">5. Class</h3>
<p>A class must start with a capital letter and every other word capitalized for example <em>class HelloWorld {}</em> or <em>class Hello{}</em> so just capitalize all the words, the same naming convention for the abstract classes as well.</p>
<p><strong>here is an example</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">HelloWorld</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    ....
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="6-interface">6. Interface</h3>
<p>An Interface must be named same as the classes naming convention, The first letter must be capitalized and all other words must be capitalized as well for example <em>interface ActionListener{}</em></p>
<p><strong>here is an example</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">ActionListener</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    ....
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="other-important-note">Other Important Note</h3>
<ul>
<li>Symbols such as $ or _ etc can be used if you want but everything must start with an alphabet</li>
<li>Not following the naming convention will not throw compile time error.</li>
</ul>
<hr>
<p><em>Note: This article was originally published on Blogger on January 29, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item></channel></rss>