What is New Operator in Java?
The object will be created by using the new operator; here the new is a keyword in java.
A very important responsibility of the new operator is to transfer the contents of a class file from the hard disk into the RAM.
Class A
{
Int i=1, j=2;
Public static void main (String args [ ])
{
A. a1 = new A ( );
a1.i = 10;
a1.j =20;
A.a2 = new A ();
a2. i= 1;
a2. j =2;
}
}
Consider the above program whenever this program is compiled and executed, first the main () method is transferred from the hard disk to the RAM. The first statement of the main method is
A a1 = new A ()
As soon as the keyword ‘new’ is encountered by the JVM it is understood that it has to load all the non-static elements of the Class file from the hard disk to the RAM. Inside the RAM some memory space would be allocated for these non–static elements and the address of this memory location would be assigned to the variable a1, where a1 is a variable of the type of class A.
Example
For example when we have a statement a1.i, then it gets access to the content of a1, which is nothing but the address of the memory location where the variable “i” is stored. Now it can get access to the variable “i” in that memory location whose address is an a1.
Example: a1.i =10;
a1 .j = 20;
New Operator Homework Help
Online Tutorspoint offers New Operator Homework Help, java assignment help & Dissertation writing help by Finance experts for Java students studying in universities in the USA, UK, and Australia.