Java

Table of Contents

Introduction

Java is a class-based, object-oriented programming language designed to be platform-independent through the "Write Once, Run Anywhere" (WORA) principle. It runs on the Java Virtual Machine (JVM) and is widely used for building enterprise-scale applications.

Platform Features:

Key Features

Object-Oriented Programming


// Class definition
public class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Methods
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Method overriding
    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + "}";
    }
}
    

Inheritance and Interfaces


// Interface
public interface Movable {
    void move();
}

// Abstract class
public abstract class Vehicle implements Movable {
    protected String type;

    public abstract void start();
}

// Concrete class
public class Car extends Vehicle {
    @Override
    public void move() {
        System.out.println("Car is moving");
    }

    @Override
    public void start() {
        System.out.println("Car engine started");
    }
}
    

Java Ecosystem

Platform Editions:

Build Tools



<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
    

Core Concepts

Collections Framework


// List example
List<String> list = new ArrayList<>();
list.add("First");
list.add("Second");

// Map example
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

// Set example
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);

// Stream API
list.stream()
    .filter(s -> s.startsWith("F"))
    .map(String::toUpperCase)
    .forEach(System.out::println);
    

Exception Handling


try {
    // Risky operation
    performOperation();
} catch (IOException e) {
    // Handle IO exception
    logger.error("IO error", e);
} catch (SQLException e) {
    // Handle SQL exception
    logger.error("Database error", e);
} finally {
    // Cleanup
    closeResources();
}

// Try-with-resources
try (FileInputStream fis = new FileInputStream("file.txt")) {
    // File operations
} catch (IOException e) {
    // Handle exception
}
    

Popular Frameworks

Enterprise Frameworks:

Spring Boot Example


@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@RestController
@RequestMapping("/api")
public class UserController {
    @GetMapping("/users")
    public List<User> getUsers() {
        return userService.findAll();
    }
}
    

Development Tools

Essential Tools:

Best Practices

Coding Guidelines:

Design Patterns Example


// Singleton pattern
public class Singleton {
    private static Singleton instance;
    
    private Singleton() {}
    
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

// Factory pattern
public interface Animal {
    void makeSound();
}

public class AnimalFactory {
    public Animal createAnimal(String type) {
        if ("dog".equals(type)) {
            return new Dog();
        } else if ("cat".equals(type)) {
            return new Cat();
        }
        throw new IllegalArgumentException("Unknown animal type");
    }
}
    

Related Resources