Posts in the “java” category

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.

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.)

Java: How to list of all the available Java/Swing fonts

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.

Java memory test - How to consume all the memory (RAM) on a computer

Here’s the source code for a Java “memory eating” program I wrote. Its purpose is to consume all of the memory (RAM) on a PC by allocating 1 MB byte arrays until it runs out of RAM:

import java.util.Vector;

public class MemoryEater
{
  public static void main(String[] args)
  {
    Vector v = new Vector();
    while (true)
    {
      byte b[] = new byte[1048576];
      v.add(b);
      Runtime rt = Runtime.getRuntime();
      System.out.println( "free memory: " + rt.freeMemory() );
    }
  }
}

It’s also important to run this Java memory eating program with the -Xmx option to make sure your JVM gets all the memory you want it to have:

$ java -Xmx1024M MemoryEater

The example shown above will try to consume up to 1,024 MB RAM (1 GB). As I write about in this How to control Java heap size (memory) allocation (xmx, xms) post, you can also specify the Xmx parameter in gigabytes, such as this setting for one gigabyte:

-Xmx1g or -Xmx1G

I wrote a program similar to this when I started getting the Windows blue screen of death several times. I figured there was some type of hardware failure going on, so I tried to consume all the RAM in the system and then read it back in to see if the hardware failure was related to the RAM.

A Java CRUD generator (and PHP, and Ruby, and ...)

Way back in the late 1990s, I wrote a Java CRUD generator, which was based on the work of someone else. It was a static code generator, but like future dynamic frameworks like Ruby on Rails, CakePHP, and others, it scanned the database and generated source code from the database table definitions.

Java best practice: Return a List, not a LinkedList

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.

Java JFrame: How to create, center, and display a JFrame

Java JFrame FAQ: How do I properly create and display a JFrame? While you're at it, how do I center a JFrame?

In this Java tutorial I'll demonstrate how to create and display a JFrame Other than the comments in the source code, I'm going to keep the code as simple as possible, so I can demonstrate how this works.

How to set the initial size of a Java JFrame

I've been getting back into the Java/Swing world during the last few days, and I thought I'd share some example code here. In my current application, one of the things I just worked on was setting the initial JFrame size to a value I liked.

Java StringBuffer versus String: When to use StringBuffer

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:

A Java Model View Controller example (Part 3)

<< Back to Part 2 of our Java MVC Example

MVC - Handling data-changing events

Whenever the user adds, edits, or deletes data, such as our Process data, all the proper GUI components need to be notified of this event so they can properly update their displays. In this application, when a user adds a new Process, this means that we need to update our MainProcessTable. We implement this as follows: