简体   繁体   中英

In java, why new operator not used before data type to allocate memory?

If we need new operator to allocate the memory for an object, then why don't we use it before data types to allocate the memory?

class-name class-var =  new class-name();
new int a;
  1. Because James Gosling said so.... (or Bjarne Stroustrup said so). Really, it is mostly a question of language design, not technical laws.

  2. javac hides these semantics from you and does what is called boxing / unboxing (and does it automatically). Those types can exist as values or as "objects" (typically implemented with a heap). When a context requires an object reference, javac emits a box instruction to move the int value into an object wrapper (int -> Integer) and passes a reference value . Many low level JVM opcodes are built to handle scalar values as are some built to handle reference values (or just references).

One prime example is storing an int into a collection. It gets boxed.

But in the end, asking why a language does what it does syntactically is just like asking an artist why he painted a painting thus. Just because. Languages are designed somewhat by whim and emotion, but in Java's case, the syntax of new was inherited from C++ so the whim might have been Bjarne Stroustrup's instead. Also consider that Scala is also a JVM language, yet it has very different syntax for some common ideas.

The key here is the compiler writer can make a decision tomorrow that "NEW Java" will be a new language that requires NEW in all caps in front of all types. It could be implemented without affecting the semantics of the language, whatsoever.

Granted, there is sound design and consistency behind the choices, but the choices are still just choices. In this case, the choice clearly indicates that an int is a primitive type, and new only returns objects, not primitives. So it is a good choice of syntax.

Java has two kinds of types: primitive data types and reference data types.

int is a primitive data type. A reference data type references an instance of a class , or an instance of an array.

For primitive data types, the new operator is not used. Primitive data types are always passed by value . For reference data types, the reference is passed by value, but you can have many references pointing to the same object or array.

So if you change anything in the object that your reference points to, all other references also see the change. For primitive data types, this doesn't happen.

More information about primitive and reference data types:

Chapter 4 of the Java Language Specification

this is mostly due to how compilers and/or interpreters will read this source. Basically you must tell the compiler/interpreter what type of object it is working with prior to assigning memory to it. This is the same in most languages, including C and Java. You basically tell the compiler/interpreter the type of object, the name of that object, then the assignment operation. new is not always to the right of the assignment operator, as-in you could be copying/cloning an existing variable name/handle of the same object type.

The reason Java primitives don't need the new operator is as @pep said, the language/platform already knows the defined size of that type. int will always be 32 bytes in Java, but MyCustomClass might not be.

Because primitive data types are already defined by the language, and named by keywords. All you have to do is declare it, and already defined memory will be reserved for it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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