July 28, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java vs Ruby: a Comparison of the Key Elements

  • December 13, 2007
  • By Deepak Vohra
  • Send Email »
  • More Articles »

Class Instantiation

In Ruby, a class is instantiated by using the class method new. For example, the Hello class in the previous example may be instantiated as follows.

hello=Hello.new("Hello Ruby")

The arguments passed to the new method are passed to the initialize function. In Java, a class is instantiated using the Java operator new.

Hello hello=new Hello();

The new operator may be used with any of the constructors defined in the class. For example, if the Hello class has the constructor that takes a String argument to set the default Hello message, the class may be instantiated as follows.

Hello hello=new Hello("Hello Java");

In Java, more than one constructors may be declared, but a Ruby class may have only one initialize method. Ruby does not generate an exception if more than one initialize method is declared, but only the last initialize method is used.

Multiple Inheritance

Both Java and Ruby have the provision to extend another class. In Java, a class is extended using the extends keyword as follows.

public class LinkedHashSet extends HashSet{}

In Ruby, a class may extend another class using '<'. For example, the Catalog class extends the ActiveRecord::Base class.

class Catalog < ActiveRecord::Base

   end

In both Ruby and Java, a class may extend only one other class. Java provides multiple inheritance with interfaces that consist of abstract methods that may be implemented by a class implementing the interfaces. Ruby provides multiple inheritance with modules and mixins.

Modules are similar to classes in that they consist of variables and methods. But, modules are different from classes in that instances of a module cannot be created and a module cannot be subclassed. A module is defined with the keyword module as follows.

module Catalog
PI=3.1419
@journal

def setJournal(journal)
@journal=journal
end
end

The Catalog module has a constant PI, an instance variable @journal and a method setJournal.

A module may be included in another class by using mixins. To include a module in a class, first specify a require statement for the module and subsequently include the module using include. For example, mixin the Catalog module in another class CatalogEntry.

require Catalog
class CatalogEntry
include Catalog

end

When a module is included in another class using include, the variables, methods, and classes in the module become available to the class including it.

Exception Handling

In Java, exceptions are handled using the try-catch-finally construct; this consists of one try block, followed by one or more catch blocks, optionally followed by a finally block. In the try block, some statements are run that might generate an exception. Each of the catch blocks handles an Exception. In the optional finally block, some statements may be run to close objects. The finally block is always run after the application exits the try block. An example of a try-catch-finally block is as follows, in which the try block creates a Connection object with a MySQL database, in the catch block SQLException is handled, and in the finally block the Connection object is closed.

try {
   String url="com:mysql:jdbc://localhost:3306/test";
   Connection connection=DriverManager.getConnection(url, "root","");
} catch (SQLException e) {
   System.err.println("Caught: SQLException: "
                      + e.getMessage());
} finally(){
   connection.close();
}

In Ruby, exceptions are handled using begin-rescue-ensure-end construct. The construct consists of one or more rescue clauses that consist of statements to run when a specified exception occurs. The optional ensure clause consists of statements that are always run whether an exception occurs or not. The rescue clause in Ruby is equivalent to the catch clause in Java. The ensure clause in Ruby is equivalent to the finally clause in Java. The begin-rescue-ensure construct is shown below.

begin

rescue RubyException1
Statements to run when RubyException1 occurs.
rescue RubyException2
Statements to run when RubyException2 occurs.
ensure
Statements to run whether an exception occurs or does not occur.

end

Conclusion

Although Ruby is similar to Java and has parallel features, Ruby is not a replacement for Java as implied by an ONJava article Ruby the Rival. Just as Java EE is the enterprise framework for developing Model-View-Controller applications with Java, Ruby on Rails is the Model-View-Controller framework for Ruby.

About the Author

Deepak Vohra, dvohra09@yahoo.com, is a Sun Certified Java Programmer and a Sun Certified Web Component Developer, and has published in devx, FTPOnline, JavaBoutique, ONJava, and java.net.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel