Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Introduction to scripting in Java, Part 2

Find out what else you need to know about scripting in this second half of the JavaWorld excerpt from Dejan Bosanac's Scripting in Java: Languages, Frameworks, and Patterns (Addison Wesley Professional, August 2007)

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

You've nailed down the functional characteristics of scripting languages and seen how Python compares to Java for banging out fast code. Now look at the big picture: how does a non-native scripting language interact with the JVM and how will the strengths and weaknesses of scripting affect the runtime performance, robustness, and maintainability of your Java applications.

Scripting Languages and Virtual Machines

A recent trend in programming language design is the presence of a virtual machine as one of the vital elements of programming platforms. One of the main elements of the Java Runtime Environment (JRE) is the virtual machine that interprets bytecode and serves as a layer between the application and operating systems. A virtual machine serves as a layer between the application and operating systems in Microsoft's .NET platform as well.

Let's now summarize briefly how the JRE works. Java programs contained in java extension source files are compiled to bytecode (files with a class extension). As I said earlier, the purpose of bytecode is to provide a compact format for intermediate code and support for platform independence. The JVM is a virtual processor, and like all other processors, it interprets code—bytecode in this case. This is a short description of the JRE, but it is needed for our further discussion. You can find a more comprehensive description at the beginning of Chapter 3, "Scripting Languages Inside the JVM."

Following this, we can say Java is a hybrid compiled-interpreted language. But even with this model, Java cannot be characterized as a scripting language because it lacks all the other features mentioned earlier.

At this point, you are probably asking what this discussion has to do with scripting languages. The point is many modern scripting languages follow the same hybrid concept. Although programs are distributed in script form and are interpreted at runtime, the things going on in the background are pretty much the same.

Let's look at Python, for example. The Python interpreter consists of a compiler that compiles source code to the intermediate bytecode, and the Python Virtual Machine (PVM) that interprets this code. This process is being done in the background, leaving the impression that the pure Python source code has been interpreted. If the Python interpreter has write privileges on the host system, it caches the generated bytecode in files with a pyc extension (the py extension is used for the scripts or source code). If that script had not been modified since its previous execution, the compilation process would be skipped and the virtual machine could start interpreting the bytecode at once. This could greatly improve the Python script's startup speed. Even if the Python interpreter has no write privileges on the system and the bytecode was not written in files, this compilation process would still be performed. In this case, the bytecode would be kept in memory.


Note - Python programs can be distributed in bytecode format, keeping the source code out of the production environment.


From this discussion, we can conclude virtual machines are one of the standard parts of modern scripting languages. So our original dilemma remains. Should we use languages that enforce a certain programming paradigm, and if so, how do we use them? The dynamic and weak typing, closures, complex built-in data structures, and so on, could be implemented in a runtime environment with the virtual machine.

There is nothing to restrict the use of a dynamic (scripting) language on the virtual machines designed for languages such as Java and C#. As long as we implement the compiler appropriate for the target virtual machine's intermediate bytecode, we will receive all the features of the scripting language in this environment. Doing this, we could benefit from the strengths of both the system-programming approach of Java, and the scripting programming model in our software development process.

We focus on projects that bring scripting languages closer to the Java platform later in this book. Also, we discuss where it's appropriate to apply the scripting style of development with traditional Java programming. Before we cover these topics, though, let's take a look at how scripting and system programming compare.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

information regarding javascriptBy Anonymous on June 22, 2009, 10:38 amWhy Java Script is better than other scripting languages for web design?

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Learn what makes a scripting language like Ruby shine and why Groovy's suddenly so groovy, in "Introduction to scripting in Java, Part 1" -- the first half of a two-part excerpt from the forthcoming Scripting in Java: Languages, Frameworks, and Patterns ( Dejan Bosanac, Addison Wesley Professional, August 2007).
  • "Build your own scripting language for Java " (Chaur Wu, JavaWorld.com, 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, JavaWorld.com, 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, JavaWorld.com, 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 some 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.