Sunday 20 April 2014

Core Java basics

Pointer Arithmetic

Pointer Arithmetic is supported by c++. Java doesn't support pointer arithmetic.

Pointer arithmetic in c++

a[0] = 10;
a[1] = 20;
a[2] = 30;                                     1000 ---> 10           20            30            40           50
a[3] = 40;                                       a             1000    1002       1004        1006        1008
a[4] = 50;
*(a+5) = 60;



*(a+0) --->  * (1000 + 0) ----> * (1000)

*(a+1) ----> * (1000 + 1) -----> * (1002)

* (a+2) -----> *(1000 + 2) ------> * (1004)

* (a+3) -----> * (1000 + 3) --------> *(1006)

* (a+4) ----->  *(1000 + 4) --------> *(1008)


p
2000
1000
-                  -
-                 -
-                 -
-                -
a       
10           20     Unreserved
2000      2002


int main(0
{
int *p;
int a = 10;
p= &a;
printf(" %d", *p);  ------>10
p++;
*p=20;
printf("%d", *p); ------->20
p--;
printf("%d", *p); ------>10
}

Java has automatic memory management. No dangling pointers and no memory leaks.

Memory Leak

When Program loss an address or ability to un reserve the memory which is reserved then the memory would be leaked.

Java simplifies pointer handling no reference / dereference operations.

No make files and no header files.


No comments:

Post a Comment