In Java, why is the new operator not used to allocate memory before the data type?

If we need the new operator to allocate memory for objects, why don't we use it

class-name class-var =  new class-name();
new int a;

Solution

>Because James Gosling said (or as Bjarne Stroustrup said) In fact, this is mainly a matter of language design, not technical rules

A major example is storing ints in collections It was boxed

But in the end, asking why language works in grammar is like asking an artist why he wants to draw a picture Just because Language is designed by whim and emotion, but in the case of Java, the syntax of new is inherited from C, so the whim may be Bjarne Stroustrup's. also consider that Scala is also a JVM language, but it has different syntax for some common ideas

The key is that the compiler writer can decide tomorrow that "new Java" will become a new language, and new is required in all capital letters before all types In any case, it can be implemented without affecting the language semantics

Of course, there is perfect design and consistency behind the choice, but the choice is still just a choice In this case, the choice clearly indicates that int is the base type, while new returns only objects, not primitives So this is a good grammar choice

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>