Java is mature. It’s tested. And it’s fast (contrary to what the anti-Java crowd may still claim). It’s also quite verbose. Going from Java to Ruby, expect your code size to shrink down considerably. You can also expect it to take less time to knock together quick prototypes.
Similarities
As with Java, in Ruby,...
* Memory is managed for you via a garbage collector.
* Objects are strongly typed.
* There are public, private, and protected methods.
* There are embedded doc tools (Ruby’s is called RDoc). The docs generated by rdoc look very similar to those generated by javadoc.
Differences
Unlike Java, in Ruby,...
* You don’t need to compile your code. You just run it directly.
* There are different GUI toolkits. Ruby users can try WxRuby, FXRuby, Ruby-GNOME2, or the bundled-in Ruby Tk for example.
* You use the end keyword after defining things like classes, instead of having to put braces around blocks of code.
* You have require instead of import.
* All member variables are private. From the outside, you access everything via methods.
* Parentheses in method calls are usually optional and often omitted.
* Everything is an object, including numbers like 2 and 3.14159.
* There’s no static type checking.
* Variable names are just labels. They don’t have a type associated with them.
* There are no type declarations. You just assign to new variable names as-needed and they just “spring up” (i.e. a = [1,2,3] rather than int[] a = {1,2,3};).
* There’s no casting. Just call the methods. Your unit tests should tell you before you even run the code if you’re going to see an exception.
* It’s foo = Foo.new( "hi") instead of Foo foo = new Foo( "hi" ).
* The constructor is always named “initialize” instead of the name of the class.
* You have “mixin’s” instead of interfaces.
* YAML tends to be favored over XML.
* It’s nil instead of null.
* == and equals() are handled differently in Ruby. Use == when you want to test equivalence in Ruby (equals() is Java). Use equal?() when you want to know if two objects are the same (== in Java).
Similarities
As with Java, in Ruby,...
* Memory is managed for you via a garbage collector.
* Objects are strongly typed.
* There are public, private, and protected methods.
* There are embedded doc tools (Ruby’s is called RDoc). The docs generated by rdoc look very similar to those generated by javadoc.
Differences
Unlike Java, in Ruby,...
* You don’t need to compile your code. You just run it directly.
* There are different GUI toolkits. Ruby users can try WxRuby, FXRuby, Ruby-GNOME2, or the bundled-in Ruby Tk for example.
* You use the end keyword after defining things like classes, instead of having to put braces around blocks of code.
* You have require instead of import.
* All member variables are private. From the outside, you access everything via methods.
* Parentheses in method calls are usually optional and often omitted.
* Everything is an object, including numbers like 2 and 3.14159.
* There’s no static type checking.
* Variable names are just labels. They don’t have a type associated with them.
* There are no type declarations. You just assign to new variable names as-needed and they just “spring up” (i.e. a = [1,2,3] rather than int[] a = {1,2,3};).
* There’s no casting. Just call the methods. Your unit tests should tell you before you even run the code if you’re going to see an exception.
* It’s foo = Foo.new( "hi") instead of Foo foo = new Foo( "hi" ).
* The constructor is always named “initialize” instead of the name of the class.
* You have “mixin’s” instead of interfaces.
* YAML tends to be favored over XML.
* It’s nil instead of null.
* == and equals() are handled differently in Ruby. Use == when you want to test equivalence in Ruby (equals() is Java). Use equal?() when you want to know if two objects are the same (== in Java).
本文比较了Java与Ruby两种编程语言的特点。介绍了它们之间的相似之处,如内存管理、对象类型及文档工具等;并详细阐述了二者的主要差异,包括编译方式、GUI工具包选择、语法结构、类型检查等方面。
238

被折叠的 条评论
为什么被折叠?



