preload
Sep 30

java_logo_2Just a short note on an item that bugs me about Java.  In C++, I tend to use exactly three kinds of method parameters.

  • A const reference.  I don’t want to copy it, but I promise not to modify it, either.
  • A reference.  I might modify it.
  • A copy.  I might modify my local copy, but not the original.  I’m getting a copy, after all.

Easy peasy in C++.  In Java?  Uh, I pass all objects by reference. The interface says nothing about modification.  Pass me a Map and maybe I’ll modify it, maybe not.  You don’t know, and you can’t enforce it.  Unless you pass me a Collections.unmodifiableMap().  Then my code might break!  There might be some odd little case left over where I do modify it, in spite of documentation comments to the contrary.  C++?  I declare it to be a const reference, and now the compiler will prevent me from modifying it.  I like that quite a bit.

I also like const methods, and you really can’t have const without const methods.  The Java way it to throw exceptions.  I’d rather have the compiler tell me something at compile-time than have the runtime system fail at runtime.  But I’m weird, I suppose.

  • Share/Bookmark

Leave a Reply