[NBLUG/talk] Java v. C++ in Linux

mrp mrp at sonic.net
Tue May 6 20:39:01 PDT 2003


On Tue, May 06, 2003 at 07:14:12PM -0700, Edward Mendoza wrote:
> 
> I would like some input on the question of Java (the language, not the
> script) v. C++ in the Linux environment.

I've always been disappointed in g++ as a C++ compiler.  The java options
are much better, within the limitations of Java.  Does anybody have
experience with CodeWarrior for Linux?

> I would like to know if most Java and C++ programmers prefer to work in a
> Linux or windoze environment?

I prefer the linux/unix environment.  The lack of highly integrated IDEs
is offset by the great tools you can use.

> If Java is so great, then why hasn't it replaced C++ as the leading
> programming language?

Goody.. my favorite subject to pontificate about! Better in what way?
This has all the makings of a really good/awful holy war. Sorta like
Perl vs. Python argument.

C was flexible, but that also means that a lot of things that are never
a good idea are perfectly legal C.  For example, find the errors:

#define until(x) while(!(x))

char *strmunch(char *foo)
{
	char *p, buf[80];
	p=buf;
	until(p=NULL);
	{
		*p++ = tolower(*foo++);
	}
	
	return buf;
}

(I haven't tried to compile that, but I suspect it will, though
maybe with a warning).
	
C++ has far too much baggage from C. C has always made it very easy to
do the wrong thing. (i.e. strings represented as statically allocated
arrays with no bounds checking, etc.)

C++ solves some of those problems, but make others much worse.  Memory
management can be a real nightmare, and too many dangerous things can
happen "automagically".  (How many people have been caught off card by
an implicit cast or copy constructor?)  It's also very easy to create
massive memory leaks or dangling pointers in C++, because it's not
always obvious when something should be explicitly delete'd or when
it will happen automaticaly especially when the allocating code is
properly "hidden".  (Every beginning C++ programmer makes the mistake
of returning a reference to an automatically allocated object, only to
have to object deallocated as soon as the original goes out of context.

Syntactic niceties like const correctness are a Good Thing, but are
so difficult to get right that most people don't even bother trying.
Templates add flexibility, but are a nightmare both to read and debug.

Java (as something of a B&D language) makes you be much more careful,
and it's garbage collection algorithm means that you don't have to do
the hard design/thinking about memory management. The bounds checking and
exception handling mean that you don't have to worry (much) about buffer
overflow attacks or uncaught error states, and the really cool libraries
mean that you aren't constantly reinventing the wheel.  However, the UI
stuff is far from platform independent (write once, debug everywhere,
write again, repeat..)  The automatic memory management means that while
there aren't "leaks" per se, but you can easily write code that spends
80% of it's time in the garbage collection thread.  And, even with JIT
compilers, it can be slow, and speed is highly dependent on the skill
of the team that wrote the JVM that your code ends up running on.

Having said that... When I write in Java, I get working code far faster
and more reliably than when writing in almost any other language. However,
the limitations (speed/environment) mean that the number of problems 
that can be solved with Java are somewhat limited.

  -- Mitch



More information about the talk mailing list