developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Memory
Disney World Tickets
Memory Upgrades
KVM over IP
Holiday Gift Ideas
Data Center Solutions
GPS Devices
Promotional Gifts
Rackmount LCD Monitor
Televisions
Boat Donations
GPS
Find Software
Condos For Sale

 


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Java vs Ruby: a Comparison of the Key Elements
By Deepak Vohra

Go to page: Prev  1  2  3  

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.

Go to page: Prev  1  2  3  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Open Source Archives

Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Whitepaper: Embeddable Content Platform for OEM's
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Is it time to make your move to the multi-threaded and parallel processing world? Find out!



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES