Month: April 2021

Learn Java by Example: Java Program to Delete Empty and Non-empty Directory

Hits: 3 Java Program to Delete Empty and Non-empty Directory In this example, we will learn to delete an empty directory, a non-empty directory, and a directory with non-empty subdirectory in Java.   Example 1: Java Program to delete an empty directory import java.io.File; class Main{ public static void main(String[] args){ try { // create …

Learn Java by Example: Java Program to Read the Content of a File Line by Line

Hits: 6 Java Program to Read the Content of a File Line by Line In this example, we will learn to read a the content of a file using various class in Java.   Example 1: Java Program to Read File Using BufferedInputStream import java.io.BufferedInputStream; import java.io.FileInputStream; class Main{ public static void main(String[] args){ try …

Learn Java by Example: Java Program to Load File as InputStream

Hits: 7 Java Program to Load File as InputStream In this example, we will learn to load a file as an input stream using the FileInputStream class in Java.   Example 1: Java Program to Load a Text File as InputStream import java.io.InputStream; import java.io.FileInputStream; public class Main{ public static void main(String args[]){ try { …

Learn Java by Example: Java Program to Convert a String into the InputStream

Hits: 1 Java Program to Convert a String into the InputStream In this program, we will learn to convert a string to an inputstream in Java.   Example: Java Program to convert String to InputStream import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class Main{ public static void main(String args[]){ // Creates a string String name …

Learn Java by Example: Java Program to Calculate the Execution Time of Methods

Hits: 5 Java Program to Calculate the Execution Time of Methods In this example, we will learn to calculate the execution time of normal methods and recursive methods in Java.   Example 1: Java Program to calculate the method execution time class Main{ // create a method public void display(){ System.out.println(“Calculating Method execution time:”); } …

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

Hits: 3 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<>(); // …

Learn Java by Example: Java Program to implement private constructors

Hits: 5 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(){ …

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

Hits: 51 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 …

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

Hits: 54 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 …

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

Hits: 3 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){ // …