Introduction to Java

Java is a popular, high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is widely used in enterprise applications, Android development, and server-side software.

1. Hello World

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Run Code on OneCompiler

2. Addition of Two Numbers

public class Addition { public static void main(String[] args) { int a = 5; int b = 3; int sum = a + b; System.out.println("Sum = " + sum); } }
Run Code on OneCompiler

3. Subtraction Example

public class Subtract { public static void main(String[] args) { int a = 10; int b = 4; System.out.println("Difference = " + (a - b)); } }
Run Code on OneCompiler

4. Loop (Print Numbers 1 to 5)

public class LoopDemo { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println(i); } } }
Run Code on OneCompiler

5. Function to Multiply

public class Multiply { public static int product(int x, int y) { return x * y; } public static void main(String[] args) { System.out.println("Product = " + product(4, 5)); } }
Run Code on OneCompiler

🧠 Java Quiz

  1. Which keyword is used to define a class in Java?

  2. What does JVM stand for?

  3. Which method is the entry point of a Java program?