Java News from Friday, May 11, 2007

By day 5, one cup of coffee at a time is no longer enough:

Bill Venners holding two cups of coffee

James Gosling's Toys keynote put me to sleep a few times. Nothing earth shattering. Sun's jumping on the 2nd Life/virtual world bandwagon. It's not clear why. There's no business value there I can see. The only reason Warcraft/2nd Life/etc. are interesting is because you can assume a different, mostly anonymous persona. That won't work for corporate meetings. The robots were cute. They should have cut the time in half and stuck to the robots.

The first morning technical session is Brian Goetz on Effective Concurrency for Java. I snuck out off a three-hour version of this one at SD West a couple of months ago because it was too basic. I'm hoping this time he'll jump straight to the more advanced material. Yep. This is much more my speed. Unfortunately the room is way too crowded to take live notes. I'll have to go over the PDFs later. A few notes that stuck in my memory:

Moore's law stopped working in 2003. CPUs really haven't gotten faster since then.

Scalability and performance are not the same thing, and are indeed orthogonal. Scalability is how much more work you can do with more resources. Perfect/linear scalability is when you multiply CPUs by 10 and get 10 times as much work done in the same amount of time.

If your CPUs do not peg at 100% but you're still having performance problems, you likely have serialization problems. (This is not object serialization but rather chunks of your code that cannot run in parallel, only serially.) Serial code does not scale and does not benefit from more CPUs. Amdahl's Law defines exactly how badly serial code can impact your scalability based on the percentage of serial and parallel operations.

Immutability is not guaranteed by making fields private, setting them once in the constructor, and then never changing them. It is also necessary to mark them final, and even this only really works in Java 5 and later. This is due to weird optimizations the virtual machine may make. A different thread can read the chunk of memory holdings its copy of a field before the original value has been copied into it from the constructor.

Scott Delap and Barry Livingston are talking about Writing Testable Desktop UIs. I'm having trouble following this talk but it seems to involve interfaces instead of classes and mock objects. I don't like that. I much prefer to test the real objects in the real app.

For the next section I'm actually scheduled to go to something about AJAX, but several people have been singing the wonders of Scala to me this conference so I think I'm going to try to sneak into Martin Odersky's Scala session instead. It's in the afternoon of the last today so maybe enough people will have left early so there'll be some extra seats. Good. There's plenty of room. The audience is small but high powered. I'm sitting behind Bill Venners, Josh Bloch, Bruce Eckel, and Brian Goetz.

French language swaps meaning of experiment and experience.

In Java it is hard to reason about correctness of programs, hard to write domain specific languages.

Generalize and unify functional programming languages with object oriented programming. In particular it unifies

Scala is a functional language in the sense that every function is a value. However assignment is supported. Functions can be anonymous, curried, and/or nested. Functions are classes and can be subclassed!

Added a pure object model (ints are objects). Added pattern matching and higher order functions. Added novel ways to abstract and compose programs.

2,000 downloads a month.

Scala interoperates seamlessly with Java class library.

The Scala for loop syntax is ugly (IMHO). However you can write in a prettier functional style that uses higher order functions instead of loops.

Scala has type inference. It is statically typed.

There are -> and <- operators. I'm not totally sure what they do.

Approximately 50% as many lines of code as Java.

Very bad: Scala supports operator overloading. I'll have to double check but I suspect his operator overloading breaks arithmetic precedence.

Implicit conversions can convert one type to another type to heal a type error. This enables ints and complex numbers to be added, even with the int on the left hand side.

Scala compiles to JVM byte code. Performance is comparable to Java except on startup time.

Romain Guy and Chet Haase are Filthy Rich Clients: Talk Dirty to Me. Custom components should only override paintComponent(), not paint(). (In AWT you should override paint().) You only override paint() in Swing if you want to handle the frame, border, children, and so forth.

Custom components can be based on images you draw in Photoshop or some such program.

Do not call Image.getScaledInstance(). It's slow. Use Graphics2D.drawImage() instead.

Use the clip (g.getClipBounds()). Only paint the regions that need to be repainted.

Use compatible images.

The timing framework is better than they said in the keynote. The keynote did it the wrong way to make JavaFX Script look better. The timing framework makes for much easier animation than they claimed on Tuesday.

Buy the book.

For the final session of the conference I decided to take one more stab at figuring out just what this closures brouhaha is all about. I'll listen to Neal explain it for the fourth time.

"Closures are intended to be simple."

Gafter's examples are too artificial to be convincing. Possibly they're too short. He shows an example and explains how it would be easier to do if Java had closures, but I can never figure out why we'd want to do the example in the first place, with or without closures.

I do think I finally understand his lock example, but it's not compelling. His joking objection to the current approach? It won't fit on on slide. I agree that not having a withLock keyword is an annoyance, but it's only a minor annoyance. Ditto his app-specific Time example. Still the idea of eliminating a lot of the boilerplate is somewhat appealing and starting to grow on me. "We probably should have a tutorial" Uh, yeah.

I wonder if one of my objections is simply the syntax. I dislike all the weird little line noise that comes with most of the proposals. I like keywords. I think one of the small improvements Java made over C++ was changing :: to extends. I don't like < in JavaFX Script. Words are longer to type but easier to read, and readability trumps writability.

For example,

{int x => x+1}

takes an int and returns x+1.

Why not

int (x) {
 return x+1;


Why can't a closure look just like a function except for the lack of the method name?

Maybe a prototype compiler by the end of the year.

Non-final local variables can be referenced in Gafter's closures. However, a closure author can prevent this and require that all local variables referenced in the closure be final.

"In my opinion there will not be any language changes in JDK 7." JDK 8 is more likely target.

"Ultimately it's going to be Sun Microsystems' decision."

One thing that worries me is that the JCP process almost guarantees that something will be done once a group starts, whether it makes sense or not. No one joins a working group who thinks the proposal is a bad idea. The group is self selected. A small group that is willing to do the work can get almost anything adopted into Java. It's too hard to reject bad ideas.