Programming/Java27 래퍼 클래스 래퍼 클래스(Wrapper class)class Main { public static void main(String[] args) { int num = 5; Integer boxnum = Integer.valueOf(num); // 명시적 박싱 Integer autobox = num; // 자동 박싱 int nubox = boxnum.intValue(); // 명시적 박싱 int autonubox = autobox; // 자동 언박싱 }}자바의 래퍼 클래스(Wrapper Class)는 기본 데이터 타입(primitive data type)을 객체로 다룰 수 있도록 해주는 클래스입니다. 자바는 기본.. 2024. 8. 9. 메모리 모델과 Object 클래스 메모리 모델가상머신은 메모리 공간을 크게 3가지 영역으로 나뉩니다.메소드 영역(Method Area)스택 영역 (Stack Area)힙 영역 (Heap Area)메소드 영역(Method Area)인스턴스 생성 및 클래스 변수의 접근을 위해서 메모리 공간에 올려지는데 올려지는 메모리 공간을 메소드영역이라고 합니다.모든 클래스 데이터, 메소드 정보 및 상수 등이 여기 저장되어 프로그램이 실행될 때 필요에 따라 사용됩니다. 스택 영역 (Stack Area)각 스레드별로 메소드 호출 시마다 스택 프레임이 생성됩니다. 스택 프레임에는 지역 변수, 매개변수, 중간 결과 등이 포함됩니다.메소드 실행과 관련된 데이터를 관리하며, 메드 호출이 끝나면 스택 프레임은 제거됩니다. 힙 영역(Heap Area)모든 객체와 그 .. 2024. 8. 8. 인터페이스와 추상 클래스 인터페이스public interface Car { void run(); void stop(); default void Klaxon() { System.out.println("클락션"); }}class Volvo implements Car { @Override public void run() { System.out.println("달립니다."); } @Override public void stop() { System.out.println("멈춥니다."); } @Override public void Klaxon() { System.out.println("경적"); }}class Ma.. 2024. 8. 7. 자바 예외처리 예외처리(Exceotion Handling)class Main { public static void main(String[] args) { try { int[] numbers = new int[5]; numbers[10] = 30; // ArrayIndexOutOfBoundsException 발생 } catch (ArrayIndexOutOfBoundsException e) { System.out.println("오류 발생: " + e.getMessage()); } finally { System.out.println("이곳은 항상 실행됩니다."); .. 2024. 8. 6. 상속 상속class Car { private int speed; public Car(int speed) { this.speed = speed; } public void printSpeed() { System.out.println("Speed : " + this.speed); }}class Volvo extends Car { private String name; public Volvo(String name, int speed) { super(speed); // 슈퍼클래스의 생성자 호출 this.name = name; } @Override public void printSpeed() { .. 2024. 8. 6. 배열 배열public class Array { public static void main(String[] args) { int[] arr = new int[] {1, 2, 3, 4, 5}; int[] ar = new int[5]; ar[0] = 1; ar[1] = 2; ar[2] = 3; ar[3] = 4; ar[4] = 5; for(int i : arr) { System.out.println(i); } for(int i : ar) { System.out.println(i); } }}배열(Array)은 동일한 .. 2024. 8. 5. 문자열 클래스 문자열(String)String str1 = "Hello";String str2 = "Hello";System.out.println(str1 == str2); // trueString str1 = new String("Hello");String str2 = new String("Hello");System.out.println(str1 == str2); // false자바에서 문자열을 만드는 방법에는 두 가지가 있습니다.리터럴 방식("")new String("")두 가지 방법의 차이점그림을 보면 위 코드에서 == 을 했을 때 리터럴은 true가 되고 new String은 false가 나오는지 알 수 있습니다.== 연산은 문자열을 비교하는 것이 아닌 문자열의 참조값을 비교하여 true/false 값을 반환하.. 2024. 8. 3. 메소드 오버로딩(Method overloading) 메소드 오버로딩public class MathOL { // 두 정수의 합을 반환 public int add(int a, int b) { return a + b; } // 세 정수의 합을 반환 public int add(int a, int b, int c) { return a + b + c; } // 두 실수의 합을 반환 public double add(double a, double b) { return a + b; }}public class Main { public static void main(String[] args) { MathOL mathOps = new MathOL(); Syste.. 2024. 8. 3. 정보 은닉과 캡슐화 정보 은닉(Information Hiding)public class BankAccount { // private 필드: 외부에서 직접 접근 불가 private double balance; // public 생성자 public BankAccount(double initialBalance) { if (initialBalance > 0) { this.balance = initialBalance; } }} 정보 은닉(Information Hiding)은 객체 지향 프로그래밍(OOP)에서 중요한 개념 중 하나로, 객체 내부의 데이터와 구현 세부 사항을 외부로부터 숨기고, 객체가 제공하는 공개 인터페이스를 통해서만 접근하도록 제한하는 방법을 말.. 2024. 8. 2. 이전 1 2 3 다음