100 Java Programming Practice Tasks
This repository provides 100 Java programming practice tasks categorized by difficulty and topic. It is designed for students and professionals who want to build their Java skills progressively.
โ Why Java?
Java is one of the most widely used programming languages in the world, and for good reason:
- Platform Independence: Thanks to the Java Virtual Machine (JVM), Java code can run on any platform without modification.
- Object-Oriented: Java promotes modular programming and code reuse through its object-oriented approach.
- Strong Community Support: With millions of developers and extensive documentation, help is always available.
- Rich API: Java provides a vast standard library that simplifies tasks like file handling, networking, and data structures.
- Performance and Scalability: With just-in-time compilers and multi-threading, Java supports high-performance and scalable applications.
- Security: Java has built-in security features that make it ideal for networked and enterprise applications.
- Versatile Usage: Java is used in web development, Android apps, enterprise solutions, embedded systems, and more.
Whether you're building a small tool or a large-scale application, Java offers the stability, support, and flexibility you need.
๐ง What is JVM?
The Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run Java programs. It is the engine that drives the Java platform's portability and flexibility.
Key Responsibilities of the JVM:
- Bytecode Execution: JVM converts compiled Java
.class files (bytecode) into machine-specific instructions at runtime.
- Memory Management: Manages memory allocation and garbage collection.
- Security: Provides a secure execution environment by sandboxing applications.
- Platform Independence: Allows Java programs to be written once and run anywhere, thanks to consistent behavior across different operating systems.
JVM vs JDK vs JRE:
- JVM: Runs Java bytecode.
- JRE (Java Runtime Environment): Contains JVM + core libraries.
- JDK (Java Development Kit): Contains JRE + development tools (compiler, debugger, etc.).
In short, JVM is the cornerstone of Java's portability and cross-platform capabilities.
๐ค Basic Java Syntax
๐ Chapter: Java Basics
This chapter introduces you to foundational Java concepts like output, input, variables, data types, conditional branching, and loops.
๐จ๏ธ Output
In Java, you can use System.out.print() or System.out.println() to display output.
System.out.println("Hello, Java!"); // with newline
System.out.print("Same line"); // without newline
๐ฏ User Input
Use Scanner class to read input:
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
๐งฎ Variables and Data Types
Variables store data. Java is statically typed.
int age = 25;
double pi = 3.1415;
char grade = 'A';
boolean isActive = true;
String name = "Alice";
๐ Branching (Conditionals)
Java supports if, else if, else, and switch statements.
int num = 10;
if (num > 0) {
System.out.println("Positive");
} else if (num < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
๐ Looping
๐ for Loop
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
๐ while Loop
int i = 1;
while (i <= 5) {
System.out.println("While: " + i);
i++;
}
๐ do-while Loop
int j = 1;
do {
System.out.println("Do-while: " + j);
j++;
} while (j <= 5);
Understanding the basic structure and syntax of a Java program is essential for beginners. Here's an example followed by a breakdown of each token:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
๐ Explanation of Tokens
public: An access modifier that means this class or method is accessible from anywhere.
class: A keyword used to declare a class.
HelloWorld: The name of the class. In Java, the file name must match the class name.
{ ... }: Curly braces enclose the body of the class and methods.
static: Means this method belongs to the class, not instances of it.
void: This method does not return any value.
main: The entry point of a Java application.
String[] args: An array of strings passed as command-line arguments to the program.
System.out.println(...): Prints the specified message to the console followed by a new line.
;: Every Java statement must end with a semicolon.
This structure forms the backbone of every Java program.
๐ ๏ธ Setting Up Java SDK and VS Code
๐งฐ Popular Java IDEs
Here are some popular Integrated Development Environments (IDEs) used for Java development:
1. IntelliJ IDEA
- Developer: JetBrains
- Edition: Community (free) & Ultimate (paid)
- Features: Smart code completion, refactoring, debugging, Spring support
- Website: https://www.jetbrains.com/idea/
2. Eclipse
- Developer: Eclipse Foundation
- Open-source and highly extensible
- Features: Plugin-rich, good for enterprise apps, JEE, Android
- Website: https://www.eclipse.org/
3. NetBeans
- Developer: Apache Software Foundation
- All-in-one IDE for Java SE, Java EE, and others
- Integrated GUI builder (Swing), Maven/Gradle support
- Website: https://netbeans.apache.org/
4. Visual Studio Code (VS Code)
- Developer: Microsoft
- Lightweight, fast, and supports Java via extensions
- Great for modern and cross-language development
- Website: https://code.visualstudio.com/
5. BlueJ
- Designed for beginners and educational use
- Simple UI, built-in object inspector
- Website: https://www.bluej.org/
6. JDeveloper
7. DrJava
- Lightweight IDE aimed at students
- Simple interface, suitable for teaching Java basics
- Website: https://drjava.org/
Now, let's move on to how to install Java SDK and set up your development environment using VS Code:
๐ ๏ธ Setting Up Java SDK and VS Code
Follow these steps to set up the Java Development Kit (JDK) and Visual Studio Code (VS Code) for Java development:
Step 1: Install Java Development Kit (JDK)
-
Go to the Oracle JDK download page or use OpenJDK.
-
Download the installer for your operating system (Windows, macOS, Linux).
-
Run the installer and follow the instructions.
-
After installation, verify by opening a terminal and typing:
java -version
javac -version
Step 2: Install Visual Studio Code
- Download VS Code from https://code.visualstudio.com.
- Install VS Code like any standard application.
Step 3: Install Java Extensions in VS Code
-
Open VS Code.
-
Go to the Extensions panel (Ctrl+Shift+X or click the square icon on the sidebar).
-
Search for and install:
- Java Extension Pack by Microsoft (includes Language Support, Debugger, Maven, etc.)
- Optionally: Spring Boot Extension Pack for enterprise development
Step 4: Configure Your Environment
- Open Command Palette (
Ctrl+Shift+P), then select Java: Configure Java Runtime.
- Make sure VS Code detects your installed JDK.
- You can create a simple
.java file and run it using the Run button that appears.
Step 4.1: Set JAVA_HOME Environment Variable (Windows)
To make Java available system-wide and allow other tools to locate it:
-
Open the Start Menu, search for and open Edit the system environment variables.
-
In the System Properties window, click on Environment Variables.
-
Under System variables, click New:
- Variable name:
JAVA_HOME
- Variable value:
C:\Program Files\Java\jdk-XX.X.X (replace with your installed JDK path)
-
Click OK to save.
-
Now select the Path variable under System variables, click Edit, then New:
-
Click OK to close all dialogs.
โ
To verify, open a new terminal and run:
java -version
javac -version
- Open Command Palette (
Ctrl+Shift+P), then select Java: Configure Java Runtime.
- Make sure VS Code detects your installed JDK.
- You can create a simple
.java file and run it using the Run button that appears.
Step 5: Compile and Run Java Code
โ
You are now ready to start Java development with VS Code!
๐ History of Java Programming
Java was developed by James Gosling and his team at Sun Microsystems and was officially released in 1995. Originally designed for interactive television, Java became a general-purpose programming language known for its Write Once, Run Anywhere (WORA) philosophy. It is a class-based, object-oriented language that is widely used in enterprise applications, Android development, web servers, and more.
Key milestones in Java history:
- 1991: Project Green initiated at Sun Microsystems
- 1995: Java 1.0 released
- 2006: Sun made Java open-source by releasing it under the GPL license
- 2010: Oracle acquired Sun Microsystems
- 2014+: Introduction of Lambda expressions, Streams API, and functional-style programming
- 2021 onwards: Faster release cycles with Java 17 being a long-term support (LTS) release
Today, Java remains one of the most popular and reliable languages for software development worldwide.
๐ข Beginner Level (Syntax, Loops, Conditions, Arrays)
- Print "Hello, World!"
- Check if a number is even or odd
- Find the largest of 3 numbers
- Reverse a number (e.g., 123 โ 321)
- Check if a number is prime
- Calculate factorial of a number
- Generate Fibonacci series up to N terms
- Check if a string is a palindrome
- Count vowels and consonants in a string
- Sort an array using Bubble Sort
- Print multiplication table of a number
- Find the GCD and LCM of two numbers
- Check if a year is a leap year
- Sum of digits of a number
- Convert Celsius to Fahrenheit and vice versa
- Find the smallest number in an array
- Find the largest number in an array
- Find the average of N numbers
- Count digits in a number
- Check if a number is Armstrong
๐ก Intermediate Level (OOP, Strings, Arrays, Collections)
- Create a class
Student with name, roll number, and marks
- Implement a simple calculator using
switch case
- Convert a decimal number to binary
- Find the second largest number in an array
- Remove duplicates from an array
- Create a basic bank system with deposit and withdraw features
- Count frequency of characters in a string
- Check if two strings are anagrams
- Sort a list of strings alphabetically
- Create a class hierarchy for Animals (inheritance and polymorphism)
- Merge two arrays
- Linear search in an array
- Binary search in a sorted array
- Replace all spaces in a string with
-
- Swap two numbers without using a third variable
- Count number of words in a sentence
- Capitalize the first letter of each word in a string
- Reverse a string without using library methods
- Print ASCII value of characters in a string
- Implement a simple Tic-Tac-Toe game
๐ Data Structures in Java
- Implement Stack using an array
- Implement Queue using LinkedList
- Reverse a LinkedList
- Implement Binary Search Tree
- Inorder, Preorder, Postorder traversal of a BST
- Find height of a binary tree
- Level order traversal of binary tree
- Implement Dijkstra's algorithm
- Implement Graph using adjacency list
- Detect cycle in a graph (DFS based)
- Implement a Min Heap
- Convert infix to postfix expression
- Evaluate postfix expression
- Implement doubly linked list
- Find the middle element of a linked list
- Check if a linked list is a palindrome
- Merge two sorted linked lists
- Find intersection point of two linked lists
- Implement LRU Cache using LinkedHashMap
- Clone a linked list with random pointers
๐ต Advanced OOP and Interfaces
- Create a shape interface and implement Circle, Square, Rectangle
- Use
Comparable and Comparator for custom sorting
- Simulate an employee management system using abstract classes
- Implement a simple library system (borrow, return books)
- Demonstrate method overloading and overriding
- Create a custom exception and use
try-catch-finally
- Create a singleton class
- Demonstrate static blocks, methods, and variables
- Use inner and anonymous classes
- Create a generic class and method
- Implement enum for traffic signals
- Build a chain of responsibility using interfaces
- Create a Factory design pattern implementation
- Implement Builder design pattern
- Implement Observer design pattern
- Implement Strategy design pattern
- Implement Dependency Injection manually
- Demonstrate shallow and deep cloning
- Override
equals() and hashCode() in a custom class
- Use
record class (Java 14+)
๐ฃ Java APIs, File Handling, Threads, Networking
- Read from and write to a text file
- Serialize and deserialize an object
- Create a multithreaded program using
Runnable
- Implement Producer-Consumer using wait/notify
- Schedule a task using
TimerTask
- Use
java.time API to display and format date/time
- Make a simple TCP Client-Server chat application
- Parse a CSV file using
BufferedReader
- Use
Scanner to read user input with error handling
- Create a mini project to manage todo items (Add/Edit/Delete with file storage)
- Download a file from a URL
- Upload a file using HTTP POST
- Monitor directory changes using WatchService
- Use
java.util.Optional to handle null values
- Create a thread pool using
ExecutorService
- Handle file not found and IO exceptions
- Create ZIP files from multiple files
- Use
Properties class to load config files
- Create a simple REST client using
HttpURLConnection
- Use
Path, Files, and Streams to process a folder of text files
๐ How to Use
- Clone this repository or copy the list.
- Start practicing from the Beginner level.
- Move to Intermediate, then Data Structures, then Advanced OOP.
- Complete the APIs, File Handling, and Threads section.
โ
Contribute
- Add solutions in
Java for each task.
- Format solution filenames as
Task_01_HelloWorld.java and so on.
- Feel free to suggest new challenges!
๐ License
This list is open-source and free to use for learning and teaching.