Home >Java >javaTutorial >What does new mean in java?
"new" can be said to be the most commonly used keyword by Java developers. We use new to create objects, use new and instantiate anything we need through the class loader.
Using the new keyword in Java makes it easy to create objects.
Need to access a file? Just create a new File instance: new File("jdbc.properties")
The new operator instantiates a class object by allocating memory to this object and returns a reference to that memory. The new operator also calls the object's constructor.
Note: "Instantiate an object of a class" means "create an object". When you create an object, you are creating an "instance" of a class, thus "instantiating" an object of the class.
The new operator requires a single, suffix parameter, which requires calling the constructor. The name of the constructor provides the name of the class that needs to be instantiated.
The new operator returns a reference to the object it creates. This reference is usually assigned to a variable of the appropriate type, such as: Point originone = new Point (23, 94);
The reference returned by the new operator does not need to be assigned to a variable. It can also be used directly in an expression. For example: int height = new Rectangle().height;
Summary:
1. The Java keyword new is an operator. It has the same or similar precedence as , -, *, / and other operators.
2. Creating a Java object requires three steps: declaring reference variables, instantiating, and initializing object instances.
3. Instantiation: It is to "create a Java object" ----- allocate memory and return a reference to the memory.
4. Initialization: It means calling the constructor method and assigning an initial value to the instance data of the class.
The above is the detailed content of What does new mean in java?. For more information, please follow other related articles on the PHP Chinese website!