AskHandle Blog
Java Basic Programs

Java Basic Programs
Java is a widely used programming language appreciated by developers for its flexibility, readability, and robustness. If you are beginning your journey into Java programming, certain basic programs may seem intimidating. This article provides clear explanations and code examples of common Java programs.
Hello World Program
The classic "Hello World" program in Java prints the text "Hello, World!" to the console. Here is the code:
1public class HelloWorld {
2 public static void main(String[] args) {
3 System.out.println("Hello, World!");
4 }
5}This program contains a class named HelloWorld with a main method. The println method outputs the text to the console. Running this program displays the greeting.
Adding Two Numbers
Adding two numbers together is a common program in Java. This helps you understand basic arithmetic operations. Below is the code for adding two numbers:
1public class AddTwoNumbers {
2 public static void main(String[] args) {
3 int num1 = 5;
4 int num2 = 10;
5 int sum = num1 + num2;
6 System.out.println("The sum is: " + sum);
7 }
8}In this program, we declare two integer variables num1 and num2, add them, and then print the result.
Check Prime Number
How do you determine if a number is prime? A prime number is greater than 1 and has no positive divisors other than 1 and itself. This program checks for prime numbers:
1public class PrimeNumberCheck {
2 public static void main(String[] args) {
3 int num = 17;
4 boolean isPrime = true;
5
6 if (num <= 1) {
7 isPrime = false;
8 } else {
9 for (int i = 2; i <= num / 2; i++) {
10 if (num % i == 0) {
11 isPrime = false;
12 break;
13 }
14 }
15 }
16
17 if (isPrime) {
18 System.out.println(num + " is a prime number.");
19 } else {
20 System.out.println(num + " is not a prime number.");
21 }
22 }
23}In this program, we check if the number is less than or equal to 1. If so, it is not prime. If not, we iterate from 2 to half of the number to find divisors.
Fibonacci Series
The Fibonacci series is a famous mathematical sequence where each number is the sum of the two preceding ones. Here is a Java program to generate the Fibonacci series:
1public class FibonacciSeries {
2 public static void main(String[] args) {
3 int n = 10;
4 int firstTerm = 0, secondTerm = 1;
5
6 System.out.print("Fibonacci Series up to " + n + " terms: ");
7
8 for (int i = 1; i <= n; ++i) {
9 System.out.print(firstTerm + " + ");
10 int sum = firstTerm + secondTerm;
11 firstTerm = secondTerm;
12 secondTerm = sum;
13 }
14 }
15}This program specifies the number of terms in the Fibonacci series and calculates each term by summing the two preceding terms.
Reverse a Number
Reversing a number changes the order of its digits. Here is a Java program to reverse a given number:
1public class ReverseNumber {
2 public static void main(String[] args) {
3 int num = 12345;
4 int reversedNumber = 0;
5
6 while(num != 0) {
7 int digit = num % 10;
8 reversedNumber = reversedNumber * 10 + digit;
9 num /= 10;
10 }
11
12 System.out.println("Reversed Number: " + reversedNumber);
13 }
14}In this program, we use a while loop to extract the last digit of the number, append it to the reversed number, and remove that digit from the original number.
You have now explored various basic Java programs. Practicing these examples will help strengthen your coding skills. Programming combines creativity and logic in problem-solving.