<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>performance &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Thu, 17 Jun 2021 11:23:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to initialize an array with zero values in Java</title>
		<link>https://codepills.com/how-to-initialize-an-array-with-zero-values-in-java/</link>
					<comments>https://codepills.com/how-to-initialize-an-array-with-zero-values-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Mon, 01 Mar 2021 19:56:37 +0000</pubDate>
				<category><![CDATA[Language basics]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[primitive type]]></category>
		<category><![CDATA[reference type]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1136</guid>

					<description><![CDATA[This article will look at how to fill up Java array with default values effectively on an array creation. <a href="https://codepills.com/how-to-initialize-an-array-with-zero-values-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>This article will look at how to fill up a Java array with default values effectively on an array creation.</p>
<p><span id="more-1136"></span></p>
<h2>Overview</h2>
<p>Arrays are essential cornerstones of every modern programming language. On a specific occasion, you need to initialize an array with zero values. This article will take you through the pros and cons of the various way how to initialize Java array with default zero values.</p>
<h2>Introduction to Java data types</h2>
<p>In Java we have several different data types. Majority of data types are called primitive data types. To this group belong <strong>byte</strong>, <strong>short</strong>, <strong>int</strong>, <strong>long</strong>, <strong>float</strong>, <strong>double</strong>, <strong>boolean</strong> and <strong>char</strong>.</p>
<p>For more information about Java&#8217;s data type, please read our article about primitive data types.</p>
<p>Speaking about Java&#8217;s data type, there is also a general notation of different data type represented by referencing the object&#8217;s type. We are talking about object references, where the type of the object is classified by the object&#8217;s referencing type. Sound confusing? If you are new to Java, let me point you out to one of the most asked Java questions Is Java pass by value or pass by reference?</p>
<h2>Java data type default values</h2>
<p>Let&#8217;s take a look what is initial (default) value in <strong>Java language</strong> for any variable initialization.</p>
<ul>
<li>The default value for type <strong>byte</strong> is zero. That means in byte the value of (byte) is <strong>0</strong>.</li>
<li>The default value for type <strong>short</strong> is zero. That means in the short the value of (short) is <strong>0</strong>.</li>
<li>The default value for type <strong>int</strong> is zero. That means in int the value of (int) is <strong>0</strong>.</li>
<li>The default value for type <strong>long</strong> is zero. That means in long the value of (long) is <strong>0L</strong>.</li>
<li>The default value for type <strong>float</strong> is positive zero. That means in float the value of (float) is <strong>0.0f</strong>.</li>
<li>The default value for type <strong>double</strong> is positive zero. That means in double the value of (double) is <strong>0.0d</strong>.</li>
<li>The default value for type <strong>boolean</strong> is <strong>false</strong>.</li>
<li>The default value for type <strong>char</strong> is the null character. That means in char the value of (char) is <strong>&#8216;\u0000&#8217;</strong>.</li>
<li>The default value for <strong>all reference</strong> types (Sting or any Object parent or its child) is <strong>null</strong>.</li>
</ul>
<p>If you have noticed, primitive data types are initialized in their form of number zero. The exception is boolean, where the default state is false. However, this representation of the default state comes from generation-lasting philosophical discussion beyond this blog&#8217;s scope.</p>
<h2>Default data type for String and arrays</h2>
<p>It is important to remember that in Java, an array is a container object that holds a fixed number of values of a single type.</p>
<p>The <i>String</i> object is unique in a certain way. It is because it is wrapping object around an array collection of primitive char data types. However, we always reference the <i>String</i> object, not <i>String</i>&#8216;s internal array.</p>
<p><strong>Array of primitive types</strong> keeps initialization value equal default value for each element of primitive data type.</p>
<pre><code class="language-java">double[] treePrimitiveDoubles = new double[3];

double zeroElementPrimitive = treePrimitiveDoubles[0];
System.out.print(zeroElementPrimitive);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">>> 0.0</code></pre>
<p><strong>Array of reference type</strong> keeps initialization value equal null. The <i>String</i> is the same; it keeps the initialization value equal null.</p>
<pre><code class="language-java">Double[] treeReferenceDoubles = new Double[3];

Double zeroElementReference = treeReferenceDoubles[0];
System.out.print(zeroElementReference);</code></pre>
<p><b>Output</b></p>
<pre><code class="language-bash">>> null</code></pre>
<p>We instantly see that we need to create the objects to fill the arrays of the reference type array.</p>
<h2>Filling default values to reference type arrays</h2>
<p>There are two different approaches for reference type arrays on how to fill up an array by a particular type. First is the classic iterative approach, where you loop through the collection and fill the position individually.</p>
<pre><code class="language-java">BankAccount[] arrayOfAllBankAccounts = new BankAccount[50];

for(int i = 0; i < arrayOfAllBankAccounts.length; i++) {
    arrayOfAllBankAccounts[i] = new BankAccount();
}</code></pre>
<p>The second option is to use a more procedural approach with the help of <code>java.util.Arrays</code>. If you want to initialize a one-dimensional array with default values, you can use the static <code>.fill()</code> method. Although, internally <code>.fill()</code> method uses a loop. However, code notation is much more concise. Let's look at the code notation:</p>
<pre><code class="language-java">BankAccount[] arrayOfAllBankAccounts = new BankAccount[50];

Arrays.fill(arrayOfAllBankAccounts, new BankAccount());</code></pre>
<h2>Performance</h2>
<p>Consider the array of arbitrary length which will store reference type for Object counterparts of primitive types such as <strong>Byte</strong>, <strong>Short</strong>, <strong>Integer</strong>, <strong>Long</strong>, <strong>Float</strong>, <strong>Double</strong>, <strong>Boolean</strong> and <strong>Char</strong> class. And imagine now that you would do this for many many arrays like this in your program. This would dramatically decrease performance of your application; you would waste a lot of machine cycles just for initialization. It would affect your application performance by considerable level.</p>
<p>Each array consisted of an Object version of primitive types that would not only take longer to initialize than a regular primitive type array, but it would even require allocating more memory than necessary. Consequently, if you want to use <i>Object</i> version of a primitive type, use them only when essential for your code solution.</p>
<p>Initialization of arrays with primitive types is quick and fast, does not allocate a lot of memory. You can prefer them over their reference type versions.</p>
<h3>String performance</h3>
<p>Java's <i>String</i> class has a special place in our hearts. As we said, <i>String</i> class uses an array of primitive characters for storing text. On each addition or change text, <i>String</i> needs to compute if the allocated array length is sufficient. If it is not, it needs to allocate a new - more significant size for the internal array of primitive characters and copy the existing text to a new array of primitive characters. Resizing mechanism naturally decrease performance. There are two ways how to mitigate this issue. First, it is highly recommended to not use concatenation on <i>String</i>s in your code. And second, if you know you will be using a lot of text manipulation, use the <i>StringBuilder</i> class.</p>
<h2>Conclusion</h2>
<p>In this article, we have seen how to initialize the array in Java with zero values. As always, you can find all codes in this article in out <a href="https://github.com/codekopf/tutorials-jvm/tree/master/java-basics/java-core" title="Tutorial JVM - Java Core" target="_blank" rel="nofollow noopener">GitHub repository</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-initialize-an-array-with-zero-values-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
