Java Basics

Commentary: Pass by value / reference / name / ...

By value. There is some confusion about Java parameter passing. Java uses pass-by-value and only pass-by-value. Actual argument values are copied into the formal parameter variables. There's no pass-by-reference. The confusion comes from the fact that, for objects, a reference is passed, but the reference is passed by value (ie, the reference value is copied into the formal parameter variable).

From the authors of Java: "There is exactly one parameter passing mode in Java - pass by value - and that helps keep things simple." The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling, section 2.6.1, page 40, 3rd paragraph.

Java's simple parameter passing. Altho there are many possible parameter passing features (see C++ for some), the Java authors choose to only include one simple idea - pass-by-value, with no default values or optional parameter (overloading often provides a satisfactory alternative), no variable length parameter lists (added finally in Java 5), no named parameters, no pass-by-reference, no const (a general Java issue) parameters, etc. Of these, the lack of pass-by-reference is the most annoying, now that variable length parameters lists have been added.

C++ programmers struggle with giving up pass-by-reference. I was upset that this powerful feature was missing, and was sure there must be some way to write swap, the standard test for pass-by-reference.

   public void swap(Object a, Object b) {
       ???? // Exchange a and b in calling program.
   }

But it can't be done. The fact that you may sometimes be able to change values in an object or data structure that is passed is not the same as being able to change the value of a variable (l-value) passed as an actual argument.

I still miss pass-by-reference. There have been frequent proposals to allow multiple method return values, which would solve most of these cases, but these proposals have been rejected by Sun, presumably because an efficient implementation would require a change to the JVM.

Some retreaded-from-C++ authors have had a problem giving up pass-by-reference in their texts, and ever more astoundingly, some even believe it is in Java (it took Deitel&Deitel 3 or four editions to figure it out).

Alternatives

Ruby is a relatively new programming language that has a lot of interesting features. If you think C++ or Java are the end points of function evolution, you might be interested in Ruby features, especially what they call "blocks", which are like method objects (sometimes called closures or continuations). It's not that the effect is impossible to achieve in Java, but that Ruby's simple syntax for it means that it makes the programs more, not less, readable to use them.

Another feature is Ruby's multiple return values (tuples), which I've also used in an earlier (now extinct) language, Prograph. Once you use multiple return values, is makes shoehorning everything into a single return value very annoying.

I've been occasionally reading Thomas's Programming Ruby, and find it very interesting, but I haven't really done anything with Ruby except for a few very simple test programs. What stopped me was that there is no good documentation on writing GUI programs. There are a couple of GUI libraries, but neither had sufficient documentation the last time I looked.