[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Java

> Java certainly has lots of nice features, but I think you're being too
> on C++.  C++ has one feature that blows java away for performance-critical
> applications: you can define your own data structures at almost the
> level.  Try implementing your own resizable array class in java and see
> efficient it is.  There's no realloc for changing the size of the array;
> you have to build a whole new array and copy the contents of the old one
> into it.

C++ also has inlined data elements (roughly corresponding to expanded
objects in Eiffel, and to structs, as opposed to classes, in C#).  Cf.:
  class Window {
    Rect bounds;

  class Window {
    Rect* bounds; // or Rect&

There's no way to state this distinction in Java, CLOS, etc.

A couple of other niceties about C++ that don't get much press:

C++ distinguishes between pointers and references.  This is frequently
advertised as a way .  But this distinction also allows you to distinguish
between variables that may hold null, and those that can't (and some style
guides recommend using the distinction between pointers and references to
document exactly this distinction): in
  void fn(int* a, int& b) {...}
*a is either an int or undefined; b is an int.

This lets you express the distinction between CL 'integer and (or 'integer
null), or between Haskell Integer and Maybe Integer; a distinction that is
missing from the Java type system.

And C++ has unwind-protect!:
  class WithDatabaseLock {
    Database& _db;
    WithDatabaseLock(Database& db) : _db(db) { db.lock(); }
    ~WithDatabaseLock() { db.unlock(); }
  fn() {
  } // db is unlocked on exceptional or normal exit

This lets you abstract the behavior of bracketing code in the same way
macros and unwind-protect do in Lisp, in a way that you can't in Java (or a
number of other more dynamic languages).