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.

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!

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.

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

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

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

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



















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!