<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Vishesh0x]]></title><description><![CDATA[Vishesh0x]]></description><link>https://blog.visheshraghuvanshi.in</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1765969092441/096e14ee-e170-4ca2-bfa1-46c289b3130d.png</url><title>Vishesh0x</title><link>https://blog.visheshraghuvanshi.in</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 06:16:46 GMT</lastBuildDate><atom:link href="https://blog.visheshraghuvanshi.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Java Operators & Bitwise Tricks]]></title><description><![CDATA[Arithmetic Operators and Expressions
Java gives you a small but powerful set of arithmetic operators. These work pretty much the way you’d expect:

+ → Addition

- → Subtraction

/ → Division

* → Multiplication

% → Modulus (returns the remainder af...]]></description><link>https://blog.visheshraghuvanshi.in/operators-and-expressions</link><guid isPermaLink="true">https://blog.visheshraghuvanshi.in/operators-and-expressions</guid><category><![CDATA[Java]]></category><category><![CDATA[Operators]]></category><category><![CDATA[bitwise operators]]></category><category><![CDATA[type casting]]></category><dc:creator><![CDATA[Vishesh Raghuvanshi]]></dc:creator><pubDate>Mon, 17 Nov 2025 16:12:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1763395826426/d0c247ce-4d1c-4fb2-b417-7bb3a3106bf7.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-arithmetic-operators-and-expressions"><strong>Arithmetic Operators and Expressions</strong></h2>
<p>Java gives you a small but powerful set of arithmetic operators. These work pretty much the way you’d expect:</p>
<ul>
<li><p><code>+</code> → Addition</p>
</li>
<li><p><code>-</code> → Subtraction</p>
</li>
<li><p><code>/</code> → Division</p>
</li>
<li><p><code>*</code> → Multiplication</p>
</li>
<li><p><code>%</code> → Modulus (returns the remainder after division)</p>
</li>
</ul>
<p>You can use these operators on all numeric data types (<code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, <code>float</code>, <code>double</code>, <code>char</code>), but <strong>not on</strong> <code>boolean</code>(Java refuses to treat <code>true</code> and <code>false</code> like numbers).</p>
<h3 id="heading-operator-precedence-highest-lowest"><strong>Operator Precedence (Highest → Lowest)</strong></h3>
<p>When you combine multiple operators in one expression, Java follows a fixed precedence order:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Precedence</td><td>Operators</td><td>Meaning</td></tr>
</thead>
<tbody>
<tr>
<td><strong>1</strong></td><td><code>*</code> <code>/</code> <code>%</code></td><td>Multiplication, Division, Modulus</td></tr>
<tr>
<td><strong>2</strong></td><td><code>+</code> <code>-</code></td><td>Addition, Subtraction</td></tr>
</tbody>
</table>
</div><p>So in an expression like:</p>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> x = <span class="hljs-number">10</span> + <span class="hljs-number">6</span> * <span class="hljs-number">2</span>;
</code></pre>
<p>Java always evaluates the <code>*</code> first, then applies the <code>+</code>, giving you <code>22</code>.</p>
<p>If you want to override the default order, parentheses still rule the world.</p>
<h2 id="heading-javas-type-promotion-rules-why-your-byte-suddenly-becomes-an-int"><strong>Java’s Type Promotion Rules (Why Your</strong> <code>byte</code> Suddenly Becomes an <code>int</code>)</h2>
<p>One thing Java loves to do (usually without asking) is <em>promote</em> smaller data types during arithmetic. So even if you start with tiny types like <code>byte</code> or <code>short</code>, the moment you add or subtract them, Java lifts them into bigger types behind the scenes. It’s part of what Java calls <strong>numeric promotion</strong>.</p>
<ul>
<li><p><code>byte</code>, <code>short</code>, and <code>char</code> all get promoted to <strong>int</strong> when used in an expression</p>
<ul>
<li><p><code>byte + short → int</code></p>
</li>
<li><p><code>char + short → int</code></p>
</li>
<li><p><code>char + int → int</code></p>
</li>
</ul>
</li>
<li><p>If either operand is <strong>long</strong>, the whole expression becomes <code>long</code></p>
<ul>
<li><code>short + long → long</code></li>
</ul>
</li>
<li><p>If either side is a <strong>float</strong>, Java goes with <code>float</code></p>
<ul>
<li><p><code>int + float → float</code></p>
</li>
<li><p><code>long + float → float</code></p>
</li>
</ul>
</li>
<li><p>And if a <strong>double</strong> enters the chat, everything becomes <code>double</code></p>
<ul>
<li><p><code>float + double → double</code></p>
</li>
<li><p><code>long + double → double</code></p>
</li>
</ul>
</li>
</ul>
<p>The idea is basically: <em>Java always promotes to the “safer” type so nothing important gets lost.</em></p>
<h2 id="heading-increment-and-decrement-operators">Increment and Decrement Operators</h2>
<p>Java gives us four operators to increase or decrease a value by 1. The only thing is <em>when</em> the increment/decrement happens.</p>
<ul>
<li><p><strong>Post-increment (</strong><code>i++</code>) → use the value first, then increase it</p>
</li>
<li><p><strong>Post-decrement (</strong><code>i--</code>) → use the value first, then decrease it</p>
</li>
<li><p><strong>Pre-increment (</strong><code>++i</code>) → increase first, then use the value</p>
</li>
<li><p><strong>Pre-decrement (</strong><code>--i</code>) → decrease first, then use the value</p>
</li>
</ul>
<p><strong>Quick examples:</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> x = <span class="hljs-number">5</span>;
System.out.println(x++); <span class="hljs-comment">// prints 5, x becomes 6</span>
System.out.println(++x); <span class="hljs-comment">// x becomes 7, prints 7</span>

