• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What are the 5 most common Java pitfalls?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I ask the senior members of this forum what are the most common pitfalls in Java programming for beginners.
Perhaps Scanner should be on the list. What else?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner how so?

I would say one of the top pitfalls I have noticed is not sticking to the naming conventions
 
Marshal
Posts: 80291
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you can find books which don’t understand Scanner, it is struck out of the beginners’ pitfalls list. It can go in the experts’ pitfalls list however . Actually, I think there is only one pitfall with Scanner, and that is misunderstanding nextLine(). People think it reads the next line, despite what it says in the API. More details in this recent post and its contained links.
There is no such thing as a commonest mistake in Java. There are however commonest mistakes in programming, and you will find the same mistakes in C#, C++, Eiffel, etc etc. In fact I often commit the supposedly commonest Java mistake in my FORTH programming.
If you Google, you will find a website with ten commonest Java mistakes. They are in order, No 10 first. I am not sure I agree with the order, but I am quite sure they were correct with what they called No 1.
The commonest pitfall I see on this forum is that people program as if they did not know what objects are, writing non‑object‑oriented code.
 
Campbell Ritchie
Marshal
Posts: 80291
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is that website. I find I am disagreeing with their first supposed error. For at least two reasons:
  • 1: It is a compiler error. Compiler errors are never severe, because you find out about them quickly.
  • 2: The error is the wrong way round. It goes on about non‑static access. On this forum, however, I see lots of people who mark things static which ought not to be static.
  • That is just my opinion, however, and the website does correctly and clearly described that error and its solution.
    Their No 6 is simply wrong.
    Note that although that page is labelled latest update 2006, it appears to have been written before the @Override annotation was introduced to pick up spelling errors when overriding.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't know the definitive most common Java pitfalls, but there are a number of things that many beginners will encounter.

    One is == versus equals(), especially with strings. Comparing strings with == doesn't work - you'll get false even if the two strings you are comparing have the same content. That's because ==, when used on variables of non-primitive types, compares references, not the content of objects. In other words, it checks if the two variables refer to the exact same object, not if the content of the two objects is the same.

    Another mistake is thinking that float and double variables have infinite accuracy. These types store floating-point values as binary fractions, and some decimal numbers, such as 0.1, cannot be stored with perfect accuracy in such a variable.

    Another thing that often comes up is the classpath, especially when working with packages. You have to understand what directories you have to put in the classpath and how Java searches the classpath for Java classes to load.

    One more is understanding that variables of non-primitive types are references, and not objects themselves. We have a campfire story to explain that.

    Another one is understanding that if you create an array, it will initially be filled with null references. If you want an array of objects of a certain type, you'll have to initialize each element of the array yourself. People forget this and wonder why they get a NullPointerException when trying to use the elements of their newly created array.
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Here is that website...Note that although that page is labelled latest update 2006, it appears to have been written before the @Override annotation was introduced to pick up spelling errors when overriding.


    Thanks for that. Quite a good list. Bookmarked.

    Although I suspect there will be a lot that disagree with number 6: as Jesper says, object-type variables and parameters ARE references, so in fact Java passes everything by value. I also agree with his points that are not in the list.

    For a bit more detail about point 7, have a look at the AvoidTheEqualityOperator page.

    Winston

     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote: have a look at the AvoidTheEqualityOperator page.


    Neat
     
    lowercase baba
    Posts: 13091
    67
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In my mind, the single biggest pitfall of newbies learning to PROGRAM is that they think that the first thing you do is sit at your computer and start typing in Java (or whatever). I don't think anybody does a good enough job explaining that a significant part of programming involves thinking, not typing.
     
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Here is that website. I find I am disagreeing with their first supposed error. For at least two reasons:

  • 1: It is a compiler error. Compiler errors are never severe, because you find out about them quickly.
  • 2: The error is the wrong way round. It goes on about non‑static access. On this forum, however, I see lots of people who mark things static which ought not to be static.
  • That is just my opinion, however, and the website does correctly and clearly described that error and its solution.



    They're not saying it's severe, they're saying it's common. And so it is; I see plenty of code on this forum that makes this mistake. I made it a lot myself when I started out. Now marking things static which ought not be static is not an error, it's a bad design decision, so it has no place on that list. And thanks for the link.
     
    Campbell Ritchie
    Marshal
    Posts: 80291
    433
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another pitfall is not following strict and consistent spacing and indentation conventions. All is well until you get your {} mismatched and can’t understand the compiler error. If you indent correctly, mismatches are easier to avoid and easier to find.
    Another frequent error which is very easy to overlook is the empty statement.
     
    Jesper de Jong
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:Although I suspect there will be a lot that disagree with number 6: as Jesper says, object-type variables and parameters ARE references, so in fact Java passes everything by value. I also agree with his points that are not in the list.


    Yes, the website is wrong when it says that in Java "objects are passed by reference".

    In fact, objects are not passed at all. As Winston said, Java only uses pass-by-value. In the case of non-primitive types, references are passed by value. Passing a reference by value is not exactly the same as passing by reference. Saying that "objects are passed" is inaccurate - since variables are not objects, but references to objects, references are passed, not objects.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'd add one more common error that is related to point 9 in Campbell's list:
    Getting the signature of an overridden method wrong.

    This occurs most frequently when overriding equals(), where many beginners make the mistake of trying to override it with:
    public boolean equals(SomeClass o) { ...

    What this actually does is to overload the equals() method, rather than override it, because the signature should be:
    public boolean equals(Object o) { ...

    However, as already pointed out, this can be easily prevented by remembering to add @Override to all overridden methods (or methods that you think are overriding ).

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 80291
    433
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dennis Deems, “It all depends what you mean by error.”
    I did say, “in my opinion,” and I think everybody here would have a different list.

    Ed Dablin, thank you for starting an interesting discussion.
     
    dennis deems
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Another pitfall is not following strict and consistent spacing and indentation conventions. All is well until you get your {} mismatched and can’t understand the compiler error. If you indent correctly, mismatches are easier to avoid and easier to find.
    Another frequent error which is very easy to overlook is the empty statement.


    The corollary to this is:
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    fred rosenberger wrote:In my mind, the single biggest pitfall of newbies learning to PROGRAM is that they think that the first thing you do is sit at your computer and start typing in Java (or whatever).


    Amen.

    I don't think anybody does a good enough job explaining that a significant part of programming involves thinking, not typing.


    Well here's mine: StopCoding.

    Winston
     
    dennis deems
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Dennis Deems, “It all depends what you mean by error.”
    I did say, “in my opinion,” and I think everybody here would have a different list.


    Fair enough -- I guess it really boils down to not understanding what static means and how to use it appropriately.
     
    dennis deems
    Ranch Hand
    Posts: 808
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    fred rosenberger wrote:I don't think anybody does a good enough job explaining that a significant part of programming involves thinking, not typing.


    Well here's mine: StopCoding.
    Winston


    I like this very much.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dennis Deems wrote:I like this very much.


    Thanks Dennis. I basically created it to save myself from early-onset CTS.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 80291
    433
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dennis Deems wrote: . . .

    Amended version
     
    Ranch Hand
    Posts: 214
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another pretty common pitfall is mixing up primitive types and their respective wrapper classes, for example int vs. Integer, Java 1.5+ makes it easy to forget about the difference with autoboxing
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    D. Ogranos wrote:for example int vs. Integer, Java 1.5+ makes it easy to forget about the difference with autoboxing


    Indeed. Which is why I'm not a big fan of it.

    Winston
     
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    D. Ogranos wrote:for example int vs. Integer, Java 1.5+ makes it easy to forget about the difference with autoboxing


    Indeed. Which is why I'm not a big fan of it.



    Much nicer than what we had before, though. But ideally I'd prefer the language to just make the difference between primitives and Objects more transparent. E.g. some languages have reference types and value types, but they have a common subclass and you can add and remove them from collections without either clunky wrapper code or invisible transformations.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Matthew Brown wrote:E.g. some languages have reference types and value types, but they have a common subclass and you can add and remove them from collections without either clunky wrapper code or invisible transformations.


    Mmm. Nice. I guess my complaint with autoboxing in Java is that it violates type safety and can run you into some tough-to-spot problems (eg, programs running much longer than they "should" do, or issues with overloaded methods). I hate to say, but I still tend do it the old-fashioned way - especially for unboxing.

    Either that, or "once an Integer; always an Integer".

    Winston
     
    I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic