In java, why new operator not used before data type to allocate memory? -
In java, why new operator not used before data type to allocate memory? -
if need new
operator allocate memory object, why don't utilize before info types allocate memory?
class-name class-var = new class-name(); new int a;
because james gosling said so.... (or bjarne stroustrup said so). really, question of language design, not technical laws.
javac
hides these semantics , called boxing / unboxing (and automatically). types can exist values
or "objects" (typically implemented heap). when context requires object reference, javac emits box instruction move int
value object wrapper (int -> integer) , passes reference value
. many low level jvm opcodes built handle scalar values built handle reference values (or references).
one prime illustration storing int
collection. gets boxed.
but in end, asking why language syntactically asking artist why painted painting thus. because. languages designed whim , emotion, in java's case, syntax of new
inherited c++ whim might have been bjarne stroustrup's instead. consider scala jvm language, yet has different syntax mutual ideas.
the key here compiler author can create decision tomorrow "new java" new language requires new
in caps in front end of types. implemented without affecting semantics of language, whatsoever.
granted, there sound design , consistency behind choices, choices still choices. in case, selection indicates int
primitive type, , new
returns objects, not primitives. selection of syntax.
java new-operator
Comments
Post a Comment