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  
New
 
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...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
A Networking Idea That's Out of This World    November 26, 2008
Security Breach Can't Halt Fedora 10's Debut    November 25, 2008
Red Hat Fedora Claims It's the Leader in Linux    November 21, 2008
Dos and Don'ts of SOA Data Access    November 20, 2008
Free Tech Newsletter -

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

Go to page: 1  2  3  Next  

Ruby is an interpreted scripting language, whereas Java is a compiled programming language. Ruby is similar to Java in that both are object-oriented languages and are strongly typed. But, Ruby is dynamically typed, whereas Java is statically typed. In Ruby, type declarations are not used; in Java, type declarations are required. Both Java and Ruby provide inheritance and have public, private, and protected methods. Ruby is simpler than Java and faster than Java too. Ruby is different from Java in a number of features. I shall compare Ruby and Java on some of the salient features.

Interpreted/Compiled

Ruby is an interpreted scripting language and is run directly without first compiling and generating the byte code. For example, a ruby file helloruby.rb may be run with the following command.

>ruby helloruby.rb

In comparison, Java applications are required to be compiled before running. For example, a Java class hellojava.java is first compiled using the javac compiler with the following command.

>javac hellojava.java

The javac compiler generates the byte code for the Java class. Subsequently, the compiled class may be run with the java.exe application.

>java hellojava

Importing Packages

In Ruby, the require statement is used to import a package or a module. For example, the extensions package/module is imported as follows.

require 'extensions'

External files may be included in a Ruby application by using load or require. For example, to include the external file catalog.rb, add the following require statement.

require "catalog.rb"

The difference between load and require is that load includes the specified Ruby file every time the method is executed and require includes the Ruby file only once.

In Java, the import statement is used to load a package. For example, a Java package java.sql is loaded as follows.

import java.sql.*;

Typed Variables

Both Ruby and Java are strongly typed. But, in Ruby variables are dynamically typed; this implies that variables do not have an explicit type associated with them. For example, a variable with a string value is declared as follows.

str="Hello Ruby"

Because the str variable does not have a type associated with it, the variable may be assigned an integer value or a value of any other type.

str=1

In Java, variables are statically typed; this implies that variables have a type associated with them. For example, a string variable is declared as follows.

String str="Hello Java";

The str variable may not be assigned an integer value.

Null Value

In Ruby, a null value is declared with nil. For example, a variable str may be assigned a nil value as follows.

str=nil

In Java, a null value is declared with null. For example, a variable str is assigned a null value as follows.

str=null;

Object Oriented

In Ruby, everything is an object—including numbers, variables, and methods. In Java, only classes have objects. For example, an object of type Class1 is created as follows.

Class1 class1=new Class1();

Member Variables

In Ruby, all member variables are private. In Java, member variables have the package access by default and may be declared public, private, or protected with the public, private, and protected identifiers. Private members may be accessed only within the class itself. The public members in Java may be accessed by any other class. Protected members in Java may be accessed within the same package as the class declaring them and in the subclasses of the class. The default package access in Java is for access within the same package as the class declaring the members in addition to the class itself.

Go to page: 1  2  3  Next  


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






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