5 Common Mistakes Made By Java Programmers

Although to err is human, but when these errors occur in a Java code that you have created after hours of hard work, they can actually be highly frustrating for the programmer himself! But there is nothing to get disappointed as even best programmers frequently do mistakes in their codes. However, what you need to focus upon is to understand and be careful of not let the same error haunt you next time.
0
0

0
0
0

java developer

We bring to you five common mistakes that are committed by both new and experienced Java developers.

1. Capitalisation Errors

This mistake is the one of the most common ones committed by almost every fourth Java developer whether a beginner or a pro. And in majority of times, it really becomes difficult to understand that when and where this particular mistake was made as usually we do not notice the capitalisation errors.

These errors are especially common in long programs that contain long function names. If you find strange errors in case of a function, it is always suggested to check capitalization. There is a huge possibility that you might have forgot to use capitalisation or have used it at the wrong place.

2. Comparing (.equals vs. = =)

It is a mistake that is more often done by beginners but we have even seen a few expert coders doing this. The reason is that many developers are not very comfortable coding in Java and thus ends up in making mistakes.

The == operator compares only two string object references, if the two references points to same object. On the other hand, the equals function compares the content of text that is present in String objects.

01 String string1 = ”Hello World!”;
02 String string2 = ”Hello World!”;
03 boolean doEqual = false;
04
05 if (string1 == string2) {
06 doEqual = true;
07 }
08
09 // vs.
10
11 if (string1.equals(string2)) {
12 doEqual = true;
13 }

Those two strings will not pass == operator and shows (doEqual = false). But the strings will pass .equals method and show (doEqual = true). The reason behind this is they are equal in value and also do not reference same object.

3. Array Index Out of Bounds

An index is used for accessing a particular element of Java array. Take for example, an array that contains five elements have indices zero to four.

If you use an index that is less than zero or more than four, it will result in Array Index Out of Bounds Exception. While you access a particular array, you should check the indices before you use them.

4. Integer Data Types

You will come across different integer data types in Java. If you end up in choosing the wrong one, it may lead to wasted memory. For an instance, the maximum value of 32-bit int type is 2,147,483,647.

So if you use an int for representing a person’s age, it does not make any sense. A smaller byte type is considered a better choice.

5. Operator Precedence

It is essential to understand Java’s priority of operators. Otherwise, expressions like “10 * 2 + 4” might be misleading and ambiguous. And without a complete understanding, the program can give unexpected results.

Original Article Post by EFY times News

7 thoughts on “5 Common Mistakes Made By Java Programmers”

  1. In your mistake 2 both compares will return true. If you declare the String in the class there will only be one String in memory. This is because String is immutable, there is no reason to allocate two references for the same value. If the strings were created using new String("Hello World!") instead your example would work as each String would have its own memory address because you told the compiler that is what you wanted. It is important to understand why the .equals and == work.

Leave a Reply

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

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>