March 3, 2021
Hot Topics:

Java vs Ruby: a Comparison of the Key Elements

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


In Java, objects may be cast to other objects if the objects being cast are of the type of the objects being cast to. For example, an object of type LinkedHashSet, linkedHashSet, may be cast to HashSet because LinkedHashSet extends HashSet.

HashSet hashSet=(HashSet)linkedHashSet;

In Ruby, no casting is used because variables are dynamically typed and may be assigned to any other type.

Class and Method Definition

Ruby defines a class/method block using the end keyword. Java uses braces to define a class/method block. A class, for example Catalog, in Ruby is defined with the class modifier as follows.

class Catalog


A Ruby class is required to begin with a capital letter. A class in Java also is defined using the class modifier and is required to begin with a capital letter as follows.

class Catalog{

In Ruby, class definitions do not have an access modifier, whereas in Java a class may have the public access modifier, the default class access being package. For example, the Catalog class in Java may be declared as follows.

public class Catalog{


Methods in Ruby begin with def and end with end. For example, a method getCatalogId is defined as follows.

def getCatalogId
return 1

By default, Ruby methods are public. Ruby methods may be specified with public, private, and protected modifiers. In Ruby, methods may return a value, but the type of the return value is not specified in the method definition. In Java, methods have package access by default and require a return type. For example, the getCatalogId method in Java may be declared as follows.

int getCatalogId(){return 1;}

Java methods may have the public, private, or protected modifiers. In Ruby, parentheses in method invocation are optional. For example, define a method hello that takes a name argument and returns a Hello message.

class Hello
def hello(name)
   return "Hello " +name

The hello method may be invoked with or without parentheses for the argument as follows.

helloObj.hello "John"

In Java, parentheses in method invocation are required. For example,define a method hello that returns a Hello message.

public class Hello{

public String hello(String name){

return "Hello "+ name;



The hello method in the Hello class may be invoked only with parentheses for the argument as follows.

Hello helloObj=new Hello();

Class Constructor

In Java, the constructor is the name of the class. For example, define a class Hello and define a constructor for the class.

public class Hello{
String defaultMsg;
public Hello(String msg){

The constructor sets the value of the String defaultMsg. Ruby provides the initialize function for class instantiation. For example, define a class and instantiate a variable in the class using the initialize function as follows.

class Hello
   def initialize(msg)


Page 2 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