Why I hate Java

22 June 2009
Java is a language I used most extensively during my earlier undergrad days, and occasionally since for the odd project. The problem is that most times I've used it recently it becomes apparent why I have usually avoided it when given the choice. The most recent case being a quick revision of Sockets. In C reading from a (TCP) socket and displaying the received data is easy enough:

char buffer[1024];
int bytesRead = recv(socket,buffer,1024,0);
if( bytesRead == -1 )
    close(socket);
else if( bytesRead > 0 )
    printf(">%s\n",buffer);

In Java doing the same thing involves 2 intermediate storage formats. And for good measure they are allocated in different ways:

ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
buffer.clear();
int bytesRead = srvSC.read(buffer);
if( bytesRead == -1 )
    srvSC.close();
else if( bytesRead > 0 )
    {
    byte[] bites = new byte[1000];
    buffer.flip();
    buffer.get(bites,0,bytesRead);
    String sBuf = new String(bites,"utf-8");
    System.out.println("IN>"+sBuf);
    }

You can guess that working out the sequence of format conversions was a process of trial-and-error. Java tries to abstract away from underlying hardware, but in cases where you have to deal with bits and bytes it is a massive pain. To make matters worse Java's architecture is to make abstract classes for everything in sight and then make implementing classes which in turn are extended; I suspect the presence of both Socket and SocketChannel classes is partly due to a design mistake somewhere along the line..