Category Archives: Java

Java Memory Management in Practice: Video Notes

This post contains the notes that accompany the video tutorial JMM in Practice on YouTube. On this video you can learn the basic principles of how the JVM performs garbage collection and manages the heap space.

Download the Source Code

You can download the source code from this GitHub repository.
It is a Maven project that you can directly import into your favourite IDE.

Installing and Running Visual VM

The Visual VM download page is located here.

To install Visual VM you just need to unzip the downloaded archive into a directory of your choice.

To run Visual VM invoke the binary for your operating system, passing the options for the JDK and user directory (this is the location where you want Visual VM to store data files).

For example, on Windows:

visualvm.exe --jdkhome <path to JDK> --userdir <path to user directory>

 

Once Visual VM is running you can activate the Visual GC plugin as follows:

1. From the menu select Tools -> Plugins
2. In the popup window select the tab “Available Plugins”
3. Check “Visual GC” from the list and click on the Install button
4. Close the popup

You should see the “Visual GC” tab now available on Visual VM.

Watch the Video

You now have all the tools you need to follow the video and replicate the experiment on your system.

Debugging JDBC Connections with SQuirreL

Most Java applications use JDBC drivers to connect to a persistent store or database. Different databases however use different JDBC drivers and connection URL formats, and it can be very useful in case of problems to have a way to test that you are using the correct type and version of JDBC driver for your database and the correct connection parameters (connection URL, login credentials, etc) without having to write any code. Squirrel SQL is a universal Java database client that can be used for this purpose.

Squirrel SQL is open source, entirely written in Java, and can be used to connect to any database (SQL or NoSQL) for which there exists a JDBC driver. In this article we will explore how quick and simple it is to test a JDBC connection with Squirrel SQL.

Installation

The first step is to download the Squirrel SQL installer which can be found here:

http://squirrel-sql.sourceforge.net/

Note: if the URL has changed just type “Squirrel SQL client” on your favourite search engine to find it.

The installer is an executable JAR file, therefore you need to have a Java Runtime Environment installed on your machine. To run the installer, double-click on the JAR file or type on the command line:

$ java -jar squirrel-sql-<version>-install.jar

Follow the setup wizard to select the installation directory, keeping the defaults on the plugin selection screen. I also recommend selecting the option to create a desktop shortcut.

Squirrel SQL shortcut icon

Setup

The installation directory will contain the squirrel-sql startup script (.bat in Windows or .sh in Linux). Run the script for your platform or double click the desktop icon to run Squirrel SQL.

Squirrel SQL main screen

Squirrel SQL main screen

We will now use Squirrel SQL to connect to an Oracle database. The same steps can be followed to connect to any type of database, SQL or NoSQL, for which there is a JDBC driver.

Of course you need to know some essential details about the database you are running such as version, the host name or IP address and the port number. If you don’t know how to collect such information consult your database documentation.

The database I am going to connect to in this example is an Oracle Database 11g Express Edition Release 11.2.0.2.0 running on localhost. The Oracle instance identifier (SID) is “xe” listening to port 1521.

Knowing the version of the database you are running will help you identify the JDBC driver you need. Normally database vendors provide a JDBC driver download website. Oracle’s is located at the following address:

http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html

The site lists the drivers for the various Oracle versions. In this case I need to download the driver for version 11g release 2. I will select the JDBC thin driver (type 4) because it is platform independent as it is entirely written in Java and use Java sockets to connect directly to the database. If you are connecting to one specific database the Type 4 thin driver is normally the recommended choice.

Download the JDBC driver for your database and save it locally. The driver I have downloaded is a Java Archive (JAR) file named ojdbc6.jar.

JDBC driver file

Installing the driver

Connecting to a database with Squirrel SQL is quite simple. First you install the JDBC driver for your database, then you create an “alias” where you configure the parameters to connect to a specific instance. Once connected you can browse the database objects or issue SQL commands.

To install the JDBC driver, click on the Drivers tab on the left hand side of Squirrel’s main screen. You will see a list of drivers appearing in alphabetical order, where you need to locate the driver for your database. In this example, we will select (with a double click) “Oracle Thin Driver”.

Squirrel SQL Driver Setup

Squirrel SQL Driver Setup

The following dialog displays showing the driver’s name, example URL and class name:

Driver Setup Dialog

Driver Setup Dialog

Now select the “Extra Class Path” tab, and click on the [Add] button. Browse to the location where you saved the JDBC driver you downloaded in the previous step, and click [Open]. The driver will be listed. Click OK to complete the installation. You will be returned to the driver’s list, and if all is OK a check mark will appear next to the installed driver.

Making the connection

Now that the JDBC driver is installed, the last step is to create an alias to connect to the database instance you are running.

To accomplish this task select the “Aliases” tab on the left-hand side of Squirrel’s main screen, then click on the blue + (plus sign) icon. The “Add Alias” dialog will display:

Coonection Setup Dialog

Coonection Setup Dialog

From the driver’s drop-down list select the JDBC driver you installed in the previous step. Give the alias an arbitrary name that describes the instance you are connecting to, fill the missing details in the JDBC URL (hostname, port number, database name), enter user name and password, then click [Test] to test the connection. Adjust the parameters if necessary until the test succeeds. You now have the correct driver and connection parameters to use in your application or to configure a data source in your application server.

Towards Functional Java: Closures

This is the third post in the Towards Functional Java series. In the previous posts we examined how to dynamically define functions for later execution using method references and lambda expressions. The capability to create and manipulate functions at runtime is at the core of functional programming.

Sometimes a lambda expression may access variables that are defined outside of its scope. These are called free variables. Consider the following code:

public class Printer {
	
  public void printSubstring(String text, int beginIndex, int endIndex) { 
    Runnable r = () -> {
      System.out.println(text.substring(beginIndex, endIndex));
    };	
    new Thread(r).start();
  }

  public static void main(String[] args) {
    Printer printer = new Printer();
    printer.printSubstring("Hello!", 0, 4);
    printer.printSubstring("My name is John", 3, 10);
    printer.printSubstring("How are you today?", 4, 11);
  }
}

 

The Runnable implementation accesses three variables that are not defined in the scope of the lambda expression: text, beginIndex, endIndex. These variables are defined in the scope of the enclosing printSubstring method which may have terminated by the time the thread is executed. Normally when a method completes execution its local variables are removed from memory.

The program however executes correctly and the lambda expression somehow retains the values of the enclosing method arguments. A function or block of code along with the captured values of its associated free variables is called a closure.

Java lambda expressions are therefore closures but with an important restriction: they only close over values but not variables. If we try to modify the value of a free variable inside a lambda expression we get a compiler error. Let's modify the Runnable implementation slightly to demonstrate this:

Runnable r = () -> {
  System.out.println(text.substring(beginIndex, endIndex--));
};	

 

We have tried to decrement the value of the free variable endIndex in the lambda expression. The compiler complains with the following message:

Local variable endIndex defined in an enclosing scope must be final or effectively final

Although it is not required that free variables be declared final, effectively they are not allowed to change. In other functional programming languages there is no such restriction, but Java had to reach a compromise since to implement “real closures” would require a massive effort and possibly compromise backward compatibility and/or support for other JVM languages.

The “closure for immutable variables” implemented by Java 8 lambda expressions is a pragmatic solution that still achieves function portability without causing too much inconvenience to the programmer.

In the next post of this series we will look at more Java 8 functional features. Feel free to leave any comments or suggestions. Thank you for reading.

Towards Functional Java: Method References

In the previous post of the Towards Functional Java series, we saw how to create a reference to a method implementation using lambda expressions and functional interfaces, passing it as an argument to methods in the same way we are used to pass object references.

Note: to try out the examples in this post, you need a Java 8 development environment. If you do not have one, you can follow this tutorial to set one up.

In the following JUnit test, we implement the functional interface Comparator so that it compares two strings by length (instead of alphabetical order) using a lambda expression, and assign the implementation to the variable comp. We then use the variable name to pass the implementation to the Collections.sort method to sort a List of strings. The API doc can be found here:

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-

@Test
public void lambdaExpressionTest() {

   Comparator<String> comp = (str1, str2) -> 
       Integer.compare(str1.length(), str2.length());

   List<String> list = new ArrayList<String>();
   list.add("abcd");
   list.add("abcdef");
   list.add("ab");

   Collections.sort(list, comp);

   assertTrue(list.get(0).equals("ab"));
}

 
Now, suppose the above functionality was already provided by a method of an existing class, as in the following example:

public class AlternativeStringComparison {	
   public int compareByLength(String str1, String str2) {
      return Integer.compare( str1.length(), str2.length());
   }
}

 
You can refer to the compareByLength method directly using the following notation:

@Test
public void lengthComparisonTest() {	
   AlternativeStringComparison comp = new AlternativeStringComparison();
      
   List<String> list = new ArrayList<String>();
   list.add("abcd");
   list.add("abcdef");
   list.add("ab");

   Collections.sort(list, comp::compareByLength);

  assertTrue(list.get(0).equals("ab"));	
}

 
The expression comp::compareByLength is called a method reference. In this case we are creating a reference to an instance method, using the notation object::instanceMethodName.

If the instance method is defined by the same class of the receiver object, you can use the notation Class::instanceMethodName. For example, the String class defines an instance method to perform a case-insensitive string comparison. See the API doc:

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-

