Java: 초보자부터 고급까지 완전 가이드 (실용 코드 예제 포함)

발행: (2025년 12월 4일 오후 12:11 GMT+9)
6 min read
원문: Dev.to

Source: Dev.to

Java란 무엇이며 왜 인기가 있나요?

Java는 클래스 기반이며 객체 지향적이고, 플랫폼에 독립적이며, 강타입(Strongly Typed) 프로그래밍 언어입니다.

주요 특징

  • 플랫폼 독립성 (“Write Once, Run Anywhere” – WORA)
  • 객체‑지향 (OOP: 클래스, 객체, 상속, 다형성)
  • 멀티‑스레드 지원
  • 보안 및 견고함
  • 방대한 생태계 (Spring Boot, Maven, Gradle, JavaFX)
  • 대규모 커뮤니티 지원

Java 설정

Java 설치

JDK를 다운로드하세요:

버전 확인

java -version
javac -version

첫 번째 Java 프로그램

Main.java 파일을 생성합니다:

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

컴파일하고 실행합니다:

javac Main.java
java Main

변수와 데이터 타입

Java는 강타입 언어이므로 모든 변수는 타입을 가져야 합니다.

public class Main {
    public static void main(String[] args) {
        int age = 25;
        double price = 99.99;
        char grade = 'A';
        boolean isActive = true;
        String name = "Farhad";

        System.out.println(name + " - " + age);
    }
}

제어 흐름문

If / Else

int score = 80;

if (score >= 90) {
    System.out.println("Excellent");
} else if (score >= 50) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Switch

String day = "Monday";

switch (day) {
    case "Monday":
        System.out.println("Start of week");
        break;
    default:
        System.out.println("Unknown day");
}

Loops

for (int i = 1; i  names = new ArrayList<>();
        names.add("Farhad");
        names.add("Rahimi");

        System.out.println(names);
    }
}

객체 지향 프로그래밍 (OOP)

클래스와 객체

class Car {
    String model;
    int year;

    void start() {
        System.out.println(model + " is starting...");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.model = "Toyota";
        c.year = 2024;
        c.start();
    }
}

생성자

class User {
    String name;
    int age;

    User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        User u = new User("Farhad", 23);
        System.out.println(u.name);
    }
}

상속

class Animal {
    void sound() {
        System.out.println("Makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

다형성

class Shape {
    void draw() {
        System.out.println("Drawing shape");
    }
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing circle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Circle();  
        s.draw();
    }
}

캡슐화

class BankAccount {
    private double balance;

    public void deposit(double amount) {
        balance += amount;
    }

    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.deposit(100);
        System.out.println(acc.getBalance());
    }
}

추상화 (인터페이스 및 추상 클래스)

인터페이스

interface Animal {
    void eat();
}

class Cat implements Animal {
    public void eat() {
        System.out.println("Cat eating");
    }
}

추상 클래스

abstract class Vehicle {
    abstract void move();
}

class Bike extends Vehicle {
    void move() {
        System.out.println("Bike moves fast");
    }
}

예외 처리

try {
    int x = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
} finally {
    System.out.println("Always runs");
}

파일 처리

import java.io.FileWriter;

public class Main {
    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter("output.txt");
            fw.write("Java file writing...");
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

실제 프로젝트에서 Java와 OOP

Java는 다음 분야에서 널리 사용됩니다:

  • Spring Boot (엔터프라이즈 백엔드)
  • Android 개발
  • 금융 서비스
  • 대규모 마이크로서비스
  • 분산 시스템
  • Hadoop을 활용한 빅데이터
  • 클라우드 애플리케이션 (AWS, GCP, Azure)

대부분의 프로덕션 환경은 Java의 신뢰성과 확장성을 선호합니다.

미니 Java 프로젝트: 간단한 To‑Do 콘솔 앱

import java.util.ArrayList;
import java.util.Scanner;

public class TodoApp {
    public static void main(String[] args) {
        ArrayList tasks = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("1. Add Task  2. View Tasks  3. Exit");
            int choice = sc.nextInt();
            sc.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("Enter task: ");
                    tasks.add(sc.nextLine());
                    break;
                case 2:
                    System.out.println("Your tasks:");
                    for (String t : tasks) System.out.println("- " + t);
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("Invalid choice");
            }
        }
    }
}

결론

Java는 백엔드 개발, 엔터프라이즈 시스템, Android 애플리케이션, 그리고 확장 가능한 아키텍처를 위한 가장 강력한 언어 중 하나로 남아 있습니다. 성숙한 생태계, 활발한 커뮤니티, 장기적인 안정성 덕분에 전 세계 개발자들에게 최고의 선택이 되고 있습니다.

마이크로서비스를 Spring Boot로 구축하든, 고성능 서버를 만들든, 혹은 크로스‑플랫폼 애플리케이션을 개발하든, Java는 성공을 위한 도구를 제공합니다.

Back to Blog

관련 글

더 보기 »

Java OOPS 개념

Forem 로고https://media2.dev.to/dynamic/image/width=65,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%...

Java에서 object와 class는 무엇인가

Object란 무엇인가? Object는 property와 behavior를 갖는 현실 세계의 엔티티이다. 예를 들어, 펜은 color, brand, height, diameter와 같은 property를 가질 수 있다.