java java和ruby的主要区别和相似之处是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2017990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what are the major differences and similarity in java and ruby?
提问by giri
I am java professional now I like to go for ruby.Are there any similarity in both the languages? and what are the major differences? As both are object oriented.
我是 Java 专业人士,现在我喜欢使用 ruby。这两种语言有什么相似之处吗?主要区别是什么?因为两者都是面向对象的。
回答by OscarRyz
What about these:
这些怎么样:
Similarities
相似之处
As with Java, in Ruby,...
与 Java 一样,在 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.
- 内存是通过垃圾收集器为您管理的。
- 对象是强类型的。
- 有公共、私有和受保护的方法。
- 有嵌入式文档工具(Ruby 的称为 RDoc)。rdoc 生成的文档看起来与 javadoc 生成的文档非常相似。
Differences
差异
Unlike Java, in Ruby,...
与 Java 不同,在 Ruby 中,...
- You don't need to compile your code. You just run it directly.
- All member variables are private. From the outside, you access everything via methods.
- 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.
- The constructor is always named “initialize” instead of the name of the class.
- You have “mixin's” instead of interfaces.
- == 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).
- 您不需要编译代码。你直接运行就可以了。
- 所有成员变量都是私有的。从外部,您可以通过方法访问所有内容。
- 一切都是一个对象,包括像 2 和 3.14159 这样的数字。
- 没有静态类型检查。
- 变量名只是标签。它们没有关联的类型。
- 没有类型声明。您只需根据需要分配新的变量名称,它们就会“弹出”(即 a = [1,2,3] 而不是 int[] a = {1,2,3};)。
- 没有铸造。只需调用方法。
- 构造函数总是命名为“初始化”而不是类的名称。
- 你有“mixin”而不是接口。
- == 和 equals() 在 Ruby 中的处理方式不同。当您想在 Ruby 中测试等价性时使用 ==(equals() 是 Java)。当您想知道两个对象是否相同(Java 中的 ==)时,请使用 equal?() 。
Taken from: To Ruby From Java
回答by ennuikiller
Besides being object oriented there are very few similarities between the two languages. Java is a statically typed compiled language while ruby is a dynamically typed interpreted language. The syntax is also very different. Java uses the c convention of terminating lines with a semi-colon while ruby uses the return character.
除了面向对象之外,这两种语言之间几乎没有相似之处。Java 是静态类型编译语言,而 ruby 是动态类型解释语言。语法也非常不同。Java 使用 c 约定用分号终止行,而 ruby 使用返回字符。
While Java does have some built in support for iterators ruby's uses of iterators is pervasive throughout the language.
虽然 Java 确实有一些对迭代器的内置支持,但 ruby 对迭代器的使用在整个语言中无处不在。
This obviously only touches upon a comparison of the two. This is a decent write-up on the comparisons
这显然只是触及了两者的比较。 这是一篇关于比较的不错的文章
回答by vonconrad
You're asking a very broad question. I like to compare scripting languages similarly to how I'd compare spoken languages, so in this case; what are the major differences and similarities between Spanish and Italian?
你问的是一个非常广泛的问题。我喜欢比较脚本语言,就像比较口语一样,所以在这种情况下;西班牙语和意大利语之间的主要区别和相似之处是什么?
If you ask that question, you're going to either get very varied or very long answers. Explaining differences between languages are difficult at best, as it's hard to pinpoint key factors.
如果你问这个问题,你会得到各种各样或很长的答案。解释语言之间的差异充其量是困难的,因为很难确定关键因素。
This is proved by the responses here so far, as well as the links other people have linked to. They're either varied or very long.
到目前为止,这里的回复以及其他人链接到的链接都证明了这一点。它们要么多种多样,要么很长。
Going back to the Spanish vs. Italian analogy, I could say that the languages are similar but still very different. If you (only) know one of them, you might be able to understand what's going on in the other, though you would probably not be able to use it very well. Knowing one definitely makes it easier for you to learn the other. One is used by a larger number of people, so you might benefit more from learning it.
回到西班牙语与意大利语的类比,我可以说语言相似但仍然非常不同。如果您(仅)知道其中一个,您可能能够了解另一个发生了什么,尽管您可能无法很好地使用它。了解一个肯定会让你更容易学习另一个。一个被更多的人使用,所以你可能会从学习中受益更多。
All of the previous paragraph can be applied to Java vs. Ruby as well. Saying that both are object oriented is like saying Spanish and Italian both are members of the Romanic language family.
上一段的所有内容也适用于 Java 与 Ruby。说两者都是面向对象就像说西班牙语和意大利语都是罗马语系的成员。
Of course, all of this is irrelevant. Most probably, your underlying question is whether it's "worth" learning Ruby instead of or in addition to Java. Unfortunately, there's no easy answer to that either. You have to weigh advantages and disadvantages with each language, such as popularity, demand and career opportunities. And then there's naturally the question of which language you prefer. You might like one language more than the other simply because it has a nicer syntax. (Similarly, you may prefer Italian because you think it's more "beautiful" than Spanish, even though the latter is more widespread and you'd have more "use" for it.)
当然,这一切都无关紧要。最有可能的是,您的潜在问题是学习 Ruby 而不是 Java 或除了 Java 之外,是否“值得”学习。不幸的是,对此也没有简单的答案。您必须权衡每种语言的优缺点,例如受欢迎程度、需求和职业机会。然后自然是您更喜欢哪种语言的问题。您可能更喜欢一种语言而不是另一种语言,因为它具有更好的语法。(同样,您可能更喜欢意大利语,因为您认为它比西班牙语更“美丽”,即使后者更广泛,并且您会有更多“用途”。)
Personally, I prefer Ruby. For many different reasons. Just like I prefer Italian.
就个人而言,我更喜欢 Ruby。由于许多不同的原因。就像我更喜欢意大利语一样。
回答by Joshua Partogi
- The Object Oriented feature in Ruby is actually very different compared to Java. In Ruby, everything is an object, including a primitive type (in Java) like integer.
In Ruby, new is like a property instead of a keyword. So to instantiate an object you would do this in Ruby:
animal = Animal.newRuby is strong typing but also dynamic. Because of its dynamicsm, Ruby enables you to do duck typing.
- Ruby's answer to multiple inheritance is mixin (which is a language feature), where in Java you would implement many interfaces.
- Ruby has got block, where you would use anonymous class to achieve the same thing in Java. But IMHO Ruby block is more powerful.
- 与 Java 相比,Ruby 中的面向对象特性实际上非常不同。在 Ruby 中,一切都是对象,包括像整数这样的原始类型(在 Java 中)。
在 Ruby 中,new 就像一个属性而不是一个关键字。因此,要实例化一个对象,您可以在 Ruby 中执行以下操作:
animal = Animal.newRuby 是强类型但也是动态的。由于其动态性,Ruby 使您能够进行鸭子输入。
- Ruby 对多重继承的回答是 mixin(这是一种语言特性),在 Java 中,您将实现许多接口。
- Ruby 有块,您可以在其中使用匿名类在 Java 中实现相同的功能。但恕我直言,Ruby 块更强大。
So I can say there's not too much similarities in Java and Ruby. Until today I can't find any similarities between the two as Ruby has gone its own path unlike many other language that derives from C language.
所以我可以说 Java 和 Ruby 没有太多相似之处。直到今天,我都找不到两者之间的任何相似之处,因为 Ruby 走自己的道路,这与许多其他源自 C 语言的语言不同。

