Java coding program examples are the best way to get a complete understanding of how popular frameworks, syntaxes, and statements execute in the Java programming language. Most programming languages work on a similar basis however, their syntaxes are different.
In this article, we will learn about some of the popular java coding program examples to clear our concepts with popular interview Java questions. If you are a beginner or a professional in Java programming this article will prove to be more beneficial for you.
Java Coding Program Examples For Beginners
Java coding program examples for beginners are a collection of Java interview questions and coding examples that will help you clear your concepts in Java programming language. If you are looking for some clarity on Java programming for internship level positions as a Java developer then check these examples below.
1. Write a Java Coding program to reverse a given string.
Approach: We can reverse a string using a loop easily parsing each character in a string. Check a simple Java Coding Program examples below.
public class ReverseString {
public static void main(String[] args) { String str = “Hello”; String reversed = “”;
// Reversing the string using a loop for (int i = str.length() – 1; i >= 0; i–) { reversed += str.charAt(i); } // Output the reversed string System.out.println(“Reversed String: ” + reversed); } } |
2. Write a Java program to check if a given string is a palindrome or not. A palindrome is a string that reads the same backward as forward.
Approach: We can check whether a string is a palindrome or not using the reverse string formula using for loop and we can use the if else condition to check whether it is a palindrome or not.
public class PalindromeCheck {
public static void main(String[] args) { String str = “madam”; String reversed = “”; // Reversing the string for (int i = str.length() – 1; i >= 0; i–) { reversed += str.charAt(i); } // Checking if the string is a palindrome if (str.equals(reversed)) { System.out.println(str + ” is a palindrome.”); } else { System.out.println(str + ” is not a palindrome.”); } } } |
3. Write a Java program to find the largest number in a given array of integers.
Approach: This problem can be approached using loops and comparison operators to find the largest number in an array.
public class LargestNumberInArray {
public static void main(String[] args) { int[] arr = {5, 12, 9, 3, 15}; int largest = arr[0]; // Assume first element is the largest // Loop through the array to find the largest number for (int i = 1; i < arr.length; i++) { if (arr[i] > largest) { largest = arr[i]; } } // Output the largest number System.out.println(“Largest number is: ” + largest); } } |
4. Write a Java program to print the Fibonacci sequence up to a given number of terms.
Approach: We will approach this Fibonacci problem using the for loop and assignment operator. Check simple Java Code Programming examples.
public class FibonacciSequence {
public static void main(String[] args) { int n = 5; // Number of terms int first = 0, second = 1; System.out.print(“Fibonacci Sequence: “); for (int i = 0; i < n; i++) { System.out.print(first + ” “); int next = first + second; first = second; second = next; } } } |
5. Write a Java program to find the sum of all elements in a given array.
Approach: This problem can be approached using loops and assignment operators. Check the logic below to understand how to calculate the sum of arrays.
public class SumOfArray {
public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int sum = 0; // Loop through the array to calculate the sum for (int i = 0; i < arr.length; i++) { sum += arr[i]; } // Output the sum System.out.println(“Sum of array elements: ” + sum); } } |
6. Write a Java program to find the factorial of a given number.
Approach: We can use the logic of repeated loop conditions using multiplication operators in Java.
public class Factorial {
public static void main(String[] args) { int n = 5; long factorial = 1; // Loop to calculate factorial for (int i = 1; i <= n; i++) { factorial *= i; } // Output the result System.out.println(“Factorial of ” + n + ” is: ” + factorial); } } |
7. Write a Java program to check if a given number is prime or not. A prime number is a number greater than 1 that has no divisors other than 1 and itself.
Approach: To check a prime number we can run a loop from 2 to half of the number to find the prime number.
public class PrimeNumberCheck {
public static void main(String[] args) { int n = 7; boolean isPrime = true; // Check if n is prime for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { isPrime = false; break; } } // Output the result if (isPrime) { System.out.println(n + ” is a prime number.”); } else { System.out.println(n + ” is not a prime number.”); } } } |
Java Coding Program Examples For Intermediate Level
Let us check some of the advanced intermediate Java coding program examples below with their codes and solving approaches.
1. Write a Java program to merge two sorted arrays into one sorted array.
Approach:
Java Coding Program Example Input: arr1 = {1, 3, 5, 7}, arr2 = {2, 4, 6, 8}
Output: Merged Array: {1, 2, 3, 4, 5, 6, 7, 8}
We can use the simple loop to merge these two arrays parsing them with additional index parsing through both arrays.
import java.util.Arrays;
public class MergeSortedArrays { public static void main(String[] args) { int[] arr1 = {1, 3, 5, 7}; int[] arr2 = {2, 4, 6, 8}; int[] mergedArray = new int[arr1.length + arr2.length]; int i = 0, j = 0, k = 0; // Merging the two arrays while (i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) { mergedArray[k++] = arr1[i++]; } else { mergedArray[k++] = arr2[j++]; } } // Adding remaining elements of arr1 while (i < arr1.length) { mergedArray[k++] = arr1[i++]; } // Adding remaining elements of arr2 while (j < arr2.length) { mergedArray[k++] = arr2[j++]; } System.out.println(“Merged Array: ” + Arrays.toString(mergedArray)); } } |
2. Write a Java program to find the length of the longest substring without repeating characters in a given string.
Java Coding Program Example: “abcabcbb”
Output: 3
Approach: This program can be approached using a loop and parsing through each character in arrays and removing and adding characters at the index.
import java.util.HashSet;
public class LongestSubstring { public static void main(String[] args) { String s = “abcabcbb”; int maxLength = 0; HashSet<Character> set = new HashSet<>(); int left = 0; for (int right = 0; right < s.length(); right++) { while (set.contains(s.charAt(right))) { set.remove(s.charAt(left)); left++; } set.add(s.charAt(right)); maxLength = Math.max(maxLength, right – left + 1); } System.out.println(“Longest Substring Length: ” + maxLength); } } |
3. Write a Java program to find the missing number from an array of integers where numbers range from 1 to n and are in a jumbled order.
Java Coding Program Examples: arr = {1, 2, 4, 5, 6}, n = 6
Output: 3
Approach: You can parse the array to look for the missing number in the naive approach, find the sum of n numbers calculate the sum of elements in the array, and find the difference to find the missing number.
public class MissingNumber {
public static void main(String[] args) { int[] arr = {1, 2, 4, 5, 6}; int n = 6; int totalSum = (n * (n + 1)) / 2; // Sum of numbers from 1 to n int arrSum = 0; // Calculate the sum of elements in the array for (int num : arr) { arrSum += num; } // The missing number int missingNumber = totalSum – arrSum; System.out.println(“Missing Number: ” + missingNumber); } } |
4. Write a Java program to check whether two given strings are anagrams of each other. Two strings are anagrams if they contain the same characters at the same frequency.
Java Coding Program Examples Input: str1 = “listen”, str2 = “silent”
Output: “listen” and “silent” are anagrams.
Approach:
import java.util.Arrays;
public class AnagramCheck { public static void main(String[] args) { String str1 = “listen”; String str2 = “silent”; // Check if lengths are equal if (str1.length() != str2.length()) { System.out.println(“Not anagrams”); return; } // Convert strings to character arrays, sort them and compare char[] arr1 = str1.toCharArray(); char[] arr2 = str2.toCharArray(); Arrays.sort(arr1); Arrays.sort(arr2); if (Arrays.equals(arr1, arr2)) { System.out.println(str1 + ” and ” + str2 + ” are anagrams.”); } else { System.out.println(str1 + ” and ” + str2 + ” are not anagrams.”); } } } |
5. Write a Java program to implement binary search on a sorted array.
Approach:
Java Coding Program Example: arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}, target = 5
Output: Element found at index 4
public class BinarySearch {
public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int target = 5; int result = binarySearch(arr, target); if (result == -1) { System.out.println(“Element not found.”); } else { System.out.println(“Element found at index: ” + result); } } public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length – 1; while (left <= right) { int mid = left + (right – left) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] < target) { left = mid + 1; } else { right = mid – 1; } } return -1; } } |
Learn Complete Java + DSA With PW Skills
Become proficient in Java programming language and build scalable Java applications with Decode Java DSA Course. Learn about the latest Java trends and tools to master application development.
Get complete tutorials for Java fundamentals, API integration, and more. Get tutored by experienced mentors and build real-world projects with Java. Learn data structures in this self paced program only on pwskills.com
Java Coding Program Examples for Beginners FAQs
Q1. How do you check whether a number is even or odd in Java?
Ans: To check whether a number is even or odd we can use a modulus operator and find out which is even and which is odd.
Q2. How do you swap two numbers without using a third variable?
Ans: We can use simple arithmetic operations where we have to use sum and difference to swap two numbers without using the third number.
Q3. What are Java code examples?
Ans: Java coding program examples are problem statements related to OOPs, functions, syntax, strings, methods, classes, and different modules in Java which can help you prepare for Java coding interviews.
Q4. Are simple code examples good for Java language?
Ans: Yes, a beginner must start writing with simple Java programs and then transition to advanced Java coding program examples later in their preparation journey.