DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Five Java Books Beginners and Professionals Should Read
  • TDD vs. BDD: Choosing The Suitable Framework
  • Front-End: Cache Strategies You Should Know
  • How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication

Trending

  • Five Java Books Beginners and Professionals Should Read
  • TDD vs. BDD: Choosing The Suitable Framework
  • Front-End: Cache Strategies You Should Know
  • How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
  1. DZone
  2. Coding
  3. Java
  4. Java as a Scripting Language?

Java as a Scripting Language?

Derek Young user avatar by
Derek Young
·
Sep. 03, 08 · News
Like (0)
Save
Tweet
Share
12.88K Views

Join the DZone community and get the full member experience.

Join For Free

I came across a language comparison (which I wish I could still find) where the author presented a code sample in many different languages. The example he chose was computing the MD5 digest of a string. He showed a verbose Java version, some python etc. Finally php:

md5("Hello World");

This example, the author asserted, showed that PHP coders can do as much with one line as a Java coder can do in 20. Of course any problem is easy in any language if there’s an available library call that’s just right.

When I have to write a program to work with lines of text I’ll usually turn to Ruby or possibly Groovy (sorry Perl – not going there anymore). Scripting languages like these are usually geared toward text processing and their built in libraries make these jobs easy. I wouldn’t jump into Java because it’s such a pain to do this kind of thing.

Ruby

File.open("myfile.txt").each { |line|
puts line if line =~ /blue/
}

some standard Java to do the same thing

import java.io.*;

public class ProcessWords {
public static void main(String [] args) throws IOException {
BufferedReader input = new BufferedReader(
new FileReader("myfile.txt"));
String line;
while ((line = input.readLine()) != null)
if (line.indexOf("blue") != -1)
System.out.println(line);
input.close();
}
}

The Java code is obviously more verbose and uglier, matching many people’s opinion of Java in general. But is this because of the language or the API? Java’s APIs are all very general and give you complete control over everything you do. Ruby’s APIs make it easy to perform this common task.

You could easily create a TextFile class in Java with a linesMatching method that returned Iterable<String> allowing you to iterate over lines that matched a regular expression. Now the task is easy:

public class ProcessWords {
public static void main(String [] args) throws IOException {
for (String line : new TextFile("myfile.txt").linesMatching("blue")) {
System.out.println(line);
}
}
}

The designers of Ant decided programmers like writing in XML. But I don’t. I’d rather write in Java than XML. Would ant build scripts be better expressed in Java?

My hypothetical translation into Java.

public class Ant implements ProjectDefinition {
void targets(Project project) {
project
.add(new Target("clean") {
void execute(Tasks tasks) {
tasks.delete("build");
}
})

.add(new Target("compile") {
void execute(Tasks tasks) {
tasks.mkdir("build/classes");
tasks.javac("src").destdir("build/classes");
}
});
}
}

Both definitions are roughly the same size. The hypothetical Java version definitely has some cruft but is reasonably compact. Like Rake though, the Java version would allow you to use the power of a real language in your build script – conditionals loops, dynamic targets, variables. A Java version would give you instant IDE support, debugging, profiling on top of that.

Both the line iteration and ant project definition examples show internal domain specific languages. The first domain is text processing. Scripting languages compete in this space, but I would argue it has a lot more to do with the libraries they provide than the language syntax. The second domain is build configurations. With the right APIs Java would do a very good job here too.

Of course Java does have several strikes against it when you actually consider its use for scripting:

  1. you have to compile all the files which turns some people off. You could easily write a ClassLoader to compile the code on the fly.
  2. Java starts up slowly, which kills the performance of very quick scripts, if that matters.
  3. Java has no meta programming. This is probably the biggest issue, although reflection and code generation can help here. (You could generate the ant task APIs for my build script example).

I think more user friendly APIs could be written for Java for areas like text processing, XML parsing and creation, threading (even easier than java.util.concurrent), and file operations. Joda Time is a great example of a library that is cleary superior to Java’s in this respect.

When I write an API (in any language) I try to think of what would make the user happiest when coding against it, not what necessarily matches the implementation. Chained method calls for example don’t help at all in the implementation of a class, but returning this from each method can help the callers in some cases and is easy enough to justify. In some cases providing a little internal DSL instead of a collection of getters/setters and unrelated methods makes the code a lot more readable and helps keep the focus on the caller instead of the implementation.

From http://dmy999.com/

Java (programming language)

Opinions expressed by DZone contributors are their own.

Trending

  • Five Java Books Beginners and Professionals Should Read
  • TDD vs. BDD: Choosing The Suitable Framework
  • Front-End: Cache Strategies You Should Know
  • How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: