5 Top Common Mistakes Every Beginner Java Programmer Makes

5 Top Common Mistakes Every Beginner Java Programmer Makes

Table of Contents

As a Java Programmer, I remember the first time I was in an algorithm lecture at college with around 40 students. The lecturer was trying to explain how each of us is unique and different. Even for one of the simplest algorithms, each of you will build a different way to implement it. Never forget that people think differently, so the way you implement a program will be different. 

Considering that approach, it makes sense to say there is no single correct way to implement a program in Java or any other language, but rather there are best practices. 

This tutorial aims to provide some examples of mistakes along with best practices in the Java language. 

Tutorial Level: BEGINNER.

Missing Break Statement In a Switch-case Construct

Switch case statements are designed to execute only one particular case based on the switch expression. If you don’t break after each case, the program will execute all case statements after the first match until it finds the next break or reaches the end of the statement.

				
					public static void main(String[] args) {
        switchCaseExample(2);
    }

 private static void switchCaseExample(int month){
        switch(month) {
            case 1:
                System.out.println("January");
            case 2:
                System.out.println("February");
            case 3:
                System.out.println("March");
            default:
                System.out.println("Invalid Month!");
        }
}
				
			
				
					// Output
February
March
Invalid Month!
				
			

Defining Unnecessary Instance Variables As a Java Programmer

Instance variables are used to hold values for objects states. Those variables are without the STATIC keyword and defined in a class outside any methods. They are supposed to be used by more than one method. They are so-called because their values will be different for each instance of that class. 

Instance variables are created with a new class instance and destroyed by the garbage collector when there aren’t any references. Those variables will be alive during all instance lifecycles even if you don’t need them, and this is such an expensive process. However, local variables are destroyed automatically when the method call ends.  

Local variables in methods always should be preferred since we want to keep each variable’s scope as short as possible.

				
					public class CoffeeChart {
    private int foam;// this instance variable is visible in CoffeeChart class only.
    public int espresso;// this instance variable is visible for any child class.
 
    public static void main(String[] args) {
    }
}

				
			

Comparing Object Data Types Inappropriately

Object data types are also called Non-primitive or Reference Data Types.

The reference of an object is stored in the stack, but the original object value will be stored in heap, so comparing two instances of an Object with equality  == will compare locations in memory but not actual values. 

Examples: String, Integer, Boolean, Double, BigDecimal, Array, List, Set, Stack, Vector, Classes, Interfaces

				
					
String name1 = new String("Fatma");
String name2 = new String("Fatma");

if (name1 == name2) {  
  // name1 and name2 are not equals
}

if (name1 != null && name1.equals(name2)) { 
   // name1 and name2 are equals"
}
				
			

Returning Null In Java

The NULL reference is the misguided invention of British computer scientist Tony Hoare; he described this situation at a software conference in 2009 as “I call it my billion-dollar mistake.”

Using null is that you need to check for its null case every time to avoid future problems. This will bring a null-oriented design.  Luckily all languages are now trying to solve the null problems with alternatives. For example, in Java 8, you can use the Optional type instead of null to point out that a value may or may not be present.

Exception Handling Mistakes of Java Programmers

Exception handling in Java is not the easiest task you may face. However, even pro developers can spend hours finding the best ways of exception handling.

Catching Exception Class

Catching Exception will catch all RuntimeExceptions. And when catching an exception, you are supposed to handle it properly; hence may not be able to deal with them to adjust the code. So the general principle is to catch the most specific exceptions you can.

				
					try {
   //some code that throws an exception
} catch (Exception e) {
}

				
			

Swallowing Exceptions

Swallowing an exception is an act of catching an exception but doing nothing to fix the issue. 

				
					try {
   //some code that throws an exception
} catch (IOException e) {
}

				
			

This is legit by syntax, the compiler will not complain, but that does not make this code piece any good.  

While the exception is caught, we do nothing to fix the problem and losing a piece of useful information from the exception. 

There are several common mistakes and best practices you should consider when implementing a program. That helps you to avoid common bugs and to implement applications that are easy to maintain. Understanding the problem you are trying to solve is the first step of implementation. Next, write good code that is easy to understand and modify by other developers.

If this list of common mistakes was a bit deep-dive for you, you could take a look at my Java in 60 minutes post. It provides you an overall about “how to build your first Java program.” If you are into Python, no worries, I’ve published a “Python in 60 minutes” post too.

5 Responses

  1. Pingback: Exceptionly
  2. Pingback: Exceptionly

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish