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




Nominate the Best Products or Technologies for Developer.com Product of the Year!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Red Hat Takes on HPC Market, Microsoft    October 3, 2008
Python's New Release Bridges the Gap    October 3, 2008
No Flash Seen on iPhone Horizon    October 2, 2008
Apple Yields to Complaints Over iPhone NDA    October 1, 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








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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES