August 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java vs Ruby: a Comparison of the Key Elements

  • December 13, 2007
  • By Deepak Vohra
  • Send Email »
  • More Articles »

Casting

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

end

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
end

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

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

helloObj=Hello.new
helloObj.hello("John")
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();
helloObj.hello("John");

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){
defaultMsg=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
   @defaultMsg
   def initialize(msg)
   @defaultMsg=msg

   end
end




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel