❓
물음표살인마 블로그
  • README
  • ALGORITHM
    • Sieve of Eratosthenes
    • Round Up
    • Binary Search
    • Union Find
    • Sorting Array
    • Lcm, Gcd
  • TechTalk Review
    • Template
  • Books
    • CS Note for Interview
      • Ch1. Design Pattern & Programming paradigm
        • 1.1.1 Singleton Pattern
        • 1.1.2 Factory Pattern
        • 1.1.4 Observer Pattern
        • 1.1.5 Proxty Pattern & Proxy Server
        • 1.1.8 Model-View-Controller Pattern
        • 1.2.1 Declarative and Functional Programming
        • 1.2.2 Object Oriented Programming
      • Ch2. Network
        • 2.2.1 TCP/IP Four-Layer Model
        • 2.2.1-1 TCP 3, 4 way handshake
        • 2.3 Network Devices L4, L7
        • 2.4.1 ARP, RARP
        • 2.4.2 Hop By Hop Communication
        • 2.4.3 IP Addressing Scheme
      • Ch3. Operating System
        • 3.1.1 Roles and Structure of Operating Systems
        • 3.2.1 Memory Hierarchy
        • 3.2.2 Memory Management
        • 3.3.1 Processes and Compilation Process
        • 3.3.3 Memory Structure of a Process
        • 3.3.4 Process Control Block (PCB)
        • 3.3.5 Multiprocessing
        • 3.3.6 Threads and Multithreading
        • 3.3.7 Shared Resources and Critical Sections
        • 3.3.8 Deadlock
        • 3.4 CPU Scheduling Algorithm
      • Ch4. Database
        • 4.1 Database Basic
        • 4.2 Normalization
        • 4.3 Transaction and Integrity
        • 4.4 Types of Databases
        • 4.5 Indexes
        • 4.6 Types of Joins
        • 4.7 Principles of Joins
      • Ch5. Data Structure
    • Learning the Basics of Large-Scale System Design through Virtual Interview Cases
      • 1. Scalability based on user counts(1/2)
      • 1. Scalability based on user counts(2/2)
      • 2.Back-of-the-envelope estimation
      • 3. Strategies for System Design Interviews
      • 4. Rate Limiter
      • 5. Consistent Hashing
      • 6. Key-Value System Design
      • 7. Designing a Unique ID Generator for Distributed Systems
      • 8. Designing a URL Shortener
      • 9. Designing a Web Crawler
      • 10. Notification System Design
      • 11. Designing a News Feed System
      • 12. Chat System Design
      • 13. AutoComplete
      • 14. Design YouTube
      • 15. Design Google Drive
      • Loadbalancer Algorithms
      • Cache tier
      • CDN, Content Delivery Network
      • Stateless Web tier
    • Computer System A programmer's perspective
    • Effective Java
      • Item 1. Consider Static Factory Methods Instead of Constructors
      • Item 2. Consider a Builder When Faced with Many Constructor Parameters
      • Item 3. Ensure Singleton with Private Constructor or Enum Type
      • Item 4. Enforce Noninstantiability with a Private Constructor
      • Item 5. Prefer Dependency Injection to Hardwiring Resources
      • Item 6. Avoid Creating Unnecessary Objects
      • Item 7. Eliminate Obsolete Object References
      • Item 8. Avoid Finalizers and Cleaners
      • Item 9.Prefer try-with-resources to try-finally
      • Item10. Adhering to General Rules When Overriding equals
        • Handling Transitivity Issues
        • Ensuring Consistency
      • Item11. Override hashCode When You Override equals
      • Item12. Always Override toString
        • Always Override toString
      • Item13. Override Clone Judiciously
      • Item14. Consider Implementing Comparable
      • Item15. Minimize the Accessibility of Classes and Members
      • Item16. Accessor Methods Over Public Fields
      • Item17. Minimize Mutability
      • Item18. Composition over inherentance
      • Item19. Design and Document for Inheritance, or Else Prohibit It
      • Item20. Prefer Interfaces to Abstract Classes
      • Item21. Design Interfaces with Implementations in Mind
      • Item22. Use Interfaces Only to define Types
      • Item23. Prefer Class Hierarchies to Tagged Classes
      • Item24. Favor Static Member Classes Over Non-Static
      • Item28. Use Lists Instead of Arrays
      • Item29. Prefer Generic Types
      • Item30. Favor Generic Methods
    • Head First Design Patterns
      • Ch1. Strategy Pattern
      • Ch2. Observer Pattern
        • Ver1. Ch2. Observer Pattern
      • Ch3. Decorator Pattern
        • Ch3. Decorator Pattern
      • Ch4. Factory Pattern
      • Ch5. Singleton Pattern
      • Ch6. Command Pattern
      • Ch7. Adapter and Facade Pattern
      • Ch8. Template Method Pattern
    • Digging Deep into JVM
      • Chapter 2. Java Memory Area & Memory Overflow
      • Chapter 3. Garbage Collector & Memory Allocation Strategy (1/2)
      • Chapter 3. Garbage Collector & Memory Allocation Strategy (2/2)
      • Chapter 5. Optimization Practice
      • Chapter 6. Class file structure
      • Chapter 8. Bytecode Executor Engine (1/2)
  • Interview Practices
    • Restful API Practices
      • Url Shortener API
      • Event Ticket Reservation API
      • Course Management API
      • Search posts by tags API
      • Online Code platform API
      • Simple Task Management API
      • Event Participation API
      • Review System API
      • Car management API
      • Online Library
    • Tech Review
      • if(kakao)
        • Kakao Account Cache Migration / if(kakao)2022
        • Improving the Anomaly Detection System for KakaoTalk Messaging Metrics / if(kakao) 2022
        • Standardizing API Case Handling Without Redeployment / if(kakaoAI)2024
        • JVM warm up / if(kakao)2022
    • Naver Computer Science
      • Process & Thread
      • TCP & UDP
      • Spring & Servlet
      • Filter & Interceptor & AOP
      • Equals() & ==
      • Dependency Injection
      • Object Oriented Programming
  • F-Lab
    • Week1
      • Client & Server
      • HTTP
      • TCP/UDP
      • REST API
      • Questions
        • Object Oriented Programming
        • HTTP
        • Process & Thread
        • Data Structure
    • Week2
      • OSI 7 layer
      • Web vs WAS
    • Week3
      • RDB vs NoSQL
      • RDB Index
      • Cache
      • Redis
      • Messaging Queue
    • Week4
      • Project - Ecommerce
    • Week5
      • ERD - 1
    • Week6
      • Ecommerce - 2
      • Role
      • pw hashing && Salt
      • CreatedAt, ModifiedAt
      • JWT
      • Copy of ERD - 1
    • Week7
      • Vault (HashiCorp Vault)
    • Week 8
      • Api Endpoints
    • Week10
      • Product Create Workflow
  • TOY Project
    • CodeMentor
      • Implementation of Kafka
      • Project Improvement (Architectural Enhancements)
      • Communication between servers in msa
  • JAVA
    • MESI protocol in CAS
    • CAS (Compare and Set)
    • BlockingQueue
    • Producer & Consumer
    • Synchronized && ReentrantLock
    • Memory Visibility
    • Checked vs Unchecked Exception
    • Thread
    • Batch delete instead of Cascade
    • Java Questions
      • Week 1(1/2) - Basic Java
      • Week 1(2/2) - OOP
      • Week 2(1/2) - String, Exception, Generic
      • Week2(2/2) Lambda, Stream, Annotation, Reflection
      • Week3(1/2) Collections
      • Week3(2/2) Threads
      • Week4 Java Concurrency Programming
      • Week5 JVM & GC
    • Java 101
      • JVM Structure
      • Java Compiles and Execution Method
      • Override, Overload
      • Interface vs Abstract Class
      • Primitive vs Object Type
      • Identity and equality
      • String, StringBuilder, StringBuffer
      • Checked Exceptions and Unchecked Exceptions
      • Java 8 methods
      • Try-with-reources
      • Strong Coupling and Loose Coupling
      • Serialization and Deserialization
      • Concurrency Programming in Java
      • Mutable vs Immutable
      • JDK vs JRE
  • SPRING
    • DIP. Dependency Inversion Principal
    • Ioc container, di practice
    • @Transactional
    • Proxy Pattern
    • Strategy Pattern
    • Template Method Pattern
    • using profile name as variable
    • Spring Questions
      • Spring Framework
      • Spring MVC & Web Request
      • AOP (Aspect-Oriented Programming)
      • Spring Boot
      • ORM & Data Access
      • Security
      • ETC
  • DATABASE
    • Enhancing Query Performance & Stability - User list
    • Ensuring Data Consistency, Atomicity and UX Optimization (feat.Firebase)
    • Redis: Remote Dictionary Server
    • Database Questions
      • Week1 DBMS, RDBMS basics
      • Week2 SQL
      • Week3 Index
      • Week4 Anomaly, Functional Dependency, Normalization
      • Week5 DB Transaction, Recovery
    • Normalization
      • 1st Normal Form
      • 2nd Normal Form
      • 3rd Normal Form
  • NETWORK
    • HTTP & TCP head of line blocking
    • HTTP 0.9-3.0
    • Blocking, NonBlocking and Sync, Async
    • Network Questions
      • Week1 Computer Network Basic
      • Week2(1/3) Application Layer Protocol - HTTP
      • Week2(2/3) Application Layer Protocol - HTTPS
      • Week2(3/3) Application Layer Protocol - DNS
      • Week3 Application Layer
      • Week4 Transport Layer - UDP, TCP
      • Week5 Network Layer - IP Protocol
    • Network 101
      • https://www.google.com
      • TCP vs UDP
      • Http vs Https
      • TLS Handshake 1.2
      • HTTP Method
      • CORS & SOP
      • Web Server Software
  • OS
    • Operating System Questions
      • Week1 OS & How Computer Systems Work
      • Week2(1/2) Process
      • Week2(2/2) Thread
      • Week3 CPU Scheduling
      • Week4 Process Synchronize
      • Week5 Virtual Memory
    • Operating System 101
      • Operating system
        • The role of the operating system
        • The composition of the operating system.
      • Process
        • In Linux, are all processes except the initial process child processes?
        • Zombie process, orphan process
        • (Linux) Daemon process
        • Process address space
        • Where are uninitialized variables stored?
        • Determination of the size of the Stack and Heap
        • Access speed of Stack vs Heap
        • Reason for memory space partitioning
        • Process of compiling a process
        • sudo kill -9 $CURRENT_PID
      • Thread
        • Composition of a thread's address space
      • Process vs Thread
        • Creation of processes and threads in Linux
      • Multiprocessing
        • Web Browser
        • Implementation of multiprocessing
        • Application areas of multiprocessing
      • Multithreading
        • Application areas of multithreading
      • Interrupt
        • HW / SW Interrupt
        • Method of handling interrupts
        • Occurrence of two or more interrupts simultaneously
      • Polling
      • Dual Mode
        • Reason for distinguishing between user mode and kernel mode
      • System call
        • Differentiation between system calls
        • Types of system calls
        • Execution process of a system call
      • Process Control Block (PCB)
        • PCB의 구조
        • 쓰레드는 PCB를 갖고 있을까?
        • 프로세스 메모리 구조
      • Context switching
        • Timing of context switching
        • Registers saved during context switching
        • Context switching in processes
        • Context switching in threads
        • Difference between context switching in processes and threads
        • Information of the current process during context switching
      • Interprocess Communication (IPC)
        • Cases where IPC is used
        • Process address space in IPC Shared Memory technique
        • Types of IPC
  • COMPUTER SCIENCE
    • Computer Architecture 101
      • 3 components of a computer
      • RAM vs ROM
      • CPU vs GPU
      • SIMD
      • Two's complement
      • Harvard Architecture vs. von Neumann Architecture
      • The structure of a CPU.
      • Instruction cycle (CPU operation method)
      • Instruction pipelining
      • Bus
      • Memory area
      • Memory hierarchy structure
        • Reason for using memory hierarchy structure
      • Cache memory
      • L1, L2, L3 Cache
      • Locality of reference (cache)
      • Fixed-point vs Floating-point
        • epresentation of infinity and NaN (Not a Number) in floating-point
      • RISC vs CISC
      • Hamming code
      • Compiler
      • Linking
      • Compiler vs Interpreter
      • Mutex vs Semaphore
      • 32bit CPU and 64bit CPU
      • Local vs Static Variable
      • Page
  • Programming Paradigm
    • Declarative vs Imperative
  • JPA, QueryDsl
    • why fetchResults() is deprecated
  • PYTHON
    • Icecream
  • FASTAPI
    • Template Page
  • LINUX
    • Template Page
  • DATA STRUCTURE
    • Counting Sort
    • Array vs Linked List
  • GIT, Github
    • git clone, invalid path error
  • INFRA
    • Template Page
  • AWS
    • Server Log Archive Pipeline
    • Image Processing using Lambda
  • DOCKER
    • Docker and VM
    • Python Executable Environment
    • Docker commands
  • docker-compose
    • Kafka, Multi Broker
  • KUBERNATES
    • !Encountered Errors
      • my-sql restarts
      • kafka producer: disconnected
    • Kubernetes Components
    • Helm
      • Helm commands
    • Pod network
    • Service network
      • deployment.yaml
      • services.yaml
    • Service type
      • Cluster IP
      • NodePort
    • service-name-headless?
    • kube-proxy
  • GraphQL
    • Template Page
  • WEB
    • Template Page
  • Reviews
    • Graphic Intern Review
    • Kakao Brain Pathfinder Review
    • JSCODE 자바 1기 Review
  • 😁Dev Jokes
    • Image
      • Plot twist
      • Priorities
      • SQL join guide
      • Google is generous
      • Genie dislikes cloud
      • buggy bugs
      • last day of unpaid internship
      • what if clients know how to inspect
      • its just game
      • how i wrote my achievement on resume
      • self explanatory
      • chr(sum(range(ord(min(str(not))))))
Powered by GitBook
On this page
  • 1. String 리터럴과 new String("")의 차이
  • 2. String, StringBuilder, StringBuffer의 차이점
  • 3. Exception과 Error의 차이
  • 4. Exception 클래스의 예시
  • 5. Checked Exception과 Unchecked Exception의 차이
  • 6. throw와 throws의 차이
  • 7. try-catch-finally 구문에서 finally의 역할
  • 8. Throwable과 Exception의 차이
  • 9. 제네릭(Generic)이란 무엇이고, 왜 사용할까요?
  • 10. 제네릭을 사용한 경험을 소개해 주세요.
  1. JAVA
  2. Java Questions

Week 2(1/2) - String, Exception, Generic

문자열, 예외, 제네릭

1. String 리터럴과 new String("")의 차이

자바에서 String은 불변 객체(immutable object)입니다. 즉, 한 번 생성된 문자열은 변경할 수 없습니다. 이를 통해 자바에서 문자열을 다루는 두 가지 주요 방식인 String 리터럴과 new String("")에 대해 알아보겠습니다.

1.1 String 리터럴

String str1 = "Hello";
String str2 = "Hello";

위 코드에서 str1과 str2는 둘 다 동일한 리터럴 "Hello"를 참조합니다. 이는 자바의 String 상수 풀 덕분입니다. String 리터럴은 JVM의 String 상수 풀에 저장되며, 동일한 리터럴이 이미 존재할 경우, 새롭게 메모리를 할당하지 않고 기존 객체를 참조합니다. 따라서 str1 == str2는 true를 반환합니다.

