Sometimes I hate Java
Java is a nice language. It got a lot of ways to do efficient OOP, a garbage collector, a wide library of functions, and a really big community. Coming from C/C++, starting Java wasn't very hard for me. I still prefer my beloved C/C++, especially because of the precise memory management. But as almost everyone says, each problem has his solution, and Java is a good tool to have. But sometimes, like those last days, I hate Java. Maybe it's because I'm not as good in Java as I am in C/C++. Maybe it's because I am in the very special cases where Java sucks. Maybe I'm just going the wrong way. But I think it's more about the flaws of Java (and I'll be happy if you prove me wrong).
Templates, templates everywhere
The first thing and most annoying for me is the template. It seems it's not that common in Java to use (or for me, abuse) of templated class/method. I know it's not always the best solution, sometimes complexifying the code even for the JVM/compilator, and I try to avoid using template in those cases. For example, I'm currently trying to make the most generic DAO service possible in a GWT application I'm working on. The goal is to put the logic and genericity in a framework, helping us manage database access very easily and properly. The logic is described in this image :
And when I say that I like templates, the DAO interface looks like that :
public interface Dao<T extends Model<V>, V> /* Our Model items can be used with String ID (UUID) or Long ID */ { public T put(T object); public T get(V idObject); public List<T> getList(Map<String, Object> filterMap, int start, int length); public <W extends Dto> List<W> getList(Class<W> dtoClass, Map<String, Object> filterMap, int start, int length); public T delete(T object); }
And as you can see, I created a generic method to return a DTO list too. Described like that, it seems pretty, with maybe a little too much templates for those who are not familiar with them. But where I really don't like it, and mostly because of Java, in my opinion, is in my DaoFactory. The way I manage to get the right DAO seems pretty dirty to me :
Class<?> daoCls = Class.forName(DAO_PACKAGE + "." + className + "Dao"); Method method = daoCls.getMethod("getInstance"); Dao<T, V> obj = (Dao<T, V>) method.invoke(null);
And it's even dirtier with the DTO :
Class<T> dtoClass = (Class<T>) Class.forName(dtoClassName); Class<?> modelClass = dtoClass.newInstance().getModelClass(); Dao<V, W> dao = getDaoObject(modelClass.getName()); /* Same method as the previous one */
I know there must be a prettier way. But I didn't found it. And one of the biggest problem was the way I got the model class from my DTO. My DTO is templated with the model class it represents, but guess what : Java cannot return the templated class.
SERIOUSLY ? This is one of the stupidest thing ever. Say you have two model class : ObjectA and ObjectB, and a DTO for
ObjectA called DtoA. DtoA is implementing Dto<A>, an interface for all the DTOs. So if I pass an ObjectB to the
DtoA, I got a runtime error. But this same runtime cannot tell me what type of object DtoA is excpecting ? Why ? Is
there a reason ? I add to add a method called getModelClass() than can return ONLY a Class<ObjectA> in this case. And as
I need this method to be in the interface (well, at this time it became an abstract class more than an interface, obviously),
I can't even have it in static, because you can't override a static method (seems logic to me). So yeah, a non static method
returning a constant value because Java sucks. (At least on this point)
Dating time
Another horrible thing in Java, the dates. I think this is mostly because of the retro-compatibility with older programs, but
why did they do that in the first place ? The logic way would be to have a Date class, that you can construct from a timestamp,
a string, another Date object, or with the classic parameters (year, month, day, hour, minute, second). And if you want to add
two days, you just call the method add(DateType.DAY, 2). For a difference between two Date just a method called for example
diff (because yeah, another bad point about Java is that you can't overried the operator, but I won't get into that, I already
miss my C++ too much). But no, that would be too simple, too clean, too easy. In Java, you need at least two class to do that.
Date and Calendar.
And it's worth mentionning that you need other classes to parse string date (like GWT's DateTimeFormat, or SimpleDateFormat for examples).
And combining these classes, the next code sample shows how to start from a string date to end with a Date object with 2 added days :
String target = "Thu Sep 28 20:29:30 JST 2000"; DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy"); Date parsedDate = df.parse(target); Calendar cal = Calendar.getInstance(); cal.setTime(parsedDate) cal.add(Calendar.DAY, 2); Date result = cal.getTime();
Yeah, that's a lot of code just to do that.
And because the wide choice of standard libraries is supposed to be one of the best side of Java, those sort of things can really make me hate Java.
As I said, sometimes I hate Java. And when I use the word sometimes, it means that most of the time it's a language I enjoy. Not my favorite, but a very good tool. And I really hope someone prove me wrong on what I said, but for now, this is what I think.