<span class="hljs-keyword">int</span> y = <span class="hljs-number">10</span>;
System.out.println(y--); <span class="hljs-comment">// prints 10, y becomes 9</span>
System.out.println(--y); <span class="hljs-comment">// y becomes 8, prints 8</span>
</code></pre>
<h2 id="heading-bitwise-operators-in-java-and-or-xor-not-shifts">Bitwise Operators in Java — AND, OR, XOR, NOT, Shifts</h2>
<p>Bitwise operators work directly on the binary representation of numbers. They’re super fast and great for low-level tasks like masks, flags, and performance-critical logic.</p>
<h3 id="heading-main-bitwise-operators"><strong>Main Bitwise Operators</strong></h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Operator</td><td>Symbol</td><td>Meaning</td></tr>
</thead>
<tbody>
<tr>
<td>AND</td><td><code>&amp;</code></td><td>Sets a bit to 1 only if both bits are 1</td></tr>
<tr>
<td>OR</td><td>`</td><td>`</td><td>Sets a bit to 1 if either bit is 1</td></tr>
<tr>
<td>XOR</td><td><code>^</code></td><td>Sets a bit to 1 if the bits are different</td></tr>
<tr>
<td>NOT</td><td><code>~</code></td><td>Flips every bit</td></tr>
<tr>
<td>Left Shift</td><td><code>&lt;&lt;</code></td><td>Shifts bits left (multiply by 2ⁿ)</td></tr>
<tr>
<td>Right Shift</td><td><code>&gt;&gt;</code></td><td>Arithmetic right shift (keeps sign bit)</td></tr>
<tr>
<td>Unsigned Right Shift</td><td><code>&gt;&gt;&gt;</code></td><td>Logical right shift (fills with 0)</td></tr>
</tbody>
</table>
</div><h2 id="heading-truth-tables"><strong>Truth Tables</strong></h2>
<h3 id="heading-and-amp"><strong>AND (</strong><code>&amp;</code>)</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>A</strong></td><td><strong>B</strong></td><td><strong>A &amp; B</strong></td></tr>
</thead>
<tbody>
<tr>
<td>0</td><td>0</td><td>0</td></tr>
<tr>
<td>0</td><td>1</td><td>0</td></tr>
<tr>
<td>1</td><td>0</td><td>0</td></tr>
<tr>
<td>1</td><td>1</td><td>1</td></tr>
</tbody>
</table>
</div><h3 id="heading-or"><strong>OR (</strong><code>|</code>)</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>A</strong></td><td><strong>B</strong></td><td>**A</td><td>B**</td></tr>
</thead>
<tbody>
<tr>
<td>0</td><td>0</td><td>0</td></tr>
<tr>
<td>0</td><td>1</td><td>1</td></tr>
<tr>
<td>1</td><td>0</td><td>1</td></tr>
<tr>
<td>1</td><td>1</td><td>1</td></tr>
</tbody>
</table>
</div><h3 id="heading-xor"><strong>XOR (</strong><code>^</code>)</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>A</strong></td><td><strong>B</strong></td><td><strong>A ^ B</strong></td></tr>
</thead>
<tbody>
<tr>
<td>0</td><td>0</td><td>0</td></tr>
<tr>
<td>0</td><td>1</td><td>1</td></tr>
<tr>
<td>1</td><td>0</td><td>1</td></tr>
<tr>
<td>1</td><td>1</td><td>0</td></tr>
</tbody>
</table>
</div><h2 id="heading-bitwise-examples"><strong>Bitwise Examples</strong></h2>
<p>int x = 10, y = 6, z;<br />Binary:</p>
<ul>
<li><p><code>x = 10 → 00001010</code></p>
</li>
<li><p><code>y = 6 → 00000110</code></p>
</li>
</ul>
<h3 id="heading-and"><strong>AND</strong></h3>
<pre><code class="lang-java">  <span class="hljs-number">00001010</span>
&amp; <span class="hljs-number">00000110</span>
-----------
  <span class="hljs-number">00000010</span> → <span class="hljs-number">2</span>
</code></pre>
<h3 id="heading-or-1"><strong>OR</strong></h3>
<pre><code class="lang-java">  <span class="hljs-number">00001010</span>
| <span class="hljs-number">00000110</span>
-----------
  <span class="hljs-number">00001110</span> → <span class="hljs-number">14</span>
</code></pre>
<h3 id="heading-xor-1"><strong>XOR</strong></h3>
<pre><code class="lang-java">  <span class="hljs-number">00001010</span>
^ <span class="hljs-number">00000110</span>
-----------
  <span class="hljs-number">00001100</span> → <span class="hljs-number">12</span>
</code></pre>
<h2 id="heading-shift-operators"><strong>Shift Operators</strong></h2>
<p>int x = 10; // 00001010<br />int z;</p>
<h3 id="heading-left-shift-ltlt"><strong>Left Shift (</strong><code>&lt;&lt;</code>)</h3>
<pre><code class="lang-java">x   =   <span class="hljs-number">00001010</span>
x &lt;&lt; <span class="hljs-number">1</span> = <span class="hljs-number">00010100</span> → <span class="hljs-number">20</span>
</code></pre>
<p>General rule:<br /><code>x &lt;&lt; k → x * 2^k</code></p>
<h3 id="heading-right-shift-gtgt-keeps-the-sign-bit"><strong>Right Shift (</strong><code>&gt;&gt;</code>) (keeps the sign bit)</h3>
<pre><code class="lang-java">x   =     <span class="hljs-number">00001010</span>
x &gt;&gt; <span class="hljs-number">1</span> = <span class="hljs-number">00000101</span> → <span class="hljs-number">5</span>
</code></pre>
<p>General rule:<br /><code>x &gt;&gt; k → x / 2^k</code></p>
<h2 id="heading-how-negative-numbers-are-stored-twos-complement"><strong>How Negative Numbers Are Stored (Two’s Complement)</strong></h2>
<p>Let’s store <code>-10</code>:</p>
<ol>
<li><p>Start with <code>+10</code><br /> <code>00001010</code></p>
</li>
<li><p>Flip all bits (1’s complement)<br /> <code>11110101</code></p>
</li>
<li><p>Add 1 (2’s complement)<br /> <code>11110110</code> → this represents <code>-10</code> in binary</p>
</li>
</ol>
<p>Since the first bit is <strong>1</strong>, the number is negative.</p>
<h3 id="heading-right-shift-with-negative-numbers"><strong>Right Shift With Negative Numbers</strong></h3>
<pre><code class="lang-java">x = -<span class="hljs-number">10</span> = <span class="hljs-number">11110110</span>
x &gt;&gt; <span class="hljs-number">1</span>  = <span class="hljs-number">11111011</span>  <span class="hljs-comment">// sign bit stays 1</span>
x &gt;&gt;&gt; <span class="hljs-number">1</span> = <span class="hljs-number">01111011</span>  <span class="hljs-comment">// fills with 0 → 123</span>
</code></pre>
<p><code>&gt;&gt;&gt;</code> always shifts in zero, even for negative numbers.</p>
<h2 id="heading-bitwise-not"><strong>Bitwise NOT (</strong><code>~</code>)</h2>
<pre><code class="lang-java">x = <span class="hljs-number">00001010</span>  <span class="hljs-comment">// 10</span>
~x= <span class="hljs-number">11110101</span>  <span class="hljs-comment">// -11</span>
</code></pre>
<p>The rule is:<br /><code>~x = -(x + 1)</code></p>
<p>So:<br />~10 = -11</p>
<h2 id="heading-bit-masking-and-merging"><strong>Bit Masking and Merging</strong></h2>
<p>Bit masking lets you <strong>check, set, or clear specific bits</strong> in an integer using bitwise operators. Think of it as controlling individual switches in a row of lights — you can flip only the ones you care about.</p>
<ul>
<li><strong>Masking (checking a bit):</strong></li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> flags = <span class="hljs-number">0b1010</span>;    <span class="hljs-comment">// 4 bits: 1010</span>
<span class="hljs-keyword">int</span> mask  = <span class="hljs-number">0b0010</span>;    <span class="hljs-comment">// check 2nd bit</span>

<span class="hljs-keyword">boolean</span> isSet = (flags &amp; mask) != <span class="hljs-number">0</span>;  <span class="hljs-comment">// true, 2nd bit is 1</span>
</code></pre>
<ul>
<li><strong>Merging (setting a bit):</strong></li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> flags = <span class="hljs-number">0b1000</span>;
<span class="hljs-keyword">int</span> mask  = <span class="hljs-number">0b0010</span>;

flags = flags | mask;   <span class="hljs-comment">// set the 2nd bit</span>
<span class="hljs-comment">// flags = 1010</span>
</code></pre>
<p>With just <code>&amp;</code> and <code>|</code>, you can <strong>read and write bits efficiently</strong>, which is crucial in low-level programming, graphics, and flags handling.</p>
<h2 id="heading-question-how-to-store-2-numbers-in-1-byte"><strong>Question: How to store 2 numbers in 1 byte?</strong></h2>
<p>A single byte is <strong>8 bits</strong>, so if both numbers are small enough (≤ 4 bits each, i.e., 0–15), you can <strong>pack them together</strong> using bitwise operations.</p>
<h3 id="heading-step-1-define-the-numbers"><strong>Step 1: Define the numbers</strong></h3>
<pre><code class="lang-java"><span class="hljs-keyword">byte</span> a = <span class="hljs-number">9</span>;  <span class="hljs-comment">// 4 bits max: 1001</span>
<span class="hljs-keyword">byte</span> b = <span class="hljs-number">6</span>;  <span class="hljs-comment">// 4 bits max: 0110</span>
</code></pre>
<h3 id="heading-step-2-pack-them-into-1-byte"><strong>Step 2: Pack them into 1 byte</strong></h3>
<pre><code class="lang-java"><span class="hljs-keyword">byte</span> packed = (<span class="hljs-keyword">byte</span>) ((a &lt;&lt; <span class="hljs-number">4</span>) | b);
</code></pre>
<ul>
<li><p><code>a &lt;&lt; 4</code> → move <code>a</code> to the <strong>higher 4 bits</strong></p>
</li>
<li><p><code>| b</code> → merge <code>b</code> into the <strong>lower 4 bits</strong></p>
</li>
</ul>
<pre><code class="lang-java">a = <span class="hljs-number">1001</span> → <span class="hljs-number">10010000</span>
b = <span class="hljs-number">0110</span> → <span class="hljs-number">00000110</span>
packed = <span class="hljs-number">10010110</span> → <span class="hljs-number">150</span> (decimal)
</code></pre>
<hr />
<h3 id="heading-step-3-unpack-the-numbers"><strong>Step 3: Unpack the numbers</strong></h3>
<pre><code class="lang-java"><span class="hljs-keyword">byte</span> a2 = (<span class="hljs-keyword">byte</span>) ((packed &gt;&gt; <span class="hljs-number">4</span>) &amp; <span class="hljs-number">0x0F</span>);  <span class="hljs-comment">// high 4 bits</span>
<span class="hljs-keyword">byte</span> b2 = (<span class="hljs-keyword">byte</span>) (packed &amp; <span class="hljs-number">0x0F</span>);         <span class="hljs-comment">// low 4 bits</span>

System.out.println(a2 + <span class="hljs-string">" "</span> + b2);  <span class="hljs-comment">// 9 6</span>
</code></pre>
<ul>
<li><p><code>&gt;&gt; 4</code> shifts high bits to low position</p>
</li>
<li><p><code>&amp; 0x0F</code> masks out unwanted bits</p>
</li>
</ul>
<p>This way, you <strong>store two 4-bit numbers in a single byte</strong> and retrieve them later.</p>
<h2 id="heading-widening-and-narrowing-type-casting"><strong>Widening and Narrowing (Type Casting)</strong></h2>
<h3 id="heading-widening-upcasting"><strong>Widening / Upcasting</strong></h3>
<ul>
<li><p>Converts <strong>smaller type → bigger type</strong> automatically.</p>
</li>
<li><p>Safe, no data loss.</p>
</li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">byte</span> b = <span class="hljs-number">10</span>;
<span class="hljs-keyword">int</span> i = b;  <span class="hljs-comment">// byte → int (widening)</span>
</code></pre>
<h3 id="heading-narrowing-downcasting"><strong>Narrowing / Downcasting</strong></h3>
<ul>
<li><p>Converts <strong>bigger type → smaller type</strong>.</p>
</li>
<li><p>Must be explicit; can lose data.</p>
</li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> i = <span class="hljs-number">130</span>;
<span class="hljs-keyword">byte</span> b = (<span class="hljs-keyword">byte</span>) i;  <span class="hljs-comment">// narrowing, b = -126</span>
</code></pre>
<h2 id="heading-question-how-to-swap-two-numbers-without-a-temp-variable"><strong>Question: How to swap two numbers without a Temp variable?</strong></h2>
<p>Bitwise XOR lets you swap numbers:</p>
<pre><code class="lang-java"><span class="hljs-keyword">int</span> a = <span class="hljs-number">5</span>, b = <span class="hljs-number">9</span>;

a = a ^ b;  <span class="hljs-comment">// Step 1</span>
b = a ^ b;  <span class="hljs-comment">// Step 2</span>
a = a ^ b;  <span class="hljs-comment">// Step 3</span>

