// 메소드 오버로딩
class MyHome {
void mySimpleRoom(int n) {...}
void mySimpleRoom(int n1, int n2) {...}
void mySimpleRoom(double d1, double d2) {...}
}
class AAA {
void simple(int p1, int p2) {...} // 1
void simple(int p1, double p2) {...} // 2
}
// 어떤 메소드가 호출?
// 캐릭터형은 int형으로 자동 형변환 => 2번이 없으면 1번 메소드가 호출
// 캐릭터형은 double형으로 자동 형변환 => 1번이 없으면 2번 메소드가 호출
// 뭔가 모호한 상황 => 그래서 자동 형변환이 일어나지 않도록 구현해야 합니다! ( 강제 형변환 사용 )
AAA inst = new AAA();
inst.simple(7, 'K');
class Person {
private int regiNum; // 주민등록 번호
private int passNum; // 여권 번호
Person(int rnum, int pnum) {
regiNum = rnum;
passNum = pnum;
}
Person(int rnum) {
regiNum = rnum;
passNum = 0;
}
void showPersonalInfo() {...}
}
this
를 이용한 다른 생성자의 호출this
: 이 인스턴스를 의미Person(int rnum) {
// 이 인스턴스의 오버로딩된 다른 생성자를 호출
this(rnum, 0);
}
this
를 이용한 인스턴스 변수의 접근class SimpleBox {
private int data;
SimpleBox(int data) {
this.data = data;
}
}
String str1 = new String("Simple String");
// 실제로 인스턴스 생성 => 참조 주소 반환 => str2는 이 인스턴스를 참조
String str2 = "The Best String";
void println() {...} // 개행 => 인자를 안받으면...
void println(int x) {...}
void println(String x) {...}
class ImmutableString {
public static void main(String[] args) {
// 이 2개는 동일
String str1 = "Simple String";
String str2 = "Simple String";
// 이 2개는 다름
String str3 = new String("Simple String");
String str4 = new String("Simple String");
if(str1 == str2)
System.out.println("str1과 str2는 동일 인스턴스 참조"); // true
else
System.out.println("str1과 str2는 다른 인스턴스 참조");
if(str3 == str4)
System.out.println("str3과 str4는 동일 인스턴스 참조");
else
System.out.println("str3과 str4는 다른 인스턴스 참조"); // true
}
}
public static void main(String[] args) {
String str1 = "Simple String";
String str2 = str1;
. . .
public static void main(String[] args) {
String str = "two";
switch(str) {
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
default:
System.out.println("default");
}
}
length()
: 문자열 길이를 반환
문자열 연결시키기
concat
: 문자열을 연결해서 새 문자열 참조값을 반환class StringConcat {
public static void main(String[] args) {
String st1 = "Coffee";
String st2 = "Bread";
String st3 = st1.concat(st2);
System.out.println(st3);
String st4 = "Fresh".concat(st3);
System.out.println(st4);
}
}
substring()
String str = "abcdefg";
// 2이후 내용 => cdefg
str.substring(2);
String str = "abcdefg";
// 2~3 내용 => cd
str.substring(2, 4);
==
을 사용하면 참조 값을 비교하게 됩니다.public static void main(String[] args) {
String st1 = "Lexicographically";
String st2 = "lexicographically";
int cmp;
// equals: 내용 비교
if(st1.equals(st2))
System.out.println("두 문자열은 같습니다.");
else
System.out.println("두 문자열은 다릅니다."); // 정답
// compareTo: 일치하면 0, 사전순 앞이면 0보다 작은 값, 사전순 뒤면 0보다 큰 값
cmp = st1.compareTo(st2);
if(cmp == 0)
System.out.println("두 문자열은 일치합니다.");
else if (cmp < 0)
System.out.println("사전의 앞에 위치하는 문자: " + st1);
else
System.out.println("사전의 앞에 위치하는 문자: " + st2);
// 대소문자 비교를 하지 않겠다.
if(st1.compareToIgnoreCase(st2) == 0)
System.out.println("두 문자열은 같습니다.");
else
System.out.println("두 문자열은 다릅니다.");
}
double e = 2.718281;
// "2.718281"를 저장하는 스트링 인스턴스 참조값을 반환
String se = String.valueOf(e);
System.out.println("funny" + "camp");
// 컴파이럴에 의한 자동변환 = 덧셈 연산은 concat 메소드 호출의 결과물!
System.out.println("funny".concat("camp"));
String str = "funny";
str += "camp"; // str = str + "camp"
// 자동변환
str = str.concat("camp");
String str = "age: " + 17;
// 컴파일러가 자동변환
String str = "age: ".concat(String.valueOf(17));
String str = "AB".concat("CD").concat("EF");
→ String str = ("AB".concat("CD")).concat("EF");
→ String str = "ABCD".concat("EF");
→ String str = "ABCDEF";
String birth = "<양>" + 7 + '.' + 16;
// 변환??
"<양>".concat(String.valueOf(7)).concat(String.valueOf('.')).concat(String.valueOf(16));
StringBuilder
: 문자열 저장 공간을 의미toString()
를 호출한 순간 String 인스턴스를 생성 => 참조값을 반환append
는 자기 자신이 속한 인스턴스의 참조값을 반환String birth = (new StringBuilder("<양>").append(7).append('.').append(16)).toString();
public static void main(String[] args) {
// 문자열 "123"이 저장된 인스턴스의 생성
StringBuilder stbuf = new StringBuilder("123");
stbuf.append(45678); // 문자열 덧붙이기
System.out.println(stbuf.toString()); // 12345678
stbuf.delete(0, 2); // 문자열 일부 삭제
System.out.println(stbuf.toString()); // 345678
stbuf.replace(0, 3, "AB"); // 문자열 일부 교체
System.out.println(stbuf.toString()); // AB678
stbuf.reverse(); // 문자열 내용 뒤집기
System.out.println(stbuf.toString()); // 876BA
String sub = stbuf.substring(2, 4); // 일부만 문자열로 반환
System.out.println(sub); // 6B
}