简体   繁体   English

java 中的 `new` 关键字是多余的吗?

[英]Is the `new` keyword in java redundant?

I am coming from C++ so there is one feature of java that I don't quite understand.我来自 C++ 所以我不太明白 java 的一个特性。 I have read that all objects must be created using the keyword new , with the exception of primitives.我已经读过所有对象都必须使用关键字new创建,除了原语。 Now, if the compiler can recognise a primitive type, and doesn't allow you to create an object calling its constructor without new , what is the reason to have the keyword new at all?现在,如果编译器可以识别原始类型,并且不允许您创建一个 object 调用其构造函数而不使用new ,那么使用关键字new的原因是什么? Could someone provide an example when two lines of code, identical except for the presence of new , compile and have different meaning/results?有人可以提供一个示例,当两行代码相同时,除了存在new ,编译并具有不同的含义/结果吗?

Just to clarify what I mean by redundant, and hopefully make my question clearer.只是为了澄清我所说的冗余的意思,并希望让我的问题更清楚。 Does new add anything? new增加什么吗? Could the language have been expressed without new for instantiation of objects via a constructor?语言是否可以在没有new的情况下通过构造函数来实例化对象?

Methods and constructors can have the same name.方法和构造函数可以具有相同的名称。

public class NewTest {

    public static void main(final String[] args) {
        TheClass();
        new TheClass();
    }

    static void TheClass() {
        System.out.println("Method");
    }

    static class TheClass {
        TheClass() {
            System.out.println("Constructor");
        }
    }
}

Whether this language design choice was a good idea is debatable, but that's the way it works.这种语言设计选择是否是一个好主意还有待商榷,但这就是它的工作方式。

Believe it or not, requiring the use of new for constructors is a namespacing thing.信不信由你,要求对构造函数使用new是一个命名空间的事情。 The following compiles just fine:以下编译得很好:

public class Foo {
    public Foo() {}
    public void Foo() {}
}

I don't now why the java language designers decided to add the new statement to the java language, but, I don't want to miss it.我现在不明白为什么java 语言设计者决定将new语句添加到 java 语言中,但是,我不想错过它。 Even if it could be redundant if it wasn't allowed to give classes, methods and fields the same name, like others have shown already in their answers:即使如果不允许给类、方法和字段赋予相同的名称,这可能是多余的,就像其他人已经在他们的答案中显示的那样:

Because - it greatly improves readability.因为——它极大地提高了可读性。 Everytime I read that new statement, I realize immediately that a new object is born.每次我读到new声明,我立刻意识到一个新的 object 诞生了。

If you came from C++, why are you surprised?如果您来自 C++,为什么会感到惊讶? Doesn't C++ have the same new , for the same purpose? C++ 没有相同的new ,用于相同的目的吗? Java tries to follow most of C/C++ syntax, that's most likely the reason. Java 试图遵循大多数 C/C++ 语法,这很可能是原因。

Commenting on artbristol's answer: It is highly improbable that Java designers laid out namespaces first, then were forced to add new keyword.评论 artbristol 的回答:Java 设计师首先布置命名空间,然后被迫添加新关键字是极不可能的。 It is very likely the opposite: new was there first, then they found that it allows them to have constructor names collide with other names.很可能正好相反: new先出现,然后他们发现它允许他们让构造函数名称与其他名称发生冲突。

The new must be written in Java to create a new object instance. new的必须写入 Java 以创建新的 object 实例。

public class Foo {

    public void test() {
        final Foo foo1 = new Foo();
        final Foo foo2 = Foo();
    }

    public Foo Foo() {
        System.out.println("hello world");
        return this;
    }
}

Note, that methods starting with an uppercase character are discouraged in Java to avoid the confusion.请注意,在 Java 中不鼓励以大写字符开头的方法以避免混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM