기본 타입
은 값 자체
를 저장하고, 참조 타입
은 객체가 생성된 메모리 번지
를 저장한다.기본 타입
은 직접 값
을 저장하고 있지만, 참조 타입
은 힙 메모리 영역의 객체 번지
를 저장하고 이 번지를 통해 객체를 참조한다.출처👉[이것이자바다] 5.1 데이터 타입 분류
구분 | 설명 |
---|---|
메소드 영역 | 바이트코드 파일의 내용이 저장되는 영역, static 변수가 저장되며 모든 객체가 접근 가능하다. |
힙 영역 | 객체가 생성되는 영역, 가비지 컬렉션에 대상이 된다. 메소드 영역과 스택 영역의 상수와 변수에서 객체의 번지를 참조한다. |
스택 영역 | 메소드를 호출할 때마다 생성되는 프레임이 저장되는 영역, 메소드 호출이 끝나면 프레임은 자동 제거된다. 프레임 내부에 로컬 변수 스택이 있는데, 여기에 기본 타입 변수와 참조 타입 변수가 생성되고 제거된다. |
==
와 !=
연산자는 변수의 값이 같은지 아닌지 비교한다.기본 타입
은 값
을 비교한다면, 참조 타입
은 객체의 번지
를 비교한다.public class ReferenceType {
public static void main(String[] args) {
int x = 5;
int y = 5;
System.out.println(x == y); // true
int[] arrayX = {1, 3, 5 ,7};
int[] arrayY = {1, 3, 5, 7};
System.out.println(arrayX == arrayY); // false
System.out.println(arrayX.equals(arrayY)); // false
int[] arrayZ = arrayY;
System.out.println(arrayZ == arrayY); // true
System.out.println(arrayZ.equals(arrayY)); // true
}
}
💡 Object객체 equals( )
- 메서드 내부에서
==
연산자를 사용해 객체의 번지가 같은지 비교한다.
스택 영역
에 생성된다.NullPointerException
예외가 발생한다.public class ReferenceType {
public static void main(String[] args) {
int[] lotto = null;
System.out.println(lotto.length); // Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "lotto" is null
}
}
문자열 리터럴이 동일하면 서로 공유하도록 설계
되어있다. 즉, 동일한 String 객체의 번지가 저장된다.new 연산자
로 새로운 객체를 만들면 문자열 리터럴이 동일해도 서로 다른 String 객체의 번지
를 가지게 된다.""
빈 문자열만으로도 String 객체를 생성할 수 있다.String 객체의 equals( ) 메서드
를 사용하면 된다.public class ReferenceType {
public static void main(String[] args) {
String food = "";
System.out.println(food.length()); // 0
String color1 = "red";
String color2 = "red";
System.out.println(color1 == color2); // true
String color3 = new String("red");
System.out.println(color1 == color3); // false
System.out.println(color2 == color3); // false
System.out.println(color1.equals(color3)); // true
}
}
💡 String객체 equals( )
- 메서드 내부에서 조건문으로
==
연산자를 사용해 객체의 번지가 같은지 비교한다.
같지 않다면,
1)instanceof
로 Argument(anObject)가 String 타입인지 확인(true 또는 false 반환)하고, 맞다면 해당 변수(aString)에 자동 캐스팅하여 할당한 값과
2) COMPACT_STRINGS 상수 값과 coder(문자열 저장 방법 : 0(UTF-16인코딩) 또는 1(Latin-1 인코딩))의 값을 비교하고,
3) 두 문자열의 길이와 각 각의 글자를 비교한다.![]()
![]()
charAt( )
메서드를 사용한다.인덱스
란 0에서부터 시작해 '문자열의 길이 - 1'까지의 번호를 말한다.length( )
메서드를 사용한다.replace( )
메서드를 사용한다.substring( )
메서드를 사용한다.beginIndex ~ endIndex -1
까지 문자열을 잘라낸다.public String substring(int beginIndex, int endIndex) { ... }
indexOf( )
메서드를 사용한다.인덱스
를, 없다면 '-1'
을 리턴한다.처음으로 나타나는 위치의 인덱스
를 리턴contains( )
메서드를 사용한다.true
, 없다면 false
를 리턴한다.split( )
메서드를 사용하면 구분자 기준으로 분리된 문자열 배열
을 얻을 수 있다.import java.util.Arrays;
public class StringEx {
public static void main(String[] args) {
String food = "다코야키, 햄버거, 문어숙회, 해물누룽지탕, 문어튀김";
System.out.println(food.length()); // 29
System.out.println(food.charAt(0)); // 다
System.out.println(food.charAt(3)); // 키
System.out.println(food.charAt(29)); // Exception in thread "main" java.lang.StringIndexOutOfBoundsException: index 29, length 29
System.out.println(food.replace("문어", "오징어")); // 다코야키, 햄버거, 오징어숙회, 해물누룽지탕, 오징어튀김
System.out.println(food); // 다코야키, 햄버거, 문어숙회, 해물누룽지탕, 문어튀김
System.out.println(food.substring(3, 7)); // 키, 햄
System.out.println(food); // 다코야키, 햄버거, 문어숙회, 해물누룽지탕, 문어튀김
System.out.println(food.indexOf("문")); // 11
System.out.println(food.indexOf("문어")); // 11
System.out.println(food.contains("누룽지")); // true
System.out.println(Arrays.toString(food.split(","))); // [다코야키, 햄버거, 문어숙회, 해물누룽지탕, 문어튀김]
}
}
new 연산자
를 사용하면 배열의 길이 지정이 가능하고, 데이터 타입의 기본값으로 초기화
된다.import java.util.Arrays;
public class ArrayEx {
public static void main(String[] args) {
int[] age;
//age = {15, 20, 45}; // 컴파일 에러 발생 : illegal start of expression
age = new int[]{15, 20, 45};
System.out.println(Arrays.toString(age)); // [15, 20, 45]
int[] year = {2021, 2022, 2023};
String[] name = {"철수", "영희", "순자"};
System.out.println(Arrays.toString(year)); // [2021, 2022, 2023]
System.out.println(Arrays.toString(name)); // [철수, 영희, 순자]
int[] height = new int[3];
System.out.println(Arrays.toString(height)); // [0, 0, 0]
String[] season = new String[4];
season[0] = "봄";
System.out.println(season[0]); // 봄
System.out.println(season[2]); // null
System.out.println(season.length); // 4
int[] month = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(month)); // [1, 2, 3, 4, 5]
System.out.println(month.length); // 5
int[] printArr = {2, 4, 6}; // (i : 0 - array[i] : 2) (i : 1 - array[i] : 4) (i : 2 - array[i] : 6)
print(printArr);
//print({2,4,6}); //컴파일 에러 발생 : illegal start of expression
}
public static void print(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.printf("(i : %d - array[i] : %d) ", i, array[i]);
}
}
}
[]
2개를, 3차원 배열이라면 대괄호[]
3개를 붙여 선언한다.중괄호
를 중첩시키거나 new 연산자
를 사용할 수 있다.import java.util.Arrays;
public class ArrayEx {
public static void main(String[] args) {
int[][] soccerScore = new int[3][4]; // 3행 4열의 다차원 배열
System.out.println(soccerScore.length); // 3
System.out.println(soccerScore[0].length); // 4
System.out.println(soccerScore[1].length); // 4
System.out.println(soccerScore[2].length); // 4
// System.out.println(score[3].length); // 런타임 에러 발생 : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
soccerScore[0][1] = 15;
System.out.println(Arrays.toString(soccerScore[0])); // [0, 15, 0, 0]
System.out.println(Arrays.toString(soccerScore[1])); // [0, 0, 0, 0]
System.out.println(Arrays.toString(soccerScore[2])); // [0, 0, 0, 0]
// System.out.println(Arrays.toString(score[3])); // 런타임 에러 발생 : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
int[][] basketballScores = {
{20, 30, 40},
{1, 3}
};
System.out.println(basketballScores.length); // 2
System.out.println(Arrays.toString(basketballScores[0])); // [20, 30, 40]
System.out.println(Arrays.toString(basketballScores[1])); // [1, 3]
System.out.println(basketballScores[0][1]); // 3
System.out.println(basketballScores[1][1]); // 3
}
}
public class ArrayEx {
public static void main(String[] args) {
// Q. 주어진 배열 항목의 전체 합과 평균을 구해 출력하는 코드를 작성해보세요.(중첩 for문 이용)
int[][] array = {
{95, 86},
{83, 92, 96},
{78, 83, 93, 87, 88}
};
int count = 0;
int sum = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
count++;
}
}
System.out.printf("전체 합 : %d, 전체 평균 : %.1f", sum, (double) sum/count); // 전체 합 : 881, 전체 평균 : 88.1
}
}
new 연산자
로 새로운 배열을 생성하면 서로 다른 객체의 번지를 갖게 된다.import java.util.Arrays;
public class ArrayEx {
public static void main(String[] args) {
String[] fruits = new String[5];
fruits[0] = "apple";
fruits[1] = "avocado";
fruits[2] = "blackberry";
fruits[3] = "avocado";
String[] brand = {"apple"};
String[] juice = new String[2];
juice[0] = new String("apple");
System.out.println(fruits.length); // 5
System.out.println(Arrays.toString(fruits)); // [apple, avocado, blackberry, avocado, null]
System.out.println(fruits[1] == fruits[3]); // true
System.out.println(fruits[0] == brand[0]); // true
System.out.println(fruits[0] == juice[0]); // false
System.out.println(brand[0] == juice[0]); // false
System.out.println(brand[0].equals(juice[0])); // true
}
}
번지가 그대로
새로운 배열에 복사돼 저장된다.import java.util.Arrays;
public class ArrayEx {
public static void main(String[] args) {
int[] firstArray = {1, 3, 5};
int[] secondArray = new int[5];
for (int i = 0; i < firstArray.length; i++) {
secondArray[i] = firstArray[i];
}
System.out.println(Arrays.toString(secondArray)); // [1, 3, 5, 0, 0]
System.out.println(firstArray[0] == secondArray[0]); // true
int[] newArray = new int[5];
System.arraycopy(firstArray, 0, newArray, 0, firstArray.length);
System.out.println(Arrays.toString(newArray)); // [1, 3, 5, 0, 0]
System.out.println(firstArray[0] == newArray[0]); // true
}
}
public class ArrayEx {
public static void main(String[] args) {
String[] colors = {"FFFFFF", "CCFFCC", "FFCCFF", "E5FFCC"};
for (String color : colors) {
if (color.contains("5")) {
System.out.println(color); // E5FFCC
}
}
}
}
public class ArrayEx {
public static void main(String[] args) {
// Q. 주어진 배열 항목에서 최대값을 출력하는 코드를 작성해보세요.(for문 이용)
int[] array = {1, 5, 3, 8, 2};
int max = 0;
for (int num: array) {
if (max < num) {
max = num;
}
}
System.out.println(max); // 8
}
}
문자열로 취급
되어 String[] 배열의 항목 값으로 저장되고, main()메서드 호출 시 매개값으로 전달된다.public class ArrayEx {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("2개의 입력 값이 필요합니다.");
System.exit(0);
}
System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
}
}
💡 인텔리제이에서 입력값을 주고 실행하는 방법
1) Edit Configurations... 클릭
2) Run/Debug Configurations 대화상자에서 Program arguments 입력란에 값을 입력한다.
한정된 값
을 갖는 타입열거 상수
는 열거 타입으로 사용할 수 있는 한정된 값
을 말하며, 관례적으로 모두 대문자 알파벳
으로 정의한다.열거타입.열거상수
형태로만 대입할 수 있다. 또한, 참조 타입이므로 null
도 대입 가능하다.public enum Rainbow {
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
INDIGO,
PURPLE
}
public class EnumEx {
public static void main(String[] args) {
System.out.println(Rainbow.RED); // RED
Rainbow color = null;
// color = "red"; // 컴파일 에러 : java: incompatible types: java.lang.String cannot be converted to Rainbow
color = Rainbow.INDIGO;
System.out.println(color); // INDIGO
}
}