7.Variables can be divided into four categories: static, stack-dynamic, explicit
ID: 3911617 • Letter: 7
Question
7.Variables can be divided into four categories: static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic. Determine which category the following Java variables belong to. (a) Local variables (declared within methods) (b) Variables declared within an interface (c) Instance variables (declared within classes without using the word static) (d) Class variables (declared within classes using the word static) (e) Objects created using the word new (f) Arrays created using the word new (g) Arrays created without using the word new (h) String literals (i) Also, briefly explain the difference between implicit and explicit heap-dynamic variablesExplanation / Answer
(a) Local Variables (declared within methods):- Stack-dynamic
The Scope of local variables declared within a method is within the method itself. We can’t access them outside the method.. The auto variables are stored in Stack. The static variables are stored in Data Segment.
(b)Variables declared within interface :- Static
Variables defined within interfaces can’t be instantiated in their own. The value of these variables must be assigned in a static context in which no instance exists.
(c)Instance variables (declared within classes without using the word static):- Implicit heap-dynamic
Heap is a memory place where the objects and its instance variables are stored. As we do not explicitly allocate space to these variables , hence they are stored on implicit heap-dynamic.
(d)Class Variables (declared within classes using word static):- Implicit heap-dynamic
Static variables are stored on Managed Heap. They are mainly stored in the PermGen(Permanent Generation) section of the heap, since they are part of the reflection data (class related data, not instance related).
(e)Objects created using the word new:- Explicit heap-dynamic
The new operator automatically creates an object of proper size, and remains a pointer of the connect type. Hence they are stored on explicit heap.
(f)Arrays created using word new:- Explicit heap-dynamic
The new operator automatically creates an object of proper size, and remains a pointer of the connect type. Hence they are stored on explicit heap.
(g)Arrays created without using word new:- Implicit heap-dynamic
In Implicit heap, allocation and deallocation are caused by assignment statements.
(h)String Literals):- Implicit heap-dynamic
String literals could be stored in the data segment or code segment. It is to the up complier. As these segments are part of heap, hence they are stored on heap.
(i) Difference Between Implicit and Explicit heap-dynamic variables:-
Implicit Heap-dynamic variables
Explicit Heap-dynamic variables
1.Allocation and Deallocation are caused by assignment statements
1.Allocated and Deallocated by explicit directives, specified by the programmer, which take effect during execution
2. All variables ,string and arrays are stored here
2. Referenced only through pointers or references
3.Advantages:Flexibility(generic code can be written)
Disadvantage:
3.Advantages:Useful for dynamic Storage Management
Disadvantage:
Implicit Heap-dynamic variables
Explicit Heap-dynamic variables
1.Allocation and Deallocation are caused by assignment statements
1.Allocated and Deallocated by explicit directives, specified by the programmer, which take effect during execution
2. All variables ,string and arrays are stored here
2. Referenced only through pointers or references
3.Advantages:Flexibility(generic code can be written)
Disadvantage:
- Inefficient, because all attributes are dynamic
- Loss of error detection
3.Advantages:Useful for dynamic Storage Management
Disadvantage:
- Inefficient
- unreliable
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.