배열 (Arrays)
💡배열(Arrays)이란?
- Java에서 배열은 동일한 타입의 여러 값을 저장할 수 있는 컨테이너 객체입니다. 배열을 사용하면 같은 타입의 변수를 여러 개 선언하는 번거로움을 줄일 수 있습니다. 1
An array is a container object that holds a fixed number of values of a single type.
💡사용하는 이유
- 같은 타입의 변수 여러 개를 만들어야 할 때 사용한다.
✅ 예시 : 배열 없이 변수 선언
let score1 = 90;
let score2 = 85;
let score3 = 78;
let score4 = 92;
✅ 예시 : 배열 사용
let scores = [90, 85, 78, 92];
배열은 참조형(Reference Type)
💡배열이 변수에 저장되는 방식
- Java에서 배열은 객체로 취급되며, 배열을 생성하면 해당 크기만큼 메모리가 할당되고, 생성된 배열 객체의 참조값이 변수에 저장됩니다. 2
In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).
✅ 예시
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
int[] arr3 = {1, 2, 3};
System.out.println(arr1); // [I@1a2b3c4d (배열 주소)
System.out.println(arr2); // [I@1a2b3c4d (같은 주소)
System.out.println(arr3); // [I@5f184fc6 (다른 주소)
System.out.println(arr1 == arr2); // true (같은 참조)
System.out.println(arr1 == arr3); // false (값은 같아도 참조는 다름)
}
}
- 배열의 길이는 생성 시 결정되며, 배열 객체의 length 필드를 통해 접근할 수 있습니다. 3
The members of an array type are all of the following: The public final field length, which contains the number of components of the array.
배열의 크기 변경
- Java에서 배열은 한 번 생성되면 그 크기를 변경할 수 없습니다. 즉, 배열의 크기는 고정되어 있으며, 런타임에 동적으로 크기를 변경할 수 없습니다. 배열의 크기를 변경하려면 새로운 배열을 생성하고 기존 배열의 요소를 복사해야 합니다. 4
The length of an array is established when the array is created. After creation, its length is fixed.
출처
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html [본문으로]
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html [본문으로]
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7 [본문으로]
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html [본문으로]
'Java > [인프런] 자바 입문' 카테고리의 다른 글
| [Java] 변수(Variables) 헷갈리는 문법 정리 (0) | 2026.02.01 |
|---|---|
| [Java] GC(Garbage Collection) (0) | 2025.04.26 |
| [Java] 변수의 생존 범위 (Scope) (1) | 2025.04.24 |
| [Java] 제어 흐름 문 (Control Flow Statements) (2) | 2025.04.22 |
| [Java] 변수(Variables) (1) | 2025.04.21 |