Master Java by Practicing Essential Programs: From Basic to Advanced

Listen to this Post

Java is one of the most widely used programming languages, and mastering it requires consistent practice. Below, we’ve compiled a list of essential Java programs, ranging from basic to advanced levels, to help you sharpen your skills. Each program is accompanied by verified code snippets and commands to ensure you can practice effectively.

1. Basic Java Programs

Hello World

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

– Command to Compile: `javac HelloWorld.java`
– Command to Run: `java HelloWorld`

Check if a Number is Even or Odd

public class EvenOdd {
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}

Swap Two Numbers Without a Third Variable

public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}

Reverse a Number

public class ReverseNumber {
public static void main(String[] args) {
int num = 1234, reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
}
}

2. Intermediate Java Programs

Palindrome Check

public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}

Find Factorial (Recursion)

public class Factorial {
static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(num));
}
}

Prime Number Check

public class PrimeNumber {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(num + (isPrime ? " is prime." : " is not prime."));
}
}

Fibonacci Series (Recursion & Iteration)

public class Fibonacci {
static int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
int n = 10;
for (int i = 0; i < n; i++) {
System.out.print(fib(i) + " ");
}
}
}

3. Advanced Java Programs

Sorting an Array (Bubble Sort)

public class BubbleSort {
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
int n = arr.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("Sorted array:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}

Find Duplicate Elements in an Array

import java.util.HashSet;
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2, 7, 8, 8, 3};
HashSet<Integer> set = new HashSet<>();
System.out.println("Duplicate elements:");
for (int num : arr) {
if (!set.add(num)) {
System.out.print(num + " ");
}
}
}
}

Reverse a String Using Recursion

public class ReverseString {
static String reverse(String str) {
if (str.isEmpty()) return str;
return reverse(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
String str = "Hello";
System.out.println("Reversed string: " + reverse(str));
}
}

Multithreading Example

class MyThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}
public class MultithreadingExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
}
}

Read and Write to a File

import java.io.*;
public class FileIO {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, File IO!");
writer.close();
BufferedReader reader = new BufferedReader(new FileReader("output.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Lambda Expressions in Java (Functional Programming)

import java.util.Arrays;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.print(n + " "));
}
}

Spring Boot REST API Example

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}

@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}

You Should Know:

  • Java Compilation and Execution: Always compile your Java programs using `javac` and run them using java.
  • Debugging: Use `System.out.println()` for quick debugging.
  • IDE: Use IDEs like IntelliJ IDEA or Eclipse for better productivity.
  • Version Control: Use Git to manage your code versions.

What Undercode Say:

Mastering Java requires consistent practice and understanding of core concepts. Start with basic programs and gradually move to advanced topics like multithreading and Spring Boot. Use Linux commands like `javac` and `java` for compilation and execution. For Windows, ensure Java is added to your PATH environment variable. Keep practicing and exploring new libraries and frameworks to stay ahead in the IT industry.

Expected Output:

  • Hello World: `Hello, World!`
    – Even/Odd Check: `10 is even.`
    – Swapped Numbers: `After swapping: a = 10, b = 5`
    – Reversed Number: `4321`
    – Palindrome Check: `madam is a palindrome.`
    – Factorial: `Factorial of 5 is 120`
    – Prime Check: `29 is prime.`
    – Fibonacci Series: `0 1 1 2 3 5 8 13 21 34`
    – Sorted Array: `11 12 22 25 34 64 90`
    – Duplicate Elements: `2 8 3`
    – Reversed String: `olleH`
    – Thread Output: `Thread is running.`
    – File IO: `Hello, File IO!`
    – Lambda Output: `1 2 3 4 5`
    – Spring Boot API: `Hello, Spring Boot!`

Keep coding and exploring!

References:

Reported By: Deepasajjanshetty Basic – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image