CHANNELS
 
 
 
 
 
 
 
 
ON THE WEB
 
 
 
 
PRINT EDITION
 
 
 
 
BZ MEDIA
 
 
 
 
ADVERTISER LINKS
 
 
 
 
 
LOADING...
LOADING...
 
 
AS OF 8/21/2008 6:58PM EST
More Thoughts on Tiger
By Allen Holub

July 1, 2004 — A month or so ago I talked about my disappointment in some of the features of Sun's Tiger (J2SE 1.5) release ("Missed Opportunities," May 1). This month I want to talk about a couple more problems that I see in the new version.

As before, bear in mind that there's a lot of good stuff in Java 1.5. It seems appropriate to devote column space to the features that might give you grief, however, since you're less likely to hear about it from the official documentation.

STATIC IMPORTSLet's start with static imports. Given this code:

package com.holub.test;

public class Constants

{ public static final String HELLO = "hello";

public static String global = " goodbye ";

public static String noNoNo()

{ return "NoNoNo!";

}

}

you can do this:

import static com.holub.test.Constants.*;

public class Foo

{ //...

public static void main(String[] args)

{ System.out.println( HELLO + global + noNoNo() );

}

}

There's no class-name prefix in front of the HELLO or global references or the call to noNoNO();

Importing a global constant like HELLO is relatively (though not completely) harmless. (The problem is that the static final reference might point to an object that can change state. A static final is not a constant. The lack of a true constant in Java has been a problem from day one.)

Nonetheless, I might be happy with static imports if they were restricted to static final fields. Unfortunately, they aren't. You can also use static import to access what amounts to global variables and functions. I've deliberately used the word "function" rather than "method" in the last sentence. The function noNoNO() is not really a method of a class of objects-it is not called to handle a message passed to some object; noNoNO() is a procedural function, pure and simple.

Yes, Virginia, you can now write a C program in Java. (In fact, if you're really a hard-core C programmer, they've added System.out.printf(), which works just like the C function with the same name.) The language is just too complex to justify its use in non-OO applications. Introducing global variables and global functions is a giant step backward.

GENERICSThe other big addition to Java is generics, which look a lot like C++ templates but are really quite different. People who know templates will be confused by generics, and people who don't know templates will not benefit at all from using the C++ syntax.

A C++ template is a "metaclass." Think of it as a macro whose arguments are type names, which expands to a class definition with the template arguments replacing placeholders in the template definition.

A Java generic is a means of telling the compiler to supply an implicit cast to method arguments or return values. You can use the two mechanisms to accomplish similar ends, but the differences are nontrivial.

Unfortunately, to use generics effectively, you have to master lots of complex and subtle details. I'm not sure that I welcome the introduction of a very-hard-to-get language feature into what used to be a simple object-oriented language. Fortunately, generics have very few uses in practice. The main application is the Collection classes (supplied in J2SE 1.5). Instead of inserting and removing generic Objects, you can specify a type.

The following code defines a LinkedList of Strings. There's no cast needed on the removeFirst() call because the compiler knows that the list has nothing but String objects in it, so supplies the cast.

LinkedList<String> c = new LinkedList<String>();

//...

c.addFirst("Hello");

String s = c.removeFirst();

People have been writing Java for years without generics, and nobody's really noticed that they were missing, other than a few die-hard C++ programmers. I programmed in C++ for eight years before moving to Java, used templates heavily in C++, and didn't miss them at all when I moved to Java.

One of the advantages of Java over C++ was that it didn't have the extra complexity of things liketemplates, which only gurus understood. This will be the case with generics as well. The loss of simplicity is unfortunate.
 


 
 
 
 
 
 
 
 
 
 
SUBSCRIBE TODAY!
 E-Newsletters:
  News on Mon/Thurs.
  Test & QA Report
  EclipseSource
 
 
JOB BOARD
 
PDF & PRINT EDITION
* Requires Resource Account!  LOGIN or SIGN UP

Download Current Issue!
ISSUE 8/15/2008 PDF

Need Back Issues?
DOWNLOAD HERE

Receive The Print Edition?
SUBSCRIBE HERE
 
REGISTER
 
GET NOTIFIED!
About all of the latest Resources
 
 
SD TIMES 100
It's time once again to
recognize the organizations
or individuals that have
demonstrated leadership in
their markets.