<?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: Zander Bailey</title>
    <description>The latest articles on DEV Community by Zander Bailey (@zmbailey).</description>
    <link>https://dev.to/zmbailey</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%2F159146%2F2755e93a-b3e5-41db-b9d3-ca13aa69b0a3.png</url>
      <title>DEV Community: Zander Bailey</title>
      <link>https://dev.to/zmbailey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zmbailey"/>
    <language>en</language>
    <item>
      <title>Unity: Spinning Power-up</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Mon, 30 Mar 2020 00:35:15 +0000</pubDate>
      <link>https://dev.to/zmbailey/unity-spinning-power-up-4m5m</link>
      <guid>https://dev.to/zmbailey/unity-spinning-power-up-4m5m</guid>
      <description>&lt;p&gt;First, what is a spinning power-up? In many action games you may find small items boost your characters abilities, or give them new ones. It is often useful to make these items noticeable to the player, and make them stand out to the player doesn’t overlook them. You could use anything to represent these items, and you might have your own special models or icons, but here is a simple tutorial to make a spinning power-up from scratch in Unity. Your power-up can be any shape, but for this example we’re going to use a classic ‘capsule’ shape. So let’s start by creating a capsule object from ‘create’ menu in the ‘hierarchy’ panel.&lt;/p&gt;

&lt;p&gt;By default your capsule will have 5 components, Transform, Mesh Filter, Mesh Renderer, Capsule Collider, and a Material. Let’s discuss each of these in turn. Transform is a combination of the objects location in 3d space, it’s rotation, and the scale of the object based on it’s original size. The Mesh Filter selects which mesh to use for rendering, and passes it to the Mesh Renderer. The Mesh Renderer renders the geometry provided by the Mesh Filter. The Capsule Collider handles collisions with the object. Normally this means that things will collide and not pass through, but if the option ‘Is Trigger’ is checked then it will have allow other object to pass through, but trigger a condition that may be referred to in scripts. The Material is the texture covering the object, by default a simple color. Now that we’ve cover what these things do, let’s see how we make our capsule a little more impressive.&lt;/p&gt;

&lt;p&gt;Don’t worry about the Transform too much, you can place it where ever it needs to go, and scale it to match the scale of your environment. For this example, I’m going to scale my capsule to 0.15 in all directions. My Capsule starts standing straight up, but that’s boring. We want the spin to be noticeable, so we’ll set it to a slight angle to the spin will be more dynamic, so we’ll set the Rotation Z to 45. But before we make it spin, let’s make sure it looks nice.&lt;/p&gt;

&lt;h4&gt;
  
  
  Making it Pretty
&lt;/h4&gt;

&lt;p&gt;Next we’re going to make the capsule a different color than the standard grey, something that pops a little more and draws the players eye. Unfortunately it’s not quite as simple as changing a color picker. Almost, but not quite. We’re going switch to the Project tab for a moment, and here we’re going to use the ‘create’ menu to make a new ‘Material’. Name it whatever you want, let’s call it ‘power_up_color’. This is going to be the material that covers the surface of the capsule. Select your new material, and in the inspector panel you’ll see a bunch of options. Look at the top, under ‘Main Maps’ is Albedo, which has a color picker next to it. We’ll set it to a nice bright blue for this example. You could adjust the ‘Metallic’ and ‘Smoothness’ options if you want it to be a little more shiny. Now that we’ve made a nice material, let’s go back to our capsule. In the Mash Renderer is the field ‘Element 0’, which will be set to ‘Default-Material’. Click the small circle to the right of this to get a selection window where you can choose your material, and we’ll select the material we just created. Now our capsule is blue! Also, now that we’re using a custom material, you can edit it directly by selecting the material component.&lt;/p&gt;

&lt;p&gt;Okay, our capsule is blue, but we can make it pop even more. In fact, we can actually make it glow! Let’s start by making a Light component using the ‘Add Component’ button at the bottom of the Inspector window. You can find it under ‘Rendering-&amp;gt;Light’ or by searching for Light. Set the range to 0.4, and the color to something very similar to whatever color you’ve made your capsule. Also check ‘Draw Halo’, so that the light will show up without being on a surface. Now the capsule is colorful and glowing. Now let’s make it spin.&lt;/p&gt;

&lt;h4&gt;
  
  
  Making it Spin
&lt;/h4&gt;

&lt;p&gt;So how do we spin it? We’re going to use a simple script, so we’ll start by making a new script. In the Project window, use the ‘create’ menu and make a new Script, we’re going to call it PowerUpSpin. Select the script and int the Inspector click ‘open’ to open it in your default script editor. For this behavior we’re only going to need the &lt;code&gt;Update&lt;/code&gt; function, All we need to do is tell it to update its Transform Rotation, and since we’re putting this in Update it will occur every frame:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great, we have a script, so let’s attach it to our capsule. With the capsule selected, click ‘Add Component’ and under ‘Scripts’ you should be able to find your custom script. And there you have it, Your glowing capsule will now rotate in place, and shine like a beacon to alert players to its presence.  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>NoSQL: Here comes MongoDB!</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Sun, 29 Mar 2020 22:22:54 +0000</pubDate>
      <link>https://dev.to/zmbailey/nosql-here-comes-mongodb-4b7d</link>
      <guid>https://dev.to/zmbailey/nosql-here-comes-mongodb-4b7d</guid>
      <description>&lt;p&gt;Previously we’ve talked about SQL databases and how to access them using various methods of querying. But there are also databases that forgo SQL in favor of other way of organizing data. These are called NoSQL databases and there are many flavors, but today we’re going to talk about one called MongoDB. MongoDB organizes its data in format closer to json objects instead of the traditional row/column structure. Once extracted from the database it is easy to convert it back into a standard table, but the versatility of json allows for more options when store other kinds of data. Python has a package that streamlines the process of connecting to and interacting with MongoDB, called PyMongo. First lets look at how to use PyMongo to connect to a database.&lt;/p&gt;

&lt;p&gt;First, of course, you’ll need to install PyMongo, but once you have it the connection is simple  enough, just one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pymongo&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MongoClient&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It’s difficult to get more specific about the parameters, because there are several formats the url can be in. You can use a host/port combination, which would appear like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;localhost&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="mi"&gt;5555&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Assuming your database is running on the default local host, and ‘5555’ is whatever port it’s using. You can also use MongoDB’s URI format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;mongodb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;localhost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5555&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="s"&gt;')
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To achieve the same thing. A quick note, if your database is running on the default host and port you can just call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since no parameters will run it with the defaults. If you are hosting your database with MongoDB Atlas your URL will be slightly more complicated. After creating your database you can use the ‘connect’ button to get a connection string, which will have a space for your password. It is highly recommended that you store your password in a separate file for security purposes, and only import it for use in the connection string.&lt;/p&gt;

&lt;p&gt;Once you are connected, you can access your database as an attribute of the client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myStore&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alternatively, if your database name is formatted such that it can’t work in this style, you can also access it using dictionary style:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Store&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is important to remember that if you call a database or collection that does not exist it will be created, so be careful not to re-create or overwrite your information. A collection is roughly MongoDB’s equivalent to a table in a normal database. Collections are created and called in much the same way as databases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But in MongoDB databases and collections are created ‘lazily’, which means that nothing has actually been saved to the server yet, and no databases or collections will be created on the server until something is actually stored in them. So let’s look at how to do that. But what is an entry going to look like in MongoDB? Here’s an example of what an entry for our customers collection might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;new_customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Eliot&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Spencer&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;
               &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="mi"&gt;555&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1234&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;espencer&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;lev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;
               &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="mi"&gt;1918&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="n"&gt;th&lt;/span&gt; &lt;span class="n"&gt;St&lt;/span&gt;&lt;span class="p"&gt;.,&lt;/span&gt; &lt;span class="n"&gt;Boston&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MA&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see this entry or document is basically a json object, so now we need to insert it into our collection. There are several ways to go about this, depending on how many documents we want to insert at one time. Right now we only have one document, so we’re going to use &lt;code&gt;.insert_one()&lt;/code&gt;, pretty simple, right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_customer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.insert_one()&lt;/code&gt; actually returns an instance of &lt;code&gt;InsertOneResult&lt;/code&gt;, which can be used to immediately obtain the id number of the newly inserted document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;c_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_customer&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;inserted_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If an inserted document does not already contain the ‘_id’ field, it will be assigned an ‘_id’ upon being inserted into a collection, and the new ‘_id’ will contain a unique value within the collection. We can also insert many documents as once, with `.insert_many()’. All we have to do is put our documents in a list like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;Python&lt;br&gt;
customer_list = [{…},&lt;br&gt;
        {…},&lt;br&gt;
        {…}]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And then insert the list using `.insert_many()’:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert_many&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we have entries in our collection, we need to be able to retrieve them. Similarly, we can use &lt;code&gt;.find_one()&lt;/code&gt;. There are two ways to use this, this first is without any parameters, which will return the first entry in the collection, and the second is to use a query based on a key-value pair:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Without parameters returns the first entry:
&lt;/span&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_one&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Query based on matching key-value
&lt;/span&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_one&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Alec&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also find object by id simply by querying based on &lt;code&gt;{‘_id’ : ‘…’}&lt;/code&gt; but there is a catch. Although an ObjectId can be represented as a string, it is not the same as a string. If you have the string version of an id it is necessary to convert it to it’s unicode representation with &lt;code&gt;ObjectId(some_id)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;You can also query for more than one document at a time, using &lt;code&gt;.find()&lt;/code&gt;. &lt;code&gt;.find()&lt;/code&gt; returns a cursor object which can iterate over all the results. Without parameters &lt;code&gt;.find()&lt;/code&gt; will simply return all items in the collection, but you can also supply a query to limit the results. Here we’ll query for customers with the last name Ford:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Ford&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are many other operations you can do with a MongoDB, but this should get you started. MongoDB may not be appropriate for all databases, but its alternate approach to database management can provide some exciting possibilities for the right project. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Android Activity Lifecycle</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Mon, 16 Mar 2020 04:49:55 +0000</pubDate>
      <link>https://dev.to/zmbailey/android-activity-lifecycle-547j</link>
      <guid>https://dev.to/zmbailey/android-activity-lifecycle-547j</guid>
      <description>&lt;p&gt;Activities are the building blocks of and Android application, and in order to make the most use out of an Activity it is necessary to understand the stages of the Android Activity Lifecycle. Understanding the stages will allow you to anticipate state changes, and make sure that operations happen at the right time so that states are preserved properly. There are six basic transitions between stages, each represented by a function that allows you to perform operations upon transition: &lt;code&gt;onCreate()&lt;/code&gt;, &lt;code&gt;onStart()&lt;/code&gt;, &lt;code&gt;onResume&lt;/code&gt;, &lt;code&gt;onPause&lt;/code&gt;, &lt;code&gt;onStop&lt;/code&gt;, and &lt;code&gt;onDestroy&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  onCreate()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;onCreate()&lt;/code&gt; is the first and possibly the most important, and actually must be implemented. It runs when the system creates the activity, and contains code to set up content and initial interaction. &lt;code&gt;onCreate()&lt;/code&gt; should contain basic startup logic that only happens once for span of the activity’s life. Often times &lt;code&gt;onCreate()&lt;/code&gt; will contain code to initialize the xml for the activity layout, or code to create custom Views instead. With &lt;code&gt;onCreate()&lt;/code&gt; the activity enters the ‘Created’ state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;onCreate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Bundle&lt;/span&gt; &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="o"&gt;)&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;onCreate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;setContentView&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;layout&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;activity_main&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;Here you can see how the &lt;code&gt;onCreate()&lt;/code&gt; function uses a saved state to set up the activity defaults, and then uses a separately defined layout file to set the activity content.&lt;/p&gt;

