New Operator in Java

New Operator

In this article we will be discussing about new operator in Java.

The operator  new in any  programming language requests for the memory allocation in heap. If enough memory is available, it initializes the memory to a pointer variable and returns its address.

New Operator in Java

New Operator in Java:

The new operator in java is used to create new objects of a class. A request will be sent to the Heap Memory for object creation. If enough memory is available, the operator new initializes the memory and returns the address of the newly allocated and initialized memory to a the object variable.

This is used to operate 3 different operations in java.

  • Declaration
  • Instantiation
  • Initialization

Declaration:

We have to declare a variable of that class type. This variable can be used to refer to that newly created object.

Syntax:

class_name Variable_name

Example:

// Java is an object of PrepInsta class
PrepInsta java;

Instantiation:

We have to create the object with “new” keyword. The new keyword instantiates a class by allocating memory for a new object and returning a reference to that memory.

Syntax:

class_name Variable_name = new class_name

Example:

PrepInsta java = new PrepInsta;

Initialization:

The java compiler provides a default compiler with no arguments which will be called by default at the time of object creation. The ‘new’ keyword is followed by a call to a constructor. Constructors are an important part of all classes and have many significant attributes.

Example

// Defining a class
class PrepInsta{

// Initializing the class variables String java; String c++; String python; }

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription