The first program — prints "Hello, World!" to verify your Java setup is working.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Complete Beginner to Pro Guide
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean isPass = true;
String name = "Md";
int marks = 85;
marks = 92; // value changed
final double PI = 3.14159; // constant
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a > b); // true
System.out.println(a % b); // 1
int marks = 78;
if (marks >= 90) {
System.out.println("A+");
} else if (marks >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
// Switch
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
default: System.out.println("Other");
}
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
int count = 1;
while (count <= 5) {
System.out.println(count);
count++;
}
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[1]); // 20
System.out.println(numbers.length); // 4
class Student {
String name;
int age;
void show() {
System.out.println(name + " is " + age);
}
}
Student s = new Student();
s.name = "Md";
s.age = 22;
s.show();
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Woof!"); }
}
Dog d = new Dog();
d.eat();
d.bark();
String text = "Hello Java 2026";
System.out.println(text.length()); // 15
System.out.println(text.toUpperCase()); // HELLO JAVA 2026
System.out.println(text.substring(0, 5)); // Hello
import java.util.ArrayList;
ArrayList list = new ArrayList<>();
list.add("Apple");
list.add("Mango");
System.out.println(list.get(1)); // Mango
list.remove(0);
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("Cleanup done");
}