comparing java and JRuby programming languages

Updated on July 15, 2010

Today, there are many programming languages in the world which could serve for any purpose. And what is the need to go for another language ? Is there anything else to be improved ? I used to think Java can beat anything in open source. But after working with JRuby, I understood that there is a scope for improvement in everything.

I started working with a new programming language called Jruby in my recent project. It is more like a scripting language and more developer friendly than java or other programming languages.

Jruby: Jruby is a java implementation of Ruby language. It means you can use java classes and objects using ruby syntax. ruby is a dynamic or interpreted language which means that it does not need a compiler. it executes directly at run time just like java and it is platform independent.

I had to try hard to gather the information about jruby as I wanted a comparative study of java and Jruby. Here are some comparisons between java and ruby syntax. I hope this would help other who are interested to learn ruby.

Inheritance:

Java A extends BJRuby A < B

defining a function:

Java public test(){}JRuby def test()

Instantiating an object

Java A a = new A();JRuby a = A.new

Line delimiter is semi colon new line itself is line delimiter

Static variable

Java static M = 100JRuby @@M = 100

Java static method(){}JRuby def self.method

Getter and setter methods

Java getname(), setname(String a)

JRuby attr_accessor: name

Constructor

Java Constructor name same as class name

JRuby constructor method is initialize ( def initialize)

for loop

Java for(int i=0;i<10;i++)

JRuby 10.times do |i|

Apart from above comparison of syntax, there are some differences between the languages.

JRuby is a strongly typed language. for example, the statement a = '3'+b (b = 2) would throwup an error where as in java in executes and assigns the value of 'a' as '32'. In ruby, You need to convert the variable b to a string before executing the stament. The statement should be a = '3'+b.to_s.

In JRuby, there is nothing called null. everything is an object. When you do not assign variable or instantiate an object, it is Nil. But Nil is still an object. So there is no way we get a null pointer exception in Ruby.

In Ruby, there is no need to define the type of the variables or arguments that you pass to a method. Everything is executed at run time. Another point is there are no classcast exceptions in ruby.

here is an example of it:

a = b.getObjectA()

a.test()

The first statement returns an instance of class A and then the method test is called on the object.

More by this Author

Spring framework provides different ways to authenticating a user against a LDAP either through spring LDAP templates or through spring security. Here I explained how to authenticate the user using the spring ldap...