Java Program to pass methods as arguments to other methods In this example, we will learn to pass methods as arguments to other methods in Java Example 1: Java program to pass method as a parameter to other method class Main{ // calculate the sum public int add(int a, int b){ // calculate sum …
Day: April 29, 2021
Java Program to pass lambda expression as a method argument In this example, we will learn to pass lambda expression as the method argument in Java. Example 1: Define lambda expressions as method parameters import java.util.ArrayList; class Main{ public static void main(String[] args){ // create an ArrayList ArrayList<String> languages = new ArrayList<>(); // add elements …
Java Program to implement private constructors In this example, we will learn to implement private constructors in Java. Example 1: Java program to create a private constructor class Test{ // create private constructor private Test (){ System.out.println(“This is a private constructor.”); } // create a public static method public static void instanceMethod(){ // create …
Java Program to Call One Constructor from another In this example, we will learn how we can call one constructor from another constructor in Java. Example 1: Java program to call one constructor from another class Main{ int sum; // first constructor Main() { // calling the second constructor this(5, 2); } // second …
Java Program to Implement Binary Search Algorithm In this example, we will learn to implement binary search algorithm in Java. Example: Java Program to Implement Binary Search Algorithm import java.util.Scanner; // Binary Search in Java class Main{ int binarySearch(int array[], int element, int low, int high){ // Repeat until the pointers low and high …
Java Program to Implement Merge Sort Algorithm In this example, we will learn to execute merge sort algorithm in Java. Example: Java Program to Implement Merge Sort Algorithm import java.util.Arrays; // Merge sort in Java class Main{ // Merge two sub arrays L and M into array void merge(int array[], int p, int q, int …
Java Program to Implement Bubble Sort algorithm In this example, we will learn to execute bubble sort algorithm in Java. Example: Java Program to Implement Bubble Sort Algorithm // import the Class import java.util.Arrays; import java.util.Scanner; class Main{ // create an object of scanner // to take input from the user Scanner input = …
Java Program to convert primitive types to objects and vice versa In this tutorial, we will learn to convert the primitive data types to their corresponding wrapper objects and vice versa in Java. Example 1: Java Program to Convert Primitive Types to Wrapper Objects class Main{ public static void main(String[] args){ // create primitive …
Java Program to convert string variables to double In this tutorial, we will learn to convert the string variables into double in Java. Example 1: Java Program to Convert string to double using parseDouble() class Main{ public static void main(String[] args){ // create string variables String str1 = “23”; String str2 = “456.6”; // …