Search Interview Questions
| More than 3000 questions in repository.|
There are more than 900 unanswered questions.
Click here and help us by providing the answer.
Have a video suggestion.
Click Correct / Improve and please let us know.
|Interview Questions and Answers|
|Ans. Because of the life cycle requirement for different type of values in java. |
variables initialized and used in functions needs to be destructed with the execution of function and hence kept in stack. Same is applicable for the object references initialized within the method. If objects would have been created in stack, they wouldnt have been passed around across methods and hence they are created on heap.
So anything that is required beyond the scope of a method or function is kept on heap which is usually garbage collected by Java.
|Help us improve. Please let us know the company, where you were asked this question :|
|Like Discuss Correct / Improve  memory management  heap  stack|
Try 1 Question(s) Test
|Does garbage collection guarantee that a program will not run out of memory?|
|What are different ways to create String Object? Explain.|
|Describe, in general, how java's garbage collector works ?|
|Describe what happens when an object is created in Java ?|
|What are various types of Class loaders used by JVM ?|
|Should we override finalize method ?|
|What are strong, soft, weak and phantom references in Java ?|
|Which memory areas does instance and static variables use ?|
|Can we call the garbage collector explicitly ?|