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


   

 
 
Download Current Issue
ISSUE 3/15/2010 PDF

Need Back Issues?
DOWNLOAD HERE

Receive the print Edition?


 
blogs tab
Microsoft lifts the hood on IE9
Microsoft is previewing IE9.
03/16/2010 01:10 PM EST

People Power enters the green technology market
People Power launches SuRF Developer's Kit for developers looking to get into green technology.
03/16/2010 11:51 AM EST

Windows Phone 7 will not support HTML 5
Windows Phone 7 will not support HTML 5.
03/15/2010 05:51 PM EST

 

Events calendar tab
3/14/2010 to 3/18/2010
Seattle, Wa.
SHARE

3/15/2010 to 3/18/2010
Santa Clara, Calif.
TechWeb

3/16/2010 to 3/19/2010
Las Vegas
Penton Media

3/17/2010 to 3/19/2010
Las Vegas
TechTarget

3/22/2010 to 3/25/2010
Santa Clara, Calif.
The Eclipse Foundation


 
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