&lt;h4&gt;
  
  
  onStart()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;onStart()&lt;/code&gt; happens once the Activity has finished creating everything. &lt;code&gt;OnStart()&lt;/code&gt; can be used in a similar fashion to &lt;code&gt;onCreate()&lt;/code&gt; at times, although setting the layout should be confined to &lt;code&gt;onCreate()&lt;/code&gt;. &lt;code&gt;onStart()&lt;/code&gt; can contain such operations as setting listeners or working with objects that require the layout to already be established. When &lt;code&gt;onStart()&lt;/code&gt; occurs the activity will become visible and the app prepares to become interactive. &lt;code&gt;onStart()&lt;/code&gt; sends the Activity in to the ‘Started’ state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;onStart&lt;/span&gt;&lt;span class="o"&gt;()&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;onStart&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;code&gt;onStart()&lt;/code&gt; is less commonly used, since many functions that might be performed at this stage could also be performed during &lt;code&gt;onCreate()&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  onResume()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;onResume()&lt;/code&gt; is the last transition before the activity is fully running, and it is a more common place to make adjustments or to load preserved data. This runs when the Activity enters the &lt;code&gt;Resumed&lt;/code&gt; state, and it comes to the foreground. Now the app is operational, and it will remain in this state until something occurs to change focus away from the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;onResume&lt;/span&gt;&lt;span class="o"&gt;()&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;onResume&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 app is now fully operational and interacting with the user. So far we have Created-&amp;gt;Started-&amp;gt;Resumed. Now that the app is running, we’re going to look at the stages of shutting down.&lt;/p&gt;

&lt;h4&gt;
  
  
  onPause()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;onPause&lt;/code&gt; is the first stage, where focus has been removed from the Activity. The ‘Paused’ state indicates that the Activity is no longer in the foreground. &lt;code&gt;onPause()&lt;/code&gt; may be used to adjust or suspend operations that need to be altered while in the ‘Paused’ state. When the Activity returns to the foreground it will trigger &lt;code&gt;onResume()&lt;/code&gt; again as the app returns to the ‘Resumed’ state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;onPause&lt;/span&gt;&lt;span class="o"&gt;()&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;onPause&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 Paused state can occur without leaving or stopping the app, so &lt;code&gt;onPause()&lt;/code&gt; and &lt;code&gt;onResume()&lt;/code&gt; should be paid close attention to if there are issues when resuming an Activity.&lt;/p&gt;

&lt;h4&gt;
  
  
  onStop()
&lt;/h4&gt;

&lt;p&gt;Like it sounds, &lt;code&gt;onStop()&lt;/code&gt; occurs when Activity is finished running and is preparing to terminate. It is also triggered when the Activity is no longer visible, for instance when another Activity is launched that covers the entire screen. &lt;code&gt;onStop()&lt;/code&gt; may be a good time to make sure you have preserved the state of your Activity, or take care of other things that need to be done before the app terminates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;onStop&lt;/span&gt;&lt;span class="o"&gt;()&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;onStop&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;There is actually one more transition, if the Activity is in the &lt;code&gt;Stopped&lt;/code&gt; state and the user navigates back to it, it will trigger the &lt;code&gt;onRestart()&lt;/code&gt; before triggering &lt;code&gt;onStart()&lt;/code&gt; again. &lt;code&gt;onRestart()&lt;/code&gt; is somewhat redundant, except that it allows the option to call functions when restarting that are not called on initial start.&lt;/p&gt;

&lt;h4&gt;
  
  
  onDestroy()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;onDestroy()&lt;/code&gt; is invoked when the Activity is about to be destroyed. This can happened for two reasons: if the activity is finishing, or when the Activity is temporarily destroyed because of a change in configuration, like rotating the device. If necessary it is possible to distinguish between the two versions by calling &lt;code&gt;isFinishing()&lt;/code&gt; to tell if it is finishing or reconfiguring. &lt;code&gt;onDestroy()&lt;/code&gt; can be used to release any resources that may not have been automatically released by destroying the Activity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;onDestroy&lt;/span&gt;&lt;span class="o"&gt;()&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;onDestroy&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;Some of these functions are used often when working with Activities, and some are rarely used at all. It depends on the design of your application, as well as your approach. Regardless of if you intend to use them, it is still handy to know exactly what is happening behind your Activities. Debugging transitions can go a lot smoother if you understand exactly when things need to be updated so that you don’t lose data or try to affect things that don’t exist yet.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SQL: Java Connection</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Sun, 08 Mar 2020 02:56:54 +0000</pubDate>
      <link>https://dev.to/zmbailey/sql-java-connection-jhb</link>
      <guid>https://dev.to/zmbailey/sql-java-connection-jhb</guid>
      <description>&lt;p&gt;In order to use SQL to perform operations with a database, to do things like sending queries and other statements, you first need to connect to a SQL database. This is accomplished differently in different languages, but for some it’s more complicated than in others. Today we’re going to talk about how to do this in Java, since it requires a few more steps and interacting is a little more complicated. The first thing you’ll want to do is create a separate text file with your ‘connection parameters’, which will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dburl=jdbc:[database url]
user=[username]
password=[password]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These are the three pieces of information you might commonly need to connect to a database, but there may be more or less parameters required for your database. It is important to store this information in a separate file ease of keeping the information in one place, as well as for for security, so you can protect the txt file and still have your code available. So once you have this in a text file you can begin creating your connection. We’ll read in your file(we’re no going to cover reading in a text file, just know that it will be stored in a String called &lt;code&gt;paramsFile&lt;/code&gt;. Once we have this string we can store it in a new Properties object (a Properties object is a kind of hash table that maps a string to a string, and is designed to be read by certain processes), so that it can be used correctly. But first, in order to read the information from the String it will have to be put through a FileInputStream. Here’s what we have so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Properties&lt;/span&gt; &lt;span class="n"&gt;connectProps&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;Properties&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;connectProps&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;load&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;FileInputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paramsFile&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before we start building our connection we’re going to start a ‘try’ block, because any connection can become unstable for a variety of reasons, so it’s always important to use error handling. Now we’re going to extract the individual parameters and pass them into a connection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&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;dburl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;connectProps&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;dburl&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="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;connectProps&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;user&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="n"&gt;pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;connectProps&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DriverManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConnection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dburl&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;pass&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;Exception&lt;/span&gt; &lt;span class="n"&gt;e&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;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nc"&gt;Connection&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="err"&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;Of course you can catch a more specific exception if you like, or have a different response, this is just an example of the structure. Now we should have a connection to the database, yay! But wait, how do we use this to make SQL queries? And how do we read the responses? Let’s take a look. The main way to write statements is to use the connection object to &lt;code&gt;.createStatement()&lt;/code&gt;. &lt;br&gt;
You can then run SQL statements, usually stored as Strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="no"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;FROM&lt;/span&gt; &lt;span class="nc"&gt;MyTable&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createStatement&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;executeQuery&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is are tow other useful methods from a statement, &lt;code&gt;.execute&lt;/code&gt; and &lt;code&gt;.executeUpdate&lt;/code&gt;. &lt;code&gt;.executeUpdate&lt;/code&gt; does the same thing but is used for updating or changing the database rather than reading from it. &lt;code&gt;.execute&lt;/code&gt; only returns a boolean, indicating if the statement will successfully return a ResultSet or not. If you want to get a little more fancy there is another level of statement, the &lt;code&gt;preparedStatment&lt;/code&gt;, which is used for dynamically composing statements. A &lt;code&gt;preparedStatement&lt;/code&gt; has most of the functionality of a regular statement, but with more options. For instance you can use variables to fill out particulars in a statement. You need to write the statement ahead of time, but with a “?” In place of each variable. You then use a special method to prepare the statement ahead of time without running it. Once the statement is prepared you can use methods like &lt;code&gt;.setString()&lt;/code&gt; or &lt;code&gt;.setInt()&lt;/code&gt; depending on what type your variables are. Here we’ll prepare a statement with two variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="no"&gt;SELECT&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="no"&gt;FROM&lt;/span&gt; &lt;span class="nc"&gt;Dogs&lt;/span&gt; &lt;span class="no"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;breed&lt;/span&gt;&lt;span class="o"&gt;=?&lt;/span&gt; &lt;span class="no"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;?;&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pstmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;prepareStatement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;pstmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setString&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;breed&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;pstmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setInt&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;weight&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pstmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;executeQuery&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;      
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One more important thing to talk about is closing connections and statements. The keen eyed observer might notice that these examples have not been closing statements. If you plan to use a statement to run more queries then that is fine, you don’t need to keep closing and re-opening statements if you don’t need to. But it is good practice to close all your statements and your connection as soon as you are done with them, and at least before terminating your program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;pstmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
    <item>
      <title>SQL: Join Statements</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Sat, 29 Feb 2020 23:20:20 +0000</pubDate>
      <link>https://dev.to/zmbailey/sql-join-statements-1m6p</link>
      <guid>https://dev.to/zmbailey/sql-join-statements-1m6p</guid>
      <description>&lt;p&gt;When you’re working with a large database, the data will probably be organized in different tables. If you’re using SQL statements to query the database you’ll need to use &lt;code&gt;JOIN&lt;/code&gt; statements in order to get all the relevant data in one place. There are several different types of &lt;code&gt;JOIN&lt;/code&gt;s, Left, Right, Inner, and Outer. They each do something different, and they can be a powerful tool if you understand how to use them. For this example we’ll work with two tables, one with costumer info and one with order info:&lt;/p&gt;

&lt;p&gt;Customers:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GWfdpi6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0gkwanpxabgmnawzwvcg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GWfdpi6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0gkwanpxabgmnawzwvcg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Orders:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gl8oOS3J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/doifojbmjgngft9zsz74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gl8oOS3J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/doifojbmjgngft9zsz74.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how the orders have barely any information on the customers, but all the data is contained in the Customers table. This is because if we store all the customers data in a separate table we can access it for many different uses, but we don’t put it directly in the Orders table, because then any u[dates would have to be done in multiple places. Instead we can just store them separately and use &lt;code&gt;JOIN&lt;/code&gt;s. Here is a basic Join statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wZ93ZyH4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w11pl9d0ncwith474cx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wZ93ZyH4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w11pl9d0ncwith474cx1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have a &lt;code&gt;LEFT JOIN&lt;/code&gt;, which takes everything from the left table(the first table mentioned), and matches everything it can from the right table. In this Join all information from the left table will be included, but information from the right table can be excluded. As seen here &lt;code&gt;JOIN&lt;/code&gt; statements are often followed with an ON operator, which specifies what column or columns should be used to match data from one table to the other. There is also a &lt;code&gt;RIGHT JOIN&lt;/code&gt;, as demonstrated here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--whlwHkwJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gifplrpp7n2l6jha4gdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--whlwHkwJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gifplrpp7n2l6jha4gdo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have all information from the right table and all matching information from the left table. Notice how there is now another row with the information for a customer who has no orders, but because we’re including all information from the right table it has included the customer anyway and filled out their order information with null values. &lt;code&gt;RIGHT JOIN&lt;/code&gt;s are not used very often, since you can usually just reverse the order of the tables and use a &lt;code&gt;LEFT JOIN&lt;/code&gt; as normal. &lt;code&gt;INNER JOIN&lt;/code&gt;s are another common way of joining tables. An &lt;code&gt;INNER JOIN&lt;/code&gt; only returns the data that has a match in both tables, so it will exclude any entries from either table that don’t match the other table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wZ93ZyH4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w11pl9d0ncwith474cx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wZ93ZyH4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w11pl9d0ncwith474cx1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This excludes the customer that has no orders, since they don’t match any entries in the Orders table. Likewise, if there were an order that did not match any existing customer ID, that order would not be displayed either. The opposite of this is an &lt;code&gt;OUTER JOIN&lt;/code&gt;, which displays all entries from both tables, whether they match the other table or not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;
&lt;span class="k"&gt;OUTER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Customer_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--whlwHkwJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gifplrpp7n2l6jha4gdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--whlwHkwJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gifplrpp7n2l6jha4gdo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it displays the customer with no orders, and if there were any orders with no matching customers they would be displayed as well. &lt;code&gt;JOIN&lt;/code&gt; statements can be paired with queries to pick out specific information from the joined tables. There are many ways to incorporate to other tools of querying into a &lt;code&gt;JOIN&lt;/code&gt; statement, which means they can be very powerful if you know how to use them.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SQL: Select Statements</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Sun, 23 Feb 2020 07:17:51 +0000</pubDate>
      <link>https://dev.to/zmbailey/sql-select-statements-3634</link>
      <guid>https://dev.to/zmbailey/sql-select-statements-3634</guid>
      <description>&lt;p&gt;Previously we looked at how to set up a database, create a table, and insert data into the table using SQL statements. Once you have a database containing data the next step is to be able to retrieve data, and to retrieve exactly the cross-section of data relevant to your needs. We briefly mentioned how to access all the data in a table, so we’ll start by looking at that statement one more time. Here is how you would write a query to return all data from the Customers table we created in the previous article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But when we don’t want everything in table we can use additional keywords to narrow the focus of our query. For instance, maybe we only want the names of our customers and we don’t need the phone numbers or e-mail. In this case we can specify the columns we want to return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;FirstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LastName&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now it will return only the first and last names of the customers. Another example, here we only want the last name and phone number of each customer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;LastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Phone&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will select only the specified columns, but what about rows? Typically a database will have hundreds if not thousands of rows and one of the first things you’ll want to do is pair it down to only the rows relevant to your work. There are many ways you can go about this, as well as ways to organize the data. We’ll start simple, and try to retrieve a single row, in this case the data for a specific customer. To do this we’ll use the &lt;code&gt;WHERE&lt;/code&gt; clause to specify a condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; 
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;FirstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;Ben&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will only return rows where the FirstName column contains the string “Ben”. In our table there is only one Ben, but maybe we don’t don’t know that, and we need to make sure we get the right one, so we’ll specify both with the operator &lt;code&gt;AND&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;FirstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;Ben&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;LastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;Jones&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now it will only return rows where FirstName is “Ben” and LastName is “Jones”. Similarly you can use other operators like &lt;code&gt;OR&lt;/code&gt; and &lt;code&gt;NOT&lt;/code&gt; to write conditions. Sometimes you’ll only want a range of data, where a numeric value is within a certain range. This turns out to be very simple to accomplish with the keyword &lt;code&gt;BETWEEN&lt;/code&gt;. Let’s say we want to pull up all customers who are at least 18, but no older than 21:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;BETWEEN&lt;/code&gt; operator is inclusive, which means that it includes the numbers specified. You can also arrange the results according to the values in a certain column by using the keyword &lt;code&gt;ORDER BY&lt;/code&gt;. As you might expect using &lt;code&gt;ORDER BY&lt;/code&gt; on a numeric column will order the rows in ascending order by the values in that column, that is smallest to largest. If you want it sorted in descending order instead you can follow it with &lt;code&gt;DESC&lt;/code&gt; to order it from largest to smallest. If you use &lt;code&gt;ORDER BY&lt;/code&gt; with a column containing Strings it will arrange them in alphabetical order, or reverse alphabetical order if you use &lt;code&gt;DESC&lt;/code&gt;. Here’s how the statement looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;LastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two more handy things to know about are the &lt;code&gt;MIN()&lt;/code&gt; and &lt;code&gt;MAX()&lt;/code&gt; functions. As you might have guessed, these are for returning the minimum or maximum of a selected column. They are typically used in the &lt;code&gt;SELECT&lt;/code&gt; part of the statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will return the maximum value from the age column, or if we use &lt;code&gt;MIN()&lt;/code&gt; it will return the minimum. You can use these and other operators and statements in different combinations to retrieve exactly the data you want from the tables in your database. These become even more powerful when combined with Joining, which we’ll discuss next time.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>SQL: Table Creation and Basic Queries</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Sun, 16 Feb 2020 02:47:33 +0000</pubDate>
      <link>https://dev.to/zmbailey/sql-table-creation-and-basic-queries-171e</link>
      <guid>https://dev.to/zmbailey/sql-table-creation-and-basic-queries-171e</guid>
      <description>&lt;p&gt;SQL (pronounced Sequel, or sometimes just S-Q-L) stands for Structured Query Language. SQL is a language for interacting with databases, which it does by ‘querying’ a database and asking for certain features or columns, often with various conditions to narrow down the results. SQL can be used with different languages, like Python, Java and others, but it always uses the same basic  syntax for its queries, so once you learn how to write SQL queries they will work with whatever language you’re working in. There are actually several versions of the SQL language, but they all support the major commands that we’ll be discussing here.&lt;/p&gt;

&lt;p&gt;Queries are written in a rather straight-forward manner, with the form ‘select - from - where’. As we go on that will make more sense, but for now we’re going to start with database and table creation, since we’ll need to have a table before we can query it. Like a normal query, creating a database is also rather straight-forward. For example, we’ll create a database for a store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE storeDB;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Very simple. If you make a mistake and need to drop your table for any reason, you do just that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP DATABASE storeDB;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we have a database, we still need at least one table, so we’ll create a table to store customers. This time, however, we need to specify the features or columns the table is going to hold, with a name and data type for each column:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Customers(
    CustID int,
    LastName String,
    FirstName String,
    Phone String);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You’ll notice that we’re only using primitive types(and strings), since most databases can only handle simple types. As before if you need to remove the table completely the syntax is very similar to dropping a database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP TABLE Customers;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we have a table, we need to be able to modify it. We decide that we no longer need to store customer phone numbers, so we can remove that column. The way to do this is by starting with &lt;code&gt;ALTER TABLE&lt;/code&gt; followed by the table name, and then &lt;code&gt;DROP COLUMN&lt;/code&gt; and the column name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE Customers
DROP COLUMN Phone;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But we also want to store customers e-mail addresses, so we need to add that in a similar fashion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE Customers
ADD COLUMN Email;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is also a way to use &lt;code&gt;ALTER TABLE&lt;/code&gt; to alter the datatype of a column, but this varies depending on which version of SQL you are using. Okay, we have a database with a table, let’s start filling it with information. To add a row to the table we need to use the &lt;code&gt;INSERT INTO&lt;/code&gt; statement. A simple &lt;code&gt;INSERT INTO&lt;/code&gt; statement uses the name of the table you want to add to, followed by &lt;code&gt;VALUES&lt;/code&gt;, and then the values you want to add. When writing the statement this way it is important to remember to write the values in the order the columns appear in the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Customers
VALUES (1, Smith, John, JohnS@mail.com);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can get more complicated and exact if we want however. What if our next Customer does not have an email specified? In that case we need to specify which columns the values should be placed in. We do that by following the table name with the column names we want to the values to go in, in the same order that we write the values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Customers (custID, FirstName, LastName)
VALUES (2, Jones, Ben)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that our table has data, we can start talking about retrieving it. The basic form of this is the &lt;code&gt;SELECT&lt;/code&gt; statement, which can have many modifiers but starts out simple. This type of statement is written &lt;code&gt;SELECT&lt;/code&gt; and then the name or names of the columns you’re interested in, and then &lt;code&gt;FROM&lt;/code&gt;, followed by the name of the table you’re selecting the data from. If we want to select all fields we just use * instead of specifying any column names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM Customers;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There, we have now created a database, a table, filled the table, and then retrieved data from the table. Next time we’ll talk about more complex and advanced &lt;code&gt;SELECT&lt;/code&gt; statements.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>Python: Dunder Methods</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Mon, 10 Feb 2020 02:36:27 +0000</pubDate>
      <link>https://dev.to/zmbailey/python-dunder-methods-3e58</link>
      <guid>https://dev.to/zmbailey/python-dunder-methods-3e58</guid>
      <description>&lt;p&gt;Python classes are not as vital as they are in a language like Java, but they are still important, and it can be very handy to have an understanding of the inner workings of classes and what makes them tick. An important part of this is Special Methods, more commonly known as ‘Dunder’ methods. The term ‘Dunder’ comes from ‘Double Underscore’, because they use the naming convention of being enclosed in double underscores, like &lt;code&gt;__init__&lt;/code&gt;. These are all methods that are run when certain conditions occur, and are not called manually by the program. A few basics to start with are &lt;code&gt;__init__&lt;/code&gt; and  &lt;code&gt;__new__&lt;/code&gt;. These two are very similar in that they both occur upon instantiation of an instance of the class. But &lt;code&gt;__init__&lt;/code&gt; occurs just after creation, and &lt;code&gt;__new__&lt;/code&gt; occurs just before. To explain, &lt;code&gt;__init__&lt;/code&gt; is called after the object is created and give special instructions for setting it up before returning the instance. &lt;code&gt;__new__&lt;/code&gt; happens before the object is created, and allows customization of the instance creation. To give an example, let’s say we’re making a class to hold grid coordinates, and we want assign the coordinates upon creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;Class&lt;/span&gt; &lt;span class="n"&gt;Coords&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;x&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;y&lt;/span&gt;

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



&lt;p&gt;This tells the class that a new instance can accept x and y coordinates as parameters, and it will immediately assign those to it’s own personal x and y variables. Now &lt;code&gt;__new__&lt;/code&gt; allows us to perform some operations just before the object is created, so now let’s say that only want to make coordinates with positive y values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;Class&lt;/span&gt; &lt;span class="n"&gt;Coords&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cis&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;x&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;y&lt;/span&gt;

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



&lt;p&gt;So now we can make sure to perform certain operations every time an instance is created, but before instantiation. &lt;code&gt;__repr__&lt;/code&gt; and &lt;code&gt;__str__&lt;/code&gt; are two more dunder methods that also have uses, where both are used for turning the object into a string but in different situations. &lt;code&gt;__repr__&lt;/code&gt; is typically used for debugging, and is the ‘official’ string representation of the object, often an ‘expression’ that could be used to create the object. So &lt;code&gt;{'a':1,'b':2,'c':3}&lt;/code&gt; becomes &lt;code&gt;"{'a': 1, 'b': 2, 'c': 3}”&lt;/code&gt;. &lt;code&gt;__str__&lt;/code&gt; on the other hand, is the string representation of the object when cast with &lt;code&gt;str()&lt;/code&gt; or the default for &lt;code&gt;print()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;One last category to talk about is what are known as ‘rich comparison’ methods. These are methods that represent the functionality for mathematical operators, like + or -. For these it’s easier to just the methods and what operators they correspond to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Class&lt;/span&gt; &lt;span class="n"&gt;Coords&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;x&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;y&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__lt__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;#Less Than: &amp;lt;
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__le__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;#Less Than or Equal To: &amp;lt;=
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__eq__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;#Equals: ==
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;other&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__ne__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;#Not Equal: !=
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&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;other&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__gt__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;#Greater Than: &amp;gt;
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__ge__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;#Greater Than or Equal To: &amp;gt;=
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;  

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



&lt;p&gt;These allow you to customize how comparisons work for objects that don’t have an obvious method of comparing. There are many other ‘Special Methods’, and they can allow you do do many interesting and useful things when creating your own classes, and I have tried to give you an idea of how they work, and how they can be useful.  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Java: Error Handling</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Sun, 02 Feb 2020 21:23:38 +0000</pubDate>
      <link>https://dev.to/zmbailey/java-error-handling-1ahg</link>
      <guid>https://dev.to/zmbailey/java-error-handling-1ahg</guid>
      <description>&lt;p&gt;Errors happen. We might like to believe our code is unbreakable, or that we have accounted for every possible input. But when we send code out into the jungle where it can be run by wild users, someone, somewhere, will probably find a way to break it. So it is important to have your code ready not just for the possible, but for the improbable and the impossible. For this reason, it is important to implement error handling, so that when someone breaks the unbreakable, the program will have some recourse. Today we’re going to talk about error handling in Java, but many languages use a similar structure once you know what to look for. One of the main ways of error handling in Java is something called a Try-catch block, which is used to catch possible errors and deal with them appropriately. This consists of two parts, the Try section, where we attempt to run some code, and the Catch section which catches an exception and deals with it in a user-specified manner. There is also a possible third section, Finally, but we’ll deal with that later.&lt;/p&gt;

&lt;p&gt;Let’s say you’re indexing into an array that might not have the index you expect, and in that case you want your program to act accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&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="n"&gt;letters&lt;/span&gt; &lt;span class="o"&gt;=&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;'b'&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;letters&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="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;Exception&lt;/span&gt; &lt;span class="n"&gt;e&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;e&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 this example, the array only has 3 entries, so trying to call the index 20 will trigger an ArrayIndexOutOfBoundsException. When that happens it will jump down to the &lt;code&gt;catch&lt;/code&gt; block and run that code instead. Usually it will be something like this, where you print the exception so we know what happened, or sometimes you might print a custom message so you know what part of the code it broke down in, although that can also be accomplished with a traceback. In this example I have used the generic class Exception for my catch block, which catches all types of exception, but you can get more specific with this part. IF you want to handle all exceptions the same, you can just use Exception, but if you want to handle some exceptions differently you can refer to specific types of exceptions. In this case, we could print a specific message if we get can ArrayIndexOutOfBoundsException, and a different message if we get anything else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&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="n"&gt;letters&lt;/span&gt; &lt;span class="o"&gt;=&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;'b'&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;letters&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="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;ArrayIndexOutOfException&lt;/span&gt; &lt;span class="n"&gt;e&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;"Bad Index! Number is too big!"&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;Exception&lt;/span&gt; &lt;span class="n"&gt;e&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;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Additionally, in Java 7+ you can look for more than one type of exception in one catch statement, like &lt;code&gt;catch(ExceptionType1 e || ExceptionType2 e)&lt;/code&gt;. Prior to Java 7 it is necessary to have multiple catch blocks even if they have the same behavior. There is also a third part you can use, called a Finally statement. Finally allows you to execute code regardless of whether or not the Try statement throws an exception:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&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="n"&gt;letters&lt;/span&gt; &lt;span class="o"&gt;=&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;'b'&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;letters&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="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;Exception&lt;/span&gt; &lt;span class="n"&gt;e&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;e&lt;/span&gt;&lt;span class="o"&gt;);&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="nc"&gt;Sytem&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;"Hi anyway!"&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;One other way to handle errors is to pass them back up to the parent function by using the &lt;code&gt;thow&lt;/code&gt; keyword. This gives you the option to create exceptions that might not otherwise be thrown by telling it when to throw exceptions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyClass&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;void&lt;/span&gt; &lt;span class="nf"&gt;checkAge&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="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;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArithmeticException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You must be at least 21 years old to order alcohol.”);
    }
    else {
      System.out.println("&lt;/span&gt;&lt;span class="nc"&gt;You&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="n"&gt;enough&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&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;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="n"&gt;checkAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;19&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 this instance, we want to ensure that if the age is less than 21 we don’t serve the client alcohol, so we throw an exception. &lt;code&gt;throw&lt;/code&gt; is not to be confused with &lt;code&gt;throws&lt;/code&gt;, which is a keyword used with a method definition, and indicates that an exception might be thrown by that method in order for it to be handled by the parent method. There are many ways to handle exceptions, and you might find variations of some or all of these in other languages. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Java Lists: Basic Operations</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Mon, 27 Jan 2020 01:40:04 +0000</pubDate>
      <link>https://dev.to/zmbailey/java-lists-basic-operations-4pgm</link>
      <guid>https://dev.to/zmbailey/java-lists-basic-operations-4pgm</guid>
      <description>&lt;p&gt;Java’s Lists and other collection types can be very powerful objects, with methods for simplifying a great deal of tasks. In order to use them properly and make the most out of Java collections, there are some basic methods common to most collection types that it is important to know about and also to understand how they sometimes differ between collection types. To start with, you probably know how to add, get, and remove objects from a List, but it’s a good place to start, and from there we’ll talk about some variations on those operations. Adding an item is pretty straight forward, just use the method &lt;code&gt;.add(item)&lt;/code&gt;. Unlike an array or a List in some other languages, you cannot simply index into a Java List.  Java Lists also require a special ‘getter’ method to retrieve an item from a List, called &lt;code&gt;.get(index)&lt;/code&gt;. To remove an item you have to use &lt;code&gt;.remove(index)&lt;/code&gt;, where ‘index’ is the index of the item you wish to remove. Let’s see the code for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* First we create the List. Notice that we cannot fill the list upon instantiation */&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;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fruit&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;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;

&lt;span class="cm"&gt;/* Here we add several items */&lt;/span&gt;
&lt;span class="n"&gt;fruit&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="s"&gt;"Orange"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;fruit&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="s"&gt;"Apple"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;fruit&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="s"&gt;"Banana"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* Retrieve an item */&lt;/span&gt;
&lt;span class="n"&gt;apple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruit&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;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* Remove an item */&lt;/span&gt;
&lt;span class="n"&gt;fruit&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;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because Java Lists cannot be indexed into like an array, you cannot reassign their values the way you would normally. Instead, we have to use &lt;code&gt;.set(index,”new entry”)&lt;/code&gt;. Say we want to alter the previous List without disturbing the overall order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;fruit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;set&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;"Cherry"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay, so now we know how to make some basic changes to a List, but let’s make it a little harder. Let’s say we have a List of unknown length, and the contents are unordered, so we don’t know the index of anything. Now we want to remove a specific string, but we need the index, right? There is a way we can get around this, with &lt;code&gt;.indexOf(item)&lt;/code&gt;. This will return the index of the specified item. Here’s how we would use it in this situation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;fruit&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="n"&gt;fruit&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;"Cherry"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alternatively we could also use this with &lt;code&gt;.set&lt;/code&gt; to change the value of an item at an unknown index. There’s another problem though, what if the item doesn’t exist? In this case we happen to know that ‘fruit’ contains an item called ‘cherry’, so it will return a valid index. But what if we can’t be sure that the item will actually exist in the list? Fortunately there’s a way we can handle that. If &lt;code&gt;indexOf&lt;/code&gt; receives an item that doesn’t exist in the List it just returns a -1. However, that will cause &lt;code&gt;.remove&lt;/code&gt; to throw an IndexOutOfBoundsException, so we’ll need to do some checking first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;If&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruit&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;"Cherry"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;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="n"&gt;fruit&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="n"&gt;fruit&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;"Cherry"&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;We can also check this with a method called &lt;code&gt;.contains(item)&lt;/code&gt;, which is designed for this purpose. Instead of a -1 it returns True if the item is contained in the List. It should be noted that the efficiency of this method is the same as writing out this operation with a for loop to check each item in the list. The exception to this is a Set. Set also has a &lt;code&gt;.contains&lt;/code&gt; method, but because Sets hash instead of index, it can check for an item considerably faster. However, depending on the intended usage this may or may not be an issue.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Java Lists: Lists of Lists</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Mon, 20 Jan 2020 00:48:44 +0000</pubDate>
      <link>https://dev.to/zmbailey/java-lists-lists-of-lists-dg0</link>
      <guid>https://dev.to/zmbailey/java-lists-lists-of-lists-dg0</guid>
      <description>&lt;p&gt;A List of Lists might seem unnecessarily complicated, but it comes up in Java more than you might think ,when when you do come across one it’s good to know how to deal with it. When dealing with a List of Lists, it is easy to lose sight of the scope of the List, because there is an extra layer to deal with. Normally when you create an empty List you can immediately begin populating it with new items, but a List of Lists takes an extra step. Remember, the List of Lists doesn’t contain any actual items, it contains the Lists that contain the items. Unlike creating a 2-dimensional array, a List of Lists contains no actual Lists upon creation, and needs to be populated with Lists before you can begin filling those individual Lists. Don’t worry, we’re going to look at some examples of how this works. Let’s say we want to make a List that contains a list for each floor of and apartment building, with the apartment numbers for each floor. Here’s how we instantiate the List:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&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;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;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;roomList&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;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;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So far, not too bad. There are two main ways we can start populating it. First, let’s add the first floor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;roomList&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="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;roomList&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="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;roomList&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="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;roomList&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="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice how first we have to add a new List to the main List, and then retrieve that List in order to start adding individual room numbers. In this case we don’t have to give the List we are adding a name since it won’t need one once it is stored inside the main List. The other way we can add a floor is to create it first, then add it. Let’s try this with the second floor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&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;floor2&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;floor2&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;201&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;floor2&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;202&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;floor2&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;203&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;roomList&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="n"&gt;floor2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case we create the floor List and finish populating it before adding it directly to the main List. Now let’s talk a little about copying a List of Lists. Earlier we went over the difference between a shallow copy and a deep copy, and how we have to copy each item to make a distinct copy of the List instead of the just the references. But now we’re dealing with a List of other things that reference other things, so how to we copy that? We have to go one level deeper. It seems a little crazy, but we have to go over each List and create a deep copy of that List, and then place those lists in a new List in order to make a deep copy of a List of Lists. IT will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* First we’ll create a new List */&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;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;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;newBuilding&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;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;&amp;gt;();&lt;/span&gt;

&lt;span class="cm"&gt;/* Next we iterate through the Lists to get every item */&lt;/span&gt;
&lt;span class="nc"&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;roomList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;span class="no"&gt;I&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
    &lt;span class="n"&gt;newBuilding&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="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="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="nl"&gt;room:&lt;/span&gt; &lt;span class="n"&gt;roomList&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="no"&gt;I&lt;/span&gt;&lt;span class="o"&gt;)){&lt;/span&gt;
        &lt;span class="n"&gt;newBuilding&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="no"&gt;I&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="n"&gt;room&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;And now we have successfully created a deep copy of our List of Lists. Lists can be very powerful, and there are many reasons to use them, but if you are going to be stacking Lists inside of other Lists it can be costly in both space and runtime, so it is wise to consider using multidimensional arrays instead.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Java Lists: Copies and Deep Copies</title>
      <dc:creator>Zander Bailey</dc:creator>
      <pubDate>Mon, 13 Jan 2020 02:07:21 +0000</pubDate>
      <link>https://dev.to/zmbailey/java-lists-copies-and-deep-copies-3en7</link>
      <guid>https://dev.to/zmbailey/java-lists-copies-and-deep-copies-3en7</guid>
      <description>&lt;p&gt;Lists are a type of collection that exists in many languages, and each language has its own way of handling them. Java’s Lists can be very useful, and with Java’s system of classes and interfaces they can be very versatile. That being said, Lists in Java can also be frustrating to deal with at times, and they are more complicated than their counterparts in a language like Python. So we’re going to talk something that can be difficult when working with Lists, copying them.  Before we go any further, it is important to note that copying lists in Java is not always the best or most efficient strategy, since creating a copy can be costly both in time and space. If your List is likely to be very large, it is advisable to look for another way to way to make your program work without copying an entire list. But if you think it’s necessary, here’s how to copy a List in Java.&lt;/p&gt;

&lt;p&gt;There are two kinds of copies, a shallow copy and a deep copy. To understand the difference, you need to understand how a List works in Java. A Lists appears to be a collection of objects, but this is not actually the case. A List is really a collection of references to objects elsewhere in memory. The reason this distinction is important is because when you copy a List normally, you’re not actually making copies of each item in the List, you’re only making a copy of the List of references. Normally, when you copy a variable, it makes an entirely new object. So if you make a copy and then change the original, it won’t affect the copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;myInt&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_int&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;myInt&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="cm"&gt;/* 
   myInt = 1
   myCopy = 0 
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The original is updated with a new value, but the original remains 0. But what happens when you try the same thing with a List?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a List&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;myList&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;myList&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;myList&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;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;myList&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;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Make a copy&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;myCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Update original&lt;/span&gt;
&lt;span class="n"&gt;myList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;set&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="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;myCopy&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;myList&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;[1,4,3]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;[1,4,3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is because we only copied the references, not the objects themselves. So how do we make a copy of the entire list, objects and all? That is what is called a Deep Copy. There’s a few ways to go about this, but to keep things simple I’m just going to to go over the basic code, without using an interface or creating a new method. The easiest way to create a Deep Copy is to create a copy of each object in the original List and store it in a new List:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a List&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;myList&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;myList&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;myList&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;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;myList&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;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Make a new List&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;myCopy&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Fill new List&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="nl"&gt;i:&lt;/span&gt; &lt;span class="n"&gt;myList&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;myCopy&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="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="c1"&gt;// Update original&lt;/span&gt;
&lt;span class="n"&gt;myList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;set&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="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;myCopy&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;myList&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;[1,2,3]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;[1,4,3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we have a distinct copy that is unaffected by changes to the original List. Next time, we’ll go discuss how to copy a List of Lists.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
