News on Monday
more>>
SharePoint Tech Report
more>>


   

 
 
Download Current Issue
ISSUE 2/1/2010 PDF

Need Back Issues?
DOWNLOAD HERE

Receive the print Edition?


 
blogs tab
Visual Studio 2010 Release Candidate Available Today
A Visual Studio 2010 release candidate is available on MSDN.
02/09/2010 09:45 AM EST

Is Microsoft eyeing Office subscription pricing?
Microsoft may be preparing to offer a new Office pricing option called "union," which charges the same for cloud as on-premises.
02/01/2010 09:38 AM EST

Facebook rewrites PHP runtime
Facebook is about to open source its own PHP runtime, written from scratch for speed.
01/30/2010 08:53 PM EST

 

Events calendar tab
2/9/2010 to 2/13/2010
San Francisco
IDG World Expo

2/10/2010 to 2/12/2010
San Francisco
BZ Media

2/17/2010 to 2/25/2010
Atlanta
Python Software Foundation

2/19/2010 to 2/20/2010
Los Angeles
SCALE

2/21/2010 to 2/24/2010
Las Vegas
IBM


 
Most Read Latest News Blog Resources

More Thoughts on Tiger




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.


Share this link: http://www.sdtimes.com/link/27969
 

Add comment


Name*
Email*  
Country     


  • Comment
  • Preview
Loading