1.2 new String("")

String str3 = new String("Hello");
String str4 = new String("Hello");

위 코드에서 str3과 str4는 각각 new 키워드를 사용하여 새롭게 String 객체를 힙 메모리에 생성합니다. 이 경우, 서로 다른 두 객체가 생성되므로 str3 == str4는 false를 반환합니다. 하지만 두 문자열의 내용은 동일하기 때문에 str3.equals(str4)는 true입니다.

1.3 차이점 정리

  • 메모리 사용: String 리터럴은 상수 풀을 이용하여 메모리를 절약할 수 있지만, new String()은 힙 메모리에 매번 새로운 객체를 생성합니다.

  • 성능: String 리터럴 방식이 메모리와 성능 면에서 효율적입니다.

  • 참조 비교: String 리터럴은 상수 풀에 저장된 동일한 문자열을 참조하지만, new String()은 매번 새로운 객체를 생성하므로 참조 비교(==)에서 다른 결과가 나옵니다.

2. String, StringBuilder, StringBuffer의 차이점

자바에서 문자열을 다루기 위한 대표적인 클래스 세 가지는 String, StringBuilder, StringBuffer입니다. 각 클래스의 주요 차이점과 사용 사례를 살펴보겠습니다.

2.1 String

  • 불변성(Immutable): String 객체는 한 번 생성되면 변경할 수 없습니다. 새로운 문자열이 필요할 경우, 기존 객체를 수정하는 것이 아니라 새로운 객체를 생성합니다.

  • 사용 사례: 변경되지 않는 문자열, 상수 문자열, 간단한 문자열 처리.

2.2 StringBuilder

  • 가변성(Mutable): StringBuilder 객체는 문자열을 변경할 수 있습니다. 문자열을 수정할 때마다 새로운 객체를 생성하지 않고, 기존 객체를 수정합니다.

  • 동기화(Synchronization): StringBuilder는 동기화되지 않으므로 멀티스레드 환경에서 안전하지 않습니다.

  • 사용 사례: 단일 스레드 환경에서 빈번한 문자열 조작이 필요한 경우. 예를 들어, 루프 내에서 문자열을 여러 번 연결하거나 변경할 때 사용됩니다.

2.3 StringBuffer

  • 가변성(Mutable): StringBuffer도 문자열을 변경할 수 있습니다.

  • 동기화(Synchronization): StringBuffer는 동기화가 지원되므로 멀티스레드 환경에서 안전하게 사용할 수 있습니다.

  • 사용 사례: 멀티스레드 환경에서 빈번한 문자열 조작이 필요한 경우.

2.4 차이점 정리

클래스
불변성
동기화 지원
주요 사용 사례

String

불변

불필요

변경되지 않는 문자열

StringBuilder

가변

미지원

단일 스레드에서의 문자열 조작

StringBuffer

가변

지원

멀티스레드에서의 문자열 조작

3. Exception과 Error의 차이

3.1 Exception

Exception은 프로그램에서 발생할 수 있는 예외적인 상황을 나타냅니다. 대부분의 경우, 개발자가 이러한 예외를 처리하여 프로그램이 정상적으로 동작할 수 있도록 할 수 있습니다. 예를 들어, 파일을 읽다가 파일이 존재하지 않는 경우(FileNotFoundException)와 같은 예외가 발생할 수 있습니다.

3.2 Error

Error는 프로그램이 복구할 수 없는 심각한 문제를 나타냅니다. 일반적으로 JVM 레벨에서 발생하며, 개발자가 이를 처리할 수 없습니다. 예를 들어, OutOfMemoryError는 JVM이 메모리를 할당할 수 없을 때 발생하며, 이 경우 프로그램은 중단될 수밖에 없습니다.

3.3 차이점 정리

  • 복구 가능성: Exception은 복구 가능하지만, Error는 복구할 수 없습니다.

  • 발생 원인: Exception은 코드 로직이나 외부 환경의 문제로 발생할 수 있으며, Error는 JVM 레벨의 심각한 문제로 인해 발생합니다.

4. Exception 클래스의 예시

자바에서 Exception 클래스는 여러 하위 클래스로 세분화되어 있습니다. 대표적인 예시들을 살펴보겠습니다.

4.1 Checked Exception 예시

  • IOException: 입출력 작업 중 발생하는 예외로, 파일이 존재하지 않거나 네트워크 연결이 끊어진 경우 발생합니다.

  • SQLException: 데이터베이스 작업 중 발생하는 예외로, SQL 구문이 잘못되었거나 데이터베이스에 접근할 수 없는 경우 발생합니다.

4.2 Unchecked Exception 예시

  • NullPointerException: 객체가 null인 상태에서 해당 객체의 메서드를 호출하거나 속성에 접근하려고 할 때 발생합니다.

  • ArrayIndexOutOfBoundsException: 배열의 인덱스가 잘못된 경우 발생하는 예외입니다.

4.3 사용자 정의 예외

public class InvalidAgeException extends Exception {
    public InvalidAgeException(String message) {
        super(message);
    }
}

사용자 정의 예외는 Exception 클래스를 상속받아 개발자가 필요에 따라 정의할 수 있습니다.

5. Checked Exception과 Unchecked Exception의 차이

자바의 예외는 Checked Exception과 Unchecked Exception으로 구분됩니다. 각 예외의 차이점과 처리 방식을 살펴보겠습니다.

5.1 Checked Exception

  • 정의: 컴파일 시점에서 검사되는 예외입니다. Exception 클래스의 하위 클래스 중 RuntimeException을 상속받지 않는 모든 예외가 Checked Exception에 해당합니다.

  • 처리 요구사항: 이러한 예외는 반드시 try-catch 블록으로 처리하거나, 메서드 시그니처에 throws 키워드를 사용하여 선언해야 합니다.

  • 예시: IOException, SQLException

5.2 Unchecked Exception

  • 정의: 컴파일 시점에서 검사되지 않는 예외입니다. RuntimeException과 그 하위 클래스들이 여기에 해당합니다.

  • 처리 요구사항: 이러한 예외는 명시적으로 처리하지 않아도 되지만, 발생할 경우 프로그램이 비정상적으로 종료될 수 있습니다.

  • 예시: NullPointerException, ArrayIndexOutOfBoundsException

5.3 차이점 정리

분류
처리 요구사항
예시

Checked Exception

반드시 처리하거나 선언해야 함

IOException, SQLException

Unchecked Exception

처리 요구사항 없음

NullPointerException, ArithmeticException

6. throw와 throws의 차이

throw와 throws는 예외 처리에서 사용되는 중요한 키워드입니다. 이 둘의 차이점과 사용 방법을 살펴보겠습니다.

6.1 throw

  • 정의: throw 키워드는 명시적으로 예외를 발생시킬 때 사용됩니다.

  • 사용 예시:

public void checkAge(int age) {
    if (age < 18) {
        throw new IllegalArgumentException("나이는 18세 이상이어야 합니다.");
    }
}

6.2 throws

  • 정의: throws 키워드는 메서드가 Checked Exception을 던질 수 있음을 선언할 때 사용됩니다.

  • 사용 예시:

public void readFile(String fileName) throws IOException {
    FileReader fileReader = new FileReader(fileName);
    // 파일 읽기 로직
}

6.3 차이점 정리

  • 사용 목적: throw는 예외를 발생시키는 데 사용되며, throws는 메서드가 어떤 예외를 던질 수 있는지를 선언합니다.

  • 위치: throw는 메서드 내에서 사용되며, throws는 메서드 시그니처에 사용됩니다.

7. try-catch-finally 구문에서 finally의 역할

finally 블록은 try-catch 구문에서 예외 발생 여부와 관계없이 항상 실행되는 부분입니다. 주요 역할과 사용 사례는 다음과 같습니다.

7.1 리소스 해제

파일, 네트워크 연결, 데이터베이스 연결 등의 리소스는 사용 후 반드시 해제해야 합니다. 이때 finally 블록을 사용하여 예외가 발생하더라도 리소스를 확실하게 해제할 수 있습니다.

