Found 2617 Articles for Java

Memory leaks in Java

AmitDiwan
Updated on 07-Jul-2020 08:19:01

364 Views

In Java, garbage collection (the work of destructor) is done automatically using garbage collection. But what if there are objects that have references to them in the code? It can’t be de-allocated, i.e their memory can’t be cleared. If such a situation occurs again and again, and the created or referred objects are not used at all, they become useless. This is what is known as a memory leak.If the memory limit is exceeded, the program gets terminated by throwing an error, i.e ‘OutOfMemoryError’. This is the reason why it is always suggested to remove all references to an object ... Read More

Max Heap in Java

AmitDiwan
Updated on 07-Jul-2020 08:17:19

2K+ Views

Max heap is a complete binary tree, wherein the value of a root node at every step is greater than or equal to value at the child node.Below is an implementation of Max Heap using library functions.Example Live Demoimport java.util.*; public class Demo{    public static void main(String args[]){       PriorityQueue my_p_queue = new PriorityQueue(Collections.reverseOrder());       my_p_queue.add(43);       my_p_queue.add(56);       my_p_queue.add(99);       System.out.println("The elements in the priority queue are : ");       Iterator my_iter = my_p_queue.iterator();       while (my_iter.hasNext())       System.out.println(my_iter.next());       my_p_queue.poll(); ... Read More

Java Virtual Machine (JVM) Stack Area

AmitDiwan
Updated on 07-Jul-2020 08:15:24

738 Views

Following are some key points to undertstand JVM Stack Area −During the creation of a thread, the Java Virtual Machine creates a separate stack.The JVM performs only two operations upon this stack. The operations are push (i.e insert) and pop (i.e delete).When a thread is currently in execution, the stack associated with it is known as runtime stack.Every method call done by the thread, intermediate calculations, assignment of local variables, calling parameters etc, are stored as an operation in the runtime stack.Once the thread stops or completes executing, the respective part from the stack is deleted.Once all the calls by ... Read More

Java Program for Iterative Quick Sort

AmitDiwan
Updated on 07-Jul-2020 08:13:14

602 Views

Following is the Java program for Iterative Quick Sort −Example Live Demopublic class Demo{    void swap_vals(int arr[], int i, int j){       int temp = arr[i];       arr[i] = arr[j];       arr[j] = temp;    }    int partition(int arr[], int l, int h){       int x = arr[h];       int i = (l - 1);       for (int j = l; j l){             my_list[++top] = l;             my_list[++top] = p - 1;       ... Read More

Java Program for Iterative Merge Sort

AmitDiwan
Updated on 07-Jul-2020 08:10:40

356 Views

Following is the Java program for Iterative Merge Sort −Example Live Demoimport java.util.Arrays; public class Demo{    public static void merge_sort(int[] my_arr){       if(my_arr == null){          return;       }       if(my_arr.length > 1){          int mid = my_arr.length / 2;          int[] left = new int[mid];          for(int i = 0; i < mid; i++){             left[i] = my_arr[i];          }          int[] right = new int[my_arr.length - mid];     ... Read More

Java Program for Gnome Sort

AmitDiwan
Updated on 07-Jul-2020 08:06:04

220 Views

Gnome Sort works with one lement at a time and gets it to the actual position. Let us see an example to implement Gnome Sort −Example Live Demoimport java.util.Arrays; public class Demo{    static void gnome_sort(int my_arr[], int n){       int index = 0;       while (index < n){          if (index == 0)             index++;          if (my_arr[index] >= my_arr[index - 1])             index++;          else{             int temp = 0;   ... Read More

Java Program for Reversal algorithm for array rotation

AmitDiwan
Updated on 07-Jul-2020 08:04:24

224 Views

Following is the Java program to implement Reversal algorithm for array rotation −Example Live Demoimport java.io.*; public class Demo{    static void rotate_left(int my_arr[], int no_of_rotation){       int n = my_arr.length;       array_reversal(my_arr, 0, no_of_rotation - 1);       array_reversal(my_arr, no_of_rotation, n - 1);       array_reversal(my_arr, 0, n - 1);    }    static void array_reversal(int my_arr[], int start, int end){       int temp;       while (start < end) {          temp = my_arr[start];          my_arr[start] = my_arr[end];          my_arr[end] = ... Read More

Java Program for Recursive Insertion Sort

AmitDiwan
Updated on 07-Jul-2020 07:57:49

724 Views

Following is the Java Program for Recursive Insertion Sort −Example Live Demoimport java.util.Arrays; public class Demo{    static void recursive_ins_sort(int my_arr[], int arr_len){       if (arr_len = 0 && my_arr[j] > last){          my_arr[j+1] = my_arr[j];          j--;       }       my_arr[j+1] = last;    }    public static void main(String[] args){       int my_arr[] = {11, 23, 67, 83, 42, 11, 0};       recursive_ins_sort(my_arr, my_arr.length);       System.out.println("The array elements after implementing insertion sort is ");       System.out.println(Arrays.toString(my_arr));    } }OutputThe ... Read More

Java Program for Recursive Bubble Sort

AmitDiwan
Updated on 07-Jul-2020 07:56:02

1K+ Views

Following is the Java Program for Recursice Bubble Sort −Example Live Demoimport java.util.Arrays; public class Demo{    static void bubble_sort(int my_arr[], int len_arr){       if (len_arr == 1)       return;       for (int i=0; i my_arr[i+1]){          int temp = my_arr[i];          my_arr[i] = my_arr[i+1];          my_arr[i+1] = temp;       }       bubble_sort(my_arr, len_arr-1);    }    public static void main(String[] args){       int my_arr[] = {45, 67, 89, 31, 63, 0, 21, 12};       bubble_sort(my_arr, my_arr.length);   ... Read More

Java Program to calculate area of a Tetrahedron

AmitDiwan
Updated on 07-Jul-2020 07:54:39

277 Views

A Tetrahedron is a polyhedron composed of four triangular faces, six straight edges, and four vertex corners.Following is the Java program to calculate area of a Tetrahedron −Example Live Demoimport java.io.*; public class Demo{    static double tetra_vol(int side){       double my_vol = (Math.pow(side, 3) / (6 * Math.sqrt(2)));       return my_vol;    }    public static void main(String[] args){       int side = 4;       double my_vol = tetra_vol(side);       my_vol = (double)Math.round(my_vol * 100) / 100;       System.out.println("The area of tetrahedron is");       System.out.println(my_vol);   ... Read More

Advertisements