IMHO What Java lacks
Up to this day, I have coded in Java for more than 6 years. Of course, I enjoy Java, its compilation engine, and above all: with Java I need not handle pointers myself, which is a great progress (I suffered a trauma when I was younger, with C/C++ pointers).
Thanks to Java 5 (Tiger), many improvements are now available. Among them, the most I use are generics and the new for
loop, ala foreach
.
Nonetheless, I think Java lacks some features I encountered in other languages. Of course, I admit that implementing new features into such a complex project like the JVM is a hard issue. But I throw my ideas, may Sun developers feel free to accept or reject them.
- Multiple heritage is impossible.
- Default parameters values are unavailable:
public Toto(int i, String sz){ // do something } public Toto(){ this(0, “tata”); } public Toto(int i){ this(i, “tata”); } public Toto(String sz){ this(0, sz); }
In PHP (or Python), all these functions may be summed up to:
[php] function Toto(i = 0, sz = "tata"){ // do something }
- Exception handling is too robust:
Let’s consider this function:
[java] public void toto() throws Exception1, Exception2, Exception3, Exception4{ // do something }
Now, in Java we need to catch the exceptions like this:
[java] public void tata() throws Exception4{ try{ toto(); } catch(Exception1 e1){ e1.printStackTrace(); } catch(Exception2 e2){ e1.printStackTrace(); } catch(Exception2 e2){ e1.printStackTrace(); } // throws the uncaught Exception4 }
Obviously, we can catch a generic java.lang.Exception, but let’s acknowledge there is more elegant way :-(( I would like to have this code:
[java] public void tata() throws Exception4{ try{ toto(); } catch(Exception1, Exception2, Exception3 eee){ eee.printStackTrace(); } // throws the uncatched Exception4 }
Plugin de coloration syntaxique
J’ai installe le plugin de coloration syntaxique| pour DotClear.
Je dois dire que c’est pratique… et tres efficace:
[cpp] while (*p++=*q++); fprintf("toto %d", 18);
[mysql] select * from toto where titi = 15
Merci aux auteurs, je vais m’amuser comme un fou! 😉
java.util.ConcurrentModificationException
Probleme rencontre ce matin en Java 5 (Tiger):
[java]
Vector toto = new Vector();
// ...remplissage du vecteur...
for (Object o : toto){
toto.remove(o);
}
Cela leve une exception de type:
java.util.ConcurrentModificationException
Au debut j’ai pense a un probleme de synchronisation, et donc j’ai ajoute un bloc synchronized(mutex){...}
, mais cela n’a pas fonctionne.
Mais en fait le probleme est different: apparemment cela vient de la facon dont le “foreach” a la Java gere le parcours des elements d’un vecteur (ou de tout autre objet iterable).
La solution que j’ai trouvee consiste a utiliser un “for” classique, qui fonctionne:
[java]
for (int i=0; i<toto.size(); i++){
Object o = toto.elementAt(i);
remove(o);
}
(je n’ai meme pas tente un remove(i)
, pour eviter les effets de bord sur les indices)
Neanmoins ce n’est pas ce qu’il y a de mieux, il aurait fallu utiliser un vrai Iterator
, mais pour etre franc j’avais legerement la flemme…