<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: M.Kabileshwaran</title>
    <description>The latest articles on Forem by M.Kabileshwaran (@kabileshwarankabil).</description>
    <link>https://forem.com/kabileshwarankabil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3206150%2F3bb89597-dd52-4b1d-9078-1fb7119a2281.png</url>
      <title>Forem: M.Kabileshwaran</title>
      <link>https://forem.com/kabileshwarankabil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kabileshwarankabil"/>
    <language>en</language>
    <item>
      <title>Day 1 of DSA: Arrays Fundamentals</title>
      <dc:creator>M.Kabileshwaran</dc:creator>
      <pubDate>Thu, 01 Jan 2026 02:07:35 +0000</pubDate>
      <link>https://forem.com/kabileshwarankabil/day-1-of-dsa-arrays-fundamentals-1l4j</link>
      <guid>https://forem.com/kabileshwarankabil/day-1-of-dsa-arrays-fundamentals-1l4j</guid>
      <description>&lt;h2&gt;
  
  
  Day 1: Arrays
&lt;/h2&gt;

&lt;p&gt;I chose &lt;strong&gt;arrays&lt;/strong&gt; as the starting point for my DSA journey. Although I am not a complete beginner—I have learned Java and basic DSA earlier—I felt arrays are a good concept to revisit and strengthen before moving to more advanced topics.&lt;/p&gt;

&lt;p&gt;However, I would not recommend absolute beginners to start directly with arrays. Beginners should first be comfortable with &lt;strong&gt;basic programming concepts such as variables, data types, and loops&lt;/strong&gt;. Once those fundamentals are clear, arrays become much easier to understand.&lt;/p&gt;

&lt;p&gt;Before learning DSA, it is important to choose a programming language. I have chosen &lt;strong&gt;Java&lt;/strong&gt; as my primary language, and in this blog, I will cover DSA concepts &lt;strong&gt;specifically from a Java perspective&lt;/strong&gt;, with brief comparisons to Python where necessary.&lt;/p&gt;

&lt;p&gt;A quick thanks to the instructor of this &lt;a href="https://youtu.be/n60Dn0UsbEk?si=V51EQKaedhnBZhJy" rel="noopener noreferrer"&gt;YouTube tutorial&lt;/a&gt;—it helped me understand the concepts better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Topics Covered in This Blog
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Why do we need Arrays?&lt;/li&gt;
&lt;li&gt;What is an Array?&lt;/li&gt;
&lt;li&gt;Arrays in Java and Python&lt;/li&gt;
&lt;li&gt;Syntax of an Array in Java&lt;/li&gt;
&lt;li&gt;Rules of Arrays in Java&lt;/li&gt;
&lt;li&gt;Declaration and Initialization&lt;/li&gt;
&lt;li&gt;Example of Creating an Array&lt;/li&gt;
&lt;li&gt;Indexing in Arrays&lt;/li&gt;
&lt;li&gt;Length of an Array&lt;/li&gt;
&lt;li&gt;Default Values in Arrays&lt;/li&gt;
&lt;li&gt;String Arrays and Memory&lt;/li&gt;
&lt;li&gt;Primitive Types vs Objects&lt;/li&gt;
&lt;li&gt;Mutability of Arrays&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why Do We Need Arrays?
&lt;/h2&gt;

&lt;p&gt;Arrays are used to store a &lt;strong&gt;collection of related values under a single name&lt;/strong&gt;, which is usually called a &lt;strong&gt;reference variable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, if we want to store the names of our friends, instead of creating multiple variables for each name, we can store all the names in a single array called &lt;code&gt;friendsNames&lt;/code&gt;. This makes the code more organized, scalable, and easier to work with.&lt;/p&gt;

&lt;p&gt;When I picture an array, I imagine a row of empty boxes on a shelf. Each box has a fixed position number, and I can drop one related item per box. That mental model makes it easier to write loops later because I know each index follows a predictable pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is an Array?
&lt;/h2&gt;

&lt;p&gt;A commonly used definition of an array is:&lt;/p&gt;

&lt;p&gt;An array is a collection of elements of the same data type stored in contiguous memory locations.&lt;/p&gt;

&lt;p&gt;This definition is &lt;strong&gt;completely accurate for low-level languages such as C and C++&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, the internal implementation of arrays can differ across programming languages.&lt;/p&gt;

