Basic Java Programs
Simple Examples to Start Learning
Java
1. Hello World Program
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
2. Add Two Numbers
public class AddNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
int sum = a + b;
[Link]("Sum: " + sum);
}
}
3. Check Even or Odd
public class EvenOdd {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0) {
[Link]("Even Number");
} else {
[Link]("Odd Number");
}
}
}
4. Find Largest of Two Numbers
public class LargestNumber {
public static void main(String[] args) {
int a = 5, b = 8;
if (a > b) {
[Link]("A is larger");
} else {
[Link]("B is larger");
}
}
}
Summary
• • These are some basic Java programs to
help beginners understand Java syntax.
• • Practice writing and running these to get
comfortable with Java.
• • Next steps: Learn about loops, arrays,
and object-oriented programming!