dharmendar
java
1 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners
Advertisement
View Answers

November 18, 2008 at 11:21 PM


Hi friend,


Difference Stack vs Heap

Stack

When a function or a method calls another function which in turns calls another function etc.,
the execution of all those functions remains suspended until the very last function returns its value.

This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other.

The stack is important to consider in exception handling and thread executions.

Heap
The heap is simply the memory used by programs to store variables.
Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.

Example of Stack :

import java.io.*;
import java.util.*;

public class StackImplement{
Stack<Integer> stack;
String str;
int num, n;
public static void main(String[] args){
StackImplement q = new StackImplement();
}
public StackImplement(){
try{
stack = new Stack<Integer>();
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(ir);
System.out.print("Enter number of elements : ");
str = bf.readLine();
num = Integer.parseInt(str);
for(int i = 1; i <= num; i++){
System.out.print("Enter elements : ");
str = bf.readLine();
n = Integer.parseInt(str);
stack.push(n);
}
}
catch(IOException e){}
System.out.print("Retrieved elements from the stack : ");
while (!stack.empty()){
System.out.print(stack.pop() + " ");
}
}

Example of Heap :

public class heap_Sort{
public static void main(String a[]){
int i;
int arr[] = {1,3,4,5,2};

System.out.println("\n Heap Sort\n---------------\n");
System.out.println("\n Unsorted Array\n\n");
for (i = 0; i < arr.length; i++)
System.out.print(" "+arr[i]);
for(i=arr.length; i>1; i--){
fnSortHeap(arr, i - 1);
}
System.out.println("\n Sorted array\n---------------\n");
for (i = 0; i < arr.length; i++)
System.out.print(" "+arr[i]);
}

public static void fnSortHeap(int array[], int arr_ubound){
int i, o;
int lChild, rChild, mChild, root, temp;
root = (arr_ubound-1)/2;

for(o = root; o >= 0; o--){
for(i=root;i>=0;i--){
lChild = (2*i)+1;
rChild = (2*i)+2;
if((lChild <= arr_ubound) && (rChild <= arr_ubound)){
if(array[rChild] >= array[lChild])
mChild = rChild;
else
mChild = lChild;
}
else{
if(rChild > arr_ubound)
mChild = lChild;
else
mChild = rChild;
}

if(array[i] < array[mChild]){
temp = array[i];
array[i] = array[mChild];
array[mChild] = temp;
}
}
}
temp = array[0];
array[0] = array[arr_ubound];
array[arr_ubound] = temp;
return;
}
}

For more information on Stack or Heap Visit to :

http://www.roseindia.net/java/java-get-example/

http://www.roseindia.net/java/example/java/util/

Thanks
http://www.roseindia.net/java/beginners/









Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  is java purely object oriented language
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  sample code for RARP using java
java
java  RARP implementation using java socket
Java
Java  how to draw class diagrams in java
java
java  explain object oriented concept in java
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  why to set classpath in java