So, I just compiled a little list of things that, I believe, make me dislike Java everyday a little more.
This is not a hateful flamewar-guided post. Just my thoughts about a subject.
Java has a lot of good points. Starting with community, specs, lot of libraries and so on. I use it everyday in my work and have been used it for almost 4 years. The things I’ll talk about today, is more related to concepts adopted by Java language itself.
So, if you only programmed in Java until today and/or have Java as a mother for you and can’t hear any kind of shit about it, go away now. If you’re not, keep reading. (or go away too, I don’t really care)
Even though people say that Java is an object oriented language, well, it is not that much. It has some concepts of OO, like classes, object, inheretance, and so on… but, it still not be a pure OOP language.
For example, in OO, everything should be an object. Java primitives, for example, are not.
autoboxing and null behaviors
Like I said before, primitives are not objects, which means that you can’t
use they with Generics, for example. You can’t make a
ints, you have
Integer wrapper class instead.
The big problem is that the behavior of the primitives and their respective wrappers are inconsistent in my point of view.
IMHO, these wrapper classes should not be wrappers and primitives should not exist. One of the seven qualities to satisfy the OOP concept is that all predefined types are objects, which not applies to Java. Anyway, I wasn’t there when Java engineers take these decisions, so, I’ll not judge. But, I believe that they decide this because in the beggining, the Java Virtual Machine was slow, and have primitive types looks good to “make it fast”. Maybe it was a bad decision. Anyway, I really wish that at least these wrappers had the same behavior that their respective primitives. That would be awesome.
Well, If you still thinking that’s not inconsistent, I’ll give you what I think is the worst example:
Everyone knows that a
boolean should have only two states, in Java, for example,
false. That’s right. Now, let’s take a look at the
Booleans let you have three states:
null, which means
that’s not actually a trully bolean value. Two states != Three states.
Yeah, I know, all that thing about reference.
null means that the
Object doesn’t point to anything, for me, it would be nice if it automagically
false, avoiding people to do wrong things.Check if a
null doens’t make any sense for me, sorry about that.
Let’s take an example from Ruby, which is considered a pure OOP language. If you
try to evaluate an expression like
puts "not nil" if not nil, it will works,
and will print
not nil, as expected. This behavior is expected because in ruby,
everything is an object, including
nil itself (of
NilClass type). In example
nil are automagically evaluated to
false, which also is an object
FalseClass type), that’s just beautiful.
That’s the behavior I expect from Java. It would be really nice to use Java
like a object, isn’t? Take another Ruby example:
t = nil puts t.class # NilClass puts t.nil? # true puts "t isn't nil" if t # nothing, t is nil
In Ruby, every object has a
nil? method, and
nil also is an
object, which is awesome. I realy miss that behavior in Java.
I also know that is something that’s comming with Java 8, but, most languages have this functionally for years, and Java 8 is not ready yet. It kinda sucks.
Anyway, I’ll really like to believe that someday I’ll could use Java Methods as objects, like it should be in a pure OOP lang… (fingers crossed)
Well, would be really nice been able to do that. For example, we don’t have a coalesce method in Java’s Object, and I really want it! So, why not been able to do something like: