Posts in the “java” category

Java ‘array of objects’ syntax examples

Java array FAQ: Can you share some examples of how to create arrays in Java (Java object arrays)?

While I generally work with lists and maps in Java, I occasionally need to create object arrays in Java. Since I don't use arrays that often, I thought I'd share some examples here so I can have a handy Java array syntax reference.

A simple Java String array

I work with the String class a lot, and here's how to create a String array in Java:

A Java Robot class mouse and keyboard/keystroke example

Java Robot class FAQ: Can you show me an example of how to use the Java Robot class?

Answer: Um, yeah, sure ... I say that a little jokingly. Okay, what really happened is that while developing this Java Robot example code on my Mac, I had to reboot it about 10 times. When you use the Java Robot class, you're poking your head out into the native operating system, and if you mess up with your GUI events -- at least on a Mac OS X system -- a lot of bad things can happen.

Java two dimensional (2d) array examples

It's been so long since I create a two-dimensional Java array in I almost couldn't remember how to do it. Fortunately I did, and I thought I'd include my sample code here in case it will help anyone else.

So, in this quick tutorial, I'll show you how to create a two-dimensional array (2D array) of Java String objects, and then I'll show you how to access each element in the array.

How to create a two-dimensional Java array

First, here's how I create the two-dimensional arrays of strings with this code:

A Java instanceof array example

While working with various "Java instanceof" tests recently, my curiosity was piqued, and I thought I'd take a look at how the instanceof operator works when testing against a Java array.

Scala varargs syntax (and examples)

Scala FAQ: How do I use the Scala varargs syntax (or, What is the Scala varargs syntax)?

You can define a Scala function that accepts a varargs parameter like this:

def printAll(strings: String*) {
    strings.map(println)
}

The only magic in using the varargs syntax is adding the * symbol after the String declaration, as highlighted in this line of code in the function declaration:

Java: How to append text to a file with the FileWriter class

Java file writing FAQ: How do I append text to the end of a text file in Java?

The short answer is that you should create a FileWriter instance with the append flag set to true, like this:

BufferedWriter bw = new BufferedWriter(new FileWriter("checkbook.dat", true));

The rest of this article explains this.

Java enum examples/tutorial (an enum reference page)

Java enumerations FAQ: Can you share some Java enum examples, such as how to declare a Java enum, and how to use a Java enum in a for loop, if/then statement, and Java switch statement?

Sure. As described in the Sun/Oracle Java documentation, “you should use enum types any time you need to represent a fixed set of constants.” Let's take a look at some enum examples to see how this works.

A Java email address validation class

I thought I'd share the source code for my Java email address validator class. I'm not sure if there's a big need for it ... I wrote it a long time ago, and I think I created it because Java's javax.mail.internet.InternetAddress class wasn't validating email addresses as deeply as I wanted it to. For instance, I think it would allow the string "fred" to be a valid email address, but on the internet you really want to see something like "fred@foo.bar". So I think that's where this class comes from.

JDBC Timestamp - How to select a Java Timestamp field from a database timestamp column

Here's a JDBC Timestamp example that shows how to read a Java Timestamp field from a database timestamp column (a MySQL Timestamp field) in a SQL SELECT statement. I pulled this source code out of a real-world Java application, so I'll break it into small steps, and hopefully it will make sense.

The MySQL timestamp field definition

My Java application uses a MySQL database, including one table named commands that has a MySQL Timestamp field I need to read. The SQL definition for this MySQL table is shown below:

[toc hidden:1]

The Java ‘keytool’ command, keystore files, and certificates

Java keytool/keystore FAQ: Can you share some Java keytool and keystore command examples?

Sure. As a little bit of background, in creating my "Hyde (Hide Your Mac Desktop)" software application, I decided to venture into the world of commercial software, selling my app for a whopping 99 cents. While that price is trivial, creating the “software licensing” code for this application was anything but trivial.

I finally decided to use a Java licensing tool named TrueLicense to assist with the software licensing, and TrueLicense quickly led me down the path of learning about the Java keytool and keystore path. So that’s what this article is about: How to use the Java keytool command to work with private and public keys, and work with intermediate certificate files.

Java Iterator Design Pattern examples

Summary: The Iterator Pattern is demonstrated using Java source code examples.

The Iterator Design Pattern is one of the most simple and frequently used design patterns. The Iterator Pattern lets you sequentially move through a collection of objects using a standard interface, and without having to know the internal representation of that collection.

[toc hidden:1]

Chain of Responsibility Pattern in Java

The Chain of Responsibility Pattern is a design pattern whose intent is to avoid coupling the sender of a request to its receivers by giving more than one object a chance to handle a request. The Chain of Responsibility works like this:

Java BufferedReader examples

Java file FAQ: Can you share some examples of the Java BufferedReader class?

When it comes to reading character input streams, the Java BufferedReader class is extremely important, and I'll demonstrate this in several different source code examples.

[toc hidden:1]

Java/OOP: The Strategy Design Pattern in Java

Summary: A discussion of the Strategy Design Pattern using Java source code examples.

The Strategy Design Pattern consists of a number of related algorithms encapsulated in a driver class often named Context. A user or a client program typically selects the algorithm they want to use, although the Context class may also select the algorithm automatically.

The intent of the Strategy Pattern is to make the algorithms easily interchangeable, and provide a means to choose the appropriate algorithm at a particular time.