Day: April 29, 2021

Learn Java by Example: Java Program to pass lambda expression as a method argument

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 …

Learn Java by Example: Java Program to implement private constructors

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 …

Learn Java by Example: Java Program to Implement Binary Search Algorithm

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 …

Learn Java by Example: Java Program to Implement Bubble Sort algorithm

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 = …

Learn Java by Example: Java Program to convert primitive types to objects and vice versa

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 …

Learn Java by Example: Java Program to convert string variables to double

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”; // …