Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

Java FAQ: Can you provide an example of the Java 5 autoboxing syntax?

Java FAQ: Can you show me an example of the Java 5 for loop syntax? (Java 5 and newer)

Answer: Sure. I've created a sample Java program to demonstrate the Java 5 for-each loop syntax, using both a List with pre-Java5 syntax, and a second example using Java 5 generics syntax.

Java 5 for loop syntax example

Without any further ado, here is the example code:

Java Fonts FAQ: How do I create a list of all the fonts available on the current platform?

Answer: To list all the fonts available to you in a Java application (a Java Swing application), I use the GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() method of the GraphicsEnvironment class, which technically returns an array of all the font family names it finds on the local system.

I think some things Amazon has introduced with their Kindle device are very interesting, but at $399 IMHO I think it's over-priced by at least $270, maybe $300-350. The only thing I can figure is that since they're not charging you for network access and bandwidth they need to get a big chunk of money from you at the start. That price is just way too high for me.

Yikes, according a Computer Associates report, Facebook's Beacon advertising system is more intrusive than advertised, tracking users who opt out or are not even logged in:

Hmph, well, I buried this story in my personal category, but a friend of mine tells me it's front-page worthy. So, here's a link to my blog titled "How much are you worth to your employer?"

The most common reason to use a StringBuffer instead of a String in your Java programs is when you need to make a lot of changes to a string of characters. For instance, a lot of times you’ll be creating a string of characters that is growing, as shown in this sample example program:

Recently I started using Fink on Mac OS X to install Unix open source software. Fink is easy to use, and simplifies the process of installing open source software applications on Mac OS X.

To learn about how to use Fink, just open a Terminal window and type this command:

fink --help

That brings up the following help text (the fink man page):

Java FAQ: How do I access/read system properties and environment variables?

If you've ever seen a Java compiler error message like "Cannot make a static reference to the non-static method doFoo" or "Cannot make a static reference to the non-static field foo", here's a quick explanation of these messages.

Java array FAQ: How do you create an array of Java int values (i.e., a Java “int array”)?

Answer: There are several ways to define an int array in Java; let’s take a look at a few examples.

1) Declare a Java int array with initial size; populate it later

If you know the desired size of your array, an you'll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:

When using Eclipse straight out of the box, if you click on a Java-provided class, like the FileWriter class, you won't get much help when the Eclipse Class File Editor is shown. All you'll see is some binary information, with these messages at the top of the file:

Source not found
The jar file classes.jar has no source attachment.
You can attach the source by clicking the Attach Source below:

(The message really should end with "by clicking the Attach Source button below:")

If you're just getting started with Java it may help to have a simple first class to look at. In the code below I'm showing a sample Java class named HelloWorld.

public class HelloWorld

  public static void main(String[] args)
    System.out.println("Hello, world");


To use this class, save it to your system in a file named Then, compile it to Java bytecode with this command:

Question: How can I tell what version of Java I'm using?

You can actually do different things on different platforms to figure out which version of Java you're using, and which versions of Java may be installed, but the one cross-platform (Unix, Linux, Mac OS X, Windows) thing you can do is open up a command-line window (a terminal on Unix and Mac, a Command windows on Windows) and type this command:

java -version

On my current Mac OS X version (10.4.10) the output from this command looks like this:

Converting a numeric IP address to a hostname with Java is straightforward, just use the InetAddress class. Here's a Java example that demonstrates this:

Looking for a gift for your favorite geek? How about a robot?

Just thumbing through a Sharper Image catalog, they're featuring robots like never before. Here are the robot toys, er, gifts, that they're featuring (with direct links to the robot URLs):

The == operator doesn't work when comparing two Java String objects, even if the Strings store the same content, because the == operator compares the two object references to each other. Since they aren't the same reference, they aren't equal.

In short, when comparing two Java String objects, compare them like this:

if (s1.equals(s2))
  // your code ...

and specifically do not compare two String objects like this:

An abstract class in Java cannot be directly instantiated; only its subclasses can be instantiated.

An abstract class may contain zero or more abstract methods. An abstract method is not defined in the abstract class. Instead its signature is declared, but it has no body. The implementation is left to the subclasses that are created from this abstract class.

Here's an example of an abstract class definition:

In short, static Java variables and methods are instantiated only once for a class. Because of this you’ll hear these referred to as class variables, as opposed to the more common instance variables. That’s because you can deal with these directly at the class level, without needing to have an instance of the class instantiated.

Java exception FAQ: What is a Java NoSuchMethodException?

Answer: Using Java, you can get a NoSuchMethodException when you're using reflection and try to dynamically use a method on a class, and the method does not actually exist. The following example Java class shows how this NoSuchMethodException can be generated: