Posts in the “java” category

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.

Design Patterns in Java

I've recently started writing a series of articles on Design Patterns in Java, i.e., Design Patterns explained using Java source code examples. Although it will take me a little while to create each design pattern example, this page will eventually contain links to all of those examples.

If you're not familiar with software design patterns, they're described on Wikipedia like this:

[toc hidden:1]

Java String array examples (with Java 5 for loop syntax)

[toc]

Java String array FAQ: Can you share some Java array examples, specifically some String array examples, as well as the new for loop syntax that was introduced back in Java 5?

Sure. In this tutorial, I’ll show how to declare, populate, and iterate through Java string arrays, including the newer for-loop syntax. Because creating a String array is just like creating and using any other Java object array, these examples also work as more generic object array examples.

How to use ‘static import’ in Java

I was just reminding myself how to write a generics class in Java, and for some reason while I was doing that I wanted to use the Java 'import static' capability so instead of typing this:

System.out.println("foo");

I could just use this:

out.println("foo");

The only thing you have to do to make this happen is to use this import static statement at the top of your class:

How to compare String equality in Java

Java String comparison FAQ: Can you share some examples of how to compare strings in Java?

If you’re like me, when I first started using Java, I wanted to use the == operator to test whether two String instances were equal, but that’s not the correct way to do it in Java.

[toc hidden:1]

Java examples of the Law of Demeter

Summary: The Law of Demeter is discussed using Java source code examples.

Whenever you talk to a good, experienced programmer, they will tell you that "loosely coupled" classes are very important to good software design.

The Law of Demeter for functions (or methods, in Java) attempts to minimize coupling between classes in any program. In short, the intent of this "law" is to prevent you from reaching into an object to gain access to a third object's methods. The Law of Demeter is often described this way:

An Easy Rules example (a Java “rules engine”)

I’m considering using Easy Rules as a simple “rules engine” in my Android Football Game application, primarily because (a) there are a ton of “rules” involved in having the computer call offensive and defensive plays, and (b) I’m trying to find a way to simplify that code and make it more maintainable.

The Easy Rules website has a Hello, world demo you can look at to get started, but after that, here is my example.

[toc hidden:1]

How to use multiple regex patterns with replaceAll (Java String class)

[toc]

Java FAQ: How can I use multiple regular expression patterns with the replaceAll method in the Java String class?

Here’s a little example that shows how to replace many regular expression (regex) patterns with one replacement string in Scala and Java. I’ll show all of this code in Scala’s interactive interpreter environment, but in this case Scala is very similar to Java, so the initial solution can easily be converted to Java.

Java int, double, float, and mixed-type arithmetic rules

[toc]

Java FAQ: What are the rules about Java arithmetic (multiplication, division) involving mixed data types?

While working on a math problem in Java just a little while ago, I realized that I wasn’t comfortable with the Java mixed-type division rules. That is, I wondered if the result of this equation:

3 / 2

the same as the result of this equation:

3 / 2.0

or this equation:

3.0 / 2.0

A Java Factory Pattern (Factory method) example

Java Design Patterns FAQ: Can you provide an example of the Factory Pattern in Java?

Sure. In this article we'll look at a small-but-complete example of the Factory Pattern ("Factory Design Pattern") implemented in Java.

[toc hidden:1]

Java enum examples/tutorial (an enum reference page)

[toc]

Java enum 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.