Jump to content

Repls available for Java programming


Kяaσh
 Share

Recommended Posts

So, if repls have so many advantages, the next obvious question is what repls are available for the Java language?

 

[move]Jython[/move]

 

Jython (formerly known as JPython) is an implementation of Python (including a repl) in the Java programming language (certified 100% Pure). It actually compiles Python to (somewhat baroque) Java source code or directly to bytecode.

 

In the spirit of Python, every attempt was made to offer seamless Java interoperability with Jython. It gives you access to all of the Java standard libraries just as if you were programming natively in the language, as well as use of the existing Java class files. So you can use the repl to play not only with the standard libraries, but with your own Java (or Jython) classes that you've compiled to bytecode.

 

One significant consideration when using the Jython repl is that you are writing Python expressions, not Java expressions. The plus side of this is that you get the conciseness and beautiful syntactic sugar of Python.

 

For example, say I want to construct a new hash table that maps a to 1, b to 2, and c to 3. All I need to write in Jython is this:

>>> h = {'a':1, 'b':2, 'c':3}

 

 

The interpreter displays >>> before each new input line.

 

Jython syntax is also quite advantageous when exploring new GUI designs. For one thing, the various fields of a GUI element can be specified as keyword arguments to the constructor, like so:

>>> from javax.swing import *
>>> f = JFrame(visible=1)

 

This example illustrates some other differences between Jython and the Java language:

 

    - Import statements have a very different syntax.

 

    - ints are used instead of booleans (1 is true, 0 is false).

 

Here's another example where the Jython code saves you some typing -- adding action listeners to GUI elements. Normally, such listeners are specified as instances of anonymous inner classes using the Command Pattern. In Python (and many other "scripting" languages), such commands can be specified more succinctly using interactive function definitions. For example, let's build on the above interactive session and add a simple action listener to a JButton:

>>> def listener(event):
...   print 'thank you'
>>>

 

 

This is an example of a function definition in Jython. The interpreter lets us know when it expects a statement to be continued on the next line by printing ellipses for the caret. This function takes a single argument and prints "thank you" to standard out. We can use it as an action listener as follows:

 

>>> panel = JPanel()
>>> panel.add(JButton('press me', actionPerformed=listener))
>>> f.getContentPane().add(panel)
>>> f.pack()

 

 

Now we'll have a window displayed on the screen with a button labeled "press me" that, when pressed, will print "thank you" to standard out. Imagine how much more wordy this would be using Java code.

 

Of course, there are disadvantages. For example:

 

    - You lose static type checking (although static checking is arguably of little value in a repl).

 

    - Because the expressions you type in the repl are not Java code, you can't copy-and-paste expressions from the repl into your program without first translating them.

 

    - When using Jython with Java code, you have the added intellectual burden of juggling two languages in your head simultaneously (although some people consider that fun).

 

 

[move]DynamicJava[/move]

 

Another Java-usable repl is DynamicJava, a true Java-based (well, that's almost right) open-source tool with a few differences:

 

    - The repl language lets you get away without specifying the static types of variables when you declare them.

 

    - You don't have to add a semicolon at the end of a statement. The interpreter also (sloppily) returns null as the result of evaluating a statement. (It would have been better if statements didn't return values at all.)

 

    - You are not restricted from accessing the private fields of objects from within the repl.

 

For beginning Java programmers, these differences can be significant because they can cause confusion. More experienced programmers will probably welcome some of the relaxed restrictions. In either case, DynamicJava is a robust and very useful software product (and it helps that it's free).

 

Created:Krash

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock