new operator in Java (original) (raw)

Last Updated : 16 Jun, 2026

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.

Syntax

ClassName objectName = new ClassName();

Java `

class Student{ Student() { System.out.println("Object Created !!"); } } public class GFG{ public static void main(String[] args) { Student s = new Student(); } }

`

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.

Java `

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];

Java `

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]);
    }
}

}

`

**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

Memory Allocation Using new

Objects and arrays created using the new operator are stored in heap memory.