The new operator in Java is used to create new objects and allocate memory dynamically during program execution. It creates an instance of a class and returns a reference to that object. The new operator plays an important role in object-oriented programming by allowing objects to be created and initialized at runtime.
- It returns the reference/address of the newly created object.
- It can be used with classes, arrays, and other objects.
- Objects created using new are stored in heap memory.
Syntax
ClassName objectName = new ClassName();
class Student{
Student() {
System.out.println("Object Created !!");
}
}
public class GFG{
public static void main(String[] args) {
Student s = new Student();
}
}
Output
Object Created !!
Steps to create an object
When an object is created in Java, it generally involves three important steps:
1. Declaration
Declaring a reference variable of a class type. At this stage, no memory is allocated for the object. Only a reference variable is created.
Syntax:
class-name var-name;
Student s;
Here, s is a reference variable of type Student, but it does not point to any object yet.
2. Instantiation
Allocating memory for the object using the new operator. Memory is allocated in the heap, and the reference variable starts pointing to the object. The new operator allocates memory and creates the object.
s = new Student();
3. Initialization
Initializing the object by invoking the constructor. The constructor initializes the object’s data members.
class Student {
int id;
String name;
Student() {
id = 1;
name = "Rahul";
}
}
Using new to Create Arrays
The new operator is also used to create arrays in Java.
Syntax:
int[] numbers = new int[5];
public class GFG{
public static void main(String[] args) {
// Creating an array using the new operator
int[] n = new int[5];
// Initializing array elements
n[0] = 10;
n[1] = 20;
n[2] = 30;
n[3] = 40;
n[4] = 50;
// Printing array elements
for (int i = 0; i < n.length; i++) {
System.out.println(n[i]);
}
}
}
Output
10 20 30 40 50
Explanation: The new operator allocates memory for the array and initializes elements with default values. For integer arrays, the default value is 0.
Types of Objects Created Using New Operator
- Class Objects: The new operator is used to create objects of a class by allocating memory in the heap. It initializes the object by calling the class constructor.
- Array Objects: The new operator is used to create arrays dynamically by allocating memory for the specified number of elements.
- Anonymous Objects: An anonymous object is created using the new operator without storing its reference in a variable. It is mainly used when an object is required only once.
- String Objects: The
newoperator can create a new String object by allocating separate memory in the heap. - Wrapper Class Objects: The
newoperator can create objects of wrapper classes like Integer, Double, and Boolean (though modern Java usually uses autoboxing instead).
Memory Allocation Using new
Objects and arrays created using the new operator are stored in heap memory.
- The object reference variable is stored in stack memory.
- The actual object data is stored in heap memory.
- Java Garbage Collector automatically removes unused objects from heap memory.