D vs Java

John Demme me at teqdruid.com
Sat Mar 18 20:30:28 PST 2006


Walter Bright wrote:

> I'm no expert on Java programming, but I get this question a lot: "What
> compelling reason does D have that would entice a Java programmer to
> switch to D?"
> 
> I know several of you have come to D from Java, and are expert Java
> programmers, so you folks' reasons would be very valuable.

I converted from Java a few years ago after just having finished a large
Java server and client system.

There are a lot of reasons, but since I tend to make gut decisions, it's
hard to say why I prefer D. I guess it boils down to three things: the lack
of a VM, it's close relationship to the metal (bare-metal) and it's
flexibility.

-Java's VM sucks up memory and CPU time like nothing else.  It takes some
time to start, and makes it difficult to interface Java programs with the
system and each other. D has not these problems.  The initialization that D
programs require is miniscule in comparison.

-D can (and does) interface with C.  Yes, yes, so can Java, but D can do it
in an easy and meaningful way.  This means that it's trivial for me to do
things like get an environment variable via a system call, whereas this is
non-trivial in Java.  

-Thanks to D being bare-metal, I can do a lot of cool stuff, like manual
memory-management (only where it makes sense, of course.)  For instance, in
my SAX parser, I make almost no memory allocations (and the few which I do
can be avoided.)  I'm able to do cool stuff like copy a String onto the
stack.  My linked list class could use a free-list of nodes and still use
new to instiante them and let the GC manage them.  Hell, we've got inline
assembly!  Again, my XML parser takes advantage of some text searching
instructions (via calls to mango.text.Text) on x86 machines.

-DMD is super-fast.  Automatically inlining function/method calls is great--
it actually speeds up my XML parser by 25%!

-All this and I haven't even talked about the language itself yet. 
Flexibility.  I can program however I like.  As a Java guy, I used to think
that pointers and goto statements were the devil, but now that I can use
them I've found that pointers help make more than a few things easier and
legible (if you understand pointers) and I've actually used the goto
statement a few times in ways which I would consider legimate.

-D's templates--  OMFG!!! (to use the parlance of our times.)  The
containers library I made for Mango makes extensive use of templates.  I
can't imagine a containers library without them, and this is only the most
basic usage of templates.  Compile time regular expressions!  Awesome! 
Generics? Phfff!  A child's plaything!

There are, however, several things which I prefer about Java:
-Application support.  Yes, the chicken-and-the-egg thing.  Since everybody
and their wicked step sister program in Java a lot of things are easier. 
It'd be nice if emacs had better D support.  I don't consider this point a
big deal, however.  It'll come with time.

-Java's package system: Java's protection attributes are consistent and
(generally) make sense. (D's are close.)  D's modules and packages are OK,
but not great.  I like that Java makes you put one class per source file
(to be public).  It makes it A LOT easier to find code.  That said, given
that not everything in D is a class, this system probably doesn't make
sense for D.  Perhaps that's another advantage to Java: compared to D it's
quite simple.  Where there's one or two reasonable ways to do something in
Java, there's 15 or 20 reasonable ways in D.  I both love and hate this.


All in all, I generally recommend D to people who understand C programming
but like Java's abstractions.  I think it's easier to blow your foot off
with D, but that's only because D is a compact fully-automatic shotgun with
builtin sniper rifle, whereas Java is a tank.

I probably got a bit off topic, but I like D because it's a swiss army
knife; it'll do whatever I want it to and really well.

~John Demme



More information about the Digitalmars-d mailing list