<?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: Firas</title>
    <description>The latest articles on DEV Community by Firas (@firas-dev).</description>
    <link>https://dev.to/firas-dev</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%2F1271122%2Fed04a638-3912-4ad7-a2b3-21f61aa33181.png</url>
      <title>DEV Community: Firas</title>
      <link>https://dev.to/firas-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/firas-dev"/>
    <language>en</language>
    <item>
      <title>Basic Java Questions &amp; Answers (Part 1) - Type-Casting</title>
      <dc:creator>Firas</dc:creator>
      <pubDate>Thu, 22 Feb 2024 12:22:21 +0000</pubDate>
      <link>https://dev.to/firas-dev/basic-java-questions-answers-part-1-type-casting-2k0</link>
      <guid>https://dev.to/firas-dev/basic-java-questions-answers-part-1-type-casting-2k0</guid>
      <description>&lt;p&gt;I’ve compiled a list of questions and answers about fundamental topics in Java that I believe every Java developer should be aware of. This is part 1, covering the concepts of type-casting, upcasting and downcasting.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is type-casting?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;it refers to the process of converting from one data type to another.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What are the conditions for type-casting?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To convert between data types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the data types must be compatible. We can convert an &lt;em&gt;integer&lt;/em&gt; to a &lt;em&gt;float&lt;/em&gt; or vice versa, but we can't convert between an &lt;em&gt;integer&lt;/em&gt; and a &lt;em&gt;String&lt;/em&gt;. A compile error will be thrown in this case.&lt;/li&gt;
&lt;li&gt;The data type that we convert into must be larger than the original data type. Otherwise, Java will complain. However, this is still possible with a workaround, which we will discuss in the next section.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What are the categories of type-casting?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are two types of type-casting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Widening conversion (implicit casting):&lt;/em&gt;&lt;/strong&gt; If we cast a smaller data type to a larger one, Java compiler will automatically perform the casting. For example, if we want to convert from an &lt;em&gt;integer&lt;/em&gt; to a &lt;em&gt;double&lt;/em&gt;, we only need to assign the &lt;em&gt;integer&lt;/em&gt; value to a &lt;em&gt;double&lt;/em&gt; because &lt;em&gt;integers&lt;/em&gt; are smaller (32 bits) than &lt;em&gt;doubles&lt;/em&gt; (64 bits), so there is no risk of data loss after conversion. Here's a code snippet to demonstrate this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;//output = 10.0, which is a double&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;We declared a double variable and assigned to it an integer value of &lt;code&gt;10&lt;/code&gt;. Since an &lt;em&gt;integer&lt;/em&gt; is smaller than a &lt;em&gt;double&lt;/em&gt;, the &lt;em&gt;double&lt;/em&gt; will easily have more than enough room to store that value.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Narrowing conversion (explicit casting):&lt;/em&gt;&lt;/strong&gt; If we cast a larger data type to a smaller one, Java compiler will complain and will not perform the conversion. For instance, attempting to cast a &lt;em&gt;double&lt;/em&gt; to an &lt;em&gt;integer&lt;/em&gt; will trigger a compile error. To solve this, we can explicitly cast the value to the smaller data type using the casting operator ():&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;10.50&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//output = 10, which is an integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Contrary to the previous example, the &lt;em&gt;double&lt;/em&gt; value of &lt;code&gt;10.50&lt;/code&gt; is assigned to an &lt;em&gt;integer&lt;/em&gt;. Without an explicit operator, a portion of data would be lost since it's larger than what an &lt;em&gt;integer&lt;/em&gt; can hold. So we use the casting operator &lt;code&gt;(int)&lt;/code&gt; next to the &lt;em&gt;double&lt;/em&gt; value so that it gets converted and stored as an &lt;em&gt;integer&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Type-casting with primitive types vs reference types&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;type-casting not only works works on primitive types, but on reference types as well, although in slightly different ways due to the inherent difference between primitive and reference type variables.&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is Upcasting?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Upcasting is a process of implicitly casting a subclass to a superclass. It is closely related to inheritance.&lt;/p&gt;

&lt;p&gt;Before we further discuss this, let's look an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Vehicle {
    private String name;

    public Vehicle() {}

        public void driveVehicle() {
            System.out.println("vehicle");
        }
}

public class Car extends Vehicle {
    private String model;

    public Car() {
        super();
    }

        public void driveCar() {
            System.out.println("car");
        }

    public String getModel() {
        return model;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, there are two classes, Vehicle and Car. The Car class extends or inherits Vehicle. In this scenario, when we create an object of type Car, we can either assign it to a reference of type Car class like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car car = new Car();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or assign it to a reference of type Vehicle, which is the superclass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Vehicle car = new Car();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an example of upcasting where the compiler implicitly casts the subclass to the superclass.&lt;/p&gt;

&lt;p&gt;Using upcasting, we can access all methods defined in the superclass from the subclass but we're restricted from calling the methods in the sublcass itself.&lt;/p&gt;

&lt;p&gt;If we attempt to call a method in the subclass based on the last example, the compiler will complain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Vehicle vehicle = new Car();
// vehicle.getModel();    Cannot resolve method 'getModel' in 'Vehicle'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;What is Downcasting?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Downcasting is the reverse of upcasting. It’s a process of casting from the superclass to subclass.&lt;/p&gt;

&lt;p&gt;Let’s modify the previous example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Upcasting
Vehicle vehicle = new Car();

// Downcasting
if(vehicle instanceof Vehicle) {
    Car car = (Car) vehicle;
    car.driveCar();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;code&gt;instanceof&lt;/code&gt; to check if the &lt;code&gt;vehicle&lt;/code&gt; reference is an instance of &lt;code&gt;Vehicle&lt;/code&gt; class before explicitly downcasting using the &lt;code&gt;(Car)&lt;/code&gt; operator. This is a way to prevent &lt;code&gt;ClassCastException&lt;/code&gt; at runtime.&lt;/p&gt;

&lt;p&gt;Downcasting is particularly useful in cases where we have an instance of a superclass but we need to use the fields and methods of a subclass. Since we can only access superclass fields and methods in this scenario, downcasting is an approach to solve this problem.&lt;/p&gt;

&lt;p&gt;There you have it. This is part one of the series of Java questions and answers. I hope what we've discussed so far was helpful. Stay tuned for part two.&lt;/p&gt;

</description>
      <category>java</category>
      <category>npe</category>
    </item>
    <item>
      <title>How to survive as a junior developer</title>
      <dc:creator>Firas</dc:creator>
      <pubDate>Thu, 01 Feb 2024 12:20:14 +0000</pubDate>
      <link>https://dev.to/firas-dev/how-to-survive-as-a-junior-developer-5dfh</link>
      <guid>https://dev.to/firas-dev/how-to-survive-as-a-junior-developer-5dfh</guid>
      <description>&lt;p&gt;Starting a career in programming can feel quite challenging, whether you are a graduate or a professional from another field who decided to try something new. There are plenty of tips for junior developers all over the internet in blogs, articles and Twitter threads, but there is no harm in sharing some tips from my perspective. After all, I've learned a lot throughout my own journey and I would like to share what helped me grow as a developer. So let's talk about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Make sure you’re comfortable with the fundamentals of the programming language&lt;/strong&gt;. Understanding fundamental concepts will make it easier to learn and understand more advanced topics.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Always seek help from colleagues and seniors at work&lt;/strong&gt;. Never feel embarrassed to ask questions because developers never really stop asking and learning no matter how many years of experience they have.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Always work on your skills&lt;/strong&gt;. You shouldn’t count too much on the help of others whenever you face a problem or something that you do not understand because as a developer, you need to have good skills such as searching for solutions and analyzing the logic behind the code. This doesn’t contradict the first advice because there are times when you should seek advice and times when depending on yourself is better.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don’t learn many things all at once&lt;/strong&gt;. Trust me, I’ve done that and it had some negative impact on me. Instead, focus on what is relevant to your work. You can allocate some other time for other tools or languages that you’re interested in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Learn by doing, not watching&lt;/strong&gt;. As a junior developer, you should still be spending some time on tutorials, provided that you always practice what you learn.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Work on side projects&lt;/strong&gt;. This will expose you to more coding and will force you to practice what you learn at work or in tutorials.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time management is important&lt;/strong&gt;. Divide the time of your day between your work, learning and other aspects of your life.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don’t be afraid to make mistakes&lt;/strong&gt;. Everyone makes mistakes. Mistakes are a crucial part of the learning process. I’ve learned a lot from mistakes, even the small ones.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It is okay to use google to search or ChatGPT for solutions&lt;/strong&gt;. In fact, it is well known that developers share problems and solutions online and you’ll likely come across a solution for your problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;If you’re working remotely, work harder&lt;/strong&gt;. As a remote developer, you would be restricted to online communication, thus lowering the chances of learning from seniors compared to working in the office, so whenever you connect with them, try to extract as much knowledge as possible. The advantage of remote work, on the other hand, is that you get more freedom compared to your colleagues, so utilize that to learn and hone your skills.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lastly, think aloud while you code&lt;/strong&gt;. In other words, provide a commentary to yourself while you code. Explain the thought process in real time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it. I hope you find these tips useful in your career and learning journey. Feel free to comment and share tips of your own.&lt;/p&gt;

</description>
      <category>developer</category>
    </item>
  </channel>
</rss>
