An event handling code that runs on a special thread called event dispatch thread(EDT). Most of the code that invokes swing methods also runs on this EDT thread. It is necessary because most of swing object methods do not thread-safe. SwingUtilities is a utility class and has one important static method, invokeLater(). This method can be used to perform a task asynchronously in the AWT event dispatcher thread.Syntaxpublic static void invokeLater(Runnable doRun)Exampleimport javax.swing.*; public class InvokeLAterLambdaTest { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { // lambda expression code JFrame frame = ... Read More
To implement static method in Interface, the Java code is as follows −Example Live Demointerface 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. ... Read More
The Static Control Flow identify static members, executes static blocks, and then executes the static main method. Let us see an example −Example Live Demopublic 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
DoubleBinaryOperator is a functional interface defined in java.util.function package. It accepts two parameters of type double as input and produces another double value as a result. DoubleBinaryOperator interface can be used as an assignment target for a lambda expression or method reference and has only one abstract method applyAsDouble().Syntax@FunctionalInterface public interface DoubleBinaryOperator { double applyAsDouble(double left, double right); }Example-1import java.util.function.DoubleBinaryOperator; public class DoubleBinaryOperatorTest { public static void main(String args[]) { // using lambda expression DoubleBinaryOperator sum = (d1, d2) -> d1 + d2; DoubleBinaryOperator mul = (d1, d2) -> d1 * d2; DoubleBinaryOperator div = ... Read More
Following is the Java program for Standard Normal Distribution −Example Live Demoimport 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.
Following is the Java program for Stooge sort −Example Live Demoimport 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
IntPredicate interface is a built-in functional interface defined in java.util.function package. This functional interface accepts one int-valued argument as input and produces a boolean value as an output. This interface is a specialization of the Predicate interface and used as an assignment target for a lambda expression or method reference. It provides only one abstract method, test ().Syntax@FunctionalInterface public interface IntPredicate { boolean test(int value); }Example for Lambda Expressionimport java.util.function.IntPredicate; public class IntPredicateLambdaTest { public static void main(String[] args) { IntPredicate intPredicate = (int input) -> { // lambda expression if(input == 100) { ... Read More
The javafx.event package provides a framework for Java FX events. The Event class serves as the base class for JavaFX events and associated with each event is an event source, an event target, and an event type. An ActionEvent widely used when a button is pressed.In the below program, we can implement an ActionEvent for a button by using method reference.Exampleimport javafx.application.*; import javafx.beans.property.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.scene.effect.*; public class MethodReferenceJavaFXTest extends Application { private Label label; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { ... Read More
If you want to specify the number of occurrences while constructing a regular expression you can use quantifiers. Java supports three types of quantifiers namely: greedy quantifiers, reluctant quantifiers and possessive quantifiers.Greedy quantifiers − Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); ... Read More
To find the sum of such series, the Java program is as follows −Example Live Demopublic 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 ... Read More