+ 1

What is purpose of new keyword in java?

my question is in c++ we can make object of a class without using new keyword,but in java there is nessassary of using new keywords . why is it so ?

29th Jun 2021, 12:57 PM
G. Yaseen
G. Yaseen - avatar
2 Answers
+ 2
in Java, with the exception of primitive types, all the others are dynamically allocated reference types, which is why they require the use of the new. Specifically, they are all objects, including arrays and to which the new is not necessary if an array literal is assigned, however, used to create an array object as happens for strings. in fact Java in its original declination (it is not clear to me how much it is changing with Oracle) aimed to remove every element of complexity or rarely used, typical of C ++. the thing is so true that even an enum type implicitly extends the java.lang.Enum class, so enums are a bit of a special class having constants defined within it that methods can be applied to ... if I remember correctly
30th Jun 2021, 12:01 PM
Ciro Pellegrino
Ciro Pellegrino - avatar
0
java has not pointers, but can assigns null to declared object, with new it is more readable that there is assign new just creating object.
29th Jun 2021, 6:41 PM
zemiak