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.
codenheim
source share