Why Learn Java?
Before diving into the code, it’s worth noting why Java is still so relevant today:
- Platform independence: Java’s “write once, run anywhere” model allows you to build apps that can run on different platforms without modification.
- Strong community and ecosystem: Java has a vast ecosystem of tools, libraries, and frameworks.
- Enterprise usage: Many large-scale systems and corporations still run on Java.
- Solid object-oriented foundation: Java teaches strong programming fundamentals useful in any language.
Now, let’s build a basic Java application to get comfortable with the syntax and structure.
Setting Up Your Environment
To build a Java application, you’ll need:
- Java Development Kit (JDK): Install from Oracle or use OpenJDK.
- Text editor or IDE: You can use something simple like VS Code or more feature-rich like IntelliJ IDEA or Eclipse.
To check your setup:
java -version
javac -version
Both commands should return a valid version number.
Our Sample App: A Simple Todo Manager
We’ll build a basic console-based todo app in Java. It will:
- Let users add tasks
- List all tasks
- Mark tasks as complete
This is a great example because it touches on input handling, arrays/lists, loops, and object-oriented programming—all core Java concepts.
Step 1: Define a Task Class
Create a file called Task.java.
public class Task {
private String description;
private boolean isCompleted;
public Task(String description) {
this.description = description;
this.isCompleted = false;
}
public void markCompleted() {
this.isCompleted = true;
}
public String toString() {
return (isCompleted ? "[x] " : "[ ] ") + description;
}
}
This class encapsulates a single task. It has a description and a flag for completion.
Step 2: Build the Main Application
Create another file called TodoApp.java.
import java.util.ArrayList;
import java.util.Scanner;
public class TodoApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Task> tasks = new ArrayList<>();
while (true) {
System.out.println("n1. Add Task");
System.out.println("2. List Tasks");
System.out.println("3. Complete Task");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // clear buffer
switch (choice) {
case 1:
System.out.print("Enter task description: ");
String desc = scanner.nextLine();
tasks.add(new Task(desc));
System.out.println("Task added.");
break;
case 2:
System.out.println("Your Tasks:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println(i + ". " + tasks.get(i));
}
break;
case 3:
System.out.print("Enter task number to complete: ");
int taskNum = scanner.nextInt();
if (taskNum >= 0 && taskNum < tasks.size()) {
tasks.get(taskNum).markCompleted();
System.out.println("Task marked as completed.");
} else {
System.out.println("Invalid task number.");
}
break;
case 4:
System.out.println("Exiting... Goodbye!");
return;
default:
System.out.println("Invalid choice.");
}
}
}
}
Step 3: Compile and Run
In your terminal, run:
javac Task.java TodoApp.java
java TodoApp
This will start your command-line todo manager.
What You Learned
This simple project helps reinforce core Java skills:
- Object-oriented programming: You defined a class with methods and properties.
- Standard input handling: Using Scanner to read user input.
- Lists and loops: You stored multiple tasks using an ArrayList and looped through them.
- Basic control flow: You used a switch statement to handle user choices.
Where to Go From Here?
Now that you have a basic app, here are some ways to extend it:
- Save and load tasks to/from a file
- Use enums for task status (e.g., PENDING, COMPLETED)
- Add due dates using Java’s LocalDate class
- Build a GUI version using JavaFX or Swing
- Convert it to a RESTful API with Spring Boot
Final Thoughts
Java may not be as trendy as some newer languages, but its reliability, rich tooling, and strong OOP foundation make it a great choice—especially for beginners. Creating a simple app like this not only helps you understand Java basics but also sets the stage for more advanced backend or desktop development projects.
Keep experimenting, building, and learning. Happy coding!
Read more on- https://keploy.io/blog/community/writing-a-potions-bank-rest-api-with-spring-boot-mongodb