| English | 의미 |
|---|---|
| immutable/ɪˈmjuːtəbl/ | 불변 |
Strings and indexing
- A
Stringis a sequence of characters, numbered from0. - In
"HELLO", index0is'H', index1is'E', … index4is'O'. s.length()gives the count of characters; the last index islength() - 1.- Off-by-one errors here are the most common String bug.
substring
s.substring(start, end)returns the characters from indexstartup to but not includingend."HELLO".substring(1, 3)→"EL"(indices1and2, not3).s.substring(start)(one argument) goes fromstartto the end.- The result's length is
end - start.
indexOf and equals
s.indexOf("lo")returns the first index where the text appears, or-1if not found.s.equals(t)tests whether two Strings have the same characters — returns aboolean.- Use
.equals(), never==, to compare String contents. ==compares references (addresses), not the characters.
Strings are immutable
- A String is immutable 不可变: its characters can never be changed once created.
- Methods like
substringandtoUpperCasereturn a new String — they don't edit the original. s.toUpperCase();alone does nothing visible; you must store the result:s = s.toUpperCase();- Immutability is why String methods always hand back a fresh value.
Compare String contents with .equals(), never ==. == tests whether two references point to the same object, not whether the characters match — so a == b can be false even when both are "cat". And Strings are immutable: s.toUpperCase() returns a new String and leaves s unchanged unless you reassign s = s.toUpperCase();.
Working with "HELLO":
"HELLO".length()→5; index0is'H', index4is'O'."HELLO".substring(1, 4)→"ELL"(indices1, 2, 3)."HELLO".indexOf("L")→2(firstL);.equals("hello")→false(case differs).
A String is indexed from 0 (last index length()-1). substring(start, end) returns indices start to end-1; indexOf finds a position (or -1). Compare contents with .equals(), not ==. Strings are immutable — methods return a new String, so reassign to keep the change.
HELLO를 0부터 인싱
substring(1, 4)는 인덱스 1, 2, 3을 취함 → ELL.
String "HELLO"에서 인덱스 0에 있는 문자는 무엇인가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
문자열은 0부터 인덱싱되므로, 인덱스 0은 'H'이다.
"HELLO".substring(1, 3)의 반환값은 무엇인가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
인덱스 1과 2(3은 포함하지 않음) → EL.
두 String이 동일한 문자를 갖는지 비교하려면 ...를 사용해야 함.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
.equals()는 내용을 비교하고, ==는 참조를 비교함.
s.toUpperCase();를 단독으로 호출하면 String s가原位 변경됨.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
String은 불변(mutable)이 아니다 — s = s.toUpperCase();로 재할당해야 함.
"HELLO".indexOf("L")의 반환값은 무엇인가(첫 번째 L)?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
첫 번째 'L'은 인덱스 2에 위치함.