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 -
Are We Ready for the Cloud?    November 7, 2008
Windows 7 Drivers to Get a Makeover    November 6, 2008
Sun Serves Up Some Java EE 6 in GlassFish    November 6, 2008
Devs Dish Some iPhone App Tips    November 5, 2008
Free Tech Newsletter -

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

Go to page: Prev  1  2  3  Next  

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

Go to page: Prev  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