You can create a reference to it as follows:

@Test
public void caseInsensitiveComparisonTest() {

   List<String> list = new ArrayList<String>();
   list.add("bobby");
   list.add("Andrew");
   list.add("john");

   Collections.sort(list, String::compareToIgnoreCase);

   assertTrue(list.get(0).equals("Andrew"));
}

 
Likewise, it is possible to create a reference to a static method with the notation Class::staticMethodName. Consider the following class:

public class AlternativeStringComparison {	
   public static int compareByLen(String str1, String str2) {
      return Integer.compare( str1.length(), str2.length());
   }
}

 
You can create a reference to its static method as follows:

@Test
public void staticLengthComparisonTest() {
		
   List<String> list = new ArrayList<String>();
   list.add("abcd");
   list.add("abcdef");
   list.add("ab");

   Collections.sort(list, AlternativeStringComparison::compareByLen);

   assertTrue(list.get(0).equals("ab"));	
}

 
Constructor methods can be also referenced using the notation Class::new. We will look at practical applications of constructor references in a later post when discussing Java 8 streams.

In summary, four types of method references have been introduced in Java 8:

Type Notation Notes
Reference to a bound instance method object::instanceMethodName  
Reference to an unbound instance method Class::instanceMethodName  
Reference to a static method Class::staticMethodName  
Reference to a constructor Class::new  

In the next post we will look at more Java 8 functional features. Feel free to leave any comments or suggestions. Thank you for reading.

Towards Functional Java: an Introduction to Lambda Expressions

A major feature of the Java 8 release is the language-level support for lambda expressions, which brings Java into the realm of functional-style programming. Other object-oriented programming languages such as Scala, JavaScript and Python had lambda expressions built in from the start, and now Java has joined the rank.

Why is this interesting? Functional programming (FP) is based on the recursive evaluation of pure functions, whose output depends solely on the input arguments to the function, avoiding side-effects (i.e. changing the state of objects). This immutability results in code that behaves more predictably and performs better in concurrent and parallel systems. This is the first of a series of posts that will explore functional programming in Java, and the starting point is to understand lambda expressions (also known as closures or first-class functions).

So, what is a lambda expression? Put simply, it is a concise syntax to create a reference to a method implementation for later execution. In Java, lambda expressions are used in conjunction with functional interfaces. A functional interface is a plain old interface that defines just one single abstract method, and is marked with the @FunctionalInterface annotation.

It works like this: you define a variable of a functional interface type, and assign a lambda expression to it. This variable can then be passed around just like any other reference, but instead of containing a reference to an object, it contains a reference to a method implementation.

Confusing? Do not worry; it will all become clear with a few practical examples. By the way, to follow the examples in this post, you need a development environment that supports Java 8. If you don’t have one, you can follow this tutorial to get it setup.

For our first example, we will use the Runnable functional interface, whose API documentation can be found here:

https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html

The Runnable interface has been around for a long time, and if you wrote multi-threaded programs in Java you will be familiar with it; it is used to define tasks for later execution in a separate thread.

If you look at the source code of Runnable, you will see that it is annotated with @FunctionalInterface. It is not mandatory to use the annotation, in fact any interface that declares a single abstract method can be used as a functional interface; but it is good practice to annotate it so the compiler will check that it is a valid definition.

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

 
By the way, if you don’t know where to find the source code of the Java library classes, this post will show you:

http://robertovormittag.net/learn-from-the-experts-a-look-at-the-java-source-code/

Enough talk. Let’s write some code. Create a Java console application on your IDE, and place this code in main()

public static void main(String[] args) {
   Runnable r = () -> {
   System.out.println("Hello lambda!");
   };

   Thread t1 = new Thread(r);
   t1.start();
}    

 

Code analysis

First, we have declared a variable r of type Runnable, and assigned a lambda expression to it. This automagically becomes the implementation of the run() method declared by the interface.

Next, we have built a Thread object passing to the constructor our Runnable implementation, and started the thread. If you run the app, the thread will run and the string “Hello from a lambda expression!” will be printed on the console.

Syntax analysis

Let’s now take a closer look at the syntax of the lambda expression. All lambda expressions consist essentially of three parts:

  • a set of parameters (in parenthesis)
  • an arrow (or rocket) operator
  • a body (with the method implementation)

In this case, because the run() method takes no parameters, we have an empty parenthesis at the start of the expression.

Let’s look at another example, this time with parameters, and implement the java.util.Comparator functional interface, defined here:

https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html

This interface declares the abstract method

int compare(T o1, T o2)

which compares its two arguments for order, and returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Note from the API documentation that Comparator also declares other non-abstract, static and default methods but that is OK; it still is a valid functional interface, as it declares only a single abstract method.

