How is memory allocated by new ?a) In a heap b) in a stack b) both a & b c) None of these.

Showing Answers 1 - 63 of 63 Answers

bharath kumar

  • Jul 14th, 2006
 

 new is a keyword that creates the instance of the class

  Was this answer useful?  Yes

pratap

  • Jul 21st, 2006
 

java object create in heap but stored in stack

  Was this answer useful?  Yes

manna bhai

  • Aug 2nd, 2006
 

   if u have instance variables in class so memory for all instance variable will be allocated in heap. not in stack stack is for method variable

siva kumar reddy

  • Aug 9th, 2006
 

hi,

actually static variables ,local variables ,method parameters are stored in stack.somany stacks are created(one stack per one thread)

instance variables and java objects are stored in heap.only heap is created.

cheers

siva

  Was this answer useful?  Yes

k.r

  • Aug 12th, 2006
 

The JVM allocates memory in two ways1) Stack: for local variables (declared in methods and constructors). this is deallocated once the method exits.2) heap: everytime an object is created using new keyword, it is allocated in the heap. theses have a longer lifespan. it the JVM runs out of space in the heap it gives a Java out-of-memory error.

janmejay

  • Aug 16th, 2006
 

Hey!NO NO........everytime an object is created using new keyword, it is allocated in the heap. these have a longer lifespan. if the JVM runs out of space in the heap it gives a Java out-of-memory error........................and local variables and methods goes to stack...and this is deallocated once the method exits and variables goes out of scope......

  Was this answer useful?  Yes

durga prasad reddy

  • Aug 19th, 2006
 

stored in heap and the reference variables are stored in stack.if heap is filled then out of memory exception occurs

  Was this answer useful?  Yes

C.R.Venkatesh

  • Aug 27th, 2006
 

Here is my 2 cents worth.


(a) Simply put, all Java Objects are allocated memory in Heap.



(b) Primitives types (not *array* of primitives) and references to objects are allocated on the stack.



(c) Importantly, each thread in the JVM gets it own stack and the VM option



-Xss decides the stack size for each thread.



Note: If the stack size is too small, eventually you see "StackOverflowError".


Example:class FooBar{    
public void get(MyTest obj)   // obj a reference to an object - stack.   
{          
int x = 10;    // Stack         
Vector tmpObj=new Vector(); // Heap         
...         
...   
}
}
 
Note:  tmpObj will be garbage collection based on the rules 
(like reference count and others) that GC maintains to check its life span.
HTH.

Kishor raja Kumar

  • Sep 25th, 2006
 

Hai All,

   By using new operator we can allocate memory.ex:new A(); this statement

   allocates memory for the class A. suupose A obj;means one reference will be create for the class A.When u write obj=new A(); then only that reference become object.for reference memory will not created.For objects only memeory will be created...

 If i am wrong plz let me know

Thanks and Regards

kishor.V

  Was this answer useful?  Yes

zakir hussain

  • Oct 14th, 2006
 

when memory is allocated in heap, it gets storage in heap.

  Was this answer useful?  Yes

gokul

  • Nov 30th, 2006
 

when a jvm encounter the new keyword then it will try create object in heap only. But String is created in String pool which is one part of the heap.

  Was this answer useful?  Yes

ramesh chandra

  • Jan 2nd, 2007
 

hi

u asked what actually new does

it creates memory for the variables just nothing but an constructor

it is operator which is used to allocate memory in c++  as per my knowledge is concerned

  Was this answer useful?  Yes

Sony V George

  • Apr 4th, 2007
 

a. ofcource the object will create on heap. When ever it will have live thread then only it will be on stack

  Was this answer useful?  Yes

Arun Ruhela

  • Jul 3rd, 2012
 

Always object allocated memory in heap and method always memory allocated in stack.

  Was this answer useful?  Yes

malik_malik

  • Sep 23rd, 2012
 

When you declare a variable with new keyword its allocated space in heap.because we dynamically create a variable and we create a variable dynamically its allocated space in heap.

  Was this answer useful?  Yes

venkat

  • Mar 7th, 2013
 

a) In a Heap

  Was this answer useful?  Yes

Pavithra

  • Dec 20th, 2013
 

In a stack

  Was this answer useful?  Yes

Harendra Pratap Singh

  • Sep 23rd, 2014
 

A) In heap

  Was this answer useful?  Yes

pawan

  • Nov 5th, 2014
 

a

  Was this answer useful?  Yes

Avinash

  • Apr 2nd, 2015
 

what if we create an object inside method itself ???

  Was this answer useful?  Yes

Monika Srivastava

  • May 4th, 2016
 

Whenever object is created,It store local and partial results in heap storage(for ex.int a=10) and instance of object is stored in stack as a form of frames.At the time of method invocation, a new frame created and destroy when method invocation is stopped.

  Was this answer useful?  Yes

vinod

  • Jul 6th, 2016
 

a) In a heap

  Was this answer useful?  Yes

pallavi sune

  • Sep 11th, 2017
 

a) In a heap

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions