Java 是 100% 面向对象的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/974583/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:45:21  来源:igfitidea点击:

Is Java 100% object oriented?

javaoop

提问by Silent Warrior

Java has primitive data types which doesn't derive from object like in Ruby. So can we consider Java as a 100% object oriented language? Another question: Why doesn't Java design primitive data types the object way?

Java 具有原始数据类型,它不像 Ruby 那样从对象派生。那么我们可以将 Java 视为 100% 面向对象的语言吗?另一个问题:为什么 Java 不以对象方式设计原始数据类型?

采纳答案by Alexandru Nedelcu

When Java first appeared (versions 1.x) the JVM was really, really slow. Not implementing primitives as first-class objects was a compromise they had taken for speed purposes, although I think in the long run it was a really bad decision.

当 Java 首次出现(版本 1.x)时,JVM 真的非常非常慢。不将原语实现为一流对象是他们为了速度目的而做出的妥协,尽管我认为从长远来看这是一个非常糟糕的决定。

"Object oriented" also means lots of things for lots of people. You can have class-based OO (C++, Java, C#), or you can have prototype-based OO (Javascript, Lua).

“面向对象”对很多人来说也意味着很多事情。您可以拥有基于类的 OO(C++、Java、C#),也可以拥有基于原型的 OO(Javascript、Lua)。

100% object oriented doesn't mean much, really. Ruby also has problems that you'll encounter from time to time.

100% 面向对象并没有多大意义,真的。Ruby 也有您不时会遇到的问题。

What bothers me about Java is that it doesn't provide the means to abstract ideas efficiently, to extend the language where it has problems. And whenever this issue was raised (see Guy Steele's "Growing a Language") the "oh noes, but what about Joe Sixpack?" argument is given. Even if you design a language that prevents shooting yourself in the foot, there's a difference between accidental complexity and real complexity (see No Silver Bullet) and mediocre developers will always find creative ways to shoot themselves.

Java 困扰我的地方在于它没有提供有效地抽象思想的方法,也没有提供在有问题的地方扩展语言的方法。每当提出这个问题时(参见 Guy Steele 的“Growing a Language”)“哦,不,但是 Joe Sixpack 呢?” 给出了论证。即使你设计了一种语言来防止自己被射中脚,意外的复杂性和真实的复杂性之间还是有区别的(参见No Silver Bullet),平庸的开发人员总会找到创造性的方法来射自己。

For example Perl 5 is not object-oriented, but it is extensible enough that it allows Moose, an object system that allows very advanced techniques for dealing with the complexity of OO. And syntactic sugaris no problem.

例如,Perl 5 不是面向对象的,但它具有足够的可扩展性以允许Moose,这是一个允许使用非常先进的技术来处理 OO 复杂性的对象系统。而且语法糖没问题。

回答by alamar

No. Javascript, for example, is.

不。例如,Javascript 就是。

What would those Integer and Long and Boolean classes be written in? How would you write an ArrayList or HashMap without primitive arrays?

那些 Integer 和 Long 以及 Boolean 类会用什么编写?如果没有原始数组,您将如何编写 ArrayList 或 HashMap?

回答by PeterMmm

Java is not 100% OO. Java may going towards 99% OO (think of auto-boxing, Scala). I would say Java is now 87% OO.

Java 不是 100% OO。Java 可能会走向 99% OO(想想自动装箱,Scala)。我会说 Java 现在 87% 是面向对象的。

Why java doesn't design primitive data types as object way ?

为什么java不将原始数据类型设计为对象方式?

Back in the 90's there were Performance reasons and at the same time Java stays backward compatible. So they cannot take them out.

早在 90 年代就有性能方面的原因,同时 Java 保持向后兼容。所以他们不能带他们出去。

回答by Michael Borgwardt

So can we consider java as 100% object oriented language?

那么我们可以将 java 视为 100% 面向对象的语言吗?

No.

不。

Another question : Why java doesn't design primitive data types as object way?

另一个问题:为什么java不将原始数据类型设计为对象方式?

Mainly for performance reasons, possibly also to be more familiar to people coming from C++.

主要是出于性能原因,也可能是为了让来自 C++ 的人更熟悉。

回答by coobird

Java, for the reason you mentioned, having primitives, doesn't make it a purely object-oriented programming language. However, the enforcement of having every program be a class makes it very oriented toward object-oriented programming.

由于您提到的原因,Java 具有原语,并不能使其成为纯粹的面向对象的编程语言。然而,让每个程序都是一个类的强制执行使它非常面向面向对象的编程。

Ruby, as you mentioned, and happened to be the first language that came to my mind as well, is a language that does not have primitives, and all values are objects. This certainly does make it more object-oriented than Java. On the other hand, to my knowledge, there is no requirement that a piece of code mustbe associated with a class, as is the case with Java.

Ruby,正如你提到的,碰巧也是我想到的第一种语言,它是一种没有原语的语言,所有值都是对象。这确实使它比 Java 更面向对象。另一方面,据我所知,没有要求一段代码必须与一个类相关联,就像 Java 的情况一样。

That said, Java does have objects that wrap around the primitives such as Integer, Boolean, Characterand such. The reason for having primitives is probably the reason given in Peter's answer -- back when Java was introduced in the mid-90's, memory on systems were mostly in the double-digit megabytes, so having each and every value be an object was large overhead.

这就是说,爪哇确实有对象周围的原语如裹IntegerBooleanCharacter和这样的。使用原语的原因可能是 Peter 的回答中给出的原因——回到 90 年代中期引入 Java 时,系统上的内存大多为两位数的兆字节,因此将每个值都作为一个对象是很大的开销.

(Large, of course is relative. I can't remember the exact numbers, but an object overhead was around 50-100 bytes of memory. Definitely more than the minimum of several bytes required for primitive values)

(大,当然是相对的。我不记得确切的数字,但对象开销大约是 50-100 字节的内存。绝对超过原始值所需的最少几个字节)

Today, with many desktops with multiple gigabytes of memory, the overhead of objects are less of an issue.

今天,由于许多台式机具有数 GB 的内存,对象的开销不再是一个问题。

回答by Remus Rusanu

To get to true 100% OO think Smalltalk for instance, where everythingis an object, including the compiler itself, and even ifstatements: ifTrue:is a message sent to a Boolean with a block of code parameter.

要实现真正的 100% OO,请考虑 Smalltalk,其中一切都是对象,包括编译器本身,甚至if语句:ifTrue:是发送到带有代码块参数的布尔值的消息。

回答by Matthew Flaschen

This is one of those questions that really only matters in an academic sense. Ruby is optimized to treat ints, longs, etc. as primitives whenever possible. Java just made this explicit. If Java had primitives be objects, there would be IntPrimitive, LongPrimitive, etc (by whatever name) classes. which would most likely be final without special methods (e.g. no IntPrimitive.factorial). Which would mean for most purposes they would be primitives.

这是真正只在学术意义上重要的问题之一。Ruby 已优化为尽可能将整数、长整数等视为原语。Java 只是明确说明了这一点。如果 Java 的原语是对象,就会有 IntPrimitive、LongPrimitive 等(无论名称如何)类。如果没有特殊方法(例如没有 IntPrimitive.factorial),这很可能是最终的。这意味着对于大多数用途,它们将是原语。

回答by Edmund

One reason Java can't obviously do away with non-object primitives (int, etc.) is that it does not support native data members. Imagine:

Java 显然不能取消非对象原语(int等)的原因之一是它不支持本机数据成员。想象:

class int extends object
{
    // need some data member here.  but what type?
    public native int();
    public native int plus(int x);
    // several more non-mutating methods
};

On second thoughts, we know Java maintains internal data per object (locks, etc.). Maybe we could define class intwith no data members, but with native methods that manipulate this internal data.

再想一想,我们知道 Java 为每个对象(锁等)维护内部数据。也许我们可以class int不使用数据成员进行定义,而是使用操作此内部数据的本机方法进行定义。

Remaining issues: Constants -- but these can be dealt with similarly to strings. Operators are just syntactical sugar and +and would be mapped do the plusmethod at compile time, although we need to be careful that int.plus(float)returns floatas does float.plus(int), and so on.

剩下的问题:常量——但这些可以像字符串一样处理。运算符只是语法糖,+并且会plus在编译时被映射到执行方法,尽管我们需要小心int.plus(float)返回floatas did float.plus(int),等等。

Ultimately I think the justification for primitives is efficiency: the static analysis needed to determine that an intobject can be treated purely as JVM integer value may have been considered too big a problem when the language was designed.

最终,我认为原语的理由是效率:在int设计语言时,确定一个对象可以纯粹作为 JVM 整数值处理所需的静态分析可能被认为是一个太大的问题。

回答by Tom Hawtin - tackline

Java clearly is not 100% OO. You can easily program it in a procedural style. Most people do. It's true that the libraries and containers tend not to be as forgiving of this paradigm.

Java 显然不是 100% OO。您可以轻松地以程序风格对其进行编程。大多数人都这样做。确实,库和容器往往不会容忍这种范式。

回答by Dafydd Rees

"Why java doesn't design primitive data types as object way ?"

“为什么java不将原始数据类型设计为对象方式?”

At Sun developer days, quite a few years ago I remember James Gosling answering this. He said that they'd liked to have totally abstracted away from primitives - to leave only standard objects, but then ran out of time and decided to ship with what they had. Very sad.

几年前,在 Sun 开发人员时代,我记得 James Gosling 回答了这个问题。他说他们希望完全从原语中抽离出来——只留下标准对象,但后来时间不够了,决定用他们现有的东西发货。很伤心。