&lt;p&gt;"Contiguous" simply means the slots are side by side in memory. The benefit is fast random access: jumping to index &lt;code&gt;i&lt;/code&gt; is quick because the program can calculate the exact memory address. The trade-off is that resizing usually means creating a brand new array and copying items over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arrays in Java
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In Java, an array can store &lt;strong&gt;only one data type&lt;/strong&gt;, which makes arrays &lt;strong&gt;homogeneous&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Arrays in Java are &lt;strong&gt;objects&lt;/strong&gt;, and they are created in the &lt;strong&gt;heap memory&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;primitive data types&lt;/strong&gt; (&lt;code&gt;int&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt;, etc.), the values are stored &lt;strong&gt;contiguously within the array object&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;reference types&lt;/strong&gt; (&lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Object&lt;/code&gt;, etc.), the array stores &lt;strong&gt;contiguous references&lt;/strong&gt;, while the actual objects may be located at different places in the heap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I like to remember that &lt;code&gt;String[] names&lt;/code&gt; really holds references. Changing &lt;code&gt;names[0] = "Bob"&lt;/code&gt; only swaps which &lt;code&gt;String&lt;/code&gt; object that slot points to; it does not move or copy the underlying &lt;code&gt;String&lt;/code&gt; objects elsewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arrays in Python
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python does not provide traditional arrays by default.&lt;/li&gt;
&lt;li&gt;What is commonly used in Python is a &lt;strong&gt;list&lt;/strong&gt;, which can store &lt;strong&gt;different data types&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A Python list stores &lt;strong&gt;references to objects&lt;/strong&gt;, not the raw values themselves.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because Python lists are dynamic, appending feels effortless. The runtime quietly reallocates and copies when needed. That is friendly for beginners but also hides the cost of growing a list compared to a fixed-size Java array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax of an Array in Java
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;datatype&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arrayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;datatype&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;datatype&lt;/code&gt; → specifies the type of elements the array can store&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[]&lt;/code&gt; → indicates that the variable is of array type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;arrayName&lt;/code&gt; → reference variable that points to the array object&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; → used to create an array object in the heap&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;datatype[size]&lt;/code&gt; → specifies the data type and the number of elements the array can hold&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When I first wrote this, the position of &lt;code&gt;[]&lt;/code&gt; confused me. &lt;code&gt;datatype[] arrayName&lt;/code&gt; and &lt;code&gt;datatype arrayName[]&lt;/code&gt; both compile in Java, but I stick to the first style because it reads as "&lt;code&gt;arrayName&lt;/code&gt; is an array of &lt;code&gt;datatype&lt;/code&gt;." The size is fixed at creation time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules of Arrays in Java
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The reference variable name must follow the same naming conventions as normal variables.&lt;/li&gt;
&lt;li&gt;All elements in an array must be of the same data type.&lt;/li&gt;
&lt;li&gt;The size of an array is &lt;strong&gt;fixed&lt;/strong&gt; once it is created and cannot be changed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If I think I will need a changing size, I start with &lt;code&gt;ArrayList&lt;/code&gt; instead. Sticking to plain arrays is great practice for learning indexing and for interview-style questions where fixed-size storage is expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declaration and Initialization
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;datatype&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// declaration&lt;/span&gt;
&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;datatype&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// initialization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;During declaration, a &lt;strong&gt;reference variable&lt;/strong&gt; is created.&lt;/li&gt;
&lt;li&gt;During initialization, the &lt;strong&gt;array object&lt;/strong&gt; is created in the heap.&lt;/li&gt;
&lt;li&gt;Declaration is checked at &lt;strong&gt;compile time&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Memory allocation for the array happens at &lt;strong&gt;runtime&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An easy mistake: calling &lt;code&gt;arr[0]&lt;/code&gt; before &lt;code&gt;arr&lt;/code&gt; is initialized triggers a &lt;code&gt;NullPointerException&lt;/code&gt; because the reference points to nothing. I keep reminding myself that declaration alone does not create the boxes—initialization does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example of Creating an Array
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates and initializes an integer array containing six elements.&lt;/p&gt;

&lt;p&gt;Whenever I declare with curly braces, I read the values out loud—"one, two, three, four, five, six"—to check I did not skip a number. Printing &lt;code&gt;nums.length&lt;/code&gt; also confirms the size matches what I expect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Indexing in Arrays
&lt;/h2&gt;

&lt;p&gt;Indexing is used to access elements of an array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array indexing in Java starts from &lt;strong&gt;0&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The first element is stored at index &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;num[0]&lt;/code&gt; → &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;num[1]&lt;/code&gt; → &lt;code&gt;7&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;num[2]&lt;/code&gt; → &lt;code&gt;8&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Length of the array = &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Last index = &lt;code&gt;length - 1 = 2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many beginners confuse the &lt;strong&gt;length of an array&lt;/strong&gt; with the &lt;strong&gt;last index&lt;/strong&gt;, but they are not the same.&lt;/p&gt;

&lt;p&gt;If we try to access an index greater than the last index, Java throws an exception called:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayIndexOutOfBoundsException&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I still pause before writing loop bounds: &lt;code&gt;for (int i = 0; i &amp;lt; num.length; i++)&lt;/code&gt; is safer than &lt;code&gt;&amp;lt;=&lt;/code&gt; because it naturally stops at the last valid index. Drawing three boxes labeled 0, 1, 2 helps me visualize where the loop should end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Length of an Array
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;length&lt;/code&gt; gives the &lt;strong&gt;number of elements&lt;/strong&gt; present in the array.&lt;/li&gt;
&lt;li&gt;It is a &lt;strong&gt;property&lt;/strong&gt;, not a method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To find the last index of an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lastIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;length&lt;/code&gt; is a field, not a method, so there are no parentheses. Using &lt;code&gt;num.length()&lt;/code&gt; by accident will not compile, which is a helpful reminder when switching between arrays and &lt;code&gt;ArrayList&lt;/code&gt; (which uses &lt;code&gt;size()&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Default Values in Arrays
&lt;/h2&gt;

&lt;p&gt;If an array is created without explicitly assigning values, Java automatically assigns &lt;strong&gt;default values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;int&lt;/code&gt; is a primitive data type, each element is initialized to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For reference types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each element is initialized to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This demonstrates the difference in behavior between &lt;strong&gt;primitive types&lt;/strong&gt; and &lt;strong&gt;reference types&lt;/strong&gt; in arrays.&lt;/p&gt;

&lt;p&gt;Defaults are great for quick experiments, but they can also hide missing data. When I see &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt; in logs, I double-check if that value is intentional or just the default placeholder I forgot to overwrite.&lt;/p&gt;

&lt;h2&gt;
  
  
  String Arrays and Memory
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;languages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Python"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"C++"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The array stores &lt;strong&gt;references&lt;/strong&gt; to &lt;code&gt;String&lt;/code&gt; objects.&lt;/li&gt;
&lt;li&gt;Each &lt;code&gt;String&lt;/code&gt; object is stored separately in the heap.&lt;/li&gt;
&lt;li&gt;The array itself is an object and is also stored in the heap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If I reassign &lt;code&gt;languages[1] = "Go"&lt;/code&gt;, only that slot changes. The original &lt;code&gt;"Python"&lt;/code&gt; string still exists until garbage collection cleans it up, which is why arrays of references feel lightweight to modify.&lt;/p&gt;

&lt;h2&gt;
  
  
  Primitive Types vs Objects
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Primitive values can be stored in the &lt;strong&gt;stack&lt;/strong&gt; (for local variables) or inside objects in the &lt;strong&gt;heap&lt;/strong&gt;, depending on where they are declared.&lt;/li&gt;
&lt;li&gt;Objects and arrays are &lt;strong&gt;always stored in the heap&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Reference variables that point to objects are stored in the &lt;strong&gt;stack&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thinking in two layers helps me: the reference lives on the stack (quick to access), while the actual array object lives on the heap (shared and resizable only by creating new objects). When I pass an array into a method, I am really passing the reference, so changes inside the method affect the original array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mutability of Arrays
&lt;/h2&gt;

&lt;p&gt;Arrays in Java are &lt;strong&gt;mutable&lt;/strong&gt;, meaning their elements can be changed after creation.&lt;/p&gt;

&lt;p&gt;However, the &lt;strong&gt;size of an array cannot be modified&lt;/strong&gt; once it is created.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// updates the second element&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;scores&lt;/code&gt; still has three elements, but the middle value is now &lt;code&gt;25&lt;/code&gt;. If I need a fourth score, I must create a new array or use a dynamic structure like &lt;code&gt;ArrayList&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Day 1 Takeaway
&lt;/h3&gt;

&lt;p&gt;Arrays form the foundation for many data structures in DSA. A clear understanding of how arrays work—especially indexing, memory behavior, and limitations—is essential before moving on to more advanced topics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Free Resources I Found Helpful
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Oracle Java Tutorials on arrays: &lt;a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html" rel="noopener noreferrer"&gt;https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GeeksforGeeks introduction to arrays: &lt;a href="https://www.geeksforgeeks.org/introduction-to-arrays/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/introduction-to-arrays/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;W3Schools Java arrays overview: &lt;a href="https://www.w3schools.com/java/java_arrays.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/java/java_arrays.asp&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;FreeCodeCamp guide on array basics: &lt;a href="https://www.freecodecamp.org/news/arrays-explained/" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/arrays-explained/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you made it here, thanks for learning with me. I am still treating this like a lab notebook: simple drawings, small code snippets, and honest mistakes I catch along the way. If you spot gaps or have a favorite beginner-friendly resource, let me know so I can keep improving this journal.&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>OOP in Java: A First-Year Student’s Cheat Sheet</title>
      <dc:creator>M.Kabileshwaran</dc:creator>
      <pubDate>Wed, 02 Jul 2025 10:50:42 +0000</pubDate>
      <link>https://forem.com/kabileshwarankabil/oop-in-java-a-first-year-students-cheat-sheet-3hao</link>
      <guid>https://forem.com/kabileshwarankabil/oop-in-java-a-first-year-students-cheat-sheet-3hao</guid>
      <description>&lt;p&gt;Think of OOP like building with blueprints and real-world creations.&lt;br&gt;
&lt;strong&gt;1. Classes vs. Objects – The Simple Difference&lt;/strong&gt;&lt;br&gt;
A class is like a recipe for a cake. It lists the ingredients (data) and steps (actions). An object is the actual cake you bake using that recipe. You can make many cakes from one recipe, just like you can create many objects from one class.&lt;br&gt;
Example:&lt;br&gt;
Class (Recipe): "How to make a car" (has wheels, color, speed).&lt;br&gt;
Object (Real Car): Your red Toyota (with 4 wheels, going 60 mph).&lt;br&gt;
In Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class = blueprint or template&lt;/li&gt;
&lt;li&gt;Object = an instance of that blueprint, a real thing created based on the class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
Think of a Car class — it describes what a car is (color, model, year) and what it can do (drive, brake). When you create a Car object, say myCar, you’re making a specific car, like a red 2020 Toyota.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;drive&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" car is driving."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Creating an object&lt;/span&gt;
        &lt;span class="n"&gt;myCar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Red"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;myCar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;myCar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;drive&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: The Red car is driving.&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. What Are Constructors? Do We Have Destructors in Java Like C++?&lt;/strong&gt;&lt;br&gt;
Constructor: &lt;br&gt;
Think of it as a special method that builds your object. When you create an object, the constructor sets up initial values — like choosing the color and year of your car right when you build it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Constructor&lt;/span&gt;
    &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Destructors?&lt;br&gt;
In C++, destructors clean up when an object is destroyed (like cleaning your room after playing). In Java, there are no destructors because Java has a garbage collector that automatically cleans up unused objects. So, you don’t have to worry about manually deleting objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What Is static?&lt;/strong&gt;&lt;br&gt;
static is like a shared property or method that belongs to the class itself, not to any specific object.&lt;br&gt;
Example: Imagine a school where every student has a name (instance variable), but the school’s name is the same for all students (static variable).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// each student has a name&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;school&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ABC High School"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// shared by all students&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can access static stuff without creating an object:&lt;br&gt;
System.out.println(Student.school);  // Output: ABC High School&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. What Are Access Modifiers?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access modifiers control who can see or use your class members (variables, methods). Think of them as privacy levels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;public: Everyone can see and use it — like a public park.&lt;/li&gt;
&lt;li&gt;private: Only the class itself can see it — like your diary.&lt;/li&gt;
&lt;li&gt;protected: The class and its subclasses (children) can see it — like family secrets.&lt;/li&gt;
&lt;li&gt;default (no modifier): Only classes in the same package can see it — like your neighborhood friends.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Example Scenarios &amp;amp; Scope of Access Modifiers&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmxs5m34eije4n3rum8a5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmxs5m34eije4n3rum8a5.png" alt=" " width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. The 4 Pillars of OOP and How to Implement Them in Java&lt;/strong&gt;&lt;br&gt;
Encapsulation — Wrapping data and methods together, hiding details. Use private variables + public getters/setters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inheritance — A class inherits properties/methods from another (like a child inheriting traits).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Eating"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;bark&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Barking"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Polymorphism — One interface, many forms. Methods can behave differently in subclasses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some sound"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bark"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Abstraction — Hiding complex details, showing only what’s necessary. Use abstract classes or interfaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bike&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bike is moving"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. How Are Objects Instantiated in Memory?&lt;/strong&gt;&lt;br&gt;
When you create an object with new, Java allocates memory for it on the heap (the big memory area for objects). The reference (like a pointer) to that object lives on the stack.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Car myCar = new Car("Blue", 2021);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;myCar is a reference on the stack.&lt;/li&gt;
&lt;li&gt;The actual Car object is stored on the heap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;8. Can We Pass Objects as Arguments to Other Objects? How?&lt;/strong&gt;&lt;br&gt;
Objects can be passed as parameters to methods or constructors, enabling interaction between objects.&lt;br&gt;
Example: A Driver object can have a Car object passed to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Driver&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;drive&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Driving a "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tesla"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Driver&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Driver&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;drive&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myCar&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Driving a Tesla&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>java</category>
      <category>object</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
