CodeGym /Java Blog /Java Developer /8 common mistakes made by rookie programmers
Author
John Selawsky
Senior Java Developer and Tutor at LearningTree

8 common mistakes made by rookie programmers

Published in the Java Developer group
Hi! Today we'll look at 8 very common mistakes made by rookie (and other) Java developers. You'll find plenty of such lists on the web: many of them are similar to one another. As we compiled our list, we were guided by one criterion: whether we made the mistakes ourselves during our studies or employment :) They are not sorted by importance — they are equally important for you to understand and remember.
  1. Comparing objects using ==.

    The == operator compares object references.

    References point to addresses in memory. If they are stored at different addresses, then comparison using == will return false.

    
    public class Vehicle {
     
        String model;
        int maxSpeed;
        int yearOfManufacture;
     
        public Car(String model, int maxSpeed, int yearOfManufacture) {
            this.model = model;
            this.maxSpeed = maxSpeed;
            this.yearOfManufacture = yearOfManufacture;
        }
     
        public static void main(String[] args) {
            Car ferrari = new Car("Ferrari 360 Spider", 280, 1996);
            Car ferrariTwin = new Car("Ferrari 360 Spider", 280, 1996);
            System.out.println(ferrari == ferrariTwin);
        }
    }
    

    To compare objects, the Object class has a special method: equals(). Frankly, its default implementation is not bad:

    
    public boolean equals(Object obj) {
        return (this == obj);
    }
    

    In the Object class itself, the equals() method is implemented as a comparison of two references. In turn, in order to correctly compare objects, you need to redefine this method according to the criteria that are relevant in your particular program for your particular objects. The criteria for equality are up to you.

    The only thing you must not forget is the list of requirements for properly overriding equals(). You can easily find them on the Internet.

  2. Using non-static variables in static methods (and vice versa).

    If you've ever seen the message "Non-static variable x cannot be referenced from a static context", welcome to the club :)

    Static methods don't have access to non-static (instance) variables.

    This makes sense: after all, a static method can be called without creating an object of its class, and all fields belong to specific objects. And herein lies the contradiction that causes the error.

    By the way, going the other way works fine: you can use static variables in non-static methods:

    
    public class Main {
     
        public int x = 10;
     
        public static int staticX = 100;
     
        public static void main(String[] args) {
     
            System.out.println(x); // Compilation error - you can't do this!
        }
     
        public void printX() {
     
            System.out.println(staticX); // But you can do this!
        }
    }
    

  3. Misunderstanding how arguments are passed to methods: by reference or by value.

    Objects and primitives are passed to methods in two different ways: first, by reference; second, by value.

    Beginners often find it difficult to understand this concept. As a result, their code behaves unexpectedly:

    
    public class Main {
     
        public static void main(String[] args) {
     
            int x = 7;
            incrementNumber(x);
            System.out.println(x);
     
            Cat cat = new Cat(7);
            catLevelUp(cat);
            System.out.println(cat.getAge());
     
        }
     
        public static void catLevelUp(Cat cat) {
     
            cat.setAge(cat.getAge()+1);
        }
     
        public static void incrementNumber(int x) {
            x++;
        }
    }
    

    If you don't know exactly which number will increase and which will not (the plain old number or the cat's age), then re-read our lesson on the topic.

  4. Ignoring coding rules.

    This applies not only to compliance with certain "technical" principles, but also to mundane naming conventions.

    All these rules (how to name variables, how to write method names) were invented for a reason. They truly affect the readability of code

    After all, the code will not always be yours alone. You might be transferred to a different project at your company. Your coworkers who inherit your code will obviously not be happy when they see something like this:

    
    public class Cat {
     
        private int S_O_M_E_T_H_I_N_G = 7;
        public String striiiiiiiiiiiiiing;
        protected double I_HAVE_NO_IDEA_WHAT_THIS_IS = 3.14;
        boolean random = Math.random() > 0.5;
     
    }
    

    Your code might have ingeniously high performance, but if it is impossible to read and understand how it actually works, then, alas, it isn't worth much.

    If you stick to coding standards, then even if your code is far from ideal, at least your more experienced coworkers will be able to tell you how it can be improved from a technical point of view :)

  5. Misunderstanding the String class

    
    public class Main {
     
        public static void main(String[] args) {
     
            String s1 = "I'm learning Java";
            String s2 = new String("I'm learning Java");
     
            System.out.println(s1 == s2);
        }
    }
    

    If you don't know why this code displays false, you obviously need to beef up your knowledge :)

    Beginners are often unaware of the String Pool and how it works.

    As a result, they do not entirely understand how to properly compare strings in their code. We explored this topic in detail in one of our lessons.

  6. Handling exceptions incorrectly.

    Beginners aren't the only ones who stumble on this. Experienced developers get tripped up too. The reasons are many.

    First, there is no universal recipe. Programs have all sorts of different errors and different error handling scenarios.

    Second, not everyone understands how a stack trace is structured. There are a lot of error handling antipatterns, and each of them is "wrong" in its own way. This means that it's a lot easier to get error handling wrong than just about anything else.

  7. Not fully understanding how operators (arithmetic, logical and others) work.

    8 common mistakes made by rookie programmers - 2

    Here's a simple example. Can you tell right away what this code will display?

    
    public class Main {
     
        public static void main(String[] args) {
     
            int i = 6;
            System.out.println(7 == i++);
        }
    }
    

    If you answered incorrectly or just guessed, then you still have knowledge gaps in this area :)

    The code will display false. Therefore, the 7 == i comparison is excuted first, and only then is the i++ operation performed.

    By the way, we also had a detailed lesson on this. Here's the link if you missed it.

  8. Omitting the word break in a switch statement.

    Many people reading this article have certainly made this mistake! :)

    
    public class Main {
     
        public static void main(String[] args) {
     
            int i = 1;
     
            switch (i) {
     
                case 1: {
                    System.out.println("The number is equal to 1");
                }
                case 2: {
                    System.out.println("The number is equal to 2");
                }
                case 3: {
                    System.out.println("The number is equal to 3");
                }
            }
        }
     }
    

    As a result, execution tumbles through every possible option:

    Output:

    The number is equal to 1
    The number is equal to 2
    The number is equal to 3

    A break statement interrupts execution of the switch statement when one of the options is done being executed. Don't forget it or you may get unexpected results :)