We will now implement this interface so that it compares two String objects based on length. Here is the code:

public static void main(String[] args) {

   Comparator<String> comp = (String str1, String str2) -> {
         return Integer.compare( str1.length(), str2.length());
   };

   int result = comp.compare("ab", "abc");
   System.out.println(result);
}    

 
Here we declare the variable comp of type Comparator<String> and assign it to the lambda expression, starting with the two String parameters in parenthesis, followed by the rocket operator, followed by the function body which returns a value. We use the Integer class utility method compare() to compare the length of the String arguments.

https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#compare-int-int-

The variable comp becomes a reference to our method implementation, and can be used in a way similar to the way object references are used. In the next line, we use comp to call our implementation passing two arguments “ab” and “abc”, capturing the return value in the variable result, which is printed to stdout.

If you run the application, you will see the value -1 printed, as expected, since the length of “ab” is less than that of “abc”.

An even more concise syntax

If the compiler can work out the type of the method parameters (in this case String), it is not even necessary to specify them (this feature is called type inference). And, if the method body is limited to a single expression, you can omit the angle brackets {} and the return keyword.

So our implementation can be written even more concisely like this:

public static void main(String[] args) {

   Comparator<String> comp = (str1, str2) -> 
      Integer.compare( str1.length(), str2.length());

   int result = comp.compare("ab", "abc");
   System.out.println(result);
}    

 
If you run the new version of the app, you will get the same result. How much concise you want to be is simply a matter of personal preference and programming style. Personally speaking, I value more much more readability and clarity in the code than conciseness, but the choice is there.

We can make a more interesting use of our Comparator implementation by applying it to the List sorting algorithm in the Collections utilities:

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-

This utility sorts the specified list according to the order induced by the specified comparator. Try the following code:

public static void main(String[] args) {

   Comparator<String> comp = (String str1, String str2) -> {
      return Integer.compare( str1.length(), str2.length());
   };

   List<String> list = new ArrayList<String>();
   list.add("abcd");
   list.add("abcdef");
   list.add("ab");

   System.out.println(list);
   Collections.sort(list, comp);
   System.out.println(list);
}    

 
Here, we pass comp (the reference to our implementation of Comparator<String>) as the second parameter to the Collections.sort method.

If you run the app, you can check from the console output that, after the call to Collections.sort, the list has been sorted according to our Comparator implementation.

We covered a lot of ground, it is time to summarize. This is what we have learned so far:

  • What is a lambda expression
  • What is a functional interface
  • How lambda expressions and functional interfaces work together
  • The syntax of lambda expressions
  • A couple of practical examples using threads and collection algorithms

In the next post we will look at method references. Feel free to leave any comments / suggestions. Thank you for reading.

Learn from the Experts: A Look at the Java Source Code

The best way to learn and improve coding skills in any programming language is to look at code written by expert programmers. The Java Development Kit (JDK) comes with the source code of the complete Java Runtime Library; it is located in the src.zip archive under the JDK installation directory.

java-src

The location of the source file archive in the JDK 7 installation

Obviously your main reference when using the Java library classes should be always the API documentation, which can be found online or downloaded separately from the JDK downloads website. Here is the URL for the Java 8 API:

https://docs.oracle.com/javase/8/docs/api/

However, it can be very instructive to take a look at the source code of some of the key classes in the library, such as those in the java.lang and java.util packages.

Using Eclipse to view the Java Runtime Library Source Code

First, make sure you have the JDK selected as the default installed JRE. To do this select from the Eclipse menu: Window → Preferences. The following dialog pops up:

Setting up the JDK as the default JRE in Eclipse Mars

Setting up the JDK as the default JRE in Eclipse Mars

Select Java → Installed JREs

If the JDK is not there, click [Add…] and follow the prompts to select your JDK installation directory. Then make this JRE default by selecting the checkbox next to it. Now click [Edit…]. The following dialog pops up:

Setting up the runtime library source attachment

Setting up the runtime library source attachment

Expand the rt.jar library (this is the runtime binary) and click on [Source Attachment…]

The source attachment configuration dialog

The source attachment configuration dialog

Browse to the external location of your src.zip and click [OK]. Apply and close all parent windows.

Now to view the source file of a library class, e.g. LinkedList, either select from the menu:
Navigate → Open Type or use the shortcut [CTRL]+[SHIFT]+[T]

The Eclipse Open Type dialog

The Eclipse Open Type dialog

Type the name of the class, or select it from the list. The source code will open in the Editor.

the Eclipse editor showing the source code for LinkedList.java

The Eclipse editor showing the source code for LinkedList.java

Happy studying!!