FileInputStream inputStream = null;
try {
    inputStream = new FileInputStream("example.txt");
    // 파일 읽기 로직
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

7.2 예외 발생 시 행동 보장

예외가 발생하더라도 반드시 실행되어야 하는 코드(예: 로그 작성, 데이터 저장)를 finally 블록에 넣어 처리할 수 있습니다.

7.3 주요 특징

  • finally 블록은 try 또는 catch 블록이 종료된 후 항상 실행됩니다.

  • return 문이 try 또는 catch 블록에 있더라도, finally 블록은 실행됩니다.

  • finally 블록은 선택적이며, 반드시 사용해야 하는 것은 아닙니다.

8. Throwable과 Exception의 차이

Throwable과 Exception은 자바의 예외 처리에서 중요한 역할을 하는 클래스입니다. 이들의 차이점을 살펴보겠습니다.

8.1 Throwable

  • 정의: Throwable은 자바의 모든 에러와 예외의 최상위 클래스입니다. 즉, 모든 예외와 오류는 Throwable 클래스를 상속받습니다.

  • 하위 클래스: Throwable의 주요 하위 클래스는 Error와 Exception입니다.

  • 역할: Throwable 클래스는 예외 또는 오류가 발생했을 때 해당 정보를 캡처하고, 이를 상위 호출 스택으로 전달할 수 있는 메커니즘을 제공합니다.

8.2 Exception

  • 정의: Exception은 Throwable의 하위 클래스이며, 대부분의 예외 상황을 나타냅니다.

  • 하위 클래스: IOException, SQLException 등 여러 Checked Exception 및 Unchecked Exception 클래스가 Exception을 상속받습니다.

  • 역할: Exception 클래스는 프로그램에서 발생할 수 있는 다양한 예외 상황을 나타내며, 대부분의 경우 이를 처리하여 프로그램이 계속 실행될 수 있도록 합니다.

8.3 차이점 정리

  • 상속 관계: Throwable은 최상위 클래스이며, Exception은 그 하위 클래스입니다.

  • 주요 역할: Throwable은 예외와 오류를 모두 포함하며, Exception은 주로 예외를 처리합니다.

9. 제네릭(Generic)이란 무엇이고, 왜 사용할까요?

9.1 제네릭(Generic)의 정의

제네릭은 자바에서 타입을 일반화하여 클래스나 메서드를 작성할 수 있는 기능입니다. 즉, 제네릭을 사용하면 데이터 타입에 구애받지 않는 코드를 작성할 수 있습니다.

9.2 제네릭의 주요 목적

  • 타입 안정성: 컴파일 시점에서 타입 체크를 통해 런타임 에러를 방지할 수 있습니다.

  • 재사용성: 데이터 타입에 독립적인 코드를 작성함으로써, 다양한 타입에서 사용할 수 있는 코드 재사용성이 높아집니다.

  • 가독성: 명시적인 타입 지정으로 코드 가독성이 향상됩니다.

9.3 제네릭의 예시

public class Box<T> {
    private T item;

    public void setItem(T item) {
        this.item = item;
    }

    public T getItem() {
        return item;
    }
}

위 코드에서 Box 클래스는 T라는 타입 파라미터를 사용하여 작성되었습니다. 이 클래스는 어떤 타입의 객체든 저장할 수 있으며, 컴파일 시점에 타입을 지정할 수 있습니다.

9.4 제네릭의 장점

  • 타입 안정성 보장: 제네릭을 사용하면 컴파일 시점에 타입 오류를 검출할 수 있어 런타임 에러를 줄일 수 있습니다.

  • 코드 중복 감소: 다양한 타입을 처리하기 위해 중복되는 코드를 줄일 수 있습니다.

  • 유연성: 제네릭을 통해 여러 타입에 대해 동일한 로직을 적용할 수 있습니다.

9.5 사용 예시: 타입 안정성을 보장한 경험

제네릭을 사용하여 타입 안정성을 보장한 예로, ArrayList<String>을 사용한 경험을 들 수 있습니다. 제네릭을 적용하지 않은 ArrayList는 Object 타입의 요소를 받아들이기 때문에, 요소를 꺼낼 때마다 타입 캐스팅을 해야 합니다. 이 과정에서 ClassCastException이 발생할 수 있지만, 제네릭을 사용하면 이러한 문제를 컴파일 시점에 방지할 수 있습니다.

10. 제네릭을 사용한 경험을 소개해 주세요.

10.1 제네릭을 통한 타입 안정성 보장

제네릭을 사용함으로써 타입 안정성을 보장한 사례는 여러 가지가 있습니다. 예를 들어, ArrayList를 사용할 때 제네릭을 적용하면, 요소의 타입을 명확히 지정할 수 있어 컴파일 타임에 타입 오류를 미리 확인할 수 있습니다.

ArrayList<String> stringList = new ArrayList<>();
stringList.add("Hello");
stringList.add("World");
// stringList.add(10); // 컴파일 오류 발생

위 코드에서 stringList는 String 타입의 요소만을 받을 수 있습니다. 만약 잘못된 타입의 데이터를 추가하려고 하면, 컴파일 타임에 오류가 발생하여 프로그램이 안정적으로 동작할 수 있도록 합니다.

10.2 제네릭을 사용한 리팩토링 경험

프로젝트에서 반복적으로 사용되는 코드를 리팩토링하면서 제네릭을 활용한 경험이 있습니다. 초기 코드에서는 다양한 타입의 데이터를 처리하는 여러 메서드가 존재했는데, 제네릭을 사용하여 코드 중복을 줄이고 유지보수성을 높일 수 있었습니다.

public <T> void printArray(T[] array) {
    for (T element : array) {
        System.out.println(element);
    }
}

이와 같은 제네릭 메서드는 배열의 타입에 관계없이 모든 배열을 출력할 수 있어 코드의 재사용성을 극대화했습니다.

PreviousWeek 1(2/2) - OOPNextWeek2(2/2) Lambda, Stream, Annotation, Reflection

Last updated 9 months ago