Watching a Directory for Modifications in Java 7

NIO 2.0 in Java 7 added support for directory change notification known as a watch service. It lets you receive notifications, when a directory is modified. There are six steps needed to set up a watch service.

  • Creating a watch service
  • Registering a directory with the watch service
  • Retrieving a watch key from the watch service queue
  • Processing the events that occurred on the registered directory
  • Resetting the watch key after processing the events
  • Closing the watch service

A watch service is an instance of the WatchService interface. You can create a watch service for the default file system as:

WatchService ws = FileSystems.getDefault().newWatchService();

Click here to read the full post in PDF format.




Understanding Method Binding in Java

Method binding is a mechanism of associating a method call in Java code to the declaration and implementation of the method being called. When we mention the phrase “declaration of the method”, we are referring to the method signature, which consists of the method name, and the order and the data type of its parameters. When we mention the phrase “implementation of the method”, we are referring to the body of the method that executes at runtime as part of the method call.

The compiler always decides the signature of the called method. Depending on the method type (static, non-static, and interface), the compiler or the runtime decides what implementation of the method is executed at runtime.

Let us consider three Java programs listed in Listing 1, Listing 2, and Listing 3.

Listing 1: An Employee class

package com.jdojo.blogs.methodbinding;

public class Employee {
    public void setSalary(double salary) {
       System.out.println("Inside Employee.setSalary()");

Listing 2: A Manager class

package com.jdojo.blogs.methodbinding;

public class Manager extends Employee {
    public void setSalary(int salary) {
        System.out.println("Inside Manager.setSalary()");

Listing 3: A Test class to test method binding

package com.jdojo.blogs.methodbinding;

public class Test {
    public static void main(String[] args) {
        /* Create a Manager object and assign its reference to a
           variable emp of the Employee type
        Employee emp = new Manager();

        /* Call the setSalary() method */
        int salary = 12000;
        emp.setSalary(salary); /* Which setSalary() method is called.
                                  Employee.setSalary() or Manager.setSalary() */


Can you tell what is printed when the Test class is run?

Click here to read the complete post.


The Fork/Join Framework

Java 5 and Java 6 added several concurrency constructs to the java.util.concurrent package to help developers deal with problems involving advanced level of concurrency. The executor framework was one of the several great additions in Java 5. Java 7 added a new implementation of the executor service, which is known as a fork/join framework. The focus of the fork/join framework is to solve those problems efficiently, which may use the divide-and-conquer algorithm, by taking advantage of the multiple processors on a machine. Before Java 7, we had several Java constructs to help us solve the problems that involved concurrency. The fork/join framework helps us solve the problems that involve parallelism. Typically, the fork/join framework is suitable in a situation where:

  • A task can be divided in multiple subtasks that can be executed in parallel.
  • When subtasks are finished, the partial results can be combined to get the final result.

The fork/join framework creates a pool of threads to execute the subtasks. When a thread is waiting on a subtask to finish, the fork/join framework uses that thread to execute other pending subtasks of other threads. The technique of an idle thread executing other thread’s task is called work stealing. The fork/join framework uses a work-stealing algorithm to enhance the performance.

Click here to read the complete post.


Translucent Window in Java 7

Let us define three terms – transparent, translucent, and opaque, before we discuss how to use a translucent window in Swing. If something is transparent, you can see through it. For example, clear water is transparent. If something is opaque, you cannot see through it. For example, a concrete wall is opaque. If something is translucent, you can see through it, but not clearly. If something is translucent, it partially allows light to pass through. For example, a plastic curtain is translucent. The terms, “transparent” and “opaque”, describe two opposite states, whereas the term “translucent” describes a state between “transparent” and “opaque”.

Java 7 lets you define the degree of translucency of a window, e.g., a JFrame. A 90% translucent window is 10% opaque. The degree of translucency for a window can be defined using the alpha value of the color component for a pixel. You can define the alpha value of a color using the constructors of the Color class as shown below.

Color(int red, int green, int blue, int alpha)

Color(float red, float green, float blue, float alpha)

The value for the alpha argument is specified between 0 and 255, when the color components are specified in terms of int values. For the float type arguments, its value is between 0.0 and 1.0. The alpha value of 0 or 0.0 means transparent (100% translucent or 0% opaque). The alpha value of 255 or 1.0 means opaque (0% translucent or not transparent).

Java 7 supports three forms of translucency in a window, which are represented by the following three constants in the WindowTranslucency enum.

  • PERPIXEL_TRANSPARENT: In this form of translucency, a pixel in a window is either opaque or transparent. That is, the alpha value for a pixel is either 0.0 or 1.0.
  • TRANSLUCENT: In this form of translucency, all pixels in a window have the same translucency, which can be defined by an alpha value between 0.0 and 1.0.
  • PERPIXEL_TRANSLUCENT: In this form of translucency, each pixel in a window can have its own alpha value between 0.0 and 1.0. This form lets you define the translucency in a window on a per pixel basis.

Click here to read the complete post.


Currency Enhancements in Java 7

Java 7 has made some enhancements to the java.util.Currency class. The Currency class has the following new methods in Java 7.

  • public static Set<Currency> getAvailableCurrencies()
  • public int getNumericCode()
  • public String getDisplayName()
  • public String getDisplayName(Locale locale)

Java 7 lets you override the default list of currencies using a file. Please follow the link below to read the full text of this post. 

Currency Enhancements in Java 7



Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!