Sometimes Java Makes Me Sad

On any given day I go back and forth as to whether I truly like Java as a language or not. On one hand the automatic memory management is really quite nice. However it has it’s downsides. Normally I find little quirks of the language that I really wish it had. First off, Java does not have unsigned integers. Now, this is not a true tragedy, and one can work around this by using long integers and bit operations. However that is normally too much work when one is just looking for a safer type.

Another downfall of Java is the pointer hiding. There are times when I really really want to be able to see exactly what is going on. (Yes I know every object is technically a pointer) There is one place in particular this has come to annoy me. I was trying to have a function change the value of an int. This would be quite easy in C or C++, but in Java it is rather tricky. Since you can’t pass the int as reference (Java only has pass by value) and the Integer class is Immutable. One hack I found was to declare an int array with a single element and pass that to the function:

public class TesterClass
    public static void main(String args[])
     int[] fooArray = new int[1];

     fooArray[0] = 42;

     System.out.println("My val == " + fooArray[0]);
     System.out.println("My val == " + fooArray[0]);

    public static void myFunc(int[] changeMe)
     changeMe[0] = 12345;


Which brings me to my next topic… why is the Integer class immutable!? Say I have the Integer class inside an ArrayList. Well I can’t simply change the value of that Integer. Why? Well because the creators of Java feel the Integer class should be immutable. To me, this makes no sense at all. Yes, I could simply make my own Integer class which allows for changing the underlying value, but why should I? Isn’t this a basic feature that it should have?! It could just have a .setValue() function. Simple! In my mind this makes using the primitive wrappers inside an ArrayList rather useless because you’d have to remove and insert them all the time if you have to change their values. Now, perhaps this is how the creators of Java wanted it. I suppose I will never know.

Overall I do like Java as a language, but sometimes I get discouraged by the potential issues of speed loss, and by some of the things I mentioned above. The control C++ gives the coder is hard to give up, and for now C++ will remain my favorite language πŸ™‚


6 Responses to Sometimes Java Makes Me Sad

  1. James says:

    Nice article. I’d like to repost this on Javalobby with your permission.
    If you’re interested send me a mail.

  2. jintoreedwine says:

    Thanks, I will be sure to do that πŸ™‚

  3. kris says:

    Why do you need unsigned integers anyway?

    Java is a OO Language. C++ is more a “pimped” C with OO constructs. in a OO language/design there a more elegant solutions to change the value of a primitive type. have you thought about having the method return the primitive value? it is more explicit what is going on and therefore more maintainable.

    just my two cents πŸ™‚

  4. jintoreedwine says:

    “Why do you need unsigned integers?”

    Well, yes, I don’t _need_ them, but in some cases it is safer to use unsigned integers, like when iterating through an array, where you can’t have a negative index πŸ™‚

    “have you thought about having the method return the primitive value?”

    It is quite true that in the example I gave I could have easily returned the int, but sometimes you want the return value to indicate something else such as whether the value of the changed integer can be trusted. In that case I would have needed a special class which would allow me to return both variables. Then we end up with a lot of extra code, and potentially more confusing code, just to do a simple operation. I do agree, however, that if the return value is feasible, that it would be the best approach πŸ™‚

    Thanks for your comment πŸ™‚ you make valid points.

  5. tom says:

    So for me in the above comment you’ve actually touched on a real problem with Java. Integers are immutable with good reason, if you need a mutable integer class you can just create one – this is not a limitation of the language.

    What is a limitation of the language is that there isn’t an easy way to return multiple arguments from a method call – you can’t write:
    public (Point, int) foo (Point p) {
    return (p, p.y*3);

  6. LolJava says:

    Yep. Java is a real pain as far as I am concerned. I have to programme in it sometimes and the kind of apps that are written in Java (boring business programmes) don’t really need a static type system, or an OO straightjacket tight enough to choke you to death.

    I am just not convinced with the whole “enterprise” argument either. All Java developers seem to think that “enterprises” are making all their systems in Java behind closed doors. I don’t know what gives them that idea. Are a few large systems implemented in Java? Sure. But there are plenty of others that have been implemented in C++, or if it’s web based software, even PHP, for that matter.

    I also do not like that Java is a whole platform by itself, instead of just being a language with a managed runtime. Taking advantage of existing platform-specific tools is quite difficult, and you almost always feel you need a similar tool written in pure Java.

    All in all, it’s too bureaucratic and corporate and in trying to do everything doesn’t really do any one thing well.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: