<?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: Marta Kravchuk</title>
    <description>The latest articles on DEV Community by Marta Kravchuk (@kmartita).</description>
    <link>https://dev.to/kmartita</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%2F3127390%2Fbcf12135-dfbc-418f-a70a-18c6754acb7f.JPG</url>
      <title>DEV Community: Marta Kravchuk</title>
      <link>https://dev.to/kmartita</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kmartita"/>
    <language>en</language>
    <item>
      <title>Arrays in Java: A Deep Dive</title>
      <dc:creator>Marta Kravchuk</dc:creator>
      <pubDate>Mon, 09 Jun 2025 19:24:09 +0000</pubDate>
      <link>https://dev.to/kmartita/arrays-in-java-a-deep-dive-4jg</link>
      <guid>https://dev.to/kmartita/arrays-in-java-a-deep-dive-4jg</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;Arrays are a fundamental data structure in Java, used to store collections of elements of the same type. This article provides a comprehensive guide to working with arrays, covering everything from basic declaration and initialization to multidimensional arrays, searching, sorting, comparing and copying. In addition, the material provides essential insights whether you’re preparing for an interview or looking to brush up on your knowledge.&lt;/em&gt;
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Array Declarations&lt;/li&gt;
&lt;li&gt;Creating an Array&lt;/li&gt;
&lt;li&gt;
Working with Arrays

&lt;ul&gt;
&lt;li&gt;Sorting Arrays&lt;/li&gt;
&lt;li&gt;Searching Arrays&lt;/li&gt;
&lt;li&gt;Comparing Arrays&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Shallow Copy vs. Deep Copy&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Java, an &lt;strong&gt;&lt;em&gt;array&lt;/em&gt;&lt;/strong&gt; is a container object that holds a &lt;em&gt;fixed number&lt;/em&gt; of elements of the &lt;em&gt;same data type&lt;/em&gt;. These elements can be either &lt;strong&gt;primitive&lt;/strong&gt; types (like &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, etc.) or &lt;strong&gt;objects&lt;/strong&gt; (like &lt;code&gt;String&lt;/code&gt; or custom classes) and arrays implementing &lt;strong&gt;interfaces&lt;/strong&gt; (like &lt;code&gt;CharSequence&lt;/code&gt;, etc.). Array functionality is part of the &lt;strong&gt;&lt;em&gt;java.util.Arrays&lt;/em&gt;&lt;/strong&gt; package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Key characteristics of Java arrays include:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The length of an array is established upon creation and cannot be changed.&lt;/li&gt;
&lt;li&gt;All elements in an array must be of the same type.&lt;/li&gt;
&lt;li&gt;Arrays can store duplicate values.&lt;/li&gt;
&lt;li&gt;Arrays can be one-dimensional, two-dimensional, or multi-dimensional by representing a matrix of data. Note that "true support" of multi-dimensional arrays is not supported, but Java offers an array of arrays to support a multi-dimensional structure.&lt;/li&gt;
&lt;li&gt;An array declared with a superclass or interface type can store subclass objects, supporting polymorphism. For example, an array of &lt;code&gt;CharSequence&lt;/code&gt; can store references to &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;StringBuilder&lt;/code&gt; objects.&lt;/li&gt;
&lt;li&gt;Array elements are accessed using an index, starting at &lt;code&gt;0&lt;/code&gt;. Therefore, the last element in an array is at index &lt;code&gt;length - 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Array Declarations
&lt;/h1&gt;

&lt;p&gt;Arrays in Java must adhere to specific declaration rules to be properly defined. The following rules outline the correct syntax for declaring arrays:&lt;/p&gt;

&lt;p&gt;1️⃣. &lt;strong&gt;Data Type&lt;/strong&gt;: Arrays can store elements of any valid Java data type, including &lt;strong&gt;&lt;em&gt;primitive&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;class&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;interface&lt;/em&gt;&lt;/strong&gt; types.&lt;br&gt;
2️⃣. &lt;strong&gt;Brackets&lt;/strong&gt;: The square brackets &lt;code&gt;[]&lt;/code&gt; indicate that a variable is an array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One-Dimensional Arrays&lt;/strong&gt;: One set of brackets is used. The brackets can be placed either after the data type or after the variable name.
&lt;/li&gt;
&lt;/ul&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;numbers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// brackets next to the type&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;  &lt;span class="c1"&gt;// brackets next to the variable name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multidimensional Arrays&lt;/strong&gt;: Sets of brackets are used on both sides to create a multidimensional array, like a table.
&lt;/li&gt;
&lt;/ul&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;matrix&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// 2D array&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="o"&gt;[][];&lt;/span&gt;               &lt;span class="c1"&gt;// 2D array&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;matrix&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;               &lt;span class="c1"&gt;// 2D array&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;matrix&lt;/span&gt; &lt;span class="o"&gt;[],&lt;/span&gt; &lt;span class="n"&gt;space&lt;/span&gt; &lt;span class="o"&gt;[][];&lt;/span&gt;  &lt;span class="c1"&gt;// 2D array and 3D array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No Size in Declaration&lt;/strong&gt;: The size of the array is not specified during declaration. The size is determined later, during array creation.
&lt;/li&gt;
&lt;/ul&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;numbers&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;numbers&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;// Initialization with size&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To consolidate these rules, consider the following examples of valid and invalid array declarations.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Valid Array Declarations:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Multiple variables of the same type can be defined on one line, including both arrays and non-array variables. Here, &lt;strong&gt;&lt;code&gt;a&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;c&lt;/code&gt;&lt;/strong&gt; are &lt;code&gt;int&lt;/code&gt; variables, while &lt;strong&gt;&lt;code&gt;b&lt;/code&gt;&lt;/strong&gt; is an array of integers (i.e., &lt;code&gt;int[]&lt;/code&gt;):
&lt;/li&gt;
&lt;/ul&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;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The following declaration implies that &lt;strong&gt;&lt;code&gt;d&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;e&lt;/code&gt;&lt;/strong&gt; are of type &lt;code&gt;int[]&lt;/code&gt; whereas, the variable &lt;strong&gt;&lt;code&gt;f&lt;/code&gt;&lt;/strong&gt; is a two-dimensional array (i.e., &lt;code&gt;int[][]&lt;/code&gt;):
&lt;/li&gt;
&lt;/ul&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;d&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Invalid Array Declarations:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Size&lt;/strong&gt; is &lt;strong&gt;not&lt;/strong&gt; part of the &lt;strong&gt;declaration&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&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="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;intArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;intArray&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="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;You &lt;strong&gt;cannot&lt;/strong&gt; define &lt;strong&gt;multiple variables&lt;/strong&gt; of &lt;strong&gt;different types&lt;/strong&gt; in the same line, separated by commas. Java is strongly typed and this is invalid:
&lt;/li&gt;
&lt;/ul&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;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;   &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a id="two"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Creating an Array
&lt;/h1&gt;

&lt;p&gt;Once an array is declared, it must be created. Java provides several ways to create and initialize arrays, each with its own characteristics:&lt;/p&gt;

&lt;p&gt;1️⃣. &lt;strong&gt;The &lt;code&gt;new&lt;/code&gt; Operator and Size&lt;/strong&gt;: The &lt;code&gt;new&lt;/code&gt; operator can be used to define the &lt;strong&gt;size&lt;/strong&gt; of the array within brackets without specifying the initial values:&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;intArray&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;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;           &lt;span class="c1"&gt;// creates an array of int with 10 elements initialised to 0&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;atringArray&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;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;  &lt;span class="c1"&gt;// creates an array of String with 10 elements, all initialised to null&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❗ Once an array's size is defined during creation, it &lt;strong&gt;cannot be changed&lt;/strong&gt;. This means an existing array object cannot be resized directly.&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;numbers&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="n"&gt;numbers&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;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;   &lt;span class="c1"&gt;// not resizing! This creates a new array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the size of the original array object is not changed. Instead, it creates a &lt;em&gt;new&lt;/em&gt; &lt;code&gt;int[]&lt;/code&gt; array with a size of 10 and assigns its reference to the &lt;code&gt;numbers&lt;/code&gt; variable. The original array (with a size of 5) is now eligible for garbage collection, assuming it is no longer referenced elsewhere. If it had some data, it's lost.&lt;/p&gt;

&lt;p&gt;When an array is declared and created, but &lt;strong&gt;no values have been assigned&lt;/strong&gt;, each slot in the array has a default value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Numeric&lt;/strong&gt; primitives are set to &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boolean&lt;/strong&gt; primitives are set to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;References&lt;/strong&gt;, including primitive data type wrappers, are set to &lt;code&gt;null&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&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="n"&gt;names&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This array points to &lt;code&gt;null&lt;/code&gt;. The code never instantiated the array, so it is just a &lt;strong&gt;&lt;em&gt;reference variable to &lt;code&gt;null&lt;/code&gt;&lt;/em&gt;&lt;/strong&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="o"&gt;[]&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;6&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is an array because it has brackets. It is an array of type &lt;code&gt;String&lt;/code&gt; since that is the type mentioned in the declaration. It has six elements because the length is 6. Each of those six slots currently is &lt;code&gt;null&lt;/code&gt; but can &lt;strong&gt;&lt;em&gt;potentially point to a &lt;code&gt;String&lt;/code&gt; object&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;2️⃣. &lt;strong&gt;The &lt;code&gt;new&lt;/code&gt; Operator and Values&lt;/strong&gt;: The &lt;code&gt;new&lt;/code&gt; operator can be used without specifying the size in brackets, provided the &lt;strong&gt;initial values&lt;/strong&gt; are specified directly within curly braces &lt;code&gt;{}&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;numbers&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="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// int array of size 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, an &lt;code&gt;int&lt;/code&gt; array of size 3 is created, and its elements are immediately initialized with the given values. The size of the array is implicitly determined by the number of values provided within the curly braces. Again, it is important to emphasize that the initial definition and size of an array cannot be changed once created.&lt;/p&gt;

&lt;p&gt;3️⃣. &lt;strong&gt;Array Initializer&lt;/strong&gt;: An array initializer, a shortcut that allows an array to be created and initialized in one statement. The initial values of the array elements are specified directly using curly braces &lt;code&gt;{}&lt;/code&gt;. This approach is called an &lt;strong&gt;&lt;em&gt;anonymous array&lt;/em&gt;&lt;/strong&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;intArray&lt;/span&gt; &lt;span class="o"&gt;=&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;7&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="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;stringArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4️⃣. And finally, let's consider how to create &lt;strong&gt;multidimensional arrays&lt;/strong&gt;. The most important thing to remember about multidimensional arrays in Java is that they are essentially &lt;em&gt;arrays of arrays&lt;/em&gt;. This allows you to represent data in a tabular format (rows and columns) or in higher-dimensional structures. While Java doesn't have true built-in multidimensional arrays, it provides the ability to create arrays where each element holds a reference to another array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Declaration and Initialization with Size&lt;/strong&gt;: The size of a multidimensional array can be specified during declaration and initialization:
&lt;/li&gt;
&lt;/ul&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;matrix&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;3&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="c1"&gt;// сreates a 2D array of 3 rows and 2 columns&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an array named &lt;code&gt;matrix&lt;/code&gt; with three elements. Each of these &lt;strong&gt;three elements&lt;/strong&gt; is a reference to an array of &lt;strong&gt;two &lt;code&gt;String&lt;/code&gt; objects&lt;/strong&gt;. It's helpful to think of the addressable range as &lt;code&gt;[0][0]&lt;/code&gt; through &lt;code&gt;[2][1]&lt;/code&gt;. To visualize this matrix, let's take a look at the code.&lt;br&gt;
For example, suppose one of these values is set as:&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;matrix&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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="s"&gt;"set"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Schematically, this can be depicted as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,0] [0,1]  //Here we set a value for matrix[0][1]
[1,0] [1,1]
[2,0] [2,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Asymmetric Arrays&lt;/strong&gt;: Multidimensional arrays don't have to be rectangular. Asymmetric arrays can be created where each row has a different number of columns:
&lt;/li&gt;
&lt;/ul&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;matrix&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;4&lt;/span&gt;&lt;span class="o"&gt;},&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="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="mi"&gt;8&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Schematically, this can be depicted as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,0] [0,1]         =&amp;gt; {1,4}
[1,0]               =&amp;gt; {3}
[2,0] [2,1] [2,2]   =&amp;gt; {9,8,7}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way to create an &lt;strong&gt;asymmetric&lt;/strong&gt; array is to initialize just an array’s &lt;strong&gt;first dimension&lt;/strong&gt; and define the size of each array component in a separate statement:&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;matrix&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;3&lt;/span&gt;&lt;span class="o"&gt;][];&lt;/span&gt;
&lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&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;4&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="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;matrix&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="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;2&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here an example visual how does work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,0] [0,1] [0,2]     =&amp;gt; {4, 9, 7}
[1,0] [1,1]           =&amp;gt; {0, 0}
[2,0]                 =&amp;gt; null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can easy create matrix in &lt;strong&gt;tabular format&lt;/strong&gt; like example:
&lt;/li&gt;
&lt;/ul&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;matrix&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="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="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;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;span class="mi"&gt;9&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;To consolidate these rules, consider the following examples of valid and invalid array creation statements.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Valid Array Creation Statements:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;new&lt;/code&gt; operator is used to &lt;strong&gt;allocate memory for the array&lt;/strong&gt;, specifying its data type and size:
&lt;/li&gt;
&lt;/ul&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;myIntArray&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;30&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;        &lt;span class="c1"&gt;// Brackets associated with the type&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;myShortArray&lt;/span&gt;&lt;span class="o"&gt;[]&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;short&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="c1"&gt;// Brackets associated with the variable name&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;An array can be &lt;strong&gt;declared first&lt;/strong&gt;, and &lt;strong&gt;then initialized&lt;/strong&gt; later using the &lt;code&gt;new&lt;/code&gt; operator:
&lt;/li&gt;
&lt;/ul&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;myStringArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;myStringArray&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;12&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 &lt;strong&gt;initial values&lt;/strong&gt; of the array elements are directly specified using &lt;code&gt;{}&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&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;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"three"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;    &lt;span class="c1"&gt;// An array of strings with 4 elements&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;An array with a &lt;strong&gt;length of zero&lt;/strong&gt; can be created:
&lt;/li&gt;
&lt;/ul&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;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;// This array has a length of 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can declare and initialize &lt;strong&gt;variables&lt;/strong&gt; like array and non array, but you need to know that both can have &lt;strong&gt;same type&lt;/strong&gt;, it's also you can chain create variables like that as:
&lt;/li&gt;
&lt;/ul&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;mySecondIntArray&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myIntArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mySecondIntArray&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;50&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Initializes both arrays&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you want to create two array, and you give to only one parameter this as will work as:&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;myIntArray&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mySecondIntArray&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;50&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// The second array is initialized to size 50, but the "first" array is not initialized.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;reference-type&lt;/strong&gt; array can be explicitly set to &lt;code&gt;null&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&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;mySecondIntArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;As arrays are &lt;strong&gt;ultimately objects&lt;/strong&gt; in Java, they can be assigned to variables of type &lt;code&gt;Object&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&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;// Valid initialization and assignment to an Object&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;new&lt;/code&gt; operator can be &lt;strong&gt;combined with the array initializer&lt;/strong&gt;, but the size cannot be specified in the brackets:
&lt;/li&gt;
&lt;/ul&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;a&lt;/span&gt;&lt;span class="o"&gt;[]&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;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="c1"&gt;// Size is determined by the initializer&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This creates a &lt;strong&gt;two-dimensional&lt;/strong&gt; array:
&lt;/li&gt;
&lt;/ul&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;stringArray&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="s"&gt;"one"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="o"&gt;},&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"three"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"four"&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;This is a valid &lt;strong&gt;two-dimensianal&lt;/strong&gt; array declaration and initialization. Only the &lt;strong&gt;first dimension&lt;/strong&gt; is required to have a size, because you can have an array of arrays, and the arrays referenced by the element indices can be of different sizes:
&lt;/li&gt;
&lt;/ul&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;matrix&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;2&lt;/span&gt;&lt;span class="o"&gt;][];&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Invalid Array Creation Statements:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;parentheses&lt;/strong&gt; in this statement generate a &lt;strong&gt;compile error&lt;/strong&gt; and size needs to be on the right side of the equation:
&lt;/li&gt;
&lt;/ul&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;stringArray&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;3&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;   &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Size&lt;/strong&gt; needs to be &lt;strong&gt;on the right side&lt;/strong&gt; of the equation:
&lt;/li&gt;
&lt;/ul&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="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;stringArray&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="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;An array initializer &lt;strong&gt;doesn't require a restatement&lt;/strong&gt; of the array type:
&lt;/li&gt;
&lt;/ul&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;a&lt;/span&gt;&lt;span class="o"&gt;[]&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="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="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This is &lt;strong&gt;invalid&lt;/strong&gt; because you are stating the &lt;strong&gt;size&lt;/strong&gt;, but the array initilizer already defines the size:
&lt;/li&gt;
&lt;/ul&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;b&lt;/span&gt;&lt;span class="o"&gt;[]&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="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="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You &lt;strong&gt;cannot use the array initializer&lt;/strong&gt; on a &lt;strong&gt;separate&lt;/strong&gt; statement line from the declaration of the array:
&lt;/li&gt;
&lt;/ul&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;myArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;span class="n"&gt;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;three&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;  &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This statement is &lt;strong&gt;invalid&lt;/strong&gt;, unlike using the &lt;code&gt;new&lt;/code&gt; operator on the right hand side of this equation. You &lt;strong&gt;cannot use array initializer&lt;/strong&gt; in a &lt;strong&gt;compound&lt;/strong&gt; statement:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;short&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;mySecondShortArray&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myShortArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mySecondShortArray&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="c1"&gt;// compilation error&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The type of &lt;strong&gt;second argument&lt;/strong&gt; is &lt;strong&gt;incorrect&lt;/strong&gt;. Only the first dimension in two-dimensional array is required to have a size. And this initialization does not give it a size, so it is incorrect:
&lt;/li&gt;
&lt;/ul&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;matrix&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;2&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;            &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You &lt;strong&gt;cannot initialize a two-dimensional&lt;/strong&gt; array and &lt;strong&gt;assign&lt;/strong&gt; it &lt;strong&gt;to a one-dimensional&lt;/strong&gt; array:
&lt;/li&gt;
&lt;/ul&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;c&lt;/span&gt;&lt;span class="o"&gt;[]&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="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;                  &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="three"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Working with Arrays
&lt;/h1&gt;

&lt;p&gt;Once you've declared and created an array, it's time to use it. This section covers common operations and functionalities associated with working with arrays in Java.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrays in Java are &lt;strong&gt;objects&lt;/strong&gt;, which means they inherit certain characteristics from the &lt;code&gt;Object&lt;/code&gt; class. So, arrays can call &lt;code&gt;equals()&lt;/code&gt; method. Remember, this would work even on an &lt;code&gt;int[]&lt;/code&gt;, too. Since &lt;strong&gt;&lt;code&gt;int&lt;/code&gt;&lt;/strong&gt; is a &lt;strong&gt;primitive&lt;/strong&gt;, therefore &lt;strong&gt;&lt;code&gt;int[]&lt;/code&gt;&lt;/strong&gt; is an &lt;strong&gt;object&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&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;bugs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"cricket"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"beetle"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;ladybug&lt;/span&gt;&lt;span class="err"&gt;”&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;alias&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bugs&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;bugs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alias&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                          &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Java has provided the &lt;code&gt;Arrays.toString()&lt;/code&gt; method that &lt;strong&gt;prints&lt;/strong&gt; an array:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&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="n"&gt;bugs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;         &lt;span class="c1"&gt;// Output: [Ljava…&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="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bugs&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// Output: [cricket, beetle, ladybug]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;length&lt;/strong&gt; property gives the number of slots have been allocated. This property is a field, &lt;strong&gt;not a method&lt;/strong&gt;, it should not contain parentheses:
&lt;/li&gt;
&lt;/ul&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;birds&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;6&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="n"&gt;birds&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="c1"&gt;// Output: 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="four"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Sorting Arrays&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Java provides a convenient &lt;code&gt;Arrays.sort()&lt;/code&gt; method for &lt;strong&gt;sorting&lt;/strong&gt; arrays in ascending order. When sorting arrays containing different data types (numbers, strings, etc.), it's important to be aware of the rules Java uses to compare these types, as this will determine the final order of the elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void Arrays.sort(T[] array)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The easy way with &lt;strong&gt;numbers&lt;/strong&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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;span class="mi"&gt;9&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="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;    &lt;span class="c1"&gt;// Output: [1, 6, 9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try this again with &lt;strong&gt;&lt;code&gt;String&lt;/code&gt;&lt;/strong&gt; 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;strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"9"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"M"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strings&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="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: [0, 10, 100, 9, M, a]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;String sorts in alphabetic order, and 1 sorts before 9. The numbers are sorted before letters, and uppercase sorts before lowercase.&lt;/p&gt;

&lt;p&gt;&lt;a id="five"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Searching Arrays&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Java provides a convenient method, &lt;code&gt;Arrays.binarySearch()&lt;/code&gt;, to efficiently &lt;strong&gt;search&lt;/strong&gt; for a specific element within an array. This method requires that the &lt;strong&gt;array already be sorted&lt;/strong&gt; for the results to be accurate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int Arrays.binarySearch(int[] array, int key)
int Arrays.binarySearch(Object[] array, Object key)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rules for &lt;strong&gt;searching&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the target element is &lt;strong&gt;found in the sorted array&lt;/strong&gt;, the method returns the index of the matching element (a &lt;strong&gt;positive&lt;/strong&gt; integer).&lt;/li&gt;
&lt;li&gt;If the target element is &lt;strong&gt;not found in the sorted array&lt;/strong&gt;, the method returns a &lt;strong&gt;negative&lt;/strong&gt; value. The returned value can be used to determine the insertion point for the element to maintain the sorted order. The formula is &lt;strong&gt;[-n] - 1&lt;/strong&gt;, where &lt;strong&gt;n&lt;/strong&gt; is an imaginary &lt;strong&gt;index&lt;/strong&gt; to insert to keep the sorted order.
&lt;/li&gt;
&lt;/ul&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;numbers&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;4&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;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="c1"&gt;// 0: match&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="c1"&gt;// 1: match&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="c1"&gt;// -1: insertion point would be index 0 =&amp;gt; [-0] - 1 = -1&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="c1"&gt;// -2: [-1] - 1 = -2&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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;// -5: [-4] - 1 = -5&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is critical to understand that &lt;code&gt;Arrays.binarySearch()&lt;/code&gt; only &lt;strong&gt;works correctly on sorted arrays&lt;/strong&gt;. If you use it on an &lt;strong&gt;unsorted&lt;/strong&gt; array, the results are unpredictable and likely incorrect. The only way can make sort (if it's necessary):&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;numbers&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;3&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;1&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="c1"&gt;// 1: match&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="c1"&gt;// output not predictable: -4&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;strings&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="s"&gt;"9"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                         &lt;span class="c1"&gt;// Now: [10, 100, 9]&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// 0: match&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;binarySearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"8"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;            &lt;span class="c1"&gt;// -3: [-2] - 1 = -3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="six"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Comparing Arrays&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Java provides two methods, &lt;code&gt;Arrays.compare()&lt;/code&gt; and &lt;code&gt;Arrays.mismatch()&lt;/code&gt;, to &lt;strong&gt;compare&lt;/strong&gt; two arrays to determine which is smaller.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: &lt;code&gt;Arrays.compare()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;T&amp;gt; int Arrays.compare(T[] array1, T[] array2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The method returns:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;negative&lt;/strong&gt; integer is if the first array is less than the second array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero&lt;/strong&gt; if the arrays are equal.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;positive&lt;/strong&gt; integer is if the first array is greater than the second array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rules that define a &lt;strong&gt;smaller&lt;/strong&gt; value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;null&lt;/code&gt; is smaller&lt;/strong&gt; than any other value.&lt;/li&gt;
&lt;li&gt;For numbers, normal numeric order applies.&lt;/li&gt;
&lt;li&gt;For strings, &lt;strong&gt;one is smaller&lt;/strong&gt; if it is a prefix of another.&lt;/li&gt;
&lt;li&gt;For strings/characters, &lt;strong&gt;numbers are smaller&lt;/strong&gt; than letters.&lt;/li&gt;
&lt;li&gt;For strings/characters, &lt;strong&gt;uppercase is smaller&lt;/strong&gt; than lowercase.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="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="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="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="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;: Positive number&lt;br&gt;
&lt;strong&gt;Reason&lt;/strong&gt;: The first element is the same, but the first array is longer.&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;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="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="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="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="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;: Zero&lt;br&gt;
&lt;strong&gt;Reason&lt;/strong&gt;: Exact match&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;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"aa"&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;   &lt;span class="c1"&gt;// -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;: Negative number&lt;br&gt;
&lt;strong&gt;Reason&lt;/strong&gt;: The first element is a substring of the second.&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;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;    &lt;span class="c1"&gt;// 32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;: Positive number&lt;br&gt;
&lt;strong&gt;Reason&lt;/strong&gt;: Uppercase is smaller than lowercase.&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;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&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="o"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;   &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;: Positive number&lt;br&gt;
&lt;strong&gt;Reason&lt;/strong&gt;: &lt;code&gt;null&lt;/code&gt; is smaller than a letter.&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;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"1"&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;     &lt;span class="c1"&gt;// -48&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;: Negative number&lt;br&gt;
&lt;strong&gt;Reason&lt;/strong&gt;: Numbers are smaller than letters&lt;/p&gt;

&lt;p&gt;Finally, this code &lt;strong&gt;does not compile&lt;/strong&gt; because the &lt;strong&gt;types are different&lt;/strong&gt;. When comparing two arrays, they must be the same array type.&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;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&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="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="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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;          &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: &lt;code&gt;Arrays.mismatch()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;T&amp;gt; int mismatch(T[] array1, T[] array2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;strong&gt;arrays are equal&lt;/strong&gt;, &lt;code&gt;mismatch()&lt;/code&gt; returns &lt;strong&gt;-1&lt;/strong&gt;. Otherwise, it returns the first index where they differ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The method returns&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the arrays are &lt;strong&gt;equal&lt;/strong&gt;, it returns &lt;strong&gt;-1&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If one array is &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt;, it returns &lt;strong&gt;0&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;If the arrays are &lt;strong&gt;not equal&lt;/strong&gt;, it returns the first index where they differ
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mismatch&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="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="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="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="c1"&gt;// -1&lt;/span&gt;

&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mismatch&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;        &lt;span class="c1"&gt;// 0&lt;/span&gt;

&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mismatch&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="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"a"&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="o"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;       &lt;span class="c1"&gt;// 0&lt;/span&gt;

&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mismatch&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="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="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="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="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="seven"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Shallow Copy vs. Deep Copy
&lt;/h1&gt;

&lt;p&gt;Each type of array implements the &lt;code&gt;Cloneable&lt;/code&gt; and &lt;code&gt;Serializable&lt;/code&gt; interfaces. Therefore, it's logical to discuss the &lt;code&gt;clone()&lt;/code&gt; method, which is related to two important concepts: &lt;strong&gt;shallow&lt;/strong&gt; and &lt;strong&gt;deep&lt;/strong&gt; copy.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Shallow Copy&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A shallow copy creates a &lt;strong&gt;new object&lt;/strong&gt; (or array) but &lt;strong&gt;does not clone the objects&lt;/strong&gt; contained within. Instead, it copies the references to the same objects in memory. As a result, changes made to the objects through one reference will be reflected in the other reference.&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="nd"&gt;@Setter&lt;/span&gt;
&lt;span class="nd"&gt;@Getter&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;Friend&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Cloneable&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;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Friend&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Friend&lt;/span&gt; &lt;span class="nf"&gt;clone&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="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Friend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shallow Copy of Primitive Array&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&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;shallowCopyOfPrimitive&lt;/span&gt;&lt;span class="o"&gt;()&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;a&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="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;copy&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;4&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;"Origin: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;"Copy: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;copy&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;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;As primitives are copied directly, the original array remains unchanged, illustrating that primitive types are independent when cloned.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shallow Copy of Object Array&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&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;shallowCopyOfObject&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Friend&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&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;Friend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monica"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&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;Friend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Ross"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="o"&gt;)};&lt;/span&gt;
    &lt;span class="nc"&gt;Friend&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Chandler"&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;"Origin: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;"Copy: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;copy&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;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Origin: [Name: Chandler, Age: 28, Name: Ross, Age: 26]
Copy: [Name: Chandler, Age: 28, Name: Ross, Age: 26]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows the behavior of shallow copying, where both arrays point to the same underlying &lt;code&gt;Friend&lt;/code&gt; objects. Changes in one affect the other because they share the same references.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Deep Copy&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A deep copy creates a &lt;strong&gt;new object&lt;/strong&gt; (or array) and also &lt;strong&gt;clones the objects&lt;/strong&gt; contained within. This means that two independent objects are created in memory, so changes made to one do not affect the other.&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;private&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;deepCopy&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Friend&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&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;Friend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monica"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&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;Friend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Ross"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="o"&gt;)};&lt;/span&gt;
    &lt;span class="nc"&gt;Friend&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;copy&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;Friend&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&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="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&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="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;     &lt;span class="c1"&gt;// Calling clone method to create independent copies&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="na"&gt;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Chandler"&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;"Origin: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;"Copy: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;copy&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;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Origin: [Name: Monica, Age: 28, Name: Ross, Age: 26]
Copy: [Name: Chandler, Age: 28, Name: Ross, Age: 26]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This illustrates deep copying, where modifications in one array do not impact the other because they hold separate object instances in memory.&lt;/p&gt;




&lt;h4&gt;
  
  
  Thanks for reading! Hope you found some useful info in this article. Got any questions or ideas? Drop them in the comments below.  😊
&lt;/h4&gt;

</description>
      <category>java</category>
      <category>fundamentals</category>
      <category>arrays</category>
    </item>
    <item>
      <title>Mastering String APIs in Java</title>
      <dc:creator>Marta Kravchuk</dc:creator>
      <pubDate>Sat, 31 May 2025 06:30:33 +0000</pubDate>
      <link>https://dev.to/kmartita/mastering-string-apis-in-java-54p6</link>
      <guid>https://dev.to/kmartita/mastering-string-apis-in-java-54p6</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;In Java, strings are fundamental for data manipulation. This article delves into the core concepts of Java's &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;StringBuilder&lt;/code&gt; classes, essential components of the Java Standard Library. It will explore how to create and manipulate String objects, understand the nuances of immutability, and leverage the string pool for efficiency. Additionally, the mutable nature of the StringBuilder class and its methods for dynamic string construction will be discussed here, providing a comprehensive understanding of string handling in Java.&lt;/em&gt;
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
Working with String Objects

&lt;ul&gt;
&lt;li&gt;Concatenation&lt;/li&gt;
&lt;li&gt;Immutability and the String Pool&lt;/li&gt;
&lt;li&gt;Essential String Methods and the Method Chaining&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Manipulate Data Using the StringBuilder Class&lt;/li&gt;
&lt;li&gt;Understanding Equality: String vs. StringBuilder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;String APIs refer to the set of methods and functionalities provided by the &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;StringBuilder&lt;/code&gt; classes, which are part of the &lt;strong&gt;Java Standard Library&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a id="one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Working with &lt;code&gt;String&lt;/code&gt; Objects
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;String&lt;/code&gt; class is fundamental in Java, representing an &lt;strong&gt;immutable sequence of characters&lt;/strong&gt;. It implements the &lt;code&gt;CharSequence&lt;/code&gt; interface, a general way of representing character sequences, also implemented by classes like &lt;code&gt;StringBuilder&lt;/code&gt;. In Java, a &lt;code&gt;String&lt;/code&gt; &lt;strong&gt;object&lt;/strong&gt; can be created as follows (the &lt;code&gt;new&lt;/code&gt; keyword is optional):&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="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fluffy"&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="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="s"&gt;"Fluffy"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In both examples, a reference variable &lt;code&gt;name&lt;/code&gt; is created, pointing to a &lt;code&gt;String&lt;/code&gt; object with the value "Fluffy". However, the creation process differs subtly. The &lt;code&gt;String&lt;/code&gt; &lt;strong&gt;class&lt;/strong&gt; is unique in that it &lt;strong&gt;doesn’t&lt;/strong&gt; require instantiation with &lt;strong&gt;&lt;code&gt;new&lt;/code&gt;&lt;/strong&gt;, it can be created directly from a literal.&lt;/p&gt;

&lt;p&gt;&lt;a id="two"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Concatenation&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Placing one &lt;code&gt;String&lt;/code&gt; object before another &lt;code&gt;String&lt;/code&gt; object and combining them (using the &lt;code&gt;+&lt;/code&gt; operator) is called &lt;strong&gt;string concatenation&lt;/strong&gt;.&lt;br&gt;
The most important rules to know are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If both operands are numeric, &lt;code&gt;+&lt;/code&gt; means numeric &lt;strong&gt;addition&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;If either operand is a &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt; means &lt;strong&gt;concatenation&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;The expression is evaluated &lt;strong&gt;left to right&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&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="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="c1"&gt;// 3&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;"a"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"b"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// "ab"&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;"a"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"b"&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="c1"&gt;// "ab3"&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="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="s"&gt;"c"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// "3c"&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;"c"&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="c1"&gt;// "c12" =&amp;gt; "c1" + 2 =&amp;gt; "c12"&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;three&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;four&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"4"&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="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="n"&gt;three&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;four&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "64" =&amp;gt; 3 + 3 + "4" =&amp;gt; 6 + “4"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a id="three"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;Immutability and the String Pool&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;In Java, the &lt;code&gt;String&lt;/code&gt; &lt;strong&gt;class&lt;/strong&gt; is &lt;strong&gt;immutable&lt;/strong&gt;. Once a &lt;code&gt;String&lt;/code&gt; object is created, its value cannot be changed. This design choice offers key benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immutable &lt;code&gt;String&lt;/code&gt; objects are inherently &lt;strong&gt;thread-safe&lt;/strong&gt;, which prevents data corruption in parallel environments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Immutability increases security&lt;/strong&gt;. For example, class names or arguments passed to methods are often represented as strings. By making strings immutable, you prevent attackers from changing these values after they are passed.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;String&lt;/code&gt; objects are immutable, their &lt;strong&gt;hash codes can be cached without worrying about changes&lt;/strong&gt;. This makes &lt;code&gt;String&lt;/code&gt; objects suitable for use as keys in &lt;code&gt;HashMap&lt;/code&gt; and other hash-based data structures, &lt;strong&gt;improving performance&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The immutability of &lt;code&gt;String&lt;/code&gt; objects is demonstrated in the following 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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1"&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;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;            &lt;span class="c1"&gt;// This call is effectively ignored&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="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// Output: "12"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;s1.concat("2")&lt;/code&gt; creates a new String object, &lt;code&gt;"12"&lt;/code&gt;, and assigns its reference to &lt;code&gt;s2&lt;/code&gt;. Even though &lt;code&gt;s2.concat("3")&lt;/code&gt; is called, it doesn't change the value of &lt;code&gt;s2&lt;/code&gt;. Because strings are immutable, &lt;code&gt;concat()&lt;/code&gt; always returns a new String object. Since the result of &lt;code&gt;s2.concat("3")&lt;/code&gt; isn't assigned to anything, it's discarded, and &lt;code&gt;s2&lt;/code&gt; remains &lt;code&gt;"12"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since strings are everywhere in Java, they can consume significant memory, especially in production applications. To mitigate this, Java reuses common strings through the &lt;strong&gt;&lt;em&gt;string pool&lt;/em&gt;&lt;/strong&gt; (also known as the &lt;em&gt;intern pool&lt;/em&gt;), a special memory area in the Java Virtual Machine (JVM) that stores &lt;code&gt;String&lt;/code&gt; literals to optimize memory usage.&lt;/p&gt;

&lt;p&gt;So, the &lt;strong&gt;string pool&lt;/strong&gt; contains &lt;code&gt;String&lt;/code&gt; &lt;em&gt;literals&lt;/em&gt; and &lt;em&gt;constants&lt;/em&gt;. For example, &lt;code&gt;x&lt;/code&gt; or &lt;code&gt;y&lt;/code&gt; are &lt;code&gt;String&lt;/code&gt; literals and will be placed in the string pool. But, the &lt;code&gt;myObject.toString()&lt;/code&gt; is a string but &lt;strong&gt;not a literal&lt;/strong&gt;, so it typically doesn’t go into the string pool directly. And &lt;code&gt;String&lt;/code&gt; objects created with the &lt;code&gt;new&lt;/code&gt; keyword are &lt;strong&gt;not automatically added&lt;/strong&gt; to the string pool:&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// x goes into the string pool&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// y goes into the string pool&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="n"&gt;x&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="c1"&gt;// true (x and y refer to the same object)&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&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;myObject&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="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// this is object&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="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;               &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;String&lt;/code&gt; object created as a result of runtime calculations (e.g., using &lt;code&gt;String&lt;/code&gt; methods), even if they have the same content, a &lt;strong&gt;&lt;em&gt;new&lt;/em&gt; &lt;code&gt;String&lt;/code&gt; &lt;em&gt;object&lt;/em&gt;&lt;/strong&gt; will be created:&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&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;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" Hello World"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;trim&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// a new String object is created&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// false (x and z are different objects)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Concatenation with non-literals also results in a &lt;strong&gt;&lt;em&gt;new&lt;/em&gt; &lt;code&gt;String&lt;/code&gt; &lt;em&gt;object&lt;/em&gt;&lt;/strong&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello world"&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;concat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello "&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;concat&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"world"&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="n"&gt;single&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a &lt;code&gt;String&lt;/code&gt; literal is created, the JVM first checks if an identical &lt;code&gt;String&lt;/code&gt; already exists in the pool. If it does, the variable simply points to the &lt;em&gt;existing&lt;/em&gt; &lt;code&gt;String&lt;/code&gt; object in the pool, avoiding the creation of a duplicate. This mechanism offers significant memory savings, especially in applications that use many identical string values.&lt;/p&gt;

&lt;p&gt;&lt;a id="four"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Essential &lt;code&gt;String&lt;/code&gt; Methods and the Method Chaining&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;For all these methods, remember that a &lt;code&gt;String&lt;/code&gt; is a sequence of characters, and Java &lt;strong&gt;indexing starts at &lt;code&gt;0&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;length()&lt;/code&gt;&lt;/strong&gt; returns the length of the &lt;code&gt;String&lt;/code&gt; object (number of characters):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&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="c1"&gt;// 7&lt;/span&gt;
&lt;span class="s"&gt;""&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="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;toLowerCase()&lt;/code&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;code&gt;toUpperCase()&lt;/code&gt;&lt;/strong&gt; return a new &lt;code&gt;String&lt;/code&gt; object with all characters converted to lowercase or uppercase, respectively:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"AniMaLs"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toLowerCase&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// "animals"&lt;/span&gt;
&lt;span class="s"&gt;"AniMaLs"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toUpperCase&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// "ANIMALS"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;equals()&lt;/code&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;code&gt;equalsIgnoreCase()&lt;/code&gt;&lt;/strong&gt; compare the content of two &lt;code&gt;String&lt;/code&gt; objects. The &lt;code&gt;equals()&lt;/code&gt; is case-sensitive, while &lt;code&gt;equalsIgnoreCase()&lt;/code&gt; is not. Both methods return a &lt;code&gt;boolean&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;             &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ANIMALS"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;             &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equalsIgnoreCase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ANIMALS"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;contains()&lt;/code&gt;&lt;/strong&gt; checks if the &lt;code&gt;String&lt;/code&gt; object contains a specified sequence of characters. Returns a &lt;code&gt;boolean&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ani"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"als"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AlS"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// false (case-sensitive)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;startsWith()&lt;/code&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;code&gt;endsWith()&lt;/code&gt;&lt;/strong&gt; check if the &lt;code&gt;String&lt;/code&gt; object starts or ends with a specified prefix or suffix. Returns a &lt;code&gt;boolean&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ani"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"als"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"als"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;replace()&lt;/code&gt;&lt;/strong&gt; returns a new &lt;code&gt;String&lt;/code&gt; object in which all occurrences of a specified sequence of characters are replaced with another sequence:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"o"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// "onimols"&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"als"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"zzz"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// "animzzz"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;charAt()&lt;/code&gt;&lt;/strong&gt; returns the &lt;code&gt;char&lt;/code&gt; at the specified index:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// a&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&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;span class="c1"&gt;// s&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&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="c1"&gt;// throws exception&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;indexOf()&lt;/code&gt;&lt;/strong&gt; finds the first index that matches the specified character or substring. Returns &lt;strong&gt;&lt;code&gt;-1&lt;/code&gt;&lt;/strong&gt; if &lt;strong&gt;no match&lt;/strong&gt; is found and &lt;em&gt;doesn’t throw&lt;/em&gt; an exception. Returns an &lt;code&gt;int&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;indexOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;indexOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"al"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;indexOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&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="c1"&gt;// 4&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;indexOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"al"&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;// -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;substring()&lt;/code&gt;&lt;/strong&gt; returns parts of the &lt;code&gt;String&lt;/code&gt; object:
&lt;/li&gt;
&lt;/ul&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="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;


&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&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="c1"&gt;// "mals"&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;indexOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'m'&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// "mals"&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&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;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// "ma"&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&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;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// empty string&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&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;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// throws exception&lt;/span&gt;
&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&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;8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// throws exception&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;trim()&lt;/code&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;code&gt;strip()&lt;/code&gt;&lt;/strong&gt; remove whitespace from the beginning and end of a &lt;code&gt;String&lt;/code&gt; object. The &lt;code&gt;trim()&lt;/code&gt; works only with ASCII-spaces, the &lt;code&gt;strip()&lt;/code&gt; (new in Java 11) and supports Unicode:
&lt;/li&gt;
&lt;/ul&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="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" abc\t "&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;trim&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="c1"&gt;// 3&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;strip&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="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;stripLeading()&lt;/code&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;code&gt;stripTrailing()&lt;/code&gt;&lt;/strong&gt; (new in Java 11). The first removes whitespace from the beginning and leaves it at the end. The &lt;code&gt;stripTrailing()&lt;/code&gt; does the opposite:
&lt;/li&gt;
&lt;/ul&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="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" abc\t "&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stripLeading&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="c1"&gt;// 5&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stripTrailing&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="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;intern()&lt;/code&gt;&lt;/strong&gt; uses an object in the string pool if one is present. If the literal is not yet in the string pool, Java will add it at this time:
&lt;/li&gt;
&lt;/ul&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&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;y&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="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;intern&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="n"&gt;x&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="c1"&gt;// true&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"rat"&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"t"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"1"&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;third&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"t"&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="s"&gt;"1"&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="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;             &lt;span class="c1"&gt;// true&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="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// true&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="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;              &lt;span class="c1"&gt;// false&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="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intern&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;     &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;String&lt;/code&gt; methods can be used sequentially, with each method call assigning its resulting &lt;code&gt;String&lt;/code&gt; object to a new 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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"AniMaL   "&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;trimmed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;trim&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;               &lt;span class="c1"&gt;// "AniMaL"&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lowercase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trimmed&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toLowerCase&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// "animal"&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lowercase&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "AnimAl"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this approach can become verbose and less readable. Instead, Java allows you to use a technique called &lt;strong&gt;method chaining&lt;/strong&gt;, where multiple method calls are chained together in a single expression:&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="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"AniMaL   "&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;trim&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toLowerCase&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// "AnimAl"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="five"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Manipulate Data Using the &lt;code&gt;StringBuilder&lt;/code&gt; Class
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;StringBuilder&lt;/code&gt; class represents &lt;strong&gt;mutable sequences of characters&lt;/strong&gt;. Most of its methods return a reference to the current object, enabling method chaining.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;StringBuilder Methods&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;StringBuilder&lt;/code&gt; class has many methods: &lt;code&gt;substring()&lt;/code&gt; &lt;strong&gt;does not change&lt;/strong&gt; the value of a &lt;code&gt;StringBuilder&lt;/code&gt;, whereas &lt;code&gt;append()&lt;/code&gt;, &lt;code&gt;delete()&lt;/code&gt;, and &lt;code&gt;insert()&lt;/code&gt; &lt;strong&gt;does change&lt;/strong&gt; 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="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"start"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"+middle"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                       &lt;span class="c1"&gt;// "start+middle"&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;same&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"+end"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// "start+middle+end"&lt;/span&gt;

&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;a&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"abc"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"de"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"f"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"g"&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="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;               &lt;span class="c1"&gt;// Output: "abcdefg"&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="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;               &lt;span class="c1"&gt;// Output: "abcdefg"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;charAt()&lt;/code&gt;, &lt;code&gt;indexOf()&lt;/code&gt;, &lt;code&gt;length()&lt;/code&gt;, and &lt;code&gt;substring()&lt;/code&gt;: these four methods &lt;strong&gt;work the same&lt;/strong&gt; as in the &lt;code&gt;String&lt;/code&gt; class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;append()&lt;/code&gt;&lt;/strong&gt; adds values (e.g., &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;Object&lt;/code&gt;, etc.) to the end of the current &lt;code&gt;StringBuilder&lt;/code&gt; object:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;append&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="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'c'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"-"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                &lt;span class="c1"&gt;// sb -&amp;gt; "1c-true"  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;insert()&lt;/code&gt;&lt;/strong&gt; adds characters at the requested index. The &lt;strong&gt;&lt;code&gt;offset&lt;/code&gt;&lt;/strong&gt;  is the &lt;em&gt;index&lt;/em&gt; where we want to insert the requested parameter:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"animals"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&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="s"&gt;"-"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                          &lt;span class="c1"&gt;// sb -&amp;gt; "animals-"&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"-"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                          &lt;span class="c1"&gt;// sb -&amp;gt; "-animals-"&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;insert&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="s"&gt;"-"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                          &lt;span class="c1"&gt;// sb -&amp;gt; "-ani-mals-"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we add and remove characters, their indexes change. Also, if &lt;code&gt;offset&lt;/code&gt; is equal to &lt;strong&gt;&lt;code&gt;length() + 1&lt;/code&gt;&lt;/strong&gt; a &lt;code&gt;StringIndexOutOfBoundsException&lt;/code&gt; will be thrown.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;delete()&lt;/code&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;code&gt;deleteCharAt()&lt;/code&gt;&lt;/strong&gt;, the first removes characters from sequence, as &lt;code&gt;deleteCharAt()&lt;/code&gt; uses to remove only one character at the specified index:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"abcdef"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&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;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// sb -&amp;gt; "adef" (the length is 4)&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deleteCharAt&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;// throws an exception (the index is out of range)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if &lt;code&gt;delete()&lt;/code&gt; is called with an end index beyond the length of the &lt;code&gt;StringBuilder&lt;/code&gt;, it will &lt;strong&gt;not throw an exception&lt;/strong&gt; but will simply delete up to the end of the &lt;code&gt;StringBuilder&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="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"abcdef"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&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;100&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// sb -&amp;gt; "a"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;replace()&lt;/code&gt;&lt;/strong&gt; deletes the characters starting with &lt;strong&gt;&lt;code&gt;startIndex&lt;/code&gt;&lt;/strong&gt; and ending before &lt;strong&gt;&lt;code&gt;endIndex&lt;/code&gt;&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pigeon dirty"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&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;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sty"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// sb -&amp;gt; "pigsty dirty"&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&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;100&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// sb -&amp;gt; "piga"&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&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;100&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// sb -&amp;gt; "pi"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;reverse()&lt;/code&gt;&lt;/strong&gt; reverses the characters in the &lt;code&gt;StringBuilder&lt;/code&gt; object:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ABC"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;                        &lt;span class="c1"&gt;// sb -&amp;gt; "CBA"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="six"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Understanding Equality: &lt;code&gt;String&lt;/code&gt; vs. &lt;code&gt;StringBuilder&lt;/code&gt;
&lt;/h1&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Equality Comparison in Java: &lt;code&gt;String&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;==&lt;/code&gt;: Can return &lt;strong&gt;&lt;code&gt;true&lt;/code&gt;&lt;/strong&gt; if both &lt;code&gt;String&lt;/code&gt; literals point to the &lt;strong&gt;same object in the string pool&lt;/strong&gt;. For &lt;code&gt;new String()&lt;/code&gt;, it typically returns &lt;strong&gt;&lt;code&gt;false&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String.equals()&lt;/code&gt;: Compares the content (sequence of characters) of the strings. Returns &lt;strong&gt;&lt;code&gt;true&lt;/code&gt;&lt;/strong&gt; if the contents &lt;strong&gt;are identical&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fluffy"&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;b&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="s"&gt;"Fluffy"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&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;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" Hello World"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;trim&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;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Equality Comparison in Java: &lt;code&gt;StringBuilder&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;==&lt;/code&gt;: Always checks for reference equality, just like other non-pooled objects. It will return &lt;strong&gt;&lt;code&gt;true&lt;/code&gt;&lt;/strong&gt; only when &lt;strong&gt;both references&lt;/strong&gt; point to the &lt;strong&gt;same StringBuilder object&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;StringBuilder.equals()&lt;/code&gt;: Compares the object references. Returns true only if both references point to the same StringBuilder object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;one&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;two&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                  &lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;three&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;three&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❗&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; If the &lt;strong&gt;class does not have&lt;/strong&gt; an &lt;code&gt;equals()&lt;/code&gt; method, Java uses the &lt;code&gt;==&lt;/code&gt; operator &lt;strong&gt;to check equality&lt;/strong&gt;. Remember that &lt;code&gt;==&lt;/code&gt; is checking for object reference equality.&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&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="kd"&gt;public&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;Cat&lt;/span&gt; &lt;span class="n"&gt;t1&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;Cat&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="n"&gt;t2&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;Cat&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="n"&gt;t3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// false&lt;/span&gt;
        &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;t3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// true&lt;/span&gt;

        &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// false: equals() is absent -&amp;gt; t1 == t2&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;The compiler is smart enough to know that &lt;strong&gt;two references can’t&lt;/strong&gt; possibly &lt;strong&gt;point to the same object&lt;/strong&gt; when they are completely &lt;strong&gt;different types&lt;/strong&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&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;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Thanks for reading! Hope you found some useful info in this article. Got any questions or ideas? Drop them in the comments below. Stay tuned here for a new Java series every week! And feel free to connect with me on &lt;a href="https://www.linkedin.com/in/marta-kravchuk-389b8079/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; 😊
&lt;/h4&gt;

</description>
      <category>java</category>
      <category>fundamentals</category>
      <category>strings</category>
    </item>
    <item>
      <title>Garbage Collection in Java: A Simple Explanation</title>
      <dc:creator>Marta Kravchuk</dc:creator>
      <pubDate>Wed, 21 May 2025 07:00:00 +0000</pubDate>
      <link>https://dev.to/kmartita/garbage-collection-in-java-simply-explained-5fif</link>
      <guid>https://dev.to/kmartita/garbage-collection-in-java-simply-explained-5fif</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;Effective memory management is essential in Java development. Fortunately, Java automates this task through a process called garbage collection (GC), which helps prevent memory leaks. This article explains Java garbage collection in simple terms, covering its core principles, the &lt;code&gt;System.gc()&lt;/code&gt; method for suggesting garbage collection, the deprecated &lt;code&gt;finalize()&lt;/code&gt; method, and modern ways to manage resources.&lt;/em&gt;
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is Java Garbage Collection?&lt;/li&gt;
&lt;li&gt;Suggesting Garbage Collection with System.gc()&lt;/li&gt;
&lt;li&gt;Why the Deprecated finalize() Should Be Avoided&lt;/li&gt;
&lt;li&gt;Modern Resource Management Techniques&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All Java &lt;strong&gt;objects&lt;/strong&gt; are stored in a &lt;strong&gt;&lt;em&gt;memory heap&lt;/em&gt;&lt;/strong&gt;. The heap, also called &lt;em&gt;free storage&lt;/em&gt;, is a large pool of unused memory that is specifically allocated for your Java program. Depending on the environment you're working in, the heap can be quite large, but its size is always limited. After all, there is &lt;em&gt;no computer with infinite memory&lt;/em&gt;. So if you create many objects without cleaning them up, your program could run out of memory and crash.&lt;/p&gt;

&lt;p&gt;&lt;a id="one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. What is Java Garbage Collection?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Garbage collection&lt;/em&gt;&lt;/strong&gt; refers to the process where Java automatically deletes objects that are no longer needed to free up memory, operating silently in the background.&lt;/p&gt;

&lt;p&gt;To understand how garbage collection works, it's important to know when an object becomes eligible for removal. In Java, an object is considered &lt;strong&gt;eligible for garbage collection&lt;/strong&gt; when it's no longer accessible to the program and therefore able to be garbage collected. This happens in two primary cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The object has &lt;strong&gt;no more references&lt;/strong&gt; pointing to it, indicating that no part of the code is actively using it.&lt;/li&gt;
&lt;li&gt;All references to the object have gone &lt;strong&gt;out of scope&lt;/strong&gt;, such as when the object was created inside a method that has finished executing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a id="two"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Suggesting Garbage Collection with &lt;code&gt;System.gc()&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Java includes a &lt;strong&gt;&lt;em&gt;built-in method&lt;/em&gt;&lt;/strong&gt; to help &lt;strong&gt;support garbage collection&lt;/strong&gt; that can be called at any time: &lt;code&gt;System.gc()&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="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;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gc&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;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; When you call &lt;code&gt;System.gc()&lt;/code&gt;, you are simply &lt;strong&gt;suggesting&lt;/strong&gt; to Java that it should &lt;strong&gt;perform garbage collection&lt;/strong&gt;. &lt;em&gt;It doesn’t guarantee that it will actually do this. The JVM may perform garbage collection at that moment, or it might be busy and choose not to. The JVM is &lt;strong&gt;free to ignore&lt;/strong&gt; the request.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The following example demonstrates this more concretely, by creating &lt;code&gt;Cat&lt;/code&gt; objects and manipulates references to them to demonstrate &lt;strong&gt;how garbage collection eligibility works&lt;/strong&gt; 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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&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;Cat&lt;/span&gt; &lt;span class="n"&gt;one&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;Cat&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 
      &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="n"&gt;two&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;Cat&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 
      &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="n"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
      &lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
      &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="n"&gt;four&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
      &lt;span class="n"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
      &lt;span class="n"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
      &lt;span class="n"&gt;two&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;Cat&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;gc&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;Initially, &lt;code&gt;Cat one&lt;/code&gt; and &lt;code&gt;Cat two&lt;/code&gt; are created, each referencing a &lt;strong&gt;new&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object. Assigning &lt;code&gt;Cat three = one&lt;/code&gt; makes &lt;strong&gt;both&lt;/strong&gt; &lt;code&gt;one&lt;/code&gt; and &lt;code&gt;three&lt;/code&gt; point to the &lt;strong&gt;same&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object. Setting &lt;code&gt;one = null&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; make that &lt;code&gt;Cat&lt;/code&gt; object eligible for garbage collection, because the variable &lt;code&gt;three&lt;/code&gt; &lt;strong&gt;still&lt;/strong&gt; holds an active reference. Assigning &lt;code&gt;Cat four = one&lt;/code&gt; assigns a &lt;code&gt;null&lt;/code&gt; reference and does not affect garbage collection eligibility. It's only when &lt;strong&gt;both&lt;/strong&gt; &lt;code&gt;one&lt;/code&gt; and &lt;code&gt;three&lt;/code&gt; are set to &lt;code&gt;null&lt;/code&gt; that the &lt;strong&gt;first&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object becomes eligible for garbage collection, because there are then &lt;strong&gt;no more active references&lt;/strong&gt; pointing to it. Similarly, when &lt;code&gt;two = null&lt;/code&gt; is executed, the &lt;strong&gt;second&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object becomes eligible, since &lt;code&gt;two&lt;/code&gt; was the &lt;strong&gt;only&lt;/strong&gt; reference to that object. The assignment where &lt;code&gt;two&lt;/code&gt; is a &lt;code&gt;new Cat()&lt;/code&gt; makes the third object collected when method scope is closed. Finally, the line &lt;code&gt;System.gc()&lt;/code&gt; &lt;strong&gt;suggests&lt;/strong&gt; to the JVM that it might be a good time to run the garbage collector. However, it &lt;strong&gt;doesn't guarantee&lt;/strong&gt; that garbage collection will actually occur. &lt;em&gt;Even if garbage collection runs, it simply reclaims the memory.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That is, in short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object becomes eligible for garbage collection after &lt;code&gt;three = null&lt;/code&gt; because no other variables reference it.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;second&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object becomes eligible for garbage collection after &lt;code&gt;two = null&lt;/code&gt; because no other variables reference it.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;third&lt;/strong&gt; &lt;code&gt;Cat&lt;/code&gt; object becomes eligible for garbage collection after the &lt;strong&gt;method ends&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The &lt;code&gt;System.gc()&lt;/code&gt; is just a &lt;strong&gt;request&lt;/strong&gt;, and the JVM may choose to &lt;strong&gt;ignore&lt;/strong&gt; it.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="three"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Why the Deprecated &lt;code&gt;finalize()&lt;/code&gt; Should Be Avoided
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;finalize()&lt;/code&gt; method in Java was part of the &lt;code&gt;Object&lt;/code&gt; class. This function can be confusing and difficult to use. In a few words, the &lt;strong&gt;garbage collector may call the &lt;code&gt;finalize()&lt;/code&gt; method once&lt;/strong&gt; — or not at all, depending on whether and when garbage collection occurs. If the garbage collector failed to collect an object and tried again later, there was no second call to &lt;code&gt;finalize()&lt;/code&gt;. In fact, it was used as a &lt;strong&gt;&lt;em&gt;finalisation&lt;/em&gt;&lt;/strong&gt; mechanism to perform certain actions before the object was destroyed by the garbage collector. However, since Java 9, this method has been &lt;strong&gt;deprecated&lt;/strong&gt; in &lt;code&gt;Object&lt;/code&gt;, as reported in the official documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The finalisation mechanism is inherently problematic.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is important to know that &lt;code&gt;finalize()&lt;/code&gt; could be executed &lt;strong&gt;zero&lt;/strong&gt; or &lt;strong&gt;once&lt;/strong&gt;. It could be executed twice.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;How the &lt;code&gt;finalize()&lt;/code&gt; method worked:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;finalize()&lt;/code&gt; method provided a last opportunity to clean up resources that the object hadn't released, such as closing open files or breaking database connections.&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;protected&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Perform cleanup, like releasing resources&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;finalize&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;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Problems with &lt;code&gt;finalize()&lt;/code&gt; method:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unreliability:&lt;/strong&gt; the method was not always called immediately after the object became unnecessary, which led to delays in freeing resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; this slowed down the garbage collector as it required additional work to be done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; the method could allow unsafe code to be executed while cleaning up objects, which created security risks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Alternatives:&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Instead of &lt;code&gt;finalize()&lt;/code&gt;, other &lt;strong&gt;&lt;em&gt;resource management techniques&lt;/em&gt;&lt;/strong&gt; are recommended, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;try-with-resources&lt;/code&gt; automatically closes resources after use, avoiding the need for garbage collection participation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Cleaner&lt;/code&gt; and &lt;code&gt;PhantomReference&lt;/code&gt; provide better control and flexibility for resource cleanup without the issues associated with &lt;code&gt;finalize()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="four"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Modern Resource Management Techniques
&lt;/h1&gt;

&lt;p&gt;Java includes the &lt;em&gt;&lt;strong&gt;try-with-resources&lt;/strong&gt;&lt;/em&gt; statement to automatically close all resources opened in a &lt;code&gt;try&lt;/code&gt; clause. This feature, known as &lt;strong&gt;automatic resource management&lt;/strong&gt;, ensures that resources are properly closed without requiring additional code in &lt;code&gt;finally&lt;/code&gt; blocks. This is especially useful for managing files, databases, and network connections, where you need to ensure that the resource is closed regardless of the success of the operation or exceptions.&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;void&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FileReader&lt;/span&gt; &lt;span class="n"&gt;file&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;FileReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&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="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&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;"An error: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&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;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; Only a &lt;strong&gt;&lt;em&gt;try-with-resources&lt;/em&gt;&lt;/strong&gt; statement is permitted to &lt;strong&gt;omit&lt;/strong&gt; both the &lt;code&gt;catch&lt;/code&gt; and &lt;code&gt;finally&lt;/code&gt; blocks. A traditional &lt;code&gt;try&lt;/code&gt; statement must have either or both.&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;void&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FileReader&lt;/span&gt; &lt;span class="n"&gt;file&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;FileReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&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="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;When using a &lt;em&gt;try-with-resources&lt;/em&gt; statement, you &lt;strong&gt;cannot use&lt;/strong&gt; just any class. Instead, Java requires that the classes you use must implement the &lt;code&gt;AutoCloseable&lt;/code&gt; &lt;strong&gt;interface&lt;/strong&gt;. When you use a &lt;em&gt;try-with-resources&lt;/em&gt; statement, the Java compiler automatically generates a &lt;strong&gt;hidden&lt;/strong&gt; &lt;code&gt;finally&lt;/code&gt; block behind the scenes. This &lt;strong&gt;implicit &lt;code&gt;finally&lt;/code&gt; block&lt;/strong&gt; is responsible for calling the &lt;code&gt;close()&lt;/code&gt; method on all resources declared in the &lt;em&gt;try-with-resources&lt;/em&gt; statement.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Cleaner&lt;/code&gt; and &lt;code&gt;PhantomReference&lt;/code&gt; mechanisms provide more flexible ways to &lt;strong&gt;perform cleanup actions&lt;/strong&gt; for objects that are no longer reachable, offering alternatives to the &lt;em&gt;deprecated&lt;/em&gt; &lt;code&gt;finalize()&lt;/code&gt; method.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;Cleaner&lt;/code&gt;&lt;/strong&gt; class was introduced in Java 9 as part of the &lt;code&gt;java.lang.ref&lt;/code&gt; package. It provides a mechanism to register cleanup actions that will be executed when an object becomes unreachable.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.lang.ref.Cleaner&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;Resource&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Cleaner&lt;/span&gt; &lt;span class="n"&gt;cleaner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cleaner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Cleaner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Cleanable&lt;/span&gt; &lt;span class="n"&gt;cleanable&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cleanable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cleaner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;register&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;CleanupAction&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;"Resource allocated."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CleanupAction&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&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;run&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;"Cleanup actions executed."&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="c1"&gt;// Other methods for the resource...&lt;/span&gt;
&lt;span class="c1"&gt;// If you need to explicitly clean up, you could provide a close method&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;close&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cleanable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clean&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;CleanerExample&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;Resource&lt;/span&gt; &lt;span class="n"&gt;resource&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;Resource&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Use the resource. The cleanup action will be called when the resource becomes unreachable&lt;/span&gt;
        &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Make the resource eligible for garbage collection&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;gc&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Suggest garbage collection&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;PhantomReference&lt;/code&gt;&lt;/strong&gt; provides a mechanism to detect when an object becomes unreachable and to perform cleanup actions before the memory is reclaimed — similar in purpose to &lt;code&gt;finalize()&lt;/code&gt;, but more reliable. The key feature of &lt;code&gt;PhantomReference&lt;/code&gt; is the use of a &lt;code&gt;ReferenceQueue&lt;/code&gt;. When an object becomes unreachable, it is automatically added to this queue.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.lang.ref.PhantomReference&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.lang.ref.ReferenceQueue&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;PhantomReferenceExample&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;ReferenceQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MyObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;queue&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;ReferenceQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;MyObject&lt;/span&gt; &lt;span class="n"&gt;obj&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;MyObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;PhantomReference&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MyObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;phantomRef&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;PhantomReference&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Make the object eligible for garbage collection&lt;/span&gt;
        &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;gc&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Suggest garbage collection&lt;/span&gt;

        &lt;span class="c1"&gt;// Check if phantom reference is added to the queue&lt;/span&gt;
        &lt;span class="nc"&gt;PhantomReference&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MyObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PhantomReference&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MyObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&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;"Phantom reference has been cleared."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;"Phantom reference not yet cleared."&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="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyObject&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Class implementation...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both approaches, &lt;code&gt;Cleaner&lt;/code&gt; and &lt;code&gt;PhantomReference&lt;/code&gt;, offer a more reliable and controlled way to manage resource cleanup compared to the &lt;code&gt;finalize()&lt;/code&gt; method.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Conclusion&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This article has provided an overview of how Java automatically frees up memory using the &lt;strong&gt;garbage collector&lt;/strong&gt;. Objects become &lt;em&gt;eligible&lt;/em&gt; for removal when they are no longer referenced or go out of scope. Local variables disappear once their block ends, instance fields become unreachable when the object itself is no longer accessible, and static variables live throughout the entire runtime of the program.&lt;br&gt;
Understanding how garbage collector works is essential for performance optimization, as &lt;strong&gt;unintentional memory leaks&lt;/strong&gt; can lead to &lt;code&gt;OutOfMemoryErrors&lt;/code&gt; and degraded application performance. Instead of relying on the deprecated &lt;code&gt;finalize()&lt;/code&gt; method, Java provides more efficient alternatives: &lt;strong&gt;&lt;em&gt;try-with-resources&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;Cleaner&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;PhantomReference&lt;/em&gt;&lt;/strong&gt; — to ensure timely cleanup of resources.&lt;/p&gt;




&lt;h4&gt;
  
  
  Thanks for reading! Hope you found some useful info in this article. Got any questions or ideas? Drop them in the comments below. Stay tuned here for a new Java series every week! And feel free to connect with me on &lt;a href="https://www.linkedin.com/in/marta-kravchuk-389b8079/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; 😊
&lt;/h4&gt;

</description>
      <category>java</category>
      <category>fundamentals</category>
      <category>garbagecollection</category>
    </item>
    <item>
      <title>Java Variables: A Complete Guide (feat. var Type Inference)</title>
      <dc:creator>Marta Kravchuk</dc:creator>
      <pubDate>Sat, 17 May 2025 07:00:00 +0000</pubDate>
      <link>https://dev.to/kmartita/java-variables-a-complete-guide-feat-var-type-inference-382k</link>
      <guid>https://dev.to/kmartita/java-variables-a-complete-guide-feat-var-type-inference-382k</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;This article provides a complete overview of Java variables, covering everything from basic concepts to modern features like local variable type inference &lt;code&gt;var&lt;/code&gt;.  It explores variable types, declaration, initialization, scope, lifetime, and naming conventions. Whether you're preparing for an interview or seeking a quick refresher, this guide provides essential insights.&lt;/em&gt;
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
What is a Variable?

&lt;ul&gt;
&lt;li&gt;Variable Types&lt;/li&gt;
&lt;li&gt;Declaring and Initializing Variables&lt;/li&gt;
&lt;li&gt;Naming Conventions and Legal Identifiers for Variables&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Variable Scope and Lifetime&lt;/li&gt;
&lt;li&gt;Local Variable Type Inference (var)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Java classes have two primary elements: &lt;strong&gt;&lt;em&gt;methods&lt;/em&gt;&lt;/strong&gt;, often called functions or procedures in other languages, and &lt;strong&gt;&lt;em&gt;fields&lt;/em&gt;&lt;/strong&gt;, more generally known as &lt;em&gt;variables&lt;/em&gt;. Together these are called the members of the class. Variables hold the state of the program, and methods operate on that state. Other building blocks include interfaces. So, variables are essentially data members within a class. &lt;/p&gt;

&lt;p&gt;&lt;a id="one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. What is a Variable?
&lt;/h1&gt;

&lt;p&gt;In Java, a &lt;strong&gt;&lt;em&gt;variable&lt;/em&gt;&lt;/strong&gt; is a named storage location that holds a value. Think of it as a container with a label (the variable name) where you can store data. Variables are essential for storing, retrieving, and manipulating information during program execution. Each variable has a specific &lt;em&gt;&lt;strong&gt;data type&lt;/strong&gt;&lt;/em&gt; (e.g., &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;) that determines the kind of values it can hold. Variables are the building blocks for storing and processing information.&lt;/p&gt;

&lt;p&gt;&lt;a id="two"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Variable Types&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;In Java, variables are categorized into two main types: &lt;strong&gt;&lt;em&gt;primitive&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;reference&lt;/em&gt;&lt;/strong&gt; types. For a detailed explanation of these types, refer to previous &lt;a href="https://dev.to/kmartita/java-data-types-a-quick-reference-guide-4oki"&gt;article&lt;/a&gt;. There you will find a brief overview of each type and their key differences.&lt;/p&gt;

&lt;p&gt;&lt;a id="three"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Declaring and Initializing Variables&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;When you &lt;strong&gt;&lt;em&gt;declare&lt;/em&gt;&lt;/strong&gt; a variable, you associate its name with a specific data type.&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="n"&gt;name&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;number&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a variable's declaration, it can be assigned a value, a process known as &lt;strong&gt;&lt;em&gt;initialization&lt;/em&gt;&lt;/strong&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The Best Zoo"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below, the previous &lt;em&gt;declarations&lt;/em&gt; and &lt;em&gt;initializations&lt;/em&gt; are &lt;strong&gt;merged&lt;/strong&gt; into concise 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="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="s"&gt;"The Best Zoo"&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;Declaring Multiple Variables:&lt;/strong&gt; you can declare many variables in the same declaration as long as they are all of the &lt;strong&gt;same type&lt;/strong&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;void&lt;/span&gt; &lt;span class="nf"&gt;sandFence&lt;/span&gt;&lt;span class="o"&gt;()&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;s1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// declared but not yet initialized&lt;/span&gt;
   &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yes"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"no"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// initialized variables&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;paintFence&lt;/span&gt;&lt;span class="o"&gt;()&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;i1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// variable 'i3' is initialized&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are &lt;strong&gt;three&lt;/strong&gt; variables: &lt;code&gt;i1&lt;/code&gt;, &lt;code&gt;i2&lt;/code&gt; (are declared), and &lt;code&gt;i3&lt;/code&gt; (is initialized).&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;num&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;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code &lt;strong&gt;doesn’t&lt;/strong&gt; compile because it tries to declare multiple variables of different types in the same statement.&lt;/p&gt;

&lt;p&gt;Here are a few ways to summarize those variable declaration examples, presented in a more informative style:&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;i1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// all declared; 'i3' is initialized&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&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;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// DOES NOT COMPILE: different types&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b2&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;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;            
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i1&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;i2&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;i3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i4&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="four"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Naming Conventions and Legal Identifiers for Variables&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;&lt;em&gt;identifier&lt;/em&gt;&lt;/strong&gt; is the name of a variable, method, class, interface, or package.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;identifiers may &lt;strong&gt;begin&lt;/strong&gt; with a &lt;u&gt;letter&lt;/u&gt;, a &lt;code&gt;$&lt;/code&gt; symbol, or a &lt;code&gt;_&lt;/code&gt; symbol. &lt;/li&gt;
&lt;li&gt;identifiers can &lt;strong&gt;include&lt;/strong&gt; &lt;u&gt;numbers&lt;/u&gt; but &lt;strong&gt;not start&lt;/strong&gt; with them.&lt;/li&gt;
&lt;li&gt;since Java 9, a &lt;strong&gt;single&lt;/strong&gt; &lt;u&gt;underscore&lt;/u&gt; &lt;code&gt;_&lt;/code&gt; is &lt;strong&gt;not allowed&lt;/strong&gt; as an identifier.&lt;/li&gt;
&lt;li&gt;you &lt;strong&gt;cannot use&lt;/strong&gt; the same name as a &lt;strong&gt;Java-reserved&lt;/strong&gt; word. &lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;first&lt;/strong&gt; character is &lt;strong&gt;not allowed&lt;/strong&gt; to be a &lt;u&gt;number&lt;/u&gt;, and reserved words are not allowed.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A &lt;strong&gt;reserved word&lt;/strong&gt; is a special keyword that Java uses for specific purposes, so you are not allowed to use it. Remember that Java is case-sensitive, so you can use versions of the keywords that only differ in case.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;abstract&lt;/td&gt;
&lt;td&gt;assert&lt;/td&gt;
&lt;td&gt;boolean&lt;/td&gt;
&lt;td&gt;break&lt;/td&gt;
&lt;td&gt;byte&lt;/td&gt;
&lt;td&gt;case&lt;/td&gt;
&lt;td&gt;catch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;char&lt;/td&gt;
&lt;td&gt;class&lt;/td&gt;
&lt;td&gt;&lt;b&gt;const&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;continue&lt;/td&gt;
&lt;td&gt;default&lt;/td&gt;
&lt;td&gt;do&lt;/td&gt;
&lt;td&gt;double&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;else&lt;/td&gt;
&lt;td&gt;enum&lt;/td&gt;
&lt;td&gt;extends&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;td&gt;final&lt;/td&gt;
&lt;td&gt;finally&lt;/td&gt;
&lt;td&gt;float&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;for&lt;/td&gt;
&lt;td&gt;&lt;b&gt;goto&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;if&lt;/td&gt;
&lt;td&gt;import&lt;/td&gt;
&lt;td&gt;instanceof&lt;/td&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;interface&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long&lt;/td&gt;
&lt;td&gt;implements&lt;/td&gt;
&lt;td&gt;native&lt;/td&gt;
&lt;td&gt;new&lt;/td&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;td&gt;package&lt;/td&gt;
&lt;td&gt;private&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;protected&lt;/td&gt;
&lt;td&gt;public&lt;/td&gt;
&lt;td&gt;return&lt;/td&gt;
&lt;td&gt;short&lt;/td&gt;
&lt;td&gt;static&lt;/td&gt;
&lt;td&gt;strictfp&lt;/td&gt;
&lt;td&gt;super&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;switch&lt;/td&gt;
&lt;td&gt;synchronized&lt;/td&gt;
&lt;td&gt;this&lt;/td&gt;
&lt;td&gt;throw&lt;/td&gt;
&lt;td&gt;throws&lt;/td&gt;
&lt;td&gt;transient&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;try&lt;/td&gt;
&lt;td&gt;void&lt;/td&gt;
&lt;td&gt;volatile&lt;/td&gt;
&lt;td&gt;while&lt;/td&gt;
&lt;td colspan="2"&gt;_ (underscore)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;❗&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; In this table, &lt;strong&gt;bold&lt;/strong&gt; text indicates reserved keywords (like &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;goto&lt;/code&gt;) that are not currently used in Java but are still restricted to avoid potential conflicts with other languages. And highlighted terms (like &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, and &lt;code&gt;null&lt;/code&gt;) aren’t actually reserved words, but literal values, which are also prohibited as identifiers and are listed here for clarity.&lt;/p&gt;

&lt;p&gt;To solidify understanding of identifier rules, consider these examples:&lt;br&gt;
&lt;strong&gt;Legal Identifiers:&lt;/strong&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;long&lt;/span&gt; &lt;span class="n"&gt;okidentifier&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;$OK2Identifier&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;_alsoOK1d3ntifi3r&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;__SStillOkbutKnotsonice&lt;/span&gt;&lt;span class="err"&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;Illegal Identifiers:&lt;/strong&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="mi"&gt;3&lt;/span&gt;&lt;span class="nc"&gt;DPointClass&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// identifiers cannot begin with a number&lt;/span&gt;
&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;hollywood&lt;/span&gt;&lt;span class="nd"&gt;@vine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// @ is not a letter, digit, $ or _&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;$coffee&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// * is not a letter, digit, $ or _&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// public is a reserved word&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// a single underscore is not allowed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beyond the rules for legal identifiers, following consistent naming conventions is crucial for code readability and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Style&lt;/em&gt;:&lt;/strong&gt; &lt;code&gt;camelCase&lt;/code&gt;&lt;br&gt;
While Java technically allows different styles, the development community largely adheres to &lt;em&gt;camelCase&lt;/em&gt;, the preferred style for most Java identifiers. In &lt;em&gt;camelCase&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;method&lt;/strong&gt; and &lt;strong&gt;variable&lt;/strong&gt; names are written with the first letter being lowercase, capitalizing subsequent words (e.g., &lt;code&gt;calculateTotal&lt;/code&gt;, &lt;code&gt;userFirstName&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;class&lt;/strong&gt; and &lt;strong&gt;interface&lt;/strong&gt; names are written with the first letter being uppercase, capitalizing subsequent words (e.g., &lt;code&gt;CustomerService&lt;/code&gt;, &lt;code&gt;Printable&lt;/code&gt;). &lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; Avoid starting class names with &lt;code&gt;$&lt;/code&gt;, as the compiler uses this symbol for internal files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Style&lt;/em&gt;:&lt;/strong&gt; &lt;code&gt;snake_case&lt;/code&gt;&lt;br&gt;
While syntactically valid, &lt;em&gt;snake_case&lt;/em&gt; is generally discouraged for variable and method names in Java. However, there are some common exceptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;static&lt;/code&gt; &lt;code&gt;final&lt;/code&gt; &lt;strong&gt;constants&lt;/strong&gt; are commonly written in uppercase &lt;em&gt;snake_case&lt;/em&gt; (e.g., &lt;code&gt;THIS_IS_A_CONSTANT&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;enum&lt;/strong&gt; &lt;strong&gt;values&lt;/strong&gt; tend to be written in uppercase &lt;em&gt;snake_case&lt;/em&gt; for consistency and readability, as in &lt;code&gt;Color.RED&lt;/code&gt;, &lt;code&gt;Color.DARK_GRAY&lt;/code&gt;, and so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="five"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  2. Variable Scope and Lifetime
&lt;/h1&gt;

&lt;p&gt;In Java, variables have a scope that determines where they can be accessed in your code, and a lifetime that dictates how long they exist. Java defines three primary types of variables based on these characteristics: &lt;strong&gt;class&lt;/strong&gt; variables, &lt;strong&gt;instance&lt;/strong&gt; variables, and &lt;strong&gt;local&lt;/strong&gt; variables.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;2.1. Class Variables (Static Fields)&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;You can define a &lt;em&gt;static field&lt;/em&gt; using the &lt;code&gt;static&lt;/code&gt; keyword. You do not need an instance of the class to access &lt;em&gt;static fields&lt;/em&gt;. You can access static class fields by writing the class name before the field. There is only one copy of a &lt;em&gt;static variable&lt;/em&gt;, no matter how many instances of the class there are.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;2.2. Instance Variables (Non-Static Fields)&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Instance variables&lt;/em&gt; in Java are variables declared within a class &lt;strong&gt;without&lt;/strong&gt; the &lt;code&gt;static&lt;/code&gt; keyword, outside of any method. Such a variable is associated with a specific instance of a class, and the value of the variable in one instance does not affect the value of the same variable in another instance of this class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default initialization values by type:&lt;/strong&gt; &lt;em&gt;Class&lt;/em&gt; and &lt;em&gt;Instance&lt;/em&gt; variables &lt;strong&gt;do not require initialization&lt;/strong&gt;, they are automatically initialized to default values. As soon as you declare them, they receive default values: &lt;code&gt;null&lt;/code&gt; for &lt;strong&gt;objects&lt;/strong&gt; and &lt;code&gt;0&lt;/code&gt; / &lt;code&gt;false&lt;/code&gt; for &lt;strong&gt;primitives&lt;/strong&gt;. &lt;br&gt;
&lt;strong&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/strong&gt; -&amp;gt; &lt;code&gt;false&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;byte&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;short&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;int&lt;/code&gt;&lt;/strong&gt; -&amp;gt; &lt;code&gt;0&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;long&lt;/code&gt;&lt;/strong&gt; -&amp;gt; &lt;code&gt;0L&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;double&lt;/code&gt;&lt;/strong&gt; -&amp;gt; &lt;code&gt;0.0&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;float&lt;/code&gt;&lt;/strong&gt; -&amp;gt; &lt;code&gt;0.0f&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;char&lt;/code&gt;&lt;/strong&gt; -&amp;gt; &lt;code&gt;'\u0000'&lt;/code&gt; (NULL)&lt;br&gt;
All object references (everything else) -&amp;gt; &lt;code&gt;null&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;&lt;em&gt;2.3. Local Variables&lt;/em&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This is a variable defined within a method (also method parameters or constructor parameters) and &lt;strong&gt;cannot be used outside the method&lt;/strong&gt;. Local variables &lt;strong&gt;must be initialized before use&lt;/strong&gt; (it’s ok to have an uninitialized local variable as long as you never use it). They do not have default values and contain garbage data until initialized.&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;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;findAnswer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;)&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;answer&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;onlyOneBranch&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;onlyOneBranch&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="n"&gt;answer&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="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;answer&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="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="n"&gt;answer&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="n"&gt;onlyOneBranch&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the method, the variable &lt;code&gt;answer&lt;/code&gt; is guaranteed to be initialized before it is used, so the code compiles successfully. However, the variable &lt;code&gt;onlyOneBranch&lt;/code&gt; is only initialized if the check is &lt;code&gt;true&lt;/code&gt;. If a check is &lt;strong&gt;&lt;code&gt;false&lt;/code&gt;&lt;/strong&gt;, &lt;code&gt;onlyOneBranch&lt;/code&gt; remains uninitialized, leading to a &lt;strong&gt;compile error&lt;/strong&gt; when trying to print it. So, the compiler knows the check can also be &lt;code&gt;false&lt;/code&gt;, resulting in uninitialized code and giving a compiler error.&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;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;findAnswer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;check&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;checkAnswer&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;findAnswer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;                     &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The call to &lt;code&gt;findAnswer()&lt;/code&gt; does not compile because it tries to use a &lt;strong&gt;local variable&lt;/strong&gt; that is &lt;strong&gt;not initialized&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The scope and lifetime of a Java variable are intrinsically linked to its type and where it is declared.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Local variable:&lt;/strong&gt;&lt;/em&gt; scope extends from the point of declaration to the end of the enclosing block.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Instance variable:&lt;/em&gt;&lt;/strong&gt; available from the point of declaration until the object is garbage collected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Class variable:&lt;/em&gt;&lt;/strong&gt; available from the moment of declaration until the program terminates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="six"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Local Variable Type Inference (var)
&lt;/h1&gt;

&lt;p&gt;Starting in Java 10, you can use the keyword &lt;code&gt;var&lt;/code&gt; instead of the type for local variables under certain conditions. To use this feature, you just type &lt;code&gt;var&lt;/code&gt; instead of the &lt;strong&gt;primitive&lt;/strong&gt; or &lt;strong&gt;reference&lt;/strong&gt; type.&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;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;whatTypeAmI&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;size&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The formal name of this feature is &lt;strong&gt;local variable type inference&lt;/strong&gt;. This means that it is used only as a &lt;strong&gt;local&lt;/strong&gt; variable. It &lt;strong&gt;cannot be used&lt;/strong&gt; as a &lt;code&gt;static var&lt;/code&gt; (class or local):&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VarKeyword&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tricky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// DOES NOT COMPILE: instance var&lt;/span&gt;
   &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"String"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// DOES NOT COMPILE: class var&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;whatTypeAmI&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;size&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="c1"&gt;// DOES NOT COMPILE: static local&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;In Java, &lt;code&gt;var&lt;/code&gt; is still a specific type defined at compile time. It &lt;strong&gt;does not change type at runtime&lt;/strong&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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reassignment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;number&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="n"&gt;number&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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"five"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// DOES NOT COMPILE     &lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To better understand how &lt;code&gt;var&lt;/code&gt; works and where it can be used, let's look at some practical code snippets.&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;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&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="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&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="n"&gt;b&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="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the types declared on a &lt;strong&gt;single line&lt;/strong&gt; must be the &lt;strong&gt;same type&lt;/strong&gt; and share the same declaration. Java &lt;strong&gt;does not&lt;/strong&gt; allow &lt;code&gt;var&lt;/code&gt; in &lt;strong&gt;multiple variable declarations&lt;/strong&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;var&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                  &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler is prompted to define the &lt;code&gt;null&lt;/code&gt; type. It can be any type of reference. The only choice the compiler can make is an &lt;code&gt;Object&lt;/code&gt;. Java developers have decided that it is better &lt;strong&gt;not to allow&lt;/strong&gt; &lt;code&gt;var&lt;/code&gt; for &lt;code&gt;null&lt;/code&gt; than to guess about intentions.&lt;/p&gt;

&lt;p&gt;While a &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;cannot be&lt;/strong&gt; initialized with a &lt;code&gt;null&lt;/code&gt; value without a type, it can be assigned a &lt;code&gt;null&lt;/code&gt; value &lt;strong&gt;after it is declared&lt;/strong&gt;, provided that the underlying data type of the &lt;code&gt;var&lt;/code&gt; is an &lt;strong&gt;object&lt;/strong&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;var&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"myData"&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="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;m&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="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// DOES NOT COMPILE: primitives are not assigned a null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;n&lt;/code&gt; is of type &lt;code&gt;String&lt;/code&gt;, which is an object that compiles without issue. But since the type of &lt;code&gt;m&lt;/code&gt; is a &lt;strong&gt;primitive&lt;/strong&gt; &lt;code&gt;int&lt;/code&gt;, it &lt;strong&gt;cannot be assigned&lt;/strong&gt; a &lt;code&gt;null&lt;/code&gt; value that &lt;strong&gt;does not compile&lt;/strong&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;var&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&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="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By the way, a &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;can be initialized&lt;/strong&gt; to a &lt;code&gt;null&lt;/code&gt; value if the &lt;strong&gt;type is specified&lt;/strong&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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;addition&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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;Also, it &lt;strong&gt;can’t be used as method parameters&lt;/strong&gt;. Since, the &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are method parameters. These are not local variables.&lt;/p&gt;

&lt;p&gt;While &lt;code&gt;var&lt;/code&gt; is &lt;strong&gt;not a reserved word&lt;/strong&gt; and is allowed to be used as an identifier, it is considered a reserved type name. Naming a local variable &lt;strong&gt;var&lt;/strong&gt; is legal. The following examples illustrate this:&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="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;var&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;Var&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;var&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"var"&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;Var&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;Var&lt;/span&gt; &lt;span class="kt"&gt;var&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;Var&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;A &lt;strong&gt;reserved&lt;/strong&gt; type name means it &lt;strong&gt;cannot be used&lt;/strong&gt; to define a type, such as a &lt;strong&gt;class&lt;/strong&gt;, &lt;strong&gt;interface&lt;/strong&gt;, or &lt;strong&gt;enum&lt;/strong&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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;var&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;     &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;var&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Summarizing the use of &lt;code&gt;var&lt;/code&gt;, let's consider the key rules.&lt;br&gt;
&lt;strong&gt;What you can do with &lt;code&gt;var&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; can be used as a &lt;strong&gt;local variable&lt;/strong&gt; in a constructor, method, or initializer block.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;value&lt;/strong&gt; of a &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;can change&lt;/strong&gt;, but the &lt;strong&gt;type cannot&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What you cannot do with &lt;code&gt;var&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;cannot be used&lt;/strong&gt; as a &lt;strong&gt;parameter&lt;/strong&gt; in a constructor and method, &lt;strong&gt;instance variables&lt;/strong&gt;, or &lt;strong&gt;class variables&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; must always &lt;strong&gt;be initialized&lt;/strong&gt; on the same line (or statement) where it is declared.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;cannot be initialized&lt;/strong&gt; with a &lt;code&gt;null&lt;/code&gt; value without a type.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; permitted in a &lt;strong&gt;multiple-variable&lt;/strong&gt; declaration.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; is a reserved type name but &lt;strong&gt;not a reserved word&lt;/strong&gt;, meaning it &lt;strong&gt;can be used&lt;/strong&gt; as an &lt;strong&gt;identifier&lt;/strong&gt; except as a class, interface, or enum name.&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  Thanks for reading! Hope you found some useful info in this article. Got any questions or ideas? Drop them in the comments below. Stay tuned here for a new Java series every week! And feel free to connect with me on &lt;a href="https://www.linkedin.com/in/marta-kravchuk-389b8079/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; 😊
&lt;/h4&gt;

</description>
      <category>java</category>
      <category>java10</category>
      <category>fundamentals</category>
      <category>variables</category>
    </item>
    <item>
      <title>Java Data Types: A Quick Reference Guide</title>
      <dc:creator>Marta Kravchuk</dc:creator>
      <pubDate>Fri, 16 May 2025 07:00:00 +0000</pubDate>
      <link>https://dev.to/kmartita/java-data-types-a-quick-reference-guide-4oki</link>
      <guid>https://dev.to/kmartita/java-data-types-a-quick-reference-guide-4oki</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;This article offers concise technical Java notes, covering primitive and reference types, literals, type conversion rules, and more. Whether you're preparing for an interview or seeking a quick refresher, this guide provides essential insights.&lt;/em&gt;
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
Primitive Types

&lt;ul&gt;
&lt;li&gt;Literals&lt;/li&gt;
&lt;li&gt;Declaring Primitive Types&lt;/li&gt;
&lt;li&gt;Type Conversion Rules&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Reference Types&lt;/li&gt;
&lt;li&gt;Wrapper Classes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Java contains two types of data: &lt;em&gt;&lt;strong&gt;primitive&lt;/strong&gt;&lt;/em&gt; types and &lt;em&gt;&lt;strong&gt;reference&lt;/strong&gt;&lt;/em&gt; types.&lt;/p&gt;

&lt;p&gt;&lt;a id="one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Primitive Types
&lt;/h1&gt;

&lt;p&gt;Java has 8 built-in data types, referred to as the Java primitive types. A &lt;strong&gt;&lt;em&gt;primitive&lt;/em&gt;&lt;/strong&gt; is &lt;strong&gt;not&lt;/strong&gt; an &lt;strong&gt;object&lt;/strong&gt; in Java and does not represent an object. A primitive is simply a single value in memory, such as a number or a character.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Keyword&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Wrapper class&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Boolean&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;8-bit integral&lt;/td&gt;
&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Byte&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;short&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;16-bit&lt;/td&gt;
&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Short&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;32-bit&lt;/td&gt;
&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Integer&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;64-bit&lt;/td&gt;
&lt;td&gt;&lt;code&gt;123L&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Long&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;float&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;32-bit floating-point&lt;/td&gt;
&lt;td&gt;&lt;code&gt;123.45f&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Float&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;double&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;64-bit floating-point&lt;/td&gt;
&lt;td&gt;&lt;code&gt;123.45&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Double&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;char&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;16-bit Unicode&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'a'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Character&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The data value of a primitive data type is stored in memory and is not a location reference of the data. This makes accessing primitive data types faster and leads to more efficient performance when it really matters. &lt;/p&gt;

&lt;p&gt;Each primitive type has a corresponding &lt;strong&gt;wrapper class&lt;/strong&gt;, which is an object type.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Differences:&lt;/strong&gt; &lt;code&gt;short&lt;/code&gt; vs. &lt;code&gt;char&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;The primary difference is that &lt;code&gt;short&lt;/code&gt; is &lt;strong&gt;signed&lt;/strong&gt; integer type (can represent both &lt;em&gt;positive&lt;/em&gt; and &lt;em&gt;negative&lt;/em&gt; values). Alternatively, &lt;code&gt;char&lt;/code&gt; is &lt;strong&gt;unsigned&lt;/strong&gt; character type (can represent only &lt;em&gt;positive including 0&lt;/em&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;short&lt;/span&gt; &lt;span class="n"&gt;bird&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'d'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// 'bird' is equal to 100 (the Unicode value of 'd')&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;mammal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;short&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;83&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 'mammal' is equal to 'S' (the Unicode character with value 83)&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;negative&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;100&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;Note:&lt;/strong&gt; If you try to set a value outside the range of &lt;code&gt;short&lt;/code&gt; or &lt;code&gt;char&lt;/code&gt;, the compiler will report an &lt;strong&gt;error&lt;/strong&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;short&lt;/span&gt; &lt;span class="n"&gt;reptile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;65535&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;fish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;short&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="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="three"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Literals&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;When a &lt;strong&gt;numeric primitive&lt;/strong&gt; is present in the code, it is called a &lt;strong&gt;&lt;em&gt;literal&lt;/em&gt;&lt;/strong&gt;. And you can use underscores in numbers to make them easier to read:&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;million1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000&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;million2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&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;Note:&lt;/strong&gt; Underscores &lt;strong&gt;cannot be&lt;/strong&gt; placed at the beginning or end of a literal, or immediately before/after a decimal point.&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;double&lt;/span&gt; &lt;span class="n"&gt;notAtStart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_1000&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mo"&gt;00&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;notAtEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1000.00&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;notByDecimal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mo"&gt;00&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;

&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;annoyingButLegal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1_00_0.0_0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Ugly, but works&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;reallyUgly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1__________2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// Also compiles&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="four"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;A declaration consists of the &lt;strong&gt;data type&lt;/strong&gt; and &lt;strong&gt;variable&lt;/strong&gt; name(s). A variable name must be a valid identifier. You can identify multiple variables of the same type on a single line, but &lt;strong&gt;not&lt;/strong&gt; multiple variables of different 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="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="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&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;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Declaring&lt;/strong&gt; &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;When declaring &lt;code&gt;float&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt; variables with decimal literals, remember that &lt;strong&gt;&lt;em&gt;decimal literals&lt;/em&gt;&lt;/strong&gt; are treated as &lt;code&gt;double&lt;/code&gt; by default. To define a &lt;code&gt;float&lt;/code&gt;, use the &lt;code&gt;'f'&lt;/code&gt; suffix.&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;double&lt;/span&gt; &lt;span class="n"&gt;decimalValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Valid double declaration&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;floatValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// 'f' is required for float&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="five"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Type Conversion Rules&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Java supports both &lt;strong&gt;widening&lt;/strong&gt; (&lt;em&gt;implicit&lt;/em&gt;) and &lt;strong&gt;narrowing&lt;/strong&gt; (&lt;em&gt;explicit&lt;/em&gt;) conversions between data types. They increase type safety, maintain code clarity, control data loss, and optimize performance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Widening Conversion&lt;/em&gt;&lt;/strong&gt;: occurs when a &lt;em&gt;smaller&lt;/em&gt; data type is converted to a &lt;em&gt;larger&lt;/em&gt; data type, allowing expanding the range of values without losing information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Java's &lt;strong&gt;primitive types&lt;/strong&gt; can be converted in the following order:&lt;br&gt;
&lt;code&gt;byte&lt;/code&gt; -&amp;gt; &lt;code&gt;short&lt;/code&gt; -&amp;gt; &lt;code&gt;int&lt;/code&gt; -&amp;gt; &lt;code&gt;long&lt;/code&gt; -&amp;gt; &lt;code&gt;float&lt;/code&gt; -&amp;gt; &lt;code&gt;double&lt;/code&gt;&lt;br&gt;
&lt;code&gt;char&lt;/code&gt; -&amp;gt; &lt;code&gt;int&lt;/code&gt; -&amp;gt; &lt;code&gt;long&lt;/code&gt; -&amp;gt; &lt;code&gt;float&lt;/code&gt; -&amp;gt; &lt;code&gt;double&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;float&lt;/span&gt; &lt;span class="n"&gt;myFloat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000L&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;myDouble&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000L&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;strong&gt;&lt;em&gt;Narrowing Conversion&lt;/em&gt;&lt;/strong&gt;: occurs when a &lt;em&gt;larger&lt;/em&gt; data type is converted to a &lt;em&gt;smaller&lt;/em&gt; data type. This can potentially lead to loss of information or truncation of data and, therefore, requires &lt;em&gt;explicit&lt;/em&gt; &lt;strong&gt;casting&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;myChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;1000L&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;myByte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;1000L&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;myShort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;short&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;1000L&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;myInt&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;1000L&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="six"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Reference Types
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;&lt;em&gt;reference&lt;/em&gt;&lt;/strong&gt; type refers to an &lt;em&gt;object&lt;/em&gt; (an instance of a class). A value is assigned to a reference in one of two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A reference can be assigned to another object of the same or compatible type.&lt;/li&gt;
&lt;li&gt;A reference can be assigned to a new object using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;An &lt;strong&gt;object&lt;/strong&gt; is a specific instance of a class that exists in memory while the program is running. An object is also referred to as an instance of the class. A variable that points to an object is called a reference. To create an instance of a class, you have to write &lt;code&gt;new&lt;/code&gt; before the class name and add parentheses after it.&lt;/em&gt;&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Park&lt;/span&gt; &lt;span class="n"&gt;p&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;Park&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;First, you declare the type you’ll create &lt;code&gt;Park&lt;/code&gt; and give the variable a name &lt;code&gt;p&lt;/code&gt;. This gives Java a place to store a reference to the object. Then you write &lt;code&gt;new Park()&lt;/code&gt; to create the object.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Differences:&lt;/strong&gt; Primitives vs. References
&lt;/h4&gt;

&lt;p&gt;There are a few important differences between &lt;em&gt;primitives&lt;/em&gt; and &lt;em&gt;reference&lt;/em&gt; types. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;em&gt;reference&lt;/em&gt;&lt;/strong&gt; types &lt;strong&gt;can be assigned&lt;/strong&gt; &lt;code&gt;null&lt;/code&gt;, which means they do not currently refer to an object. But instead, &lt;strong&gt;&lt;em&gt;primitive&lt;/em&gt;&lt;/strong&gt; types will give you a compiler &lt;strong&gt;error&lt;/strong&gt; if you attempt to assign them &lt;code&gt;null&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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 &lt;strong&gt;&lt;em&gt;reference&lt;/em&gt;&lt;/strong&gt; types &lt;strong&gt;can be used&lt;/strong&gt; to &lt;em&gt;call methods&lt;/em&gt;, assuming the reference is &lt;strong&gt;not&lt;/strong&gt; &lt;code&gt;null&lt;/code&gt;. And, &lt;strong&gt;&lt;em&gt;primitives&lt;/em&gt;&lt;/strong&gt; do not have methods declared on them.
&lt;/li&gt;
&lt;/ul&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="n"&gt;reference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&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;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reference&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;len&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="c1"&gt;// DOES NOT COMPILE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="seven"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Wrapper Classes
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Wrapper classes&lt;/em&gt;&lt;/strong&gt; encapsulate &lt;em&gt;primitive types&lt;/em&gt; into &lt;em&gt;objects&lt;/em&gt;. They offer methods for converting between primitives and objects.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Common Methods&lt;/strong&gt;:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;parse()&lt;/code&gt;: used for converting &lt;strong&gt;objects&lt;/strong&gt; back &lt;strong&gt;to primitives&lt;/strong&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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// String to int&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseDouble&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3.14"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// String to double&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;valueOf()&lt;/code&gt;: &lt;strong&gt;returns&lt;/strong&gt; an &lt;strong&gt;instance&lt;/strong&gt; of the corresponding &lt;strong&gt;wrapper class&lt;/strong&gt;. If the value is a primitive, it is converted to the appropriate wrapper object.&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;Integer&lt;/span&gt; &lt;span class="n"&gt;wrappedInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&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="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;wrappedDouble&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&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;&lt;code&gt;MIN_VALUE&lt;/code&gt; and &lt;code&gt;MAX_VALUE&lt;/code&gt;: &lt;strong&gt;constants&lt;/strong&gt; representing the smallest and largest possible values of that primitive type.&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;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MIN_VALUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// -2147483648&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MAX_VALUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 2147483647&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Autoboxing&lt;/strong&gt; and &lt;strong&gt;Unboxing&lt;/strong&gt;:
&lt;/h4&gt;

&lt;p&gt;With &lt;strong&gt;&lt;em&gt;autoboxing&lt;/em&gt;&lt;/strong&gt;, the compiler automatically converts a primitive to the corresponding wrapper. Unsurprisingly, &lt;strong&gt;&lt;em&gt;unboxing&lt;/em&gt;&lt;/strong&gt; is the process in which the compiler automatically converts a wrapper class back to a primitive.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Primitive type&lt;/th&gt;
&lt;th&gt;Wrapper class&lt;/th&gt;
&lt;th&gt;Example of initializing&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Boolean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Boolean.valueOf(true)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Byte.valueOf((byte) 1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;short&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Short&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Short.valueOf((short) 1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Integer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Integer.valueOf(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Long.valueOf(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;float&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Float&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Float.valueOf((float) 1.0)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;double&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Double&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Double.valueOf(1.0)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;char&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Character&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Character.valueOf('c’)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;pounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an example of autoboxing as the &lt;code&gt;int&lt;/code&gt; primitive is autoboxed into an &lt;code&gt;Integer&lt;/code&gt; object.&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;Character&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"robot"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charAt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line demonstrates that autoboxing can involve methods. The &lt;code&gt;charAt()&lt;/code&gt; method returns a primitive &lt;code&gt;char&lt;/code&gt;. It is then autoboxed into the wrapper object &lt;code&gt;Character&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;char&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, this line shows an example of &lt;strong&gt;unboxing&lt;/strong&gt;. The &lt;code&gt;Character&lt;/code&gt; object is unboxed into a primitive &lt;code&gt;char&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;❗&lt;strong&gt;Note:&lt;/strong&gt; There are two tricks in the space of autoboxes and unboxes. The first is related to &lt;code&gt;null&lt;/code&gt; values. This harmless-looking code throws &lt;strong&gt;exceptions&lt;/strong&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;var&lt;/span&gt; &lt;span class="n"&gt;heights&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;heights&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;heights&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// NullPointerException&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding &lt;code&gt;null&lt;/code&gt; to a list is &lt;strong&gt;legal&lt;/strong&gt;, as a &lt;code&gt;null&lt;/code&gt; reference can be assigned to any reference variable. However, when attempting to unbox that &lt;code&gt;null&lt;/code&gt; to an &lt;code&gt;int&lt;/code&gt; primitive, a problem arises. Java tries to get the &lt;code&gt;int&lt;/code&gt; value of &lt;code&gt;null&lt;/code&gt;, but since any method call on &lt;code&gt;null&lt;/code&gt; results in a &lt;code&gt;NullPointerException&lt;/code&gt;, that is precisely what occurs. Therefore, caution is advised when dealing with &lt;code&gt;null&lt;/code&gt; in relation to autoboxing.&lt;/p&gt;

&lt;p&gt;❗&lt;strong&gt;Note:&lt;/strong&gt; And the second one, be careful using autoboxing with &lt;code&gt;Integer&lt;/code&gt; in collections, as it can affect the behavior of methods like &lt;code&gt;remove()&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="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&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="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&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="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&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;// the list 'numbers' contains the elements [1, 3, 5]&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&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="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&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="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="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// [1]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;remove()&lt;/code&gt; method has two overloaded versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;remove(int index)&lt;/code&gt; removes the element at the specified index.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;remove(Object obj)&lt;/code&gt; removes the first occurrence of the object &lt;code&gt;obj&lt;/code&gt; from the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since &lt;code&gt;numbers.remove(1)&lt;/code&gt; receives an &lt;code&gt;int&lt;/code&gt;, Java invokes &lt;code&gt;remove(int index)&lt;/code&gt;, deleting the element at index &lt;code&gt;1&lt;/code&gt; (this is a value &lt;strong&gt;3&lt;/strong&gt;). The list then becomes &lt;code&gt;[1, 5]&lt;/code&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  Thanks for reading! Hope you found some useful info in this article. Got any questions or ideas? Drop them in the comments below. More cool Java stuff is coming soon!
&lt;/h4&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>fundamentals</category>
      <category>datatypes</category>
    </item>
    <item>
      <title>The Path to OCP Java SE 11 Certification (1Z0-819): What It Takes and How I Did It</title>
      <dc:creator>Marta Kravchuk</dc:creator>
      <pubDate>Sun, 11 May 2025 18:23:18 +0000</pubDate>
      <link>https://dev.to/kmartita/the-path-to-ocp-java-se-11-certification-1z0-819-what-it-takes-and-how-i-did-it-546p</link>
      <guid>https://dev.to/kmartita/the-path-to-ocp-java-se-11-certification-1z0-819-what-it-takes-and-how-i-did-it-546p</guid>
      <description>&lt;p&gt;If you're working with Java professionally and seriously aim to elevate your skills, taking the OCP Java SE certification may be a natural step forward. It’s not just about passing an exam — it’s a different way to learn and practice the material, deepening your understanding of Java and boosting your proficiency as a developer. And honestly, the preparation process in itself can be an invaluable experience.&lt;/p&gt;

&lt;p&gt;Why even consider this certification? The benefits are definitely worth exploring, and there are numerous motivations to do that. These certifications are widely recognized in the Java community, opening doors to new opportunities, interesting projects, and career advancement. Nevertheless, research suggests that many companies prioritize certified developers, believing it helps mitigate potential project risks. Keep in mind that any certification isn't a magic bullet — it's often the learning process that truly unlocks your capabilities.&lt;/p&gt;

&lt;p&gt;About a month ago, I became an &lt;em&gt;Oracle Certified Java SE 11 Professional&lt;/em&gt; after successfully passing the 1Z0-819 exam. The summary and syllabus for it can be found &lt;a href="https://education.oracle.com/java-se-11-developer-also-available-in-chs-for-taiwan/pexam_1Z0-819" rel="noopener noreferrer"&gt;here&lt;/a&gt;. So, how did I prepare? Well, that’s the key question and I decided to share my experience in case it helps someone else. While this article mainly covers the Java SE 11 exam, most OCP exams are structurally similar and require about the same amount of preparation. I hope some of my tips will be useful not only for passing the Java SE 11 certification but also when preparing for other Long-Term Support (LTS) versions, such as Java 17 and 21.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Let’s be honest, the exam is tough, but it’s definitely achievable. If you’re determined to pass, persistence and daily effort are crucial. For me, it was a long-awaited goal, a target I had kept postponing for too long. I’ve been working with Java a good 8+ years, mainly on building automation frameworks, but in reality, this exam was a whole new ballgame for me. It took me two attempts to pass, so prepare yourself to dedicate a lot of time and to keep going even when faced with difficulties.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As for how long it takes to prepare, that’s hard to say right away, since it really depends on your current workload and how many hours per day you can dedicate. In my case, I devoted more than three months entirely to focused preparation for this exam.&lt;/p&gt;

&lt;p&gt;First, let’s review what it takes to pass the exam, and then we’ll consider the benefits you can gain from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Effective Study Materials and Approaches
&lt;/h2&gt;

&lt;p&gt;The exam has been popular for years, leading to a wide demand for quality study resources. Fortunately, there are many options available, and I found that the following are among the most effective:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Complete Study Guide:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.amazon.com/Oracle-Certified-Professional-Developer-Complete/dp/1119619130/" rel="noopener noreferrer"&gt;&lt;em&gt;OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide&lt;/em&gt;&lt;/a&gt; by Scott Selikoff and Jeanne Boyarsky is, in my opinion, the best resource for preparation. It covers 100% of the exam objectives for exams 1Z0-819, 1Z0-815, 1Z0-816, and the upgrade exam 1Z0-817.&lt;/p&gt;

&lt;p&gt;The book provides clear explanations for each topic, with practice questions after every chapter, totaling 22 chapters. It also includes 4 full practice exams, which can be taken through the &lt;a href="http://www.wiley.com/go/sybextestprep" rel="noopener noreferrer"&gt;Wiley Efficient Learning&lt;/a&gt; portal, allowing you to take these exams and chapter review questions online in an environment that closely resembles the actual exam you'll face.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The guide is split into two parts, each consisting of 11 chapters. The reason it’s organized this way is due to recent changes in Oracle’s certification structure. In October 2020, Oracle moved from separate exams (1Z0-815 and 1Z0-816) to a single exam (1Z0-819). Before this change, passing both exams was required to earn certification. Now, a single exam (1Z0-819) covers the topics of both previous exams, minus some removed topics. This explains why the book is split into two parts, each aligned with the previous exam structure.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A key thing I wish I knew earlier. Initially, I bought the &lt;em&gt;OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815&lt;/em&gt;, thinking I needed to study both parts separately. Later, I realized that this was unnecessary and a waste of money, because Oracle’s latest exams consolidate the objectives of both parts into one. It’s more cost-effective and efficient to buy the &lt;strong&gt;&lt;em&gt;Complete Study Guide&lt;/em&gt;&lt;/strong&gt; right away, as it covers both previous exams in a single resource. Incidentally, the second book &lt;em&gt;OCP Oracle Certified Professional Java SE 11 Programmer II Study Guide: Exam 1Z0-816 and Exam 1Z0-187&lt;/em&gt;, has become quite hard to find nowadays. As you might have guessed, it’s much wiser to buy the Complete Study Guide right from the start.&lt;/p&gt;

&lt;p&gt;For studying, I find that having both formats can be very helpful. The platforms like Amazon offer the choice between eBook and paperback editions. In the early stages, the printed book is great for practicing tests, highlighting key points, and making handwritten notes to reinforce understanding. The eBook, on the other hand, makes it easy to copy code snippets, save notes, and quickly find information. And combining both approaches helps organize your preparation effectively and saves time during last-minute review. Ultimately, everyone has their own preferences, so you should choose what works best for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Udemy courses by Tim Buchalka:
&lt;/h3&gt;

&lt;p&gt;These video series, &lt;a href="https://www.udemy.com/course/java-se-11-developer-1z0-819-ocp-course-part-1/" rel="noopener noreferrer"&gt;part 1&lt;/a&gt; and &lt;a href="https://www.udemy.com/course/java-se-11-developer-1z0-819-ocp-course-part-2/" rel="noopener noreferrer"&gt;part 2&lt;/a&gt;, are a valuable supplement to your study plan. They include theory, practical demonstrations, and quick quizzes after each section, making complex topics easier to understand and remember. They’re especially useful for reviewing tough topics after going them through the Complete Study Guide.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The exam includes challenging topics like &lt;strong&gt;Concurrency&lt;/strong&gt;, &lt;strong&gt;I/O&lt;/strong&gt;, and &lt;strong&gt;NIO.2&lt;/strong&gt;. I strongly recommend dedicating additional time to brushing up on these areas. Watching the relevant videos in these series can help you grasp the concepts more clearly and prepare more confidently for questions on these topics.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  3. Practice Tests and Virtual Simulators:
&lt;/h3&gt;

&lt;p&gt;Thorough exam preparation requires extensive practice with mock questions and simulators. These tools not only help identify weak spots but also improve time management and help you get comfortable with the exam format.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;One of the most valuable resources for me was the paper book &lt;em&gt;&lt;a href="https://www.amazon.com/dp/1119696135?ref=ppx_yo2ov_dt_b_fed_asin_title" rel="noopener noreferrer"&gt;OCP Oracle Certified Professional Java SE 11 Developer Practice Tests: Exam 1Z0-819 and Upgrade Exam 1Z0-817&lt;/a&gt;&lt;/em&gt; by Scott Selikoff and Jeanne Boyarsky. A book from the same authors as the previously mentioned study guide. It contains over 1,000 questions across 13 goal-based chapters and includes 3 full-length practice exams. It's perfect for practicing without the pressure of a timer, using pencil and eraser to thoroughly review each question. And, just like the study guide, you can register on the Wiley Efficient Learning portal to access all practice tests online. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A key part of my preparation was using multiple practice platforms. I started with &lt;em&gt;&lt;a href="https://enthuware.com" rel="noopener noreferrer"&gt;Enthuware&lt;/a&gt;&lt;/em&gt;'s mock exams, known for their high difficulty. So, I didn’t complete all of them, but they were invaluable for identifying my weak spots, deepening my understanding, and getting familiar with the exam’s challenging nature. Additionally, I found a &lt;em&gt;&lt;a href="https://www.udemy.com/course/ocp-java-se-11-developer-1z0-819-practice-exam/" rel="noopener noreferrer"&gt;simulator&lt;/a&gt;&lt;/em&gt; on Udemy, based on the author’s personal experience, featuring around 100 questions covering a wide range of topics with detailed explanations after each question.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main benefit of such simulators is that they replicate the pressure and difficulty of the real exam, helping you to normalize the test environment. Regularly doing these tests builds confidence, improves timing, and reduces exam anxiety. I recommend scheduling multiple tries, each with a timer, to simulate real exam conditions. This way, you get used to managing your time effectively and ensure you're mentally prepared.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Throughout my preparation, I completed countless of questions, making a daily 90-minute Java quiz part of my routine. This consistent effort turned practicing into a ritual, which eventually reassured me that I was on the right track.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&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%2Fd2q6qvihr3hwq37r6bhu.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%2Fd2q6qvihr3hwq37r6bhu.png" alt="The Study Guides: theory and tests" width="800" height="885"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Real Exam Itself
&lt;/h2&gt;

&lt;p&gt;The first step is purchasing the exam attempt. Each attempt is single-use, so if you fail, you’ll need to buy another. To pass, you must score at least 68%. You’ll face 50 multiple-choice questions in just 90 minutes, so effective time management is crucial. To this end, developing a clear strategy for answering questions and pacing yourself can greatly improve your chances.&lt;/p&gt;

&lt;p&gt;Moreover, Oracle provides important instructions and short video tutorials that can be found &lt;a href="https://www.oracle.com/ua/education/certification/exam-prep/#yourComp" rel="noopener noreferrer"&gt;here&lt;/a&gt;. They cover everything from the process of purchasing the attempt, passing the exam readiness check, to testing your computer for compliance with the exam requirements. Watching these materials is essential to ensure you are fully prepared both mentally and technically for the real test.&lt;/p&gt;

&lt;p&gt;The exam costs between $240 and $300, depending on your location. It’s not cheap, so it’s wise to treat it as a key part of your Personal Development Plan and discuss the possibility of sponsorship with your company's management. Many organizations, especially those involved in Java enterprise projects, highly value industry certifications and actively encourage their engineers to pursue them for professional growth.&lt;/p&gt;

&lt;p&gt;To give you a clearer picture, here is my story. To start, the exam is strictly supervised: your camera must be on, the screen shared, and it’s just you in the room, no extra voices or talking. If you have pets or noisy neighbors, consider that in advance and find a quiet spot. Support staff may make comments or ask questions during the session, such as requesting your email, so just be prepared for that.&lt;/p&gt;

&lt;p&gt;My first attempt was pretty uncomfortable. I mean, the silence, constant focus on the screen, and the pressure were a lot to handle, honestly, I wasn’t fully prepared for that kind of atmosphere. My approach to answering questions was unclear, so I spent too much time on some, and out of anxiety, I started changing answers near the end. &lt;/p&gt;

&lt;p&gt;But, the second time was completely different story. I knew what to expect and had a clear plan. Here’s what worked for me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, instead of strictly tracking time, I focused on pacing. If I felt stuck on a question, I quickly decided whether to mark it for review without an answer or make an educated guess and also mark it. The goal was to avoid getting bogged down and missing easier questions, to attempt all 50 questions, and to have approximately 30 minutes left for reviewing the more challenging ones.&lt;/li&gt;
&lt;li&gt;Second, in addition to the above, I only marked unsure questions for review later. Remember, it’s always worth making an educated guess, since unanswered questions guarantee zero points.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These strategies helped me manage my time effectively, avoid getting stuck on difficult questions, and approach the exam with a sense of control. Just to clarify, in both attempts, I received the results immediately after finishing, with no delays.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Make sure to read each question carefully, reading several times if needed. The exam creators often make questions tricky to throw you off, so learning to spot their tricks is key to passing. And of course, practicing with mock tests sharpens your eye for common errors and patterns in code, helping you focus your attention where it’s needed most. Building this skill can make a big difference in quickly identifying mistakes and selecting the correct answer with confidence.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Is It Worth the Effort?
&lt;/h2&gt;

&lt;p&gt;If you’re considering whether to pursue this certification, ask yourself: do you want to have an official proof of your skills? Want to stand out during a recruitment process? Or maybe you aim to explore Java APIs more deeply, fill in gaps in your knowledge, or get familiar with the latest language features? Perhaps you want to learn to read code faster, review it more effectively, or simply boost your confidence after a challenging achievement. And beyond that, many find that preparing for the exam motivates them to write articles, join discussions, and engage more actively in the tech community.&lt;/p&gt;

&lt;p&gt;So, what did I really gain from this experience? Something even better! It significantly enhanced my Java expertise. Of course, it involved digging deeper into the fundamentals, but it also meant tackling technologies less commonly encountered in daily work. I'm referring to the less-traveled corners of the Java ecosystem: JDBC, Serialization, Java Platform Module System, Parallel Streams, and even intricate aspects of the Reflection API. Furthermore, working with Concurrency: diving into Threads, Runnables, Callables, and ExecutorServices, and understanding thread-safe code and how to avoid common concurrency issues like race conditions, deadlocks, and thread starvation.&lt;/p&gt;




&lt;p&gt;The truth is, the path isn’t easy. Preparation was intense, and I had to make sacrifices — yes, it cost me both time and money. But looking back, I can confidently say that every minute and every penny was worth it. Not just the final certification, but the journey itself, along with all the challenges faced and the knowledge gained, made it truly valuable. Most importantly, I proved to myself that perseverance and determination truly pay off and that I am ready to go to the next stage.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, it means there’s more to come. Stay tuned for the next series of Java articles, where I’ll share even more insights on core Java topics. I’ll give you the information and tools you need to deepen your understanding of the language and be well-prepared for challenging technical interviews — or at least give you a solid foundation for building something awesome.&lt;/p&gt;

&lt;p&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%2F9oqpryr8qrmotn4z0nnu.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%2F9oqpryr8qrmotn4z0nnu.png" alt="My OCP Java SE 11 Certification" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java11</category>
      <category>certification</category>
      <category>oracle</category>
      <category>personaldevelopment</category>
    </item>
  </channel>
</rss>
