Posts in the “java” category

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]

A Java array length example

Java array FAQ: How do I determine the Java array length, i.e., the length of a Java array?

Answer: While you might logically expect there to be a length method on a Java array, there is actually a public length attribute on an array (instead of a length method). Therefore, to get the Java array length, you just have to access this array length attribute.

Here's a source code example that demonstrates how to determine the Java array length for an array named toppings:

Java “file exists” testing

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

Solution: To see if a file exists in Java code, use the Java File.exists method. Here’s an example that shows the basic technique:

A Java Month class (that can be used in a calendar application)

Summary: This blog post shares some source code for a Java Month class that you can use in your applications, such as a Java calendar applications.

I was just cleaning up some old bits, and ran across a Java Month class that I've used on several calendar-related projects. If you happen to be working on a Java calendar application, and need a class to represent the months in a year, this source code may be helpful, so I thought I'd share it here.

Converting a Scala class file to decompiled Java source code

As a Scala newbie, I'm curious about how the process of converting a Scala class back to Java source code works. What I really want to see is how my Scala source code is converted to Java source code. Besides plain old curiosity, I think that understanding more about how Scala works can also be very important to my understanding of Scala (such as the apply() method, and so on).

A Java "extract method" refactoring example

Summary: A Java "extract method" refactoring example is demonstrated.

If you don't own a copy of Martin Fowler's Refactoring book, I highly recommend it. The basic idea of refactoring source code is that the code "smells" in one way or another, and there are a variety of ways to improve smelly code. More specifically, Mr. Fowler describes refactoring as this:

A simple RxJava 2 “Hello, world” example

As a brief note, and assuming that you already know a little bit about RxJava, here’s the simplest possible RxJava 2 “Hello, world” example I think you can create:

package hello;

import io.reactivex.Observable;

public class HelloWorld {

    public static void main(String[] args) {
        Observable<String> observable = Observable.just("Hello, world");
        observable.subscribe(System.out::println);
    }

}

A Java method that returns a random boolean value based on a probability

If you ever need a Java method that returns a boolean value based on a given probability, I can confirm that this method works:

/**
 * `probability` should be given as a percentage, such as
 * 10.0 (10.0%) or 25.5 (25.5%). As an example, if `probability` 
 * is 60% (60.0), 100 calls to this function should return ~60 
 * `true` values.
 * (Note that Math.random returns a value >= 0.0 and < 1.0.)
 */
static boolean getRandomBoolean(float probability) {
    double randomValue = Math.random()*100;  //0.0 to 99.9
    return randomValue <= probability;
}

Creating and populating a Java ArrayList (Java 9 and newer)

If you want to create and populate a Java ArrayList with Java 9, 11, and newer, you can use this syntax:

List<> ints = ArrayList<Integer>(List.of(1,2,3));

As shown, this uses the usual ArrayList constructor and the Java List.of method. Once you have an ArrayList like this you can continue to add new elements to it as usual:

ints.add(4);

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)it.next();
}

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.

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.