1. Comparing objects with ==
The favorite mistake of newbie programmers is comparing objects (especially strings) using the ==
operator. For example:
Scanner console = new Scanner(System.in);
String s1 = console.nextLine();
String s2 = console.nextLine();
if (s1 == s2)
{
System.out.println("The strings are equal");
}
This code will never display "The strings are equal", because the if
statement compares references to two different string objects.
Here is the correct version of the code:
Scanner console = new Scanner(System.in);
String s1 = console.nextLine();
String s2 = console.nextLine();
if (s1.equals(s2))
{
System.out.println("The strings are equal");
}
2. Changing a String
object
Newbie programmers often forget that all objects of the class are immutable, and that every method of the String
class returns a new object — the current object never changes.
Example:
String s = "Hello";
s.toUpperCase (); // Convert to uppercase
This code is very similar to the correct code, but it won't work as expected. The toUpperCase()
method does not change the object on which it is called. The correct code would look like this:
String s = "Hello";
String result = s.toUpperCase(); // Convert to uppercase
3. Forgetting to initialize objects that are elements of an array
Another common mistake is to forget to initialize an array variable. Example:
int[] array;
array[0] = 1;
array[0] = 2;
This code won't work: you need to explicitly set the array variable equal to a reference to the container object that will store the elements of the array.
int[] array = new int[10];
array[0] = 1;
array[0] = 2;
4. Using a local variable instead of an instance variable.
Newbies don't like to come up with long and meaningful names for variables. They frequently use single-letter names: a
, b
, i
, etc. That's a cruel thing to do when the code has several variables like that:
Put the number 99 into 100 cells of an array |
---|
|
The code above will not compile. The correct version looks like this:
Put the number 99 into 100 cells of an array |
---|
|
5. Removing a collection item
Very often there are situations when a certain element needs to be removed from a collection. The code looks roughly like this:
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 0, -5, -7, -12, 5, 15);
for (Integer value: list)
if (value < 0)
list.remove(value);
This code will not work, because you cannot use a for-each loop to simultaneously traverse the elements of a collection and modify that collection.
There are several solutions. First, you can go traverse one collection and change another:
Solution 1 |
---|
|
Second, since Java 8, collections have a removeIf()
method, to which you can pass a rule (lambda function) that indicates which elements to remove.
Example:
Solution 2 |
---|
|
6. Placing several classes with the public
modifier into a single file
There can be only one public class in a file. More classes can be declared in a file, but they must either be inner classes of a public class, or not have the public
modifier. Example:
Contents of the Solution.java file | Note |
---|---|
|
This is not allowed: two public classes in a single file. |
|
But you can do this. The Main class is not public |
|
And you can do this. The Main class is a nested class |
7. Calling ordinary (non-static) methods of a class from the static main()
method
Sometimes newbie programmers try to access non-static variables and methods from the main()
method or other static methods. Such code will not work, of course.
public class Solution
{
public int n = 100;
public int[] createArray()
{
return new int[n];
}
public static void main(String[]args)
{
int[] array = createArray();
}
}
The main method can only refer to static methods/variables. Well, or it must first create an instance of the Solution
class, and only then call non-static methods of that object. Example:
Solution 1 | Solution 2 |
---|---|
|
|
8. Declaring a constructor like a method
Another common mistake is incorrectly declaring a class constructor. The name of a constructor must be the same as the name of the class, and a constructor has no result type. The most common mistakes look like this:
|
There shouldn't be a return type here |
|
Invalid constructor name. The name of the constructor must match the class name |
|
this is missing. The value variable will be assigned to itself |
|
That is all correct |
9. Incorrect inheritance of interfaces
Java's creators tried to make it very close to English, so they chose different keywords for certain related concepts.
When a class inherits a class, you have to use the extends
keyword:
class Pet
{
}
class Cat extends Pet
{
}
When a class inherits an interface, you need to use the implements
keyword:
interface Meow
{
}
class Cat implements Meow
{
}
When an interface inherits an interface, use the extends
keyword:
interface Meow
{
}
interface Voice extends Meow
{
}
10. Omitting break
in a switch
statement
And the last mistake for us today, but not the last one for beginners, is failing to include a break
statement in a switch
statement. Example
Wrong | Right |
---|---|
|
|
GO TO FULL VERSION