Why do C# and Java bother with the “new” operator?

后端 未结 9 1035
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 13:59

Why does the new operator exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose?

For insta

相关标签:
9条回答
  • 2020-12-03 14:37
    Class1 obj = Class1();
    

    In C# and Java, you need the "new" keyword because without it, it treats "Class1()" as a call to a method whose name is "Class1".

    0 讨论(0)
  • 2020-12-03 14:38
    1. It's a self documenting feature.
    2. It's a way to make it possible to name a method "Class1" in some other class
    0 讨论(0)
  • 2020-12-03 14:41

    The usefulness is of documentation - it's easier to distinguish object creations from method invocations than in Python.

    The reason is historic, and comes straight from the C++ syntax. In C++, "Class1()" is an expression creating a Class1 instance on the stack. For instance: vector a = vector(); In this case, a vector is created and copied to the vector a (an optimizer can remove the redundant copy in some cases).

    Instead, "new Class1()" creates a Class1 instance on the heap, like in Java and C#, and returns a pointer to it, with a different access syntax, unlike Java and C++. Actually, the meaning of new can be redefined to use any special-purpose allocator, which still must refer to some kind of heap, so that the obtained object can be returned by reference.

    Moreover, in Java/C#/C++, Class1() by itself could refer to any method/function, and it would be confusing. Java coding convention actually would avoid that, since they require class names to start with a upper case letter and method names to start with a lower case one, and probably that's the way Python avoids confusion in this case. A reader expects "Class1()" to create an object, "class1()" to be a function invocation, and "x.class1()" to be a method invocation (where 'x' can be 'self').

    Finally, since in Python they chose to make classes be objects, and callable objects in particular, the syntax without 'new' would be allowed, and it would be inconsistent to allow having also another syntax.

    0 讨论(0)
  • 2020-12-03 14:43

    The reason Java chose it was because the syntax was familiar to C++ developers. The reason C# chose it was because it was familiar to Java developers.

    The reason the new operator is used in C++ is probably because with manual memory management it is very important to make it clear when memory is allocated. While the pythonesque syntax could work, it makes is less obvious that memory is allocated.

    0 讨论(0)
  • 2020-12-03 14:44

    The new operator allocates the memory for the object(s), which is it's purpose; as you say it also self documents which instance(s) (i.e. a new one) you're working with

    0 讨论(0)
  • 2020-12-03 14:45

    In addition to remarks above AFAIK they were planning to remove new keyword for Java 7 in early drafts. But later they cancelled it.

    0 讨论(0)
提交回复
热议问题