AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 619 of 840

Java Program for Reversal algorithm for array rotation

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 408 Views

Following is the Java program to implement Reversal algorithm for array rotation −Exampleimport 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] = temp; ...

Read More

Static Control Flow in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

The Static Control Flow identify static members, executes static blocks, and then executes the static main method. Let us see an example −Examplepublic class Demo{    static int a = 97;    public static void main(String[] args){       print();       System.out.println("The main method has completed executing");    }    static{       System.out.println(a);       print();       System.out.println("We are inside the first static block");    }    public static void print(){       System.out.println(b);    }    static{       System.out.println("We are inside the second static block");    }   ...

Read More

Static method in Interface in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

To implement static method in Interface, the Java code is as follows −Exampleinterface my_interface{    static void static_fun(){       System.out.println("In the newly created static method");    }    void method_override(String str); } public class Demo_interface implements my_interface{    public static void main(String[] args){       Demo_interface demo_inter = new Demo_interface();       my_interface.static_fun();       demo_inter.method_override("In the override method");    }    @Override    public void method_override(String str){       System.out.println(str);    } }OutputIn the newly created static method In the override methodAn interface is defined, inside which a static function is defined. Another ...

Read More

Max Heap in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 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.Exampleimport 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

Thread Interference Error in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 272 Views

Let us see an example to understand the concept of Thread Interference error −Exampleimport java.io.*; class Demo_instance{    static int val_1 = 6;    void increment_val(){       for(int j=1;j

Read More

Merge two sets in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

To merge two sets in Java, the code is as follows −Exampleimport 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

Memory Consistency Error in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 662 Views

When the concept of multithreading is implemented, it is possible that changes made by one thread wouldn’t be visible to the other thread. This indicates that the view of each thread is inconsistent with respect to each other. This is known as memory consistency error.CPU might initiate main memory access in a different order, whereas the threads might access them in a different order.This is usually true when write operation is being performed, thereby avoiding the CPU wait time.The write operation is an atomic one, meaning no other operation would be performed by other threads when a write operation is ...

Read More

Run-time Stack mechanism in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ 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

Can we call run() method directly instead of start() in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 489 Views

Yes, we can do that. Let us see an example −Exampleclass my_thread extends Thread{    public void run(){       try{          System.out.println ("The thread " + Thread.currentThread().getId() + " is currently running");       }       catch (Exception e){          System.out.println ("The exception has been caught");       }    } } public class Main{    public static void main(String[] args){       int n = 6;       for (int i=1; i

Read More

How to write an empty function in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

Let us see how to write an empty function in Java −Exampleimport java.util.Vector; public class Demo{    public static void my_empty_fun(){    }    public static void main(String[] args){       System.out.println("In the main function");       my_empty_fun();    }   }OutputIn the main functionAn empty function is basically creating a function without defining any operations inside it. A class named Demo contains an empty function named ‘my_empty_fun’ which is just completed by placing two flower brackets, without adding any functionality into it. In the main function, a print statement is written after which the empty function is ...

Read More
Showing 6181–6190 of 8,392 articles
« Prev 1 617 618 619 620 621 840 Next »
Advertisements