Found 7442 Articles for Java

Run-time Stack mechanism in Java

AmitDiwan
Updated on 07-Jul-2020 08:23:32

992 Views

Eeverytime a process or a code or a thread needs to run in Java, a runtime stack is created so as to store the operations performed while executing the thread.Every entry in the run-time stack is known as stack frame or activation record. Once a function has been called by the process, its associated data is deleted from the runtime stack.Once all the functions have been called, the runtime stack will be empty. This means it needs to be removed from the memory.At this point in time, the runtime stack is destroyed and then the thread is also terminated.A termination ... Read More

Merge two sets in Java

AmitDiwan
Updated on 07-Jul-2020 08:21:48

3K+ Views

To merge two sets in Java, the code is as follows −Example Live Demoimport java.util.stream.*; import java.util.*; import java.io.*; public class Demo{    public static Set set_merge(Set set_1, Set set_2){       Set my_set = set_1.stream().collect(Collectors.toSet());       my_set.addAll(set_2);       return my_set;    }    public static void main(String[] args){       Set my_set_1 = new HashSet();       my_set_1.addAll(Arrays.asList(new Integer[] { 34, 67, 89, 102 }));       Set my_set_2 = new HashSet();       my_set_2.addAll(Arrays.asList(new Integer[] { 77, 11, 0 , -33}));       System.out.println("The first set contains " ... Read More

Merge arrays into a new object array in Java

AmitDiwan
Updated on 07-Jul-2020 08:20:12

335 Views

Following is the Java program to merge array into a new object array in Java −Example Live Demoimport java.util.stream.Stream; import java.util.Arrays; import java.io.*; public class Demo{    public static Object[] concat_fun(T[] my_obj_1, T[] my_obj_2){       return Stream.concat(Arrays.stream(my_obj_1), Arrays.stream(my_obj_2)).toArray();    }    public static void main (String[] args){       Integer[] my_obj_1 = new Integer[]{67, 83, 90};       Integer[] my_obj_2 = new Integer[]{11, 0, 56};       Object[] my_obj_3 = concat_fun(my_obj_1, my_obj_2);       System.out.println("The two objects merged into a single object array : " +  Arrays.toString(my_obj_3));    } }OutputThe two objects merged into ... Read More

Memory leaks in Java

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

501 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

3K+ 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

984 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 18-Nov-2024 22:32:01

858 Views

In this program, we will perform an iterative quick sort in Java to sort an array of integers. Instead of using recursion, the program uses an iterative approach with an auxiliary stack to manage the sorting process. The output will display the sorted array after applying the quick sort algorithm. Problem Statement Write a Java program to sort an array of integers using the iterative quick sort method. Below is the demonstration of the same − Input {34, 76, 41, 32, 11, 0, 91, 102, -11} Output After iteratively performing quick sort, the array is -11 0 11 32 34 ... Read More

Java Program for Iterative Merge Sort

Alshifa Hasnain
Updated on 25-Feb-2025 19:21:22

541 Views

In this article, we will learn about iterative Merge sort in Java. Unlike the traditional iterative Merge Sort, the following Java program implements a recursive merge step while dividing the array and an iterative approach for merging. What is merge sort? Merge Sort is a popular sorting algorithm that follows the Divide and Conquer rule. It recursively divides the array into two halves, sorts each half, and then merges them back in a sorted manner.The following are three main steps to perform Merge sort in Java − Divide: The array is split into two halves ... Read More

Java Program for Gnome Sort

AmitDiwan
Updated on 04-Dec-2024 22:05:31

401 Views

Gnome Sort, also known as Stupid Sort, is a simple sorting algorithm that works by iterating through a list, comparing adjacent elements, and swapping them if they are in the wrong order. If a swap occurs, the algorithm moves one step backward to recheck the order, otherwise, it moves forward. This process continues until the array is sorted. In this article, we will explain the working of Gnome Sort using Java. Gnome Sort Approach Start from the first element. Compare the current element with the previous one: ... Read More

Java Program for Reversal algorithm for array rotation

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

335 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

Advertisements