Introduction to scripting in Java, Part 1

Learn what makes a scripting language like Ruby shine and why Groovy's suddenly so groovy, in this two-part excerpt from the forthcoming Scripting in Java: Languages, Frameworks, and Patterns (Addison Wesley Professional, August 2007).

1 2 3 4 Page 4
Page 4 of 4

Functions as method arguments

Many scripting languages, even object-oriented ones, introduce standalone functions as so-called "first-class language citizens." Even if you do not have true support for closures, you can pass your functions as method arguments.

The Python language, for example, defines a filter() function that accepts a list and the function to be executed on every item in the list:

def over(item) :
    threshold = 10
    return item > threshold
newList = filter(over, orig)

In this example, we defined the over() function, which basically does the same job as our closure from the previous example. Next, we called the filter() function and passed the over() function as the second argument. Even though this mechanism is not as convenient as closures are, it serves its purpose well (and that is to pass blocks of code as data around the application).

Of course, you can achieve similar functionality in other nonscripting languages. For example, Java developers have the concept of anonymous inner classes serving the same purpose. Let's implement a similar solution using this approach:

package net.scriptinginjava.ch1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

interface IFilter {
 public boolean filter(Integer item);

public class Filter {

    private static List select(List list, IFilter filter) {
        List result = new ArrayList();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Integer item = (Integer);
 if (filter.filter(item)) {

        return result;

    public static void main(String[] args) {
        Integer[] arr = new Integer[]{
                    new Integer(5), 
                    new Integer(7), 
                    new Integer(13), 
                    new Integer(32)
        List orig = Arrays.asList(arr);
        List newList = select(orig, 
 new IFilter() {
 private Integer threshold 
 = new Integer(10); 
 public boolean filter(Integer item) {

 return item.compareTo(threshold) > 0;

First we defined the IFilter interface with a filter() method that returns a Boolean value indicating whether the condition is satisfied.

Note - Some closure proponents say that the existence of this "named" interface breaks the anonymous concept at the beginning.

Our Filter class contains a select() method equal to the methods we saw in the earlier Ruby and Python examples. It accepts a list to be handled and the implementation of the IFilter interface that filters the values we want in our new list. At the end, we implement the IFilter interface as the anonymous inner class in the select() method call.

As a result, the program prints this result list to the screen:

[13, 32]

From this example, we can see even though a similar concept is possible in system-programming languages, the syntax is more complex. This is an important difference because the natural syntax for some functionality leads to its frequent use, in practice. Closures have simple syntax for passing the code around the application. That is why you see closures used more often in languages that naturally support them than you see similar structures in other languages (anonymous inner classes in Java, for example).

Hopefully, closures will be added in Java SE 7, which will move Java one step closer to the flexibility of scripting languages.


In this section of the chapter, I discussed some basic functional characteristics of scripting languages. Many experts tend to categorize a language as scripting or system programming, not by these functional characteristics but by the programming style and the role the language plays in the system. However, these two categorizations are not independent, so to understand how scripting can fit into your development process, it is important to know the functional characteristics of the scripting language and the implications of its design. The differences between system-programming and scripting languages are described later in this chapter, helping us to understand how these two approaches can work together to create systems that feature the strengths of both programming styles.

It is important to note that the characteristics we've discussed thus far are not independent among each other. For example, whether to use static or dynamic typing depends on when the type checking is done. It is hard to implement dynamic typing in a strictly compiled environment. Thus, interpreter and dynamic typing somehow fit naturally together and are usually employed in scripting environments. The same is true for the compiler and static typing found in system-programming environments.

The same is true for the generation and execution of other programs, which is a natural thing to do in interpreted environments and is not very easy (and thus is rarely done) in compiled environments.

To summarize, these characteristics are usually found in scripting programming environments. Not all languages support all the features described earlier, which is a decision driven by the primary domain for which the language is used. For example, although Python is a dynamic language, it introduces strong typing, making it more resistible to type misuse and more convenient for development of larger applications.

These characteristics should serve only as a marker when exploring certain languages and their possible use in your development process. More important is the language's programming style, a topic we discuss shortly.

This content is an excerpt from the new book titled Scripting in Java, authored by Dejan Bosanac, Copyright 2007 Pearson Education, Inc., published by Addison-Wesley Professional, August 2007, ISBN 0321321936. For additional information, please visit:

Copyright (c) 2007 Pearson Education. All rights reserved.

Dejan Bosanac is a professional software developer and technology consultant. He specializes in the integration and interoperability of diverse technologies, especially those related to Java and the Web. He has spent several years developing complex software projects, ranging from highly trafficked Web sites to enterprise applications, and was a member of the JSR 223 Expert Group.

Learn more about this topic

  • "Build your own scripting language for Java " (Chaur Wu,, April 2006) introduces JSR 223, Scripting for the Java Platform and shows you how to use it to integrate a roll-your-own scripting language into the Java platform.
  • "Choosing a Java scripting language: Round two" (David Kearns,, March 2005) compares the performance of a wide variety of scripting languages compatible with the (pre JSR 223) Java platform, including Jacl, Jython, Rhino, BeanShell, Groovy, JudoScript, JRuby, and Pnuts.
  • "Ruby for the Java world" (Joshua Fox,, July 2006) explores the benefits of incorporating scripting into your Java development practice and explains why Ruby (or JRuby) is a good choice for Java developers.
  • "Dynamic Languages -- ready for the next challenges, by design" (David Ascher, PhD, ActiveState, July 2004) digs deeper into the pros and cons of (specifically) dynamically typed scripting languages.
  • Once you understand what differentiates a dynamically typed language from a statically typed one, you're ready to explore the pros and cons of strong versus weak typing." Start with this interview between Bill Venners and Guido van Rossum.
  • According to Sun, Javadoc is "a tool for generating API documentation in HTML format from doc comments in source code."
  • In "Use the best tool for the job" (Artima Developer, March 2003) , Bill Venners makes the case for combining scripting and a systems language.
  • "Scripting: Higher Level Programming for the 21st Century" (John K. Ousterhout , Tcl Developer Xchange, 1998) suggests a checklist for deciding when scripting glue is a better alternative to your programming language of choice.
  • There's more to learn more about enterprise Java development. Check out the Java Enterprise Edition Research Center.
  • Get previews of upcoming articles -- sign up for JavaWorld's free Enterprise Java newsletter.

This story, "Introduction to scripting in Java, Part 1" was originally published by JavaWorld.

Copyright © 2007 IDG Communications, Inc.

1 2 3 4 Page 4
Page 4 of 4