Top Ten Errors Java Programmers Make

Tags:

I read an article (linked below) on “Top Ten Errors Java Programmers Make“. The author has mentioned the errors which even the most experienced programmers often commit or the New to Java programmers may commit in the future. Not only has he listed the errors but also given the possible solution for the same. I found the article really informative.

One can read the article here.

I would like to add some more:

  • A .java source file can contain more than one class but it can contain atmost one Top level class (or interface) definition that is public and the name of the source file must be same as that of the public class (or interface).
  • Static methods cannot be overridden but can be hidden if they are not final (Read about Overriding vs Hiding here).
  • The objects created in the String pool are not subjected to GC until the class is unloaded by the JVM.
  • Only methods or code blocks can be marked “synchronized”.
  • Local classes cannot access non-final variables.
  • -0.0 == 0.0 is true.
  • Collection (singular) is an Interface, but Collections (plural) is a helper class.
  • continue must be in a loop (e.g., for, do, while). It cannot appear in case constructs.
  • Instance initializers are executed only if an object is constructed.
Do you have some key points in Java to share? Add it as comments to the post :)
Article Type: 
Opinion/Editorial
2.333335
Average: 2.3 (3 votes)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

ThomasMueller replied on Mon, 2008/06/16 - 9:54am

Use break in case statements.
String.trim() etc. returns the result.
Synchronize carefully to avoid Java level deadlock.
InputStream.read() etc. doesn't always read fully.
Double.NaN != Double.NaN.

 

David replied on Mon, 2008/06/16 - 10:45am

Very nice post

David Qiao replied on Mon, 2008/06/16 - 11:57am

My favorite one is many people think "abc"=="abc" is true. I even saw it in JDK source code.

Stefan Schulz replied on Mon, 2008/06/16 - 1:45pm

There is one top error beginners in Java make that also found its way into the referenced article: Java is always pass by value and never pass by reference.

Masoud Kalali replied on Mon, 2008/06/16 - 1:51pm

I would like to add one more string related item:

a.equalIgnoreCase("constant");

when a can be null.

 

Slava Imeshev replied on Mon, 2008/06/16 - 2:02pm


Some of the mentioned "errors" are not programming errors at all because the code could would not compile. Some  other are stylisitc isssues rather than errors etc. DZ: garbage.

Raveman replied on Mon, 2008/06/16 - 2:04pm

omg, tell me you dont make those mistakes!! i think many of your friends would make a lot of less mistakes if they use ide, but maybe you work at sun and you care about compilation errors.

 both lists are very wierd, but maybe you are not programmers or just learning, then maybe its not that bad.

ArtB replied on Mon, 2008/06/16 - 2:20pm

Should mention that the best-way to avoid the "overrides" problem is with the @Override annotation. If the method doesn't actually override a real method it is a compile time error! Unfortunately that does not work for Interfaces :( (maybe we need a @Implements ?).

Although the *BIGGEST* mistake I see in Java is people who don't get OOP and treat objects as structs and just have getters/setters on their objects and all the manipulation in static methods.

 

@David Qiao

BTW, "abc"=="abc" IS CORRECT because modern (1.4+ IIRC) compilers will only create one object per string literal per class.  Though other forms such as ("ab" + "c") == "abc" may not be... but I'm not sure if javac is smart enough yet.

dustinywoods replied on Mon, 2008/06/16 - 2:34pm in response to: stefan.schulz

"Java is always pass by value and never pass by reference."

I think comments like this just fuel the confusion. There isn't just two options across all languages, value vs. reference, there needs to be more explanation related to Java. Java is pass by value for primitives. Java is pass by reference value for Objects.

Dmitriy Setrakyan replied on Mon, 2008/06/16 - 5:17pm in response to: thomasmueller

Thomas,

I think your list is much better than the original post which has many items that won't even compile (hence impossible to get burned by them). I myself remember long hours of debugging until I figured out that "Double.NaN != Double.NaN" ;-)

Best,
Dmitriy Setrakyan
GridGain - Grid Computing Made Simple

Stefan Schulz replied on Mon, 2008/06/16 - 6:31pm in response to: dustinywoods

"Java is pass by reference value for Objects."

This sounds funny, but I guess means the right thing: Java always is pass by value.

The meaning of this: for primitive types, a copy of the primitive value gets passed. And for objects, a copy of the reference to the object gets passed.

Pass by reference means to pass a reference/handle on the variable referencing an object, which allows to change the variable itself (i.e., where it "points" to). 

The problem I have with an article titled "Top mistakes" is that it should not introduce wrong terminology.

Hermann Rodrigues replied on Mon, 2008/06/16 - 6:35pm in response to: ArtB

"abc" == "abc" is TRUE because the Java Language Specification says that the expression must call String.intern() to construct both String objects and that the objects returned by intern() must be the same. And this is defined that way this since Java 1.0, I think. Just my 2 cents. ;) Best,

David Qiao replied on Mon, 2008/06/16 - 6:45pm

Really? I tried in intellij to inspect "abc"=="abc" and ite tells me false. I double confirmed that before posting.

Hermann Rodrigues replied on Mon, 2008/06/16 - 7:08pm

Strange. The Java Language Specification says the following about "abc" == "abc":

--------------------------------------------------------------

From http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5

Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.

Thus, the test program consisting of the compilation unit (§7.3):

    package testPackage;
    class Test {
            public static void main(String[] args) {
                    String hello = "Hello", lo = "lo";
                    System.out.print((hello == "Hello") + " ");   /// <----- This is the offending test case.
                    System.out.print((Other.hello == hello) + " ");
                    System.out.print((other.Other.hello == hello) + " ");
                    System.out.print((hello == ("Hel"+"lo")) + " ");
                    System.out.print((hello == ("Hel"+lo)) + " ");
                    System.out.println(hello == ("Hel"+lo).intern());
            }
    }
    class Other { static String hello = "Hello"; }

and the compilation unit:

    package other;
    public class Other { static String hello = "Hello"; }

produces the output:

    true true true true false true

--------------------------------------------------------------

As you can see, the first test case evaluates to true, as expected.

Best,

dustinywoods replied on Mon, 2008/06/16 - 7:17pm in response to: stefan.schulz

"The problem I have with an article titled "Top mistakes" is that it should not introduce wrong terminology."

Agreed. Below is just one example of why this gets misunderstood so often.

http://edocs.bea.com/wls/docs70/programming/classloading.html#1073491

Application Classloading and Pass by Value or Reference

Dino replied on Mon, 2008/06/16 - 7:32pm in response to: dustinywoods

dustinywoods wrote:

"The problem I have with an article titled "Top mistakes" is that it should not introduce wrong terminology."

Agreed. Below is just one example of why this gets misunderstood so often.

http://edocs.bea.com/wls/docs70/programming/classloading.html#1073491

Application Classloading and Pass by Value or Reference

 

Maybe he should add this to the "Top Mistakes" list.

David Qiao replied on Mon, 2008/06/16 - 7:33pm

Interesting. I guess it is probably a bug in IntelliJ expression evaluation tool. See below. It prints out true but the expression evaluation says false. It must use an older compiler when evaluating.

Walter Laan replied on Tue, 2008/06/17 - 4:40am in response to: js22605

Eclipse's watch/inspect while debugging gives false as well. I guess they both don't intern String when evaluating. "Hello".intern() == "Hello".intern() returns true.

Stefan Schulz replied on Tue, 2008/06/17 - 6:59am

Another typical error: implementing hashcodes based on mutable data. Always makes one surprise, why keys in hash-based containers cannot be found.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.