Pain-Free Exploratory Programming with JavaSE 6.0

You know how sometimes you’re exploring a new Java API, and you don’t want to create a class (public static void main blah blah), compile it, (or create project if you’re using an IDE), typing System.out.println to see the output, just to find out how that API works? I really hate that. Especially if it’s a 3rd party library

Java SE 6.0 in this regard. It makes it really easy to explore an API without having to compile a class just to see how it works.

Trying Out Regular Expressions

For instance, let’s say we want to escape all single quotation marks, double quotation marks, and backslashes in a Java String (say because you want to pass it to JavaScript). A Perl guru can probably write the regex in 3 seconds, but my regex is rusty. I want to be able to try it out, and then paste the result into my code with just a few modifications.

Since I have Java 6.0, I just have to start jrunscript. By default it gives me a JavaScript console. Now it’s easy to try out different regular expressions:

js> var s = new java.lang.String("Testin' this thing: Test' test \" test \\ ");

The objective is to add an escaping backslash in front of ‘, “, and \ within the string. First trial:

js> s.replaceAll("[\\"'\\\\]", "\\$1");
script error: unterminated string literal (<STDIN>#1) in <STDIN> at line number 1

Hmmm. That didn’t go so well. Oh, too many backslashes for the double quotation marks! Second trial–I just pressed the up arrow, edit the expression, and press Enter:

js> s.replaceAll("[\"'\\\\]", "\\$1");
Testin$1 this thing: Test$1 test $1 test $1

Meh. Third trial:

js> s.replaceAll("[\"'\\\\]", "\\$0");
Testin$0 this thing: Test$0 test $0 test $0

Ah, right, the $ needs to be escaped:

js> s.replaceAll("[\"'\\\\]", "\\\\$0");
Testin\' this thing: Test\' test \" test \\

Sweet! No class, no compiling, no System.out.println, just trying immediately the API you want.

Trying Out New Third-Party Libraries

Java’s Calendar and Date are quite… emasculating. I believe that using them too much shorten your lifespan and in all probability has disastrous effect on your sex life. Self-respecting developers who care about sex use Joda Time. Joda Time has been submitted as a JSR, and I’m wishing it every success. May java.util.Date and java.util.Calendar die a horrible death ASAP.

So, let’s say you’ve downloaded Joda’s JAR and would like to explore. Just run jrunscript this way (obviously change the location of the Joda’s JAR accordingly:

jrunscript -cp C:\apps\jars\joda-time-1.4.jar

and import the package:

js> importPackage(org.joda.time);

and we can start exploring right away:

js> var now = new DateTime();
js> now
js> var oneMonth = new Period().withMonths(1);
js> oneMonth
js> Period().withDays(31));

It’s easy to get all the static members of, say, Period:

js> for(m in Period) { println(m); }

and the instance members:

js> for(m in (new Period())) { println(m); }

Nice! Trying out Swing also becomes real easy with this. Frankly my Java development is all Web these days, though.


2 thoughts on “Pain-Free Exploratory Programming with JavaSE 6.0

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s