📌 Chapter 11
⭐ 메소드 오버로딩
class MyHome{
void myRoom(int n){};
void myRoom(int n1, int n2){};
void myRoom(double d1, double d2){};
}
- 매개변수의 선언이 다르면 동일한 이름의 메소드 정의를 허용(인자 수, 타입)
int myRoom(){};
double myRoom(){};
class AAA{
void simple(int n1, int n2){};
void simple(int n1, double d1){};
}
class Main{
AAA aaa = new AAA();
aaa.simple(7, 'K');
aaa.simple(7, (int)'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() {
System.out.println("주민등록 번호: " + regiNum);
if(passNum != 0)
System.out.println("여권 번호: " + passNum + '\n');
else
System.out.println("여권을 가지고 있지 않습니다. \n");
}
}
class ConOverloading {
public static void main(String[] args) {
Person jung = new Person(335577, 112233);
Person hong = new Person(775544);
jung.showPersonalInfo();
hong.showPersonalInfo();
}
}
✅ this
- 이 인스턴스를 의미
- 생성자 내의
this는 오버로딩된 다른 생성자를 의미한다
Person(int rnum){
this(rnum, 0);
}
class SimpleBox {
private int data;
SimpleBox(int data) {
this.data = data;
}
}
⭐ String클래스
String str1 = new String("Hello");
String str2 = "Hello";
✅ println()
void println(){};
void println(int x){};
void println(String str){};
- println도 다음과 같이 오버로딩 되어있다.
- 문자열은 참조값을 인자로 전달함
✅ 차이점
class ImmutableString {
public static void main(String[] args) {
String str1 = "Simple String";
String str2 = "Simple String";
String str3 = new String("Simple String");
String str4 = new String("Simple String");
if(str1 == str2)
System.out.println("str1과 str2는 동일 인스턴스 참조");
else
System.out.println("str1과 str2는 다른 인스턴스 참조");
if(str3 == str4)
System.out.println("str3과 str4는 동일 인스턴스 참조");
else
System.out.println("str3과 str4는 다른 인스턴스 참조");
}
}
==연산은 참조변수의 참조 값에 대한 비교연산을 수행함
- String인스턴스는
Immutable(변경할 수 없는)인스턴스이다. => 인스턴스 수 최소화
⭐ String클래스의 메소드
✅ 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
class SubString {
public static void main(String[] args) {
String st1 = "abcdefg";
String st2 = st1.substring(2);
System.out.println(st2);
String st3 = st1.substring(2, 4);
System.out.println(st3);
}
}
✅ equals, compareTo, compareToIgnoreCase
class CompString {
public static void main(String[] args) {
String st1 = "Lexicographically";
String st2 = "lexicographically";
int cmp;
if(st1.equals(st2))
System.out.println("두 문자열은 같습니다.");
else
System.out.println("두 문자열은 다릅니다.");
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("두 문자열은 다릅니다.");
}
}
equals는 인스턴스가 지니는 문자열의 내용을 비교(대소문자 구분)
compareTo는 사전편찬상 순서를 비교(같으면 0, 앞이 더 빠르면 음수, 뒤가 더 빠르면 양수)
compareToIgnoreCase는 compareTo와 비슷하지만 대소문자를 무시함
✅ String.valueOf()
- 기본자료형의 값을 문자열로 변환
- valueOf()가 static메소드이기 때문에 인스턴스 생성안하고 호출가능
✅ 문자열의 + 연산
- 컴파일러가 자동으로 +연산에 대해 concat메소드의 호출로 바꿔줌
System.out.println("age" + 17);
⭐ StringBuilder 클래스
String birth = "양력" + 11 + '/' + 30;
birth = "양력".concat(String.valueOf(11)).concat(String.valueOf('/').concat(String.valueOf(30)));
- 기본 자료형의 값을 문자열로 변환하는 과정을 여러번 거쳐야하는 불편함이 존재
- 인스턴스 생성이 엄청 많음
- 이를 해결하기 위해 StringBuilder클래스가 탄생
String birth = (new StringBuilder("양력").append(11).append('/').append(30)).toString();
- StringBuilder클래스는 내부적으로 문자열을 저장하기 위한 메모리공간을 지님
- 이 메모리공간은 String클래스의 메모리 공간과 달리 문자를 추가하거나 삭제하는 것이 가능
- 같은 StringBuilder인스턴스에 대해 append()
✅ 메소드 종류
public StringBuilder append(int i)
public StringBuilder delete(int start, int end)
public StringBuilder insert(int offset, String str)
public StringBuilder replace(int start, int end, String str)
public StringBuilder reverse()
public StringBuilder substring(int start, int end)
public StringBuilder toString()
✅ StringBuffer클래스
- StringBuffer는 스레드에 안전하다.
- 멀티스레드에 안전하게 설계된 StringBuffer는 속도가 느리다.
- 이외에는 StringBuilder와 동일