<?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>Basics · MangoDriod</title><link>https://md.eknath.dev/tags/basics/</link><description>Basics updates from MangoDriod</description><language>en-us</language><atom:link href="https://md.eknath.dev/tags/basics/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Wed, 25 Nov 2026 09:45:04 +0530</lastBuildDate><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>