Day18: How java handles deallocation of dynamic block

 How java handle deallocation of dynamic block

Before we can understand how java handles deallocation of dynamic block, we first have to understand terminology called as : 
  1. Garbage Block
  2. Garbage Collector
Garbage block in java are those are not pointed by or referred by any reference. If an object reference or an array reference stop pointing to its object and array garbage block.
int [ ] arr = new int[];

Now inside JVM there is a  special software called as garbage collector which scans heap area time to time, identified garbage block and then automatically destroy them.
As mention above the GC remains under the control of JVM and programmer has no control no role in this process. Then we say that in java Allocation of dynamic block is done as programmer request. But the deallocation is automatically done by the JVM and so we also say Java have automatic memory management.

Initializing an Array

We can initialize an array in three format:

(1) int [ ] months = new int[12];
        month[] = 31;
        month[1]=28;
        month[2] = 31;
        month[3] = 30;
        month[4] = 31;
        month[5] = 30;
        month[6] = 31;
        month[7] = 31;
        month[8] = 30;
        month[9] = 31;
        month[10] = 30;
        month[11] = 31;
(2) int[ ] months = new int[]{31, 28, 31, 30, 31, ....31}
(3) int [ ] months = {31, 28, 31, 30, 31, ....31}

Using Length Property

Basically length property is used to count the length of an array i.e., number of elements inside an array.
Syntax  :
        arr.length

Example : 

class StaticArray
{
public static void main(String [ ] args)
{
int [ ] arr = {10,5,11,56,78,21,34,90,22,35,61};
System.out.println("Array Size is "+ arr.length);
}
}

Output : Array Size is 11



As we know every array in java is an object so its contain some method and data member(properties). One of very useful property of an array is length which return size of array.

int [ ] arr = {10,5,11,56,78,21,34,90,22,35,61}
System.out.println("Array Size is "+ arr.length)

But length is a final property means we cannot assigned any value to length.
So the following code will give syntax error:
        int[ ] arr = {10,5,11,56,78,21,34,90,22,35,61}
        arr.length = 10 //Syntax error


Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?