<?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: Ben L </title>
    <description>The latest articles on DEV Community by Ben L  (@travelcoder).</description>
    <link>https://dev.to/travelcoder</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%2F1287025%2Fb65bf9c3-b2ec-417d-95e4-975f0ca6f0bf.png</url>
      <title>DEV Community: Ben L </title>
      <link>https://dev.to/travelcoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/travelcoder"/>
    <language>en</language>
    <item>
      <title>Pros and Cons of Arrays in Java</title>
      <dc:creator>Ben L </dc:creator>
      <pubDate>Tue, 20 Feb 2024 14:29:17 +0000</pubDate>
      <link>https://dev.to/travelcoder/pros-and-cons-of-arrays-in-java-3if9</link>
      <guid>https://dev.to/travelcoder/pros-and-cons-of-arrays-in-java-3if9</guid>
      <description>&lt;p&gt;Arrays are a fundamental data structure in Java, and they provide a convenient way to store and manage elements. &lt;/p&gt;

&lt;p&gt;While arrays are useful, they also have disadvantages. Here's a breakdown of the pros and cons of using arrays in Java.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros of Arrays
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Speedy Element Access:&lt;/strong&gt; Arrays allow for quick access to any element. This is because elements in an array are stored in contiguous memory locations, enabling direct access via each element's index.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Versatile Storage:&lt;/strong&gt; An array can store various types of data, including strings, primitive data types (like int), and objects from any class you create.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Built-in Java Support:&lt;/strong&gt; Being a core part of the Java language, arrays do not require imports to be used. This built-in support simplifies coding and reduces the need for external dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Cons of Arrays
&lt;/h4&gt;

&lt;p&gt;Despite their advantages, arrays have certain drawbacks that can limit their usefulness in many scenarios.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fixed Size:&lt;/strong&gt; Perhaps the most significant limitation of arrays is their fixed size. Once an array is created, its size cannot be altered. If you need to store more elements than the array can hold, you'll need to create a new, larger array and copy the elements from the old array to the new one, which can be an inefficient way to handle your program. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inefficient Resizing:&lt;/strong&gt; Related to the fixed size issue, resizing an array to accommodate more elements or to shrink its size is cumbersome and resource-intensive. This resizing process involves creating a new array and manually copying elements, which can be time-consuming and waste resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Wasted Memory Space:&lt;/strong&gt; If an array is allocated more space than it needs, it can lead to inefficient use of memory. This is particularly problematic when memory usage is a critical concern.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Difficulty in Removing Elements:&lt;/strong&gt; Arrays do not provide a straightforward way to remove elements. To delete an element from the middle of an array, you typically have to shift all subsequent elements one position to the left.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Using == vs the equals() Method in Java</title>
      <dc:creator>Ben L </dc:creator>
      <pubDate>Tue, 20 Feb 2024 14:05:53 +0000</pubDate>
      <link>https://dev.to/travelcoder/using-vs-the-equals-method-in-java-1anf</link>
      <guid>https://dev.to/travelcoder/using-vs-the-equals-method-in-java-1anf</guid>
      <description>&lt;p&gt;The == operator is used to compare the identities of reference data types (not the contents of primitive data types). &lt;/p&gt;

&lt;p&gt;It basically checks if the references point to the same object. Even if the actual values in those two objects are identical, it will return a false value if they are separate objects. &lt;/p&gt;

&lt;p&gt;Using the Equals() Method&lt;/p&gt;

&lt;p&gt;If you want to compare the contents of reference data types, then you should use the Equals() method, which is available via the Object class, although must override it in the classes you create. &lt;/p&gt;

&lt;p&gt;For example, let’s say we would like to compare two student IDs to check if they are the same. We can use the equals() method.  &lt;/p&gt;

&lt;p&gt;Here’s a walkthrough: &lt;/p&gt;

&lt;p&gt;Let’s say we created a studentID in the class Student: &lt;/p&gt;

&lt;p&gt;private int studentID;&lt;/p&gt;

&lt;p&gt;Now, we want to create a method to compare two student IDs. &lt;/p&gt;

&lt;p&gt;To start with, you have to declare the method and pass an Object as the parameter: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;public boolean equals(Object obj);&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The next line will use the == operator, already discussed, to check if the objects we will be comparing are the same object in the memory. If they are, there will be no more need to continue with the code. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;if (this == obj) return true;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If they are not the same object, we must check if the Object passed as a parameter is of the class Student so we can compare the IDs: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;if (obj instanceof Student){&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If it is, we can proceed with casting the object as a Student object, after which we can compare it: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Student a = (Student) obj;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now we can compare the values of current student ID and that belonging to the Object typecast to a Student object “a”:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;return studentID == a.studentID;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Remember to include an else block, which specifies that if the passed object was not an object of the class Student, we simply call the equals() method of the superclass: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;else{&lt;br&gt;
return super.equals(obj)&lt;br&gt;
}&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s see the entire code again: &lt;/p&gt;

&lt;p&gt;@&lt;em&gt;Override &lt;br&gt;
public boolean equals(Object obj){&lt;br&gt;
if (this == obj) return true; &lt;br&gt;
if (obj instanceof Student){&lt;br&gt;
    Student a == (Student) obj;&lt;br&gt;
    return studentID = a.studentID;&lt;br&gt;
    }&lt;br&gt;
    else{&lt;br&gt;
     return super.equals(obj)&lt;br&gt;
    }&lt;br&gt;
}&lt;/em&gt;&lt;br&gt;
_&lt;/p&gt;

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