Comments (20)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Abdul Alhazred Level 7, Minsk, Belarus
13 April 2024
Isn't there in the first snippet mistake with creating objects? Type of the variable is "Car" instead of "Vehicle" yet constructor hasn't got the name of the class. Don't throw stones at me, I'm just newbie. I can get something wrong
Aldo Luna Bueno Level 28, Peru
12 February 2022
"Your code might have ingeniously high performance, but if it is impossible to read and understand how it actually works, then, alas, it isn't worth much."
Agent Smith Level 38
23 August 2020
I would rephrase it to: "8 mistakes every Java learner makes at least once!" :D
Blaise Level 20, London, United Kingdom
17 May 2020
Students are brought/linked here from Level 10, Lesson 9. Not sure why, but switch statement hasn't been introduced yet at that point. You can find a short article about it here: https://www.geeksforgeeks.org/switch-statement-in-java/ or a longer one here: https://www.javatpoint.com/java-switch
David Level 29, Paris, France
5 April 2020
Nice!!!
Valeri Burlacu Level 15, Bamberg, Germany
27 March 2020
Output: The number is equal to 1 The number is equal to 2 The number is equal to 3 System.out.println(... Output: The number is equal to 1 The number is equal to 2 The number is equal to 3
HaeWon Chung Level 17, Boston, United States
27 March 2020
For the last code regarding switch and break, I don't understand why it executes case 2 and case 3. i doesn't match with 2 or 3. So in switch statement, it will execute cases after it matches? By the way, the output should have a line break in between sentences. It's println.
Seb Level 41, Crefeld, Germany
11 January 2020
Very nice little summary - thanks for that.
Chrisantus Makokha Level 32, Nairobi, Kenya
24 October 2019
Please change the link for the operators section (number 7). The current link is linking to a facebook page that is not even related to programming.
KARAN BHOITE Level 15, Nagpur, India
21 October 2019
in operator related point provided link is taking me another platform plz help