Posts in the “java” category

Java “abstract’ meaning: What does it mean when a method or class is abstract?

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:

Java String comparison FAQ: Why doesn't == work when comparing two String objects?

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:

Java version - what version of Java am I using

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:

Java Hello world application

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 HelloWorld.java. Then, compile it to Java bytecode with this command:

A Java tool to access Grand Central Dispatch (GCD) on Mac OS X

I just read this announcement on the Apple Java Mailing List about a new library/tool named GCDExecutorService.

As the announcement states, this is "an implementation of a Java ExecutorService that completes tasks (Runnables and Callables) by passing them to Grand Central Dispatch to be run." On his web page the author later writes, "GCDExecutorService is a Java ExecutorService (Executor) that runs tasks by passing them to Cocoa's NSOperationQueue."

Java regex examples - common regular expressions

Java FAQ: Can you share some examples of common Java regular expressions?

The following list shows some of the most common regular expressions that are used in computer programming. I haven’t tested any of these yet with Java, but I think most will work off the shelf, or should at least work with minor tweaks.

An interesting note is that there are several ways to write the same regular expression, and I intentionally tried to stress that in the examples below.

A Java regular expression example (featuring Pattern and Matcher classes)

Summary: Java regular expressions in Java 1.4, featuring regex expressions and pattern matching, using the new Java Pattern and Matcher classes.

The following Java example offers an introduction to regular expressions in Java 1.4. In this code we're creating a regular expression that can search for a date. Specifically, that date must be in a format of two digits, followed by a hyphen, followed by two digits, followed by a hyphen, followed by four digits.

This date pattern is created in this line of code:

A Java Preferences API example/tutorial

Using the Java Preferences API couldn’t be much easier. In a Swing application I was just working on I wanted to remember the last output directory a user accessed, and the following steps show all that I had to do: