Saturday, November 7, 2020

Java Program To Count Number Of Digits In Number

1. Overview

In this programming series, you'll learn how to count the digits in a number in java. This can be done in many ways using for loop with simple iterative approach, recursive, log based and string conversion methods.

Let us jump into the example programs.




Friday, November 6, 2020

Java Program To Print ALL Characters From A to Z Using Loops

1. Overview

In this article, You'll learn how to display the english alphabetics in order in java. And also will show the example programs to print uppercase and lowercase characters.

2. Example To Print Alphabets From a to z Lowercase


First, write the simple program running a for loop with char starts from a to till z.
public class PrintLowerCaseAlphabets {

	public static void main(String[] args) {

		// first character
		char firstChar = 'a';

		// last character
		char lastChar = 'z';

		// print lowercase letters from a to z
		for (char c = firstChar; c <= lastChar; c++) {
			System.out.print(c + " ");

		}

	}

}

Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z 

In the above program, we ran the loop through a to z but internally it uses the ASCII codes such as from 65 to 90.

3. Example To Print Alphabets From A to Z Uppercase


With the little changes to the above program, you can print the upper case letters.

Just replace the code 'a' with upper 'A' and 'z' with upper 'Z'. Then, it uses internally ASCII codes from 97 to 122.
public class PrintUpperCaseAlphabets {

	public static void main(String[] args) {

		// first character
		char firstChar = 'A';

		// last character
		char lastChar = 'Z';

		// print uppercase letters from A to Z
		for (char c = firstChar; c <= lastChar; c++) {
			System.out.print(c + " ");

		}

	}

}

Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

4. Conclusion


in this article, you've seen the two ways to print the lower and upper case letters from a to z and A to Z in java with example programs.

Instead of running loop through characters, alternatively can run through the ASCII codes range.

Example programs are GitHub.

Selection Sort in java with Algorithm, Example

Selection Sort in java

In this tutorial, We will learn about another sorting technique where auxiliary space is minimized. As of now we have discussed about the following

Implementation of Bubble Sort
Implementation of Optimized Bubble Sort
Implementation of Insertion Sort
Several Java Example programs

Selection Sort in java


In computer science, selection sort is a sorting algorithm, Selection sort works from left to right typically. It finds the smallest element index and its swap with the current indexed element.

It is specifically an in-place comparison sort. It has O(n2) time complexity and worst than Insertion sort and better than Bubble sort.

Java 8 Optional filter() Method Example

1. Overview

In this tutorial, We'll discuss how to use Predicate with Optional class. The Java 8 Optional class has a method filter() which takes Predicate as an argument.

Optional is a class and it is in java.util package. Optional is declared as final in its source code. Because no other classes can be inherited and to stop overriding the behavior.

Java 8 Optional filter


API Note: If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.