Java原语实现

发布于 2024-10-21 23:56:03 字数 786 浏览 3 评论 0原文

Java 有基本类型的对象(Integer)和原始版本(int)。

原始版本更快/更轻等。所以一般来说你应该使用它们。

我想知道为什么 Java 的设计者不仅有对象类型,而且使用原始版本作为幕后的优化。

所以:

Integer foo(Integer alpha)
{
    Integer total = 0;
    for(Integer counter = 0; counter < alpha; counter++)
    {
        total += counter;
    }
    return total;
}

将被编译成如下代码:

int foo(int alpha)
{
    int total = 0;
    for(int counter = 0; counter < alpha; counter++)
    {
        total += counter;
    }
    return total;
}

本质上,这个假设的 java 编译器会将 Integer、Double、Float 等的实例转换为等效的原始类型。只有在真正需要对象的情况下(例如将元素放入容器中)才会涉及实际的 Integer 对象。

注意:上面的代码在 Integer 对象上使用了运算符,我知道这实际上是不允许的。由于我正在发明假设的 Java 编译器,所以我会假装这个编译器对 Integer/Float/Double 具有特殊的大小写,就像对 String 一样。

Java has both object, Integer, and primitive version, int, of basic types.

The primitive versions are faster/lighter/etc. so in general you should use them.

What I am wondering is why the designers of Java didn't only have the object types and use the primitive versions as optimizations behind the scenes.

So:

Integer foo(Integer alpha)
{
    Integer total = 0;
    for(Integer counter = 0; counter < alpha; counter++)
    {
        total += counter;
    }
    return total;
}

Would be compiled into code something like:

int foo(int alpha)
{
    int total = 0;
    for(int counter = 0; counter < alpha; counter++)
    {
        total += counter;
    }
    return total;
}

Essentially, this hypothetical java compiler would convert instances of Integer, Double, Float, etc into the equivalent primitive types. Only in the cases where objects were really needed (say to put the element in a container) would actual Integer objects be involved.

NOTE: the above code has used operators on Integer objects which I know isn't actually allowed. Since I'm inventing hypothetical Java compilers I'll pretend this one has special casing for Integer/Float/Double like it does for String.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-10-28 23:56:04

您需要某种方式向编译器发出信号,表明您需要盒装版本,对吧?否则,它怎么知道你想要的是有属性的版本还是原始版本?当您将 Integer 传递给计算 Integer.MAX_VALUE 的方法时会发生什么?

You need some way to signal to the compiler that you want the boxed version, right? Otherwise, how would it know whether you wanted the version that has properties or the primitive version? What happens when you pass your Integer to a method that evaluates Integer.MAX_VALUE?

绳情 2024-10-28 23:56:03

我想知道的是为什么 Java 的设计者不仅有对象类型并使用原始版本作为幕后的优化。

要了解 Java 设计决策背后的原因,您需要了解做出这些决策的历史背景。

基本类型和引用类型之间的强烈区别已融入到 JDK 1.0 之前的语言设计中。在 JDK 1.5 之前,仅此而已:如果您想将整数放入集合(例如),则显式使用 Integer.valueOf(int)

。添加到 JDK 1.5 中的类型系统中,它必须以向后兼容的方式完成。他们想出了一个很好的妥协方案……但如果他们从零开始的话,他们会/可能实现的目标并不是这样。

(他们第一次没有/无法“做对”的原因......早在 1990 年代初可能与原始语言范围以及他们获得第一个版本所面临的时间压力有关如果他们花了额外的几个月/几年的时间试图把它做好,那么该项目很可能会被终止……或者营销窗口将会关闭。)

What I am wondering is why the designers of Java didn't only have the object types and use the primitive versions as optimizations behind the scenes.

To understand the reasons behind the Java design decisions, you need to understand the historical context in which they were made.

The strong distinction between primitive types and reference types was baked into the language design pre JDK 1.0. Prior to JDK 1.5, that's all there was: if you wanted to put integers into collections (for example), you explicitly used Integer.valueOf(int) etc.

When auto-boxing / auto-unboxing was added to the type system in JDK 1.5, it had to be done in a way that was backwards compatible. What they came up with is a good compromise ... but not what they would / could have achieved if they had started from a clean sheet.

(And the reason that they didn't / couldn't "get it right" first time ... way back in the early 1990s is probably to do with the original language scope and the time pressures they were under to get the first release out. If they had spent extra months / years trying to get it right, the chances are that the project would have been killed ... or that the marketing window would have closed.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文