Java 21 introduces several modern features that simplify coding and make your programs faster and cleaner. These Java 21 coding challenges help you practice these concepts hands-on. Let’s dive in.

1. Check if a Number is Prime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kscodes; public class PrimeCheck { public static boolean isPrime(int number) { if (number <= 1) return false; for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) return false; } return true; } public static void main(String [] args) { System.out.println(isPrime(17)); // true } } |
Explanation:
This method checks divisibility from 2 up to the square root of the number for efficiency.
2. Reverse a String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kscodes; public class StringReverse { public static String reverse(String input) { return new StringBuilder(input).reverse().toString(); } public static void main(String [] args) { System.out.println(reverse("hello")); // olleh } } |
Explanation:StringBuilder
makes it easy to reverse text in a single line.
3. Factorial Using Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kscodes; public class Factorial { public static long factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } public static void main(String[] args) { System.out.println(factorial(5)); // 120 } } |
Explanation:
This classic recursion example multiplies the number by factorial of n - 1
until it hits zero.
4. Check for Palindrome
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes; public class Palindrome { public static boolean isPalindrome(String str) { String clean = str.replaceAll("[^a-zA-Z]", "").toLowerCase(); return clean.equals(new StringBuilder(clean).reverse().toString()); } public static void main(String[] args) { System.out.println(isPalindrome("Madam")); // true System.out.println(isPalindrome("Madam1")); // false } } |
Explanation:
The method ignores case and non-letter characters for a clean comparison.
5. Find Maximum in Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes; import java.util.Arrays; public class MaxFinder { public static int findMax(int[] nums) { return Arrays.stream(nums).max().orElseThrow(); } public static void main(String[] args) { System.out.println(findMax(new int[]{5, 8, 3, 9})); // 9 } } |
Explanation:
Java 21’s streams simplify operations like finding the max.
6. Fibonacci Series Using Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.kscodes; public class FibonacciLoop { public static void printFibonacci(int n) { int a = 0, b = 1; for (int i = 1; i <= n; i++) { System.out.print(a + " "); int next = a + b; a = b; b = next; } } public static void main(String[] args) { printFibonacci(7); // 0 1 1 2 3 5 8 } } |
Explanation:
This approach uses simple variables to track the series without recursion.
7. Count Vowels in a String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes; public class VowelCounter { public static int countVowels(String input) { return (int) input.toLowerCase().chars() .filter(c -> "aeiou".indexOf(c) >= 0) .count(); } public static void main(String[] args) { System.out.println(countVowels("Java 21")); // 2 } } |
Explanation:
Use Java 21 streams to count characters efficiently.
8. Armstrong Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes; public class ArmstrongCheck { public static boolean isArmstrong(int num) { int original = num, sum = 0; while (num > 0) { int digit = num % 10; sum += digit * digit * digit; num /= 10; } return sum == original; } public static void main(String[] args) { System.out.println(isArmstrong(153)); // true } } |
Explanation:
Armstrong numbers are the sum of cubes of their digits equal to the number itself.
9. Sum of Digits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes; public class DigitSum { public static int sumDigits(int num) { int sum = 0; while (num != 0) { sum += num % 10; num /= 10; } return sum; } public static void main(String[] args) { System.out.println(sumDigits(1234)); // 10 } } |
Explanation:
Use a while loop to extract digits and keep summing them.
10. Remove Duplicates from Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.kscodes; import java.util.Arrays; import java.util.stream.IntStream; public class RemoveDuplicates { public static int[] removeDupes(int[] arr) { return IntStream.of(arr).distinct().toArray(); } public static void main(String[] args) { int[] result = removeDupes(new int[]{1, 2, 2, 3, 4, 4}); System.out.println(Arrays.toString(result)); // [1, 2, 3, 4] } } |
Explanation:distinct()
in streams removes all duplicate values with ease.
Final Words
These coding challenges are designed to help you sharpen your Java 21 skills by practicing both core and modern Java techniques like streams and string handling. Keep building and experimenting to become a pro.