<?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>DEV Community: Camilo</title>
    <description>The latest articles on DEV Community by Camilo (@camilozuluaga).</description>
    <link>https://dev.to/camilozuluaga</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%2F2995031%2F8adf58e4-3bd6-471b-8046-fa74f32a5bf6.jpeg</url>
      <title>DEV Community: Camilo</title>
      <link>https://dev.to/camilozuluaga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/camilozuluaga"/>
    <language>en</language>
    <item>
      <title>Reference Types vs Primitive Types - Java</title>
      <dc:creator>Camilo</dc:creator>
      <pubDate>Thu, 19 Jun 2025 00:58:18 +0000</pubDate>
      <link>https://dev.to/camilozuluaga/reference-types-vs-primitive-types-java-km</link>
      <guid>https://dev.to/camilozuluaga/reference-types-vs-primitive-types-java-km</guid>
      <description>&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Let's say you create an array in Java:&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;array1&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have our variable &lt;code&gt;array1&lt;/code&gt; that holds our &lt;a href="https://www.geeksforgeeks.org/anonymous-array-java/" rel="noopener noreferrer"&gt;anonymous array&lt;/a&gt;. You decide to create a second variable called &lt;code&gt;array2&lt;/code&gt;, and you set it equal to &lt;code&gt;array1&lt;/code&gt;.&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;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we print our two arrays, we expect to get the same content, and that is what actually happens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; [1, 2, 3, 4, 5]
&amp;gt;&amp;gt;&amp;gt; [1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, what if you want to change a number in &lt;code&gt;array1&lt;/code&gt;?, let's change our second index (&lt;em&gt;number 3&lt;/em&gt;) to 9.&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="n"&gt;array1&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="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, let's print our two arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; [1, 2, 9, 4, 5]
&amp;gt;&amp;gt;&amp;gt; [1, 2, 9, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow, why our second array has the number 9 as well?&lt;/p&gt;

&lt;p&gt;This is the time when we have to look at the &lt;a href="https://www.geeksforgeeks.org/dsa/stack-vs-heap-memory-allocation/" rel="noopener noreferrer"&gt;memory allocation&lt;/a&gt;, more specifically, the Stack and the Heap.&lt;/p&gt;

&lt;p&gt;In java, data is classified in &lt;strong&gt;Reference and Primitive types&lt;/strong&gt;, let's first look at how primitive types are saved in memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Primitive Types
&lt;/h3&gt;

&lt;p&gt;Java supports eight primitive data types, which you can see &lt;a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;, i'll be using &lt;code&gt;int&lt;/code&gt; for the demo. &lt;br&gt;
So imagine, we have the following code:&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&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="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since these are primitive data types, both the variable and the value are saved on the stack, so when you declared &lt;code&gt;y&lt;/code&gt; to be equal to &lt;code&gt;x&lt;/code&gt;, what is going to happen is that the value from &lt;code&gt;x&lt;/code&gt; will get copied, and it will be saved in &lt;code&gt;y&lt;/code&gt;, in this case each variable has its own value now. If you wanted to change the value of &lt;code&gt;y&lt;/code&gt;, you would only be modifying that value for that 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="n"&gt;y&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below, you have the visual representation:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/E171PvqH9ns"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Reference Types
&lt;/h3&gt;

&lt;p&gt;Reference types hold references to objects and provide access to those objects stored somewhere in memory allocated on the heap, there are four kinds of &lt;a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html" rel="noopener noreferrer"&gt;reference types&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's go back to our example and explain 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="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array1&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="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, what's happening here?, as mentioned before, reference types hold &lt;strong&gt;references&lt;/strong&gt; to the object, in this case, an array. The variable &lt;code&gt;array1&lt;/code&gt; is holding a reference in the stack, the variable is saved but this time, with the reference (&lt;em&gt;imagine the reference as an arrow pointing to the object&lt;/em&gt;). As it happened before with primitive types, it copied the value from the variable to another, in this case the value we are copying is a reference, so now both &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; are pointing to the same object on the heap, that's why any change to one of the variables will reflect on the other.&lt;br&gt;
Let's create an array just for &lt;code&gt;array2&lt;/code&gt;:&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="n"&gt;array2&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;span class="c1"&gt;// Array of size 5&lt;/span&gt;
&lt;span class="n"&gt;array2&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;9&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Updating the value of index 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;code&gt;array2&lt;/code&gt; is pointing to a totally different object, and it's no longer pointing to the same object as &lt;code&gt;array1&lt;/code&gt;, when we now update a value of &lt;code&gt;array2&lt;/code&gt;, it won't affect &lt;code&gt;array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Below, you have the visual representation:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/xqSC3JHS8Wg"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And that's all! Hope you enjoyed this little post.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
