Java Articles

Page 20 of 450

Method Overloading and Ambiguity in Varargs in Java

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

There are ambiguities while using variable arguments in Java. This happens because two methods can definitely be valid enough to be called by data values. Due to this, the compiler doesn’t have the knowledge as to which method to call.Examplepublic class Demo {    static void my_fun(double ... my_Val){       System.out.print("fun(double ...): " + "Number of args: " + my_Val.length );       for(double x : my_Val)       System.out.print(x + " ");       System.out.println();    }    static void my_fun(boolean ... my_Val){       System.out.print("fun(boolean ...) " + "The number of arguments: ...

Read More

Search a string in Matrix Using Split function in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 167 Views

To search a string in Matrix using split function, the code is as follows −Exampleimport java.util.*; public class Demo {    public static int search_string(String[] my_matrix, String search_string){       for (String input : my_matrix){          String[] my_value = input.split(search_string);          if (my_value.length >= 2 || my_value.length == 0){             return 1;          }          else if (my_value.length == 1 && input.length() != my_value[0].length()){             return 1;          }       }   ...

Read More

Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 349 Views

To find the sum of such series, the Java program is as follows −Examplepublic class Demo {    static long my_val = 1000000007;    public static long compute_val(long my_int){       return ((my_int % my_val) * (my_int % my_val)) % my_val;    }    public static void main(String[] args){       long my_int = 45687234;       System.out.println("The computed value is ");       System.out.print(compute_val(my_int));    } }OutputThe computed value is 335959495A class named Demo defines a function named ‘compute_val’ that computes and returns the sum of a specific series. In the main function, the long ...

Read More

Java Program for Stooge Sort

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 224 Views

Following is the Java program for Stooge sort −Exampleimport java.io.*; public class Demo {    static void stooge_sort(int my_arr[], int l_val, int h_val){       if (l_val >= h_val)       return;       if (my_arr[l_val] > my_arr[h_val]){          int temp = my_arr[l_val];          my_arr[l_val] = my_arr[h_val];          my_arr[h_val] = temp;       }       if (h_val-l_val+1 > 2){          int temp = (h_val-l_val+1) / 3;          stooge_sort(my_arr, l_val, h_val-temp);          stooge_sort(my_arr, l_val+temp, h_val);     ...

Read More

Java Program for Standard Normal Distribution (SND)

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 418 Views

Following is the Java program for Standard Normal Distribution −Exampleimport java.io.*; import java.util.*; public class Demo{    public static void main(String[] args){       double std_dev, val_1, val_3, val_2;       val_1 = 68;       val_2 = 102;       val_3 = 26;       std_dev = (val_1 - val_2) / val_3;       System.out.println("The standard normal deviation is: " + std_dev);    } }OutputThe standard normal deviation is: -1.3076923076923077A class named Demo contains the main function, where certain double values are defined, and the formula for standard deviation is applied ((val_1 - val_2) / val_3) on these values and the resultant output is displayed on the console.

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

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

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

Thread Interference Error in Java

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

Widening Primitive Conversion in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 307 Views

Following is an example showing widening primitive conversion −Examplepublic class Demo {    public static void main(String[] args) {       System.out.print("H" + "E");       System.out.print('L');       System.out.print('L');       System.out.print('O');    } }OutputHELLOA class named Demo contains the main function. Here, the ‘print’ function is used to print specific characters in double quotes and then in single quotes. When the process of widening primitive conversion happens, the presence of ‘+’ operator is a must. This ‘+’ operator expects integer on both the left hand and right hand sides.

Read More
Showing 191–200 of 4,495 articles
« Prev 1 18 19 20 21 22 450 Next »
Advertisements