System.out.println(a + <span class="hljs-string">" "</span> + b);  <span class="hljs-comment">// 9 5</span>
</code></pre>
<p>No extra memory needed.</p>
]]></content:encoded></item><item><title><![CDATA[Data Types]]></title><description><![CDATA[Data Types
In Java, data types define the type of data that a variable can hold. They also determine the amount of memory allocated to the variable and the range of values it can store.
1. Categories of Data Types
Java data types are broadly classifi...]]></description><link>https://blog.visheshraghuvanshi.in/data-types</link><guid isPermaLink="true">https://blog.visheshraghuvanshi.in/data-types</guid><category><![CDATA[Java]]></category><category><![CDATA[data types]]></category><category><![CDATA[literals]]></category><dc:creator><![CDATA[Vishesh Raghuvanshi]]></dc:creator><pubDate>Sun, 02 Nov 2025 07:40:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762068808323/24d1d2c0-06ce-4f21-a34a-eb2a7ae325a3.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-data-types">Data Types</h2>
<p>In Java, <strong>data types</strong> define the type of data that a variable can hold. They also determine the amount of memory allocated to the variable and the range of values it can store.</p>
<h3 id="heading-1-categories-of-data-types"><strong>1. Categories of Data Types</strong></h3>
<p>Java data types are broadly classified into two categories:</p>
<ol>
<li><p><strong>Primitive Data Types</strong></p>
</li>
<li><p><strong>Non-Primitive (Reference) Data Types</strong></p>
</li>
</ol>
<p>This section focuses on <strong>primitive data types</strong>.</p>
<h3 id="heading-2-primitive-data-types"><strong>2. Primitive Data Types</strong></h3>
<p>Java has <strong>eight primitive data types</strong>, which are divided into four groups based on the kind of values they store:</p>
<h4 id="heading-a-integral-types"><strong>A. Integral Types</strong></h4>
<p>Used to store whole numbers (integers).</p>
<ul>
<li><p><strong>byte</strong></p>
</li>
<li><p><strong>short</strong></p>
</li>
<li><p><strong>int</strong></p>
</li>
<li><p><strong>long</strong></p>
</li>
</ul>
<h4 id="heading-b-floating-point-types"><strong>B. Floating-Point Types</strong></h4>
<p>Used to store numbers with decimal points.</p>
<ul>
<li><p><strong>float</strong></p>
</li>
<li><p><strong>double</strong></p>
</li>
</ul>
<h4 id="heading-c-character-type"><strong>C. Character Type</strong></h4>
<p>Used to store a single Unicode character.</p>
<ul>
<li><strong>char</strong></li>
</ul>
<h4 id="heading-d-boolean-type"><strong>D. Boolean Type</strong></h4>
<p>Used to store logical values.</p>
<ul>
<li><strong>boolean</strong></li>
</ul>
<h3 id="heading-3-primitive-data-type-details">3. Primitive Data Type Details</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Type</strong></td><td><strong>Size</strong></td><td><strong>Range</strong></td><td><strong>Default</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>byte</strong></td><td>1 byte</td><td>-128 to 127</td><td>0</td></tr>
<tr>
<td><strong>short</strong></td><td>2 byte</td><td>-32768 to 32767</td><td>0</td></tr>
<tr>
<td><strong>int</strong></td><td>4 byte</td><td>-2147483648 to 2147483647</td><td>0</td></tr>
<tr>
<td><strong>long</strong></td><td>8 byte</td><td>—</td><td>0</td></tr>
<tr>
<td><strong>float</strong></td><td>4 byte</td><td>±1.4E−45 to ±3.4E+38</td><td>0.0f</td></tr>
<tr>
<td><strong>double</strong></td><td>8 byte</td><td>±4.9E−324 to ±1.7E+308</td><td>0.0d</td></tr>
<tr>
<td><strong>char</strong></td><td>2 byte</td><td>0 to 65535</td><td>\u0000</td></tr>
<tr>
<td><strong>boolean</strong></td><td>depends on JVM (1 bit or 1 byte)</td><td>true / false</td><td>false</td></tr>
</tbody>
</table>
</div><h2 id="heading-what-are-variables">What are Variables ?</h2>
<p>A <strong>variable</strong> is a name given to a memory location that stores a value. In Java, variables are used to store data that can be used and modified during program execution.</p>
<p>Each variable has:</p>
<ol>
<li><p>A <strong>data type</strong> — which defines the kind of value it can store (e.g., integer, float, character, etc.)</p>
</li>
<li><p>A <strong>name</strong> — which identifies the variable in the program</p>
</li>
<li><p>A <strong>value</strong> — which represents the actual data stored</p>
</li>
</ol>
<p>The general syntax for declaring a variable is:</p>
<p><code>dataType variableName = value;</code></p>
<p><code>int i = 175;</code> ⬜ ⬜ ⬜ ⬜</p>
<p><code>float f = 25.3f;</code> ⬜ ⬜ ⬜ ⬜</p>
<p><code>char c = ‘A’;</code> ⬜ ⬜</p>
<p><code>byte b = 5;</code> ⬜</p>
<h2 id="heading-rules-for-variable-names">Rules for Variable Names</h2>
<p>In Java, variable names (also called <em>identifiers</em>) must follow specific rules to ensure that the code is valid and readable. The following are the key rules and best practices for naming variables:</p>
<h4 id="heading-1-case-sensitivity"><strong>1. Case Sensitivity</strong></h4>
<p>Variable names in Java are <strong>case-sensitive</strong>.<br />This means <code>age</code>, <code>Age</code>, and <code>AGE</code> are considered three different variables.</p>
<h4 id="heading-2-allowed-characters"><strong>2. Allowed Characters</strong></h4>
<p>A variable name can contain:</p>
<ul>
<li><p>Alphabets (<code>A–Z</code>, <code>a–z</code>)</p>
</li>
<li><p>Digits (<code>0–9</code>)</p>
</li>
<li><p>The underscore (<code>_</code>)</p>
</li>
<li><p>The dollar sign (<code>$</code>)</p>
</li>
</ul>
<h4 id="heading-3-starting-character"><strong>3. Starting Character</strong></h4>
<p>A variable name <strong>must start</strong> with:</p>
<ul>
<li><p>A letter (A–Z or a–z)</p>
</li>
<li><p>An underscore (<code>_</code>)</p>
</li>
<li><p>Or a dollar sign (<code>$</code>)</p>
</li>
</ul>
<p>It <strong>cannot start with a digit</strong>.</p>
<h4 id="heading-4-not-a-keyword"><strong>4. Not a Keyword</strong></h4>
<p>A variable name <strong>cannot be a Java keyword</strong> (like <code>class</code>, <code>int</code>, <code>if</code>, <code>return</code>, etc.).</p>
<h4 id="heading-5-avoid-class-names-in-use"><strong>5. Avoid Class Names in Use</strong></h4>
<p>If a class with a certain name already exists, avoid using that same name for a variable to prevent confusion and ambiguity.</p>
<h4 id="heading-6-no-length-limit"><strong>6. No Length Limit</strong></h4>
<p>There is <strong>no limit</strong> to the length of a variable name.<br />However, names should be kept <strong>meaningful and concise</strong> for readability.</p>
<h4 id="heading-7-use-camel-case-convention"><strong>7. Use Camel Case Convention</strong></h4>
<p>Java developers commonly follow the <strong>camelCase</strong> naming convention for variables:</p>
<ul>
<li><p>The first word starts with a lowercase letter.</p>
</li>
<li><p>Each subsequent word begins with an uppercase letter.</p>
</li>
</ul>
<h2 id="heading-what-are-literals">What are Literals</h2>
<p>A <strong>literal</strong> is a constant value that is directly written in the code. It represents a fixed value that does not change during program execution. In Java, literals are used to assign values to variables of various data types.</p>
<h3 id="heading-1-integer-literals"><strong>1. Integer Literals</strong></h3>
<p>Integer literals represent whole numbers (without a fractional part). For example:</p>
<p><code>z = 2 x + 17 y;</code></p>
<p>Here, <code>2</code> and <code>17</code> are <strong>integer literals</strong>.<br />Variables of type <code>byte</code>, <code>short</code>, and <code>int</code> can be initialized using integer literals (of type <code>int</code>), as long as the value fits within the range of the target type.</p>
<p>A <strong>long</strong> variable is initialized using a long literal by appending <code>L</code> or <code>l</code> at the end of the number:</p>
<p><code>long distance = 4124L;</code></p>
<h3 id="heading-2-floating-point-literals"><strong>2. Floating-Point Literals</strong></h3>
<p>Floating-point literals represent real numbers that contain a decimal point.<br />A <strong>float</strong> literal is written with an <code>f</code> or <code>F</code> suffix:</p>
<p><code>float rate = 2.34f;</code></p>
<p>A <strong>double</strong> literal can be written either with or without a <code>d</code> or <code>D</code> suffix:</p>
<p><code>double price = 456.41;</code></p>
<p><code>double tax = 2.34D;</code></p>
<h3 id="heading-3-character-literals"><strong>3. Character Literals</strong></h3>
<p>A <strong>character literal</strong> represents a single character enclosed within single quotes:</p>
<p><code>char c = 'A';</code></p>
<h3 id="heading-4-string-literals"><strong>4. String Literals</strong></h3>
<p>A <strong>string literal</strong> represents a sequence of characters enclosed within double quotes:</p>
<p><code>String s = "Java";</code></p>
<h3 id="heading-5-boolean-literals"><strong>5. Boolean Literals</strong></h3>
<p>A <strong>boolean literal</strong> has only two possible values: <code>true</code> or <code>false</code>.</p>
<p><code>boolean isActive = true;</code></p>
<p><code>boolean isComplete = false;</code></p>
<h3 id="heading-6-number-system-literals"><strong>6. Number System Literals</strong></h3>
<p>Java supports several types of number system literals:</p>
<ul>
<li><p><strong>Binary literals</strong> (prefix <code>0b</code> or <code>0B</code>):<br />  <code>int binaryNum = 0b1010;</code> // equivalent to 10 in decimal</p>
</li>
<li><p><strong>Octal literals</strong> (prefix <code>0</code>):<br />  <code>int octalNum = 012;</code> // equivalent to 10 in decimal</p>
</li>
<li><p><strong>Hexadecimal literals</strong> (prefix <code>0x</code> or <code>0X</code>):<br />  <code>int hexNum = 0xA;</code> // equivalent to 10 in decimal</p>
</li>
</ul>
<h2 id="heading-integral-data-type">Integral Data Type</h2>
<p>When Java was first introduced, <strong>32-bit systems</strong> were the standard in computing. Because of this, Java designed the <code>int</code> data type to occupy <strong>4 bytes (32 bits)</strong>.<br />If integers had been stored in only 2 bytes (16 bits), it would have underutilized the processor’s capabilities, leading to slower performance.<br />Hence, <code>int</code> in Java is always <strong>32 bits</strong>, regardless of the underlying machine architecture, ensuring platform independence.</p>
<h3 id="heading-understanding-how-integers-are-stored"><strong>Understanding How Integers Are Stored</strong></h3>
<p>To understand how <strong>integral data types</strong> (like <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>) store numbers, we need to look at <strong>binary representation</strong>.</p>
<p>Let’s take the example of the <code>byte</code> data type.</p>
<ul>
<li><p>A <code>byte</code> is <strong>1 byte = 8 bits</strong>.</p>
</li>
<li><p>It can store values from <strong>–128 to 127</strong>.</p>
</li>
<li><p>The <strong>most significant bit (MSB)</strong> — the <strong>7th bit</strong> (counting from 0 to 7, right to left) — is used as the <strong>sign bit</strong>.</p>
<ul>
<li><p><code>0</code> → positive number</p>
</li>
<li><p><code>1</code> → negative number</p>
</li>
</ul>
</li>
</ul>
<p><strong>Example: Representing 127</strong><br /><code>01111111</code></p>
<ul>
<li><p>The first bit (0) indicates it’s <strong>positive</strong>.</p>
</li>
<li><p>The remaining 7 bits represent the value <code>1111111</code>, which equals <strong>127</strong> in decimal.</p>
</li>
</ul>
<h4 id="heading-example-representing-5"><strong>Example: Representing -5</strong></h4>
<p>To store a negative number like <strong>-5</strong>, Java (and most programming languages) uses a system called <strong>Two’s Complement</strong> representation.</p>
<p>Here’s how it works step by step:</p>
<ol>
<li><p><strong>Find the binary of 5:</strong><br /> <code>00000101</code></p>
</li>
<li><p><strong>Find the One’s Complement:</strong><br /> (Invert all bits: 0 → 1 and 1 → 0)<br /> <code>11111010</code></p>
</li>
<li><p><strong>Find the Two’s Complement:</strong><br /> (Add 1 to the One’s Complement)<br /> <code>11111010 + 1 = 11111011</code></p>
</li>
<li><p><strong>Result:</strong><br /> <code>11111011 → represents -5</code></p>
</li>
</ol>
<h2 id="heading-float-data-type">Float Data Type</h2>
<p>In Java, <strong>floating-point data types</strong> are used to represent numbers that have a fractional part — that is, numbers with <strong>decimal points</strong>. Examples include <code>3.14</code>, <code>0.001</code>, or <code>-98.6</code>.</p>
<p>Java provides two floating-point types:</p>
<ul>
<li><p><strong>float</strong> (4 bytes, single precision)</p>
</li>
<li><p><strong>double</strong> (8 bytes, double precision)</p>
</li>
</ul>
<p>Both types follow the <strong>IEEE 754 standard</strong> for representing floating-point numbers in binary form.</p>
<h3 id="heading-understanding-floating-point-representation"><strong>Understanding Floating-Point Representation</strong></h3>
<p>A floating-point number is represented internally using <strong>scientific notation</strong>.<br />In this format, a number is expressed as:</p>
<p>Number = Mantissa × 10^(Exponent)</p>
<ul>
<li><p>The <strong>mantissa (or significand)</strong> represents the actual digits of the number.</p>
</li>
<li><p>The <strong>exponent</strong> represents the power of 10 (or 2 in binary form) that determines where the decimal point is placed.</p>
</li>
</ul>
<p>This allows very large or very small numbers to be stored efficiently.</p>
<h3 id="heading-example-representing-16352"><strong>Example: Representing 163.52</strong></h3>
<p>Let’s understand how <code>163.52</code> can be expressed in floating-point form.</p>
<p>163.52 = 16352 / 100 = 16352 × 10^−2</p>
<ul>
<li><p><strong>Mantissa:</strong> 16352</p>
</li>
<li><p><strong>Exponent:</strong> -2</p>
</li>
</ul>
<p>This can be written as: <code>16352E-2</code></p>
<p>In Java notation, the letter <code>E</code> (or <code>e</code>) is used to represent “×10 to the power of”.<br />So, <code>16352E-2</code> means: 163.52</p>
<h3 id="heading-how-float-is-stored-internally"><strong>How Float Is Stored Internally</strong></h3>
<p>A <code>float</code> in Java is a <strong>32-bit (4-byte)</strong> value and is divided into three parts as per the <strong>IEEE 754 single-precision</strong> format:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Part</strong></td><td><strong>Bits</strong></td><td><strong>Description</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Sign Bit</strong></td><td>1</td><td>Indicates whether the number is positive (<code>0</code>) or negative (<code>1</code>).</td></tr>
<tr>
<td><strong>Exponent</strong></td><td>8</td><td>Stores the exponent (power of 2), adjusted by a bias of 127.</td></tr>
<tr>
<td><strong>Mantissa (Fraction)</strong></td><td>23</td><td>Stores the fractional part of the number.</td></tr>
</tbody>
</table>
</div><p>So, a 32-bit float looks like this: [ Sign (1 bit) ][ Exponent (8 bits) ][ Mantissa (23 bits) ]</p>
<p>For a deeper look at <strong>how these bits actually represent floating-point numbers in memory</strong> (with binary breakdowns and examples), check out this detailed explanation:</p>
<p>👉 <a target="_blank" href="https://en.wikipedia.org/wiki/IEEE_754">Understanding IEEE 754 Floating-Point Representation (external link)</a></p>
<p>👉 <a target="_blank" href="https://www.log2base2.com/storage/how-float-values-are-stored-in-memory.html">How float or double values are stored in memory? (external link)</a></p>
]]></content:encoded></item><item><title><![CDATA[Introduction to Java]]></title><description><![CDATA[History of Java
1991 → Project started by James Gosling and team at Sun MicrosystemsInitially named "Oak" after a tree near Gosling's officeLater renamed "Green" and finally "Java" after Java coffee
1995 → First public release of Java 1.0Known for "W...]]></description><link>https://blog.visheshraghuvanshi.in/introduction-to-java</link><guid isPermaLink="true">https://blog.visheshraghuvanshi.in/introduction-to-java</guid><category><![CDATA[Java Scanner]]></category><category><![CDATA[Java]]></category><category><![CDATA[technology]]></category><category><![CDATA[java history]]></category><category><![CDATA[java-basics]]></category><category><![CDATA[programming]]></category><category><![CDATA[#javabasics]]></category><category><![CDATA[JDK]]></category><category><![CDATA[jvm]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[Learn Java]]></category><dc:creator><![CDATA[Vishesh Raghuvanshi]]></dc:creator><pubDate>Tue, 28 Oct 2025 09:54:02 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1761644338202/dba5848e-9515-49af-b6bf-c3c49455752e.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-history-of-java">History of Java</h2>
<p><strong>1991 →</strong> Project started by James Gosling and team at Sun Microsystems<br />Initially named <strong><em>"Oak"</em></strong> after a tree near Gosling's office<br />Later renamed <strong><em>"Green"</em></strong> and finally <strong><em>"Java"</em></strong> after <strong>Java coffee</strong></p>
<p><strong>1995 →</strong> First public release of Java 1.0<br />Known for <em>"Write Once, Run Anywhere"</em> (WORA) — <strong>platform independence</strong></p>
<p><strong>1997 →</strong> Java Community Process (JCP) started for collaboration &amp; standardization<br />JDK 1.1 released with major features like <strong>JavaBeans</strong> and <strong>JDBC</strong><br />JVM standardized to ensure platform-independent execution</p>
<p><strong>1998 →</strong> Release of Java 2 (J2SE 1.2), a major milestone<br />Introduced different editions: J2SE (Standard), J2EE (Enterprise), J2ME (Mobile)</p>
<p><strong>2006 onwards →</strong> Naming changed to Java SE (Standard Edition), Java EE (Enterprise), Java ME (Mobile)</p>
<h2 id="heading-jdk-jvm-and-jre">JDK, JVM, and JRE</h2>
<p><img src="https://miro.medium.com/v2/resize:fit:700/1*lLBwoFv6RAZ15vRuK6yFQg.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p><strong>JVM (Java Virtual Machine):</strong> The core runtime component that interprets compiled Java bytecode and translates it into machine-specific instructions. It manages memory, security, garbage collection, and provides a platform-independent execution environment.</p>
</li>
<li><p><strong>JRE (Java Runtime Environment):</strong> Includes the JVM along with standard libraries and classes required to run Java applications. It does <em>not</em> include development tools like a compiler or debugger. JRE is for executing Java programs only.</p>
</li>
<li><p><strong>JDK (Java Development Kit):</strong> This package includes the JRE plus development tools such as the Java compiler (<code>javac</code>), debugger, and other utilities required for writing, compiling, and debugging Java applications.</p>
</li>
</ul>
<h2 id="heading-how-a-java-program-is-run">How a Java Program is Run</h2>
<p>Running a Java program involves several steps:</p>
<ol>
<li><p><strong>Writing the Source Code:</strong> Create a <code>.java</code> file with Java source code.</p>
</li>
<li><p><strong>Compilation:</strong> The <code>javac</code> compiler converts the source code into bytecode, a platform-independent intermediate format, stored in <code>.class</code> files.</p>
</li>
<li><p><strong>Loading:</strong> The JVM loads the bytecode into memory.</p>
</li>
<li><p><strong>Verification:</strong> JVM verifies the bytecode to ensure it is valid and secure.</p>
</li>
<li><p><strong>Execution:</strong> The JVM interprets or just-in-time compiles the bytecode into native machine code and runs the program.</p>
</li>
</ol>
<h2 id="heading-skeleton-of-a-java-program">Skeleton of a Java Program</h2>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.lang.*; <span class="hljs-comment">// It will automatically be imported even if we don't import it</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HelloWorld</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        System.out.println(<span class="hljs-string">"Hello, World!"</span>);
    }
}
</code></pre>
<ul>
<li><p><code>public</code>: Access modifier, means the class/method is accessible from anywhere.</p>
</li>
<li><p><code>class</code>: Defines a class named <code>HelloWorld</code>.</p>
</li>
<li><p><code>static</code>: Means the method belongs to the class, not a specific instance. To use main method without creating an object of the class we use static here.</p>
</li>
<li><p><code>void</code>: The method returns no value.</p>
</li>
<li><p><code>main</code>: Entry point of any standalone Java application.</p>
</li>
<li><p><code>String[] args</code>: An array of command-line arguments.</p>
</li>
<li><p><code>System.out.println</code>: Prints text to the console.</p>
<ul>
<li><p><code>System</code> is a <strong>built-in class</strong> in the Java standard library (<code>java.lang</code> package).</p>
</li>
<li><p><code>out</code> is a <strong>static variable</strong> inside the <code>System</code> class which <strong>points to</strong> an <strong>object</strong> of type <code>PrintStream</code>.</p>
</li>
<li><p><code>println</code> is a <strong>method</strong> that belongs to the <code>PrintStream</code> class</p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-reading-from-keyboard">Reading from Keyboard</h2>
<p>The <code>Scanner</code> class in <code>java.util</code> package is used to read user inputs from various sources, including the keyboard.</p>
<h3 id="heading-example">Example</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.util.Scanner;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InputExample</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Scanner input = <span class="hljs-keyword">new</span> Scanner(System.in); <span class="hljs-comment">// Create Scanner object</span>
        System.out.print(<span class="hljs-string">"Enter your name: "</span>);
        String name = input.nextLine(); <span class="hljs-comment">// Reads a line of text</span>
        System.out.println(<span class="hljs-string">"Hello, "</span> + name);

        input.close(); <span class="hljs-comment">// Close the scanner</span>
    }
}
</code></pre>
<p><code>Scanner input = new Scanner(</code><a target="_blank" href="http://System.in"><code>System.in</code></a><code>);</code> creates a Scanner object to read input from the keyboard</p>
<p><strong>Methods:</strong></p>
<ul>
<li><p><code>next()</code>: Reads and returns the next single token (word) as a String.</p>
</li>
<li><p><code>nextLine()</code>: Reads and returns an entire line of input as a String.</p>
</li>
<li><p><code>nextInt()</code>: Reads the next token as an <code>int</code> value.</p>
</li>
<li><p><code>nextDouble()</code>: Reads the next token as a <code>double</code> value.</p>
</li>
<li><p><code>nextFloat()</code>: Reads the next token as a <code>float</code> value.</p>
</li>
<li><p><code>nextLong()</code>: Reads the next token as a <code>long</code> value.</p>
</li>
<li><p><code>nextShort()</code>: Reads the next token as a <code>short</code> value.</p>
</li>
<li><p><code>nextByte()</code>: Reads the next token as a <code>byte</code> value.</p>
</li>
<li><p><code>nextBoolean()</code>: Reads the next token as a <code>boolean</code> value.</p>
</li>
<li><p><code>hasNext()</code>: Checks if there is another token available.</p>
</li>
<li><p><code>hasNextLine()</code>: Checks if there is another line available.</p>
</li>
<li><p>Similarly <code>hasNextInt()</code>, <code>hasNextFloat()</code>, etc.</p>
</li>
<li><p><code>useRadix(int radix)</code>:Sets the default number base (radix) the scanner uses to interpret numeric input (e.g., decimal, hexadecimal). If radix = 2 then the input will be taken in Binary.</p>
</li>
<li><p><code>close()</code>: Closes the Scanner object, freeing resources.</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>