GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Objects
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — new operator in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

new operator in Java

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.

  • 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();

Java
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.

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

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 new operator can create a new String object by allocating separate memory in the heap.
  • Wrapper Class Objects: The new operator 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.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.