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 …
Month: April 2021
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”; // …
Java Program to convert int type variables to double In this program, we will learn to convert the integer (int) variables into the double in Java. Example 1: Java Program to Convert int to double using Typecasting class Main{ public static void main(String[] args){ // create int variables int a =33; int b = …
Java Program to convert int type variables to String In this program, we will learn to convert the integer (int) variables into the string in Java. Example 1: Java Program to Convert int to string using valueOf() class Main{ public static void main(String[] args){ // create int variable int num1 = 36; int num2 …
Java Program to convert string type variables into int In this program, we will learn to convert the String type variables into the integer (int) in Java. Example 1: Java Program to Convert string to int using parseInt() class Main{ public static void main(String[] args){ // create string variables String str1 = “23”; String …