"Hi, Amigo! Let's continue to talk about mistakes. This time, we'll explore mistakes that the compiler will not always help you with. Pay attention and you'll learn something about yourself."
"I'm ready to listen, Diego. I hope this won't be too embarrassing for me."
Comparing objects with ==
"Our list of top favorite newbie programmer mistakes begins with comparing objects (especially strings) using the ==
operator"
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");
}
"I've done that quite often. Now I can clearly see that this code will never display "The strings are equal", because the if
statement compares references to two different string objects."
"Yes. That's why the correct option would be:
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");
}
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."
"It wasn't that long ago that I learned what immutable means, but I think I've done this.
"I'm fairly certain of it. Example:
String s = "Hello";
s.toUpperCase (); // Convert to uppercase
"This code is very similar to 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
"Exactly. I've done that, but I didn't even really understand what was wrong. Thank you for the clarification!"
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. Correct version:
int[] array = new int[10];
array[0] = 1;
array[0] = 2;
Using a local variable instead of an instance variable.
"Newbies don't like to come up with long and meaningful names for variables."
"That's so true. For get things done quickly, I sometimes give variables names like a
, b
, and i
."
"Don't do that. 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 |
---|
|
"It's much more difficult to make mistakes in code with proper names. The correct version looks like this:
Put the number 99 into 100 cells of an array |
---|
|
Removing a collection item
"Have you already looked into collections?"
"Literally with just one eye."
"If you don't know what I'm talking about, then make a note to yourself to take a look in the future. 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 |
---|
|
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 |
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 |
---|---|
|
|
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 |
|
The constructor name is invalid. It must match the class name |
|
this is missing. The value variable will be assigned to itself |
|
That is all correct. |
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, or, more precisely, implements it, 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 Meov
{
}
Omitting break
in a switch
statement
"And the last mistake for today, but not the last one for beginners, is failing to include a break
statement in a switch
statement. Example:
Wrong | Right |
---|---|
|
|
"You know, Diego... Judging by the set of errors you presented here, it feels like you've been reading my personal journal... Or you've been watching me solve tasks."
"Ha! Have no doubt about it. I have read, tracked, and continue to do so. So be on the alert!"
"???"
"Don't you worry. I'm just kidding. Be vigilant and make fewer stupid mistakes."
GO TO FULL VERSION