String str = null;
String str = 'a'; //에러
System.out.println(str.length()) //에러
String 변수;
변수 = 'abc';
===
String 변수 = 'abc';
String a = new String("abc");
String b = new String("abc");
boolean result = 원본문자열.equals(비교문자열):
boolean result = str1.equals(str2):
같은 타입의 데이터를 연속된 공간에 나열하고, 각 데이터에 인덱스를 부여해놓은 자료구조
배열의 특징
**타입[] 변수 = new 타입[길이];**
**String[] names = new String[30];**
**String[] names = new String[30];**
선언
타입[] 변수; // int[] intArr;
타입 변수[]; // int intArr[];
생성
값 목록 이용
타입[] 변수 = { 값0, 값1, 값2 ... };
String[] names = { 'a', 'b' };
// 아래처럼 선언 후 중괄호를 이용한 배열 생성은 허용 x -> new 연산자 이용
타입[] 변수;
변수 = { 값0, 값1, 값2 ... };
변수 = new 타입[] { 값0, 값1, 값2 ... };
names = new String[] { 'a', 'b', 'c' };
String[] names = null;
names = new String[] { 'a', 'b', 'c' };
int sum = add( new int[] { 83, 90, 87 } );
타입[] 변수 = new 타입[길이];
타입[] 변수 = null;
변수 = new 타입[길이];
ex.
int[] scores = new int[30]; // int배열이므로 0~29까지 **0**으로 초기화
String[] names = new String[30]; //String 배열이므로 0~29까지 **null**로 초기화
배열
// 1. 값 목록 이용
int[][] scores = { {95, 80}, {92, 96} };
// 2. new 연산자로 크기 선언
int[][] scores = new int[2][3];
배열은 참조타입 - String[] 의 항목도 String 변수와 같은 취급(문자열리터럴이 동일하면 동일객체)
String[] str = new String[3]; //배열선언
str[0] = "Java";
str[1] = "Java";
str[2] = new String("Java"); //객체 새로 생성
str[0] == str[1] // True : str[0] 과 str[1] 은 같은 객체, 같은 값
str[0].equals(str[1]); // True : str[0] 과 str[1] 은 같은 객체, 같은 값
str[0] == str[2] // False : 값은 같지만 참조하는 객체는 다름
str[0].equals(str[2]); // True : 참조객체는 다르지만 값은 같음.
배열 복사
배열 생성 후 크기 변경할 수 없을 때 : 더 큰 배열 만들고 이전 배열 값 복사
복사방법
for 문
for (타입변수 : 배열) { // 1. 배열안에 첫번째 값이 존재하면 타입변수 안에 저장.
실행문; // 2. 실행, 다시 1번으로. 값이 있으면 ~ . 반복. }
System.arraycopy()
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
System.arraycopy(arr1, 0, arr2, 0, 0, arr1.length);
열거상수 중 하나의 상수를 저장하는 타입
선언
// 열거타입선언
public enum Week { Monday, Tuesday, Wednesday }
// 열거타입에 변수 선언
Week today;
Week day;
// 변수에 저장
today = Week.Monday;
// 선언 + 저장
Week today = Week.Moday;
// 변수에 null 값 저장 가능
day = Week.null;