public class Object {
...
public native int hashCode(); <--내용x
//네이티브 메서드: os의 메서드(c언어로 작성됨)
equals()를 오버라이딩하면, hashCode()도 오버라이딩해야 한다.
--> equals()의 결과가 true인 두 객체의 해시코드는 같아야 하기 때문
(참고만)System.identityHashCode(Object obj)는 Object클래스의 hashCode()와 동일
toString() : 객체를 문자열(String)으로 변환하기 위한 메서드
(객체==iv집합이므로, 객체를 문자열로 변환하다는 것은
iv의 값을 문자열로 변환한다는 것과 같다.)
protected Object clone()
//객체 자신의 복사본을 반환
public boolean equals(Object obj)
//객체 자신과 객체 obj가 같은 객체인지 알려준다.(같으면 true)
protected void finalize()
//객체가 소멸될 때 가비지 컬렉터에 의해 자동적으로 호출.
//이 때 수행되어야하는 코드가 있을 때 오버라이딩한다.(거의 사용안함)
public Class getClass()
//객체 자신의 클래스 정보를 담고 있는 Class인스턴스를 반환
//Class의 C가 대문자인 것은 클래스(설계도)의 정보를 담기 위한 것.
public int hashCode()
//객체 자신의 해시코드를 반환
public String toString()
//객체 자신의 정보를 문자열로 반환
public void notify()
//객체 자신을 사용하려고 기다리는 쓰레드를 하나만 깨운다.
public void notifyAll()
//객체 자신을 사용하려고 기다리는 모든 쓰레드를 깨운다.
[
public void wait()
public void wait(long timeout)
public void wait(long timeout, int nanos)
]
//다른 쓰레드가 notify()나 notifyAll()을 호출할 때까지
//현재 쓰레드를 무한히 또는 지정된 시간(timeout, nanos)동안
기다리게 한다.(timeout은 천분의 1초, nanos는 10^9분의 1초)
//equals를 오버라이딩
class Value {
int value;
Value(int value) {
this.value = value;
}
public boolean equals(Object obj) {
if(!(obj instanceof Value)) return false;
return this.value==((Value)obj).value;
}
}
//equals를 오버라이딩
class Person {
long id;
public boolean equals(Object obj) {
if(!(obj instanceof Person)) return false;
Person p = (Person)obj;
return this.id ==p.id;
}
Person(long id) {
this.id = id;
}
}
String클래스 = 데이터(char[]) + 메서드(문자열 관련)
내용을 변경할 수 없는 불변(immutable) 클래스
덧셈 연산자(+)를 이용한 문자열 결합은 성능이 떨어짐.
문자열의 결합이나 변경이 잦다면, 내용을 변경가능한 StringBuffer를 사용
String str1 = "abc"; //분자열 리터럴 "abc"의 주소가 str1에 저장됨
String str2 = "abc"; //분자열 리터럴 "abc"의 주소가 str2에 저장됨
String str3 = new String("abc"); //새로운 String인스턴스 생성
String str4 = new String("abc"); //새로운 String인스턴스 생성
new String("abc);-->항상 새로운 문자열이 만들어 짐
String(char[] value)
//주어진 문자열(value)을 갖는 String인스턴스를 생성
//char[] --> String
//char[] c = {'H','e','l','l','o'};
//String s = new String(s);
//==> s = "Hello"
//String-->char[]
//--> tocharArray() 사용
(StringBuffer는 내용 변경 가능)
String(StringBuffer buf)
//StringBuffer인스턴스가 갖고있는 문자열과 같은 내용의 String인스턴스를 생성
//StringBuffer --> String
char charAt(int index)
//문자 1개 반환
int compareTo(String str)
//문자열(str)과 사전순서로 비교.
//같으면 0, 사전순으로 이전이면 음수, 이후면 양수를 반환
//정렬 할 때 사용
String concat(String str)
//문자열(str)을 뒤에 덧붙임
boolean contains(CharSequence s)
//지정된 문자열(s)이 포함되어 있는지 검사
boolean endsWith(String suffix)
//지정된 문자열(suffix)로 끝나는지 검사한다.
boolean equalsIgnoreCase(String str)
//대소문자 구분없이 비교
int indexOf(int ch)
//주어진 문자(ch)가 문자열에 존재하는지 확인하여 위치(index)를 알려줌.
//못 찾으면 -1을 반환
//(index는 0부터 시작)
int indexOf(int ch, int pos)
int indexOf(String str)
int lastIndexOf(int ch)
//뒤에서부터 확인
int lastIndexOf(String str)
int length()
String[] split(String regax)
(regax: 정규식, 규칙이 있는 식)
String[] split(String regex, int limit)
//문자열을 지정된 수로 자른다
boolean startsWith(String prefix)
//주어진 문자열(prefix)로 시작하는지 검사
String substring(int begin)
String substring(int begin, int end)
//문자열을 찾음. 시작위치는 범위에포함.But 끝 위치의 문자는 포함X
(begin<=x && x<end)
String toLowerCase()
String toUpperCase()
//소문자 또는 대문자로 반환
String trim()
//좌 우 공백을 없앰
static String valueOf(boolean b 등 기본형)
//기본형을 문자열로 반환
//참조형의 경우 toString()을 호출한 결과를 반환
//""더하는 것 보다 성능이 빠름
String animals = "dog,cat,bear";
String[] arr = animals.split(",");
String str = String.join("-",arr);
System.out.println(str); //dog-cat-bear
숫자를 문자열로 바꾸는 방법
--> 숫자+"", valueOf
문자열을 숫자로 바꾸는 방법
Integer.parseInt("문자열")
--> 반환타입 int, 옛날 방법
Integer.valueOf("문자열")
--> 반환타입 Integer이지만 반환타입 int도 가능, 새로운 방법
StringBuffer()
//16문자를 담을 수 있는 버퍼를 가진 StringBuffer 인스턴스를 생성
StringBuffer(int length)
StringBuffer(String str)
//크기지정
StringBuffer append(기본형과 Object obj)
int capacity()
//StringBuffer의 버퍼 크기(char[]크기)를 알려줌
char charAt(int index)
StringBuffer delete(int start, int end)
//문자 n개 제거, 끝 위치의 문자는 제외
StringBuffer deleteCharAt(int index)
//문자 1개 제거
StringBuffer insert(int pos, 기본형+Object obj)
//지정된 위치(pos)에 추가, pos는 0부터 시작
int length()
StringBuffer replace(int start, int end, String str)
//from ~ to를 변환
StringBuffer reverse()
//문자열 뒤집기
void setCharAt(int index, char ch)
지정된 위치의 문자 하나를 ch로 바꾼다
void setLength(int newLength)
//길이 변경
String toString()
//StringBuffer를 String으로 반환
String substring(int start)
String substring(int start, int end)
//from ~ to 문자 잘라내기
싱글 쓰레드 : 한번에 1개 작업
멀티 쓰레드 : 한번에 n개 작업
static double abs(double a)
//float, int, long도 가능
//절대값 반환
static double ceil(double a)
//올림
static double floor(doubla a)
//floor는 바닥-->버림
static double max(double a, double b)
//큰 것 반환
static double min(double a, double b)
//작은 것 반환
static double random()
static double rint(double a)
//짝수 반올림, 1.5, 2.5 등은 반올림해서 짝수를 만듬
static long round(double a)
static long round(double a)
//일반 반올림
문자열 -> 기본형
: Interger.parstInt("100"), ...
문자열 -> 래퍼클래스
: Integer.valueOf("100"), ...
래퍼클래스 -> 문자열
Byte b = new Byte("100");
String str = b.toString();
int -> Integer //오토박싱
Integer -> int //언박싱