In java, why is the new statement not used before the data type to allocate memory?

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

 class-name class-var = new class-name(); new int a; 
+7
java new-operator
source share
4 answers
  • Because James Gosling said so .... (or Bjarne Stroustrup said so). In fact, it is mainly about language design, and not about technical laws.

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

One striking example is storing int in a collection. He gets a box.

But in the end, asking why the language does what it does syntactically, it simply asks the artist why he painted the picture this way. Just because. Languages ​​are created on a whim and emotion, but in the case of Java, the syntax new was inherited from C ++, so Bjarne Stroustrup's could be a whim instead. Also note that Scala is also a JVM language, but it has very different syntax for some common ideas.

The key point here is that the compiler can decide tomorrow that "NEW Java" will be a new language that requires new in all caps in front of all types. This can be implemented without affecting the semantics of the language.

Of course, there is the right design and sequence of choice, but the choice is still just a choice. In this case, the choice clearly indicates that int is a primitive type, and new returns objects, not primitives. So this is a good choice of syntax.

+3
source share

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

int is a primitive data type. The reference data type refers to 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, a 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 referenced by your link, all other links also see the change. For primitive data types, this does not happen.

Additional information on the types of primitive and reference data:

Chapter 4 Java Language Specifications

+2
source share

this is mainly because of how the compilers and / or interpreters will read this source. Basically, you should tell the compiler / interpreter what type of object it is working on before assigning memory to it. This is the same in most languages, including C and Java. You basically tell the compiler / interpreter the type of the object, the name of that object, and then the assignment operation. new not always to the right of the assignment operator, since you can copy / clone an existing variable name / descriptor of the same type of object.

The reason for Java primitives does not require the new operator, as indicated by @pep, the language / platform already knows a certain size of this type. int will always be 32 bytes in Java, but MyCustomClass may not be.

0
source share

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

0
source share

All Articles