Posts in the “java” category

Java 5 for loop syntax example

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 JFrame size: How to set the JFrame size

Java JFrame FAQ: How do I set the size of a JFrame?

Solution: There are several different ways to set the size of a Java JFrame, but I generally use the setPreferredSize method of the JFrame class in combination with the Java Dimension class, like this:

jframe.setPreferredSize(new Dimension(400, 300));

A JFrame size example

Here's the source code for a complete "JFrame size" example, showing how to use this setPreferredSize method call in an actual Java program.

Java Timestamp example: How to create a “current timestamp” (i.e., now)

Java date/time FAQ: When working with the Timestamp class, how do I create a “Java current timestamp”? For instance, how do I create a JDBC Timestamp object to represent the “current time” (“now”)?


You can create a “current time” JDBC Timestamp in just a few lines of code, using the Java Calendar class and a java.util.Date instance, as shown in this example code:

[toc hidden:1]

Java: How to create and throw a custom exception


Java exceptions FAQ: How do I create a custom exception in Java?

As a solution, here’s a quick example that shows how to create and throw a custom exception class in Java. In this tutorial I'll demonstrate how to:

  • Create a custom exception class in Java
  • Throw our custom Java exception
  • Catch our custom exception, and
  • Look at the output from our custom exception when we print a stack trace

A `printf` format reference page (cheat sheet) (C, Java, Scala, etc.)

Summary: This page is a printf formatting cheat sheet or reference page. I originally created this cheat sheet for my own programming purposes, and then thought I would share it here.

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.

Java: How to square a number


Java math FAQ: How do I square a number in Java?

You can square a number in Java in at least two different ways:

  1. Multiply the number by itself
  2. Call the Math.pow function

A Java enum switch/case statement example

Java enum FAQ: Can you share a Java enum switch example, i.e., how to use an enum with a Java switch statement?

In my earlier Java enum examples tutorial, I demonstrated how to declare a simple Java enum, and then how to use a Java enum with a variety of Java constructs, including a Java switch statement, a for loop, and an if/then statement.

What is a Java NumberFormatException?

Java exception FAQ: What is a Java NumberFormatException?

Answer: A Java NumberFormatException usually occurs when you try to do something like convert a String to a numeric value, like an int, float, double, long, etc.

The best way to show a NumberFormatException is by example, so here’s an example where I intentionally write bad Java code to throw a NumberFormatException:

Java “file exists” testing

Java file FAQ: How can I test to see if a file or directory exists in Java (or Scala)?

Solution: Use the Java File.exists method. Here’s an example that shows the basic technique:

File tmpDir = new File("/var/tmp");   // create a File object
boolean exists = tmpDir.exists();     // call its 'exists' method

The exists method of the Java File class returns true if the file or directory exists, and false otherwise.

Tomcat connection pool - a Tomcat JNDI DBCP connection pool example

Here's a quick demonstration of how to create a Tomcat connection pool (database connection pool) using the Tomcat DBCP library.

I'm not going to go into a detailed explanation here of how Tomcat DBCP works, other than to say that it works for me, and I've tried to include everything here that you'll need to implement your own Tomcat DBCP database connection pool in your web applications.

Java temporary file: How to create and delete temporary files

Java File I/O FAQ: How do I create a Java temporary file? Also, after I create a temporary file, when is it deleted?

I just ran into this question on the Mac/Java mailing list, so I thought I'd write a quick "Java temporary file" test to explore this. Here are the results.

Java JMX tutorial: A “Hello world” JMX application

Java JMX FAQ: Can you share a simple Java JMX example?

Here is some sample Java source code for some JMX tests that I created recently. I got a large percentage of this code from Sun's JMX MBean tutorial when I first started working with JMX, and I think it's some decent "Hello World" starter code. The main thing I've done here is (a) clarify what they've written and (b) added a shell script to start the JMX application. (I'm also about to publish some other JMX source code that I know I wrote myself.)