Java 5 FAQ: Can you share some examples of the Java 5 for loop syntax?

Sure. As a bit of background, in the days before Java 5 you could create a for loop to iterate over a collection of strings like this:

// assumes there is a method named "getList()"
List list = getList();

for (Iterator it = list.iterator(); it.hasNext();) {
  String value=(String);

Java 5 for loop syntax

That’s not too bad, but with the release of Java 5 your for loops can now be a little tighter, like this:

Java for loop FAQ: Can you show me an example of a Java for loop?

Answer: Sure. Here's an example of a Java for loop using the original Java syntax:

Java while loop FAQ: Can you show me an example of a Java while loop?

Sure, here's a Java while loop example:

package com.devdaily.javasamples;

public class WhileTest
  public static void main(String[] args)
    int i = 0;
    while (i < 5)
      System.out.println("i = " + i);

This example prints out the value of the variable i until i gets to 5, at which point the loop stops.

Question: Can you show me an example of the Java if/then syntax?

Answer: Sure, here you go:

Java switch case statement FAQ: Can you provide an example of a Java switch/case statement syntax?

Here's a sample Java method that takes an int argument and attempts to turn that int into a month string, using a Java switch case statement to make that decision:

As I started to mention in another blog post, your Java code will be more flexible when you learn to return more-general object references. In most cases other developers only need to see your interface, not your implementation. Put another way, does it matter to anyone else if you used a LinkedList or an ArrayList? If it doesn't matter, then return a List, or perhaps even a Collection.

I find that I learn a lot -- especially initially -- when I can see source code examples. To that end, here's some sample Java code showing a LinkedList. This uses Java syntax prior to Java 5:

A friend sent me a link to this story about Google planning a service to store user's data. I read the article, and all I can say is "no thanks". I might use it as a backup service, but I sure don't want to make it my primary copy.

Count me in as one of the football fans who are upset about tonight's game between the Green Bay Packers and the Dallas Cowboys being shown on the NFL Network. Okay, compared to a lot of other things this is a pretty trivial concern, but moving games to a premium cable channel sets a bad precedent. The NFL can do whatever they want, but this being America, consumers can also say "Screw you NFL", and this is your time to vote.

Note: Amazingly, I wrote this review way back in 2007. I just tried to use Poseidon UML again in 2010, and I wouldn't change a word of it. (Okay, I'll change one thing: I now use UMLet instead of MagicDraw UML.)

A nice feature of Eclipse is that you can easily generate hashCode and equals methods for your Java class using the editor. You'll really appreciate this capability when you need to create these methods whenever you're doing anything related to sorting, comparisons, comparators, etc.

I just learned that Eclipse can automatically create (or suggest) variable names for you. The suggestions are based on the class of the object you're about to create, and any characters you've typed for the variable name so far.

For example, if I'm in a method and I type Connection, followed by a space, then press the [Control][Space] keys, Eclipse adds a variable named connection, so my line changes to Connection connection.

Using Eclipse, if your cursor is positioned in between the parentheses of a method call, the [Control][Spacebar] keystroke will show you the signature of the method, i.e., the parameters that the method accepts. I just read where the documentation shows that you should use the [Control][Shift][Spacebar] keystroke to see method parameters, but I've found that [Control][Spacebar] works fine for me.

I've just recently started using Eclipse a lot, and I just typed System.err.println() one more time than I cared to. I just looked it up, and Eclipse also has templates, and one of the pre-built templates lets me type sys, then hit the [Ctrl][Spacebar] keystroke. This brings up a list of options I can choose from, one of those options being to insert the System.err.println() text in place of my sys text.

Java File I/O FAQ: Using Java, how can you test to see if a file or directory exists?

Java test to see if a file or directory exists

Answer: Here's a sample Java method that shows this test. All you have to do is use the exists() method of the File class ( to perform this test.

Develop with pleasure. That's the IntelliJ IDEA slogan, and at least through Version 5 I think they nailed it on the head.

So this product review is simple: If you're a Java developer go get an eval copy of IntelliJ IDEA right now and give it an honest eval. You won't be sorry.

Working on tag support for this blog, I've cut a little sample code out of a Java servlet, and I'm showing it below. I was looking at the HttpServletRequest, and was curious about the difference between request.getPathInfo(), request.getPathTranslated(), and request.getRequestURI(), so I created this demo code. Here's the example code from the servlet:

AppleScript Finder FAQ: How do I get the full Mac OS X Finder path in an AppleScript script?

I don't know if this Java/JDBC sample program will help anyone, but I thought I'd share it here. It's a variation of a program I use to connect to a JDBC database (in this case a Postgresql database) whenever I need to look at some information. In this particular case I wrote the program because I didn't have access to the psql command-line tool, so I created this Java program, compiled it, and ran it