The new keyword
The new keyword in java programming language is used to create a new instance of a class or an array. Keywords are basically reserved words which have specific meaning relevant to a compiler. The new keyword allocates new objects and initialize them.
Syntex: Here is the syntax that displays how to create an instance of a class or an array by using the new operator.
Creating a simple java object (non array object) by using the new operator.
String sName = new String(); |
Creating an array object by using the new operator.
Array arr = new Array(); |
Note: Here are some points that must be considered while creating an instance of a class or an array.
-
The argument that follows the new keyword must be a class name followed by a series of constructor arguments having parentheses.
-
The collection of arguments must be same as the signature of a constructor for the class.
-
The variable type on the left side of the assignment operator must be compatible with the class or interface which is being instantiated.