An Interview Question.
I recently faced this Question in an Interview. I could not crack it. Please help buddies.
The question is..
In C, we use malloc() to allocate memory dynamically. Similarly in Java, if we need to allocate memory dynamically how will you solve it? :confused:
Seriously? You're interviewing for Java programming jobs, and you want an explanation of new? Maybe I am misunderstanding you, since you posted in the New to Java forum. What was the interview for?
Originally Posted by vinoth
The interview covered C,C++, and Java.
Well, I know 'new' is used to instantiate an Object.
It is used as..
<class_name> <object_name>= new <class_name()>;
But my question is.. How is 'new' equivalent to 'malloc()'??
So, you know what new does...surely you can figure out where the equivalence is?
Malloc and New
Actually you can not allocate memory dynamically in java like you do in C/C++ using malloc.
Because in malloc you can specify the size of the memory which you want to allocate but in java thought we have new operator but we can not specify the size of the memory in new. ;)
The size of the memory is calculated automatically by JVM execution engine.
That's why Java is secure language. :cool:
Originally Posted by dgaur
byte memory= new byte;