Java

Here’s a basic Java program that prints “Hello, World!” and shows the structure of a Java class:

 
public class Main {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
 

How it works

  • public class Main → defines a class named Main
  • main method → the entry point where execution starts
  • System.out.println → prints text to the console

If you want something more practical, here’s a slightly more interesting example: a program that adds two numbers.

 
import java.util.Scanner;

public class AddNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print(“Enter first number: “);
int a = scanner.nextInt();

System.out.print(“Enter second number: “);
int b = scanner.nextInt();

int sum = a + b;

System.out.println(“Sum = “ + sum);
scanner.close();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *