January 24, 2021
Hot Topics:

Java vs Ruby: a Comparison of the Key Elements

  • By Deepak Vohra
  • Send Email »
  • More Articles »

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.


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.


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.


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


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.

Page 1 of 3

This article was originally published on December 13, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date