Master Java Class Exercises with Solutions: Boost Your Coding Skills

Robert Jul 13, 2026

Mastering Java requires practice, and there's no better way to hone your skills than by working on hands-on exercises. Whether you're a novice or an experienced programmer, these Java class exercises with solutions will not only help you understand concepts better but also challenge you to apply what you've learned.

Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻

In this article, we'll explore a range of topics, from basic Java syntax to object-oriented programming concepts. Each exercise comes with a solution, allowing you to check your answers and deepen your understanding. So, let's dive right in and get coding!

Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners

Java Syntax and Data Types

Starting with the basics, it's crucial to have a strong grasp of Java's syntax and data types. These first few exercises will help you reviewers these foundational concepts.

30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners

Before we proceed, ensure you have a Java development environment set up. If you haven't already, download and install the Java Development Kit (JDK) and an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse.

Exercise 1: Hello World

an image of a computer screen with text
an image of a computer screen with text

The classic "Hello, World!" program is a simple yet powerful exercise that introduces you to the basics of writing and running a Java program. Create a new Java class called Main and write a program that prints "Hello, World!" to the console.

Solution:
```java public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } ```

Exercise 2: Data Types

the info sheet shows how to learn in 30 days
the info sheet shows how to learn in 30 days

Write a program that declares variables of each of Java's eight primitive data types and initializes them with appropriate values. Then, print the values to the console.

Solution:
```java public class Main { public static void main(String[] args) { int i = 10; short s = 100; long l = 1000L; float f = 10.0f; double d = 10.0; boolean b = true; char c = 'a'; byte by = 100; System.out.println("Int: " + i); System.out.println("Short: " + s); System.out.println("Long: " + l); System.out.println("Float: " + f); System.out.println("Double: " + d); System.out.println("Boolean: " + b); System.out.println("Char: " + c); System.out.println("Byte: " + by); } } ```

Control Structures and Operators

Java Program Structure Explained ☕ | Beginner-Friendly Guide
Java Program Structure Explained ☕ | Beginner-Friendly Guide

Next, let's explore Java's control structures and operators. These exercises will help you understand and apply if-else statements, switch-case, loops, and various operators.

Exercise 3: If-Else Statement

Steps to becoming a java pro
Steps to becoming a java pro
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Project Ideas For Beginners
Java Project Ideas For Beginners
java array operations
java array operations
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Learning Roadmap 2026 ☕🚀
Java Learning Roadmap 2026 ☕🚀
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Echo Answered! Outstanding Python, Java, JavaScript Pros: Call Heard – Order for Resonant Results!
Echo Answered! Outstanding Python, Java, JavaScript Pros: Call Heard – Order for Resonant Results!
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
the roadmap to learn java from zero - to - job ready info sheet
the roadmap to learn java from zero - to - job ready info sheet
Abstract Class in Java (Quick Guide)
Abstract Class in Java (Quick Guide)
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
core java classes in Pune
core java classes in Pune
Java Programming, Java
Java Programming, Java

Write a program that takes an integer input from the user and prints whether it's positive, negative, or zero.

Solution:
```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int num = scanner.nextInt(); if (num > 0) { System.out.println("Positive"); } else if (num < 0) { System.out.println("Negative"); } else { System.out.println("Zero"); } } } ```

Exercise 4: Switch-Case

Write a menu-driven program that allows the user to perform basic arithmetic operations (addition, subtraction, multiplication, and division) with two numbers.

Solution:
```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = scanner.nextDouble(); System.out.print("Enter second number: "); double num2 = scanner.nextDouble(); System.out.print("Choose an operation (+, -, *, /): "); char op = scanner.next().charAt(0); double result; switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 == 0) { System.out.println("Error: Division by zero"); return; } result = num1 / num2; break; default: System.out.println("Error: Invalid operation"); return; } System.out.printf("%.2f %c %.2f = %.2f%n", num1, op, num2, result); } } ```

These exercises should give you a solid foundation in Java syntax and concepts. Keep practicing and exploring new topics to continuously improve your skills. Happy coding!