Over a million developers have joined DZone.
Platinum Partner

Sometimes Java Makes Me Sad

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

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 :)

From http://jintoreedwine.wordpress.com

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}