1.
Write and Execute Java Program in Eclipse
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming in Eclipse!");
}
}
// Output:
// Welcome to Java Programming in Eclipse!
2. Java Program using Command Line Arguments
public class CommandLineExample {
public static void main(String[] args) {
System.out.println("Arguments passed:");
for (String arg : args) {
System.out.println(arg);
}
}
}
// Compile & Run:
// javac CommandLineExample.java
// java CommandLineExample Hello Java World
// Output:
// Arguments passed:
// Hello
// Java
// World
3. Understanding OOP & Java Basics
class Student {
String name;
int roll;
void display() {
System.out.println("Name: " + name + ", Roll: " + roll);
}
public static void main(String[] args) {
Student s = new Student();
s.name = "Rahul";
s.roll = 101;
s.display();
}
}
// Output:
// Name: Rahul, Roll: 101
4. Inheritance and Polymorphism
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal a = new Dog(); // runtime polymorphism
a.sound();
}
}
// Output:
// Dog barks
5. Exception Handling and Multithreading
public class ExceptionThreadDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught: " + e);
}
Thread t = new Thread(() -> {
for (int i = 0; i < 3; i++) {
System.out.println("Thread running: " + i);
}
});
t.start();
}
}
// Output:
// Caught: java.lang.ArithmeticException: / by zero
// Thread running: 0
// Thread running: 1
// Thread running: 2
6. Java Packages
// File: mypackage/Message.java
package mypackage;
public class Message {
public void show() {
System.out.println("Hello from Package!");
}
}
// File: TestPackage.java
import mypackage.Message;
public class TestPackage {
public static void main(String[] args) {
Message m = new Message();
m.show();
}
}
// Output:
// Hello from Package!
7. Java I/O Package
import java.io.*;
public class FileWriteRead {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("sample.txt");
writer.write("Hello Java I/O!");
writer.close();
FileReader reader = new FileReader("sample.txt");
int i;
while ((i = reader.read()) != -1)
System.out.print((char) i);
reader.close();
} catch (IOException e) {
System.out.println("IO Error: " + e);
}
}
}
// Output:
// Hello Java I/O!
8. Spring Framework Application (Simple REST Controller)
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Framework!";
}
}
// Output (via browser or Postman):
// Hello from Spring Framework!
9. RESTful Web Services using Spring Boot
@RestController
@RequestMapping("/api")
public class RestApiController {
@GetMapping("/student")
public Map<String, String> getStudent() {
Map<String, String> map = new HashMap<>();
map.put("name", "Raj");
map.put("course", "Java");
return map;
}
}
// Output (GET /api/student):
// {
// "name": "Raj",
// "course": "Java"
// }
10. Frontend Web Application with Spring Boot and Thymeleaf
// HomeController.java
@Controller
public class HomeController {
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("message", "Welcome to Spring Boot Web!");
return "home";
}
}
// home.html (Thymeleaf Template)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>Home</title></head>
<body><h1 th:text="${message}"></h1></body>
</html>
// Output (in browser):
// Welcome to Spring Boot Web!