Object class
-java.lang 패키지에 속한 클래스이다.
-Object는 모든 클래스에 강제로 상속되며 Object만이 아무 클래스도 상송 받지 않는 유일한 클래스이다.(최상위 클래스)
메소드 | 설명 |
boolean equals(Object obj) | obj가 가리키는 객체와 현재 객체를 비교하여 같으면 true 리턴 |
Class getClass() | 현 객체의 클래스 값을 리턴 |
int hashCode() | 현 객체에 대한 해시 코드 값 리턴 |
String toString() | 현 객체에 대한 문자열 표현을 리턴 |
void notify() | 현 객체에 대해 대기하고 있는 하나의 스레드를 깨운다. |
void notifyAll() | 현 객체에 대해 대기하고 있는 모든 스레드를 깨운다. |
void wait() | 다른 스레드가 깨울 때까지 현재 스레드를 대기하게 한다. |
프로세스와 스레드
프로세스 : 운영체제에서 실행 중인 프로그램, 독립적인 메모리 공간을 가짐
스레드 : 프로세스 내에서 실행되는 작업 단위. 같은 메모리 공간을 공유
스레드는 프로세스 내에서 실행되는 독립적인 실행 흐름
예제 6-1
class Point{
private int x,y;
public Point(int x,int y) {
this.x=x;
this.y=y;
}
}
public class ObjectPropertyEx {
public static void print(Object obj) {
System.out.println(obj.getClass().getName());//클래스 이름
System.out.println(obj.hashCode());//해시코드 값
System.out.println(obj.toString());//객체를 문자열로 만들어 출력
System.out.println(obj);//객체 출력
}
public static void main(String[] args) {
Point p=new Point(2,3);
print(p);
}
}
출력결과:
Point
515132998
Point@1eb44e46
Point@1eb44e46
이때, toString()의 경우, return 값은 "클래스 이름+@+해시코드16진수"이다.
이때 객체+문자열 연산이나, 객체를 출력하는 경우 toString()이 자동을 호출된다.
System.out.println(p); 의 경우
System.out.println(p.toString()); 으로 자동 변환된다.
String s=p+"점"; 의 경우
String s=p.toString()+"점";으로 자동 변환
클래스에 toString() 만들기
Object의 toString()을 오버라이딩 하여 자신만의 문자열을 리턴할 수 있다.
예제 6-2
class Point{
private int x,y;
public Point(int x,int y) {
this.x=x;
this.y=y;
}
public String toString(){
return "Point("+x+","+y+")";
}
}
public class ToStringEx {
public static void main(String[] args) {
Point p=new Point(2,3);
System.out.println(p.toString());
System.out.println(p);
System.out.println(p+"입니다.");
}
}
출력결과:
Point(2,3)
Point(2,3)
Point(2,3)입니다.
객체 비교와 equals() 매소드
두 객체가 같은지 비교해야 될 경우에 equals()를 사용한다.
그렇다면 ==와 equals()는 뭐가 다를까?
1. ==연산자
-> 두 객체의 내용물이 같은지 비교합니다. 즉 두 레퍼런스가 동일한 객체를 가르키는 지 비교합니다.
2. boolean equals(Object obj)
객체의 내용이 같은지 비교하는 메소드이다.
예를들어
String a = new String("Hello");
String b = new String("Hello");
가 있다면
a와b는 서로 다른 객체이므로 a==b라면 false 값을 리턴하고
a.equals(b)라고 하면 객체의 내용이 같으므로 true 값을 리턴한다.
예제6-3
class Point{
private int x,y;
public Point(int x,int y) {
this.x=x;
this.y=y;
}
public boolean equals(Object obj) {
Point p=(Point)obj; //객체 obj를 Point로 다운캐스팅
if(x==p.x&&y==p.y) return true;
else return false;
}
}
public class EqualsEx {
public static void main(String[] args) {
Point a=new Point(2,3);
Point b=new Point(2,3);
Point c=new Point(3,4);
if(a==b)
System.out.println("a==b");
if(a.equals(b))
System.out.println("a is equals to b");
if(a.equals(c))
System.out.println("a is equal to c");
}
}
출력결과
a is equals to b
예제 6-4
class Rect{
private int width;
private int height;
public Rect(int width, int height)
{
this.width=width;
this.height=height;
}
public boolean equals(Object obj) {
Rect p=(Rect)obj;
if (width*height==p.width*p.height) return true;
else return false;
}
}
public class RectEqualsEx {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rect a=new Rect(2,3);
Rect b=new Rect(3,2);
Rect c=new Rect(3,4);
if(a.equals(b)) System.out.println("a is equal to b");
if(a.equals(c)) System.out.println("a is equal to c");
if(b.equals(c)) System.out.println("b is equal to c");
}
}
면적이 같은지 객체를 비교해보는 예제입니다.
출력결과 :
a is equal to b
'전공수업정리 > Java' 카테고리의 다른 글
[JAVA]입출력 스트림과 파일 입출력 (0) | 2024.05.27 |
---|---|
[JAVA]StringBuffer 클래스 (0) | 2024.05.16 |
[JAVA]String 클래스 (1) | 2024.05.15 |
[JAVA] 모듈 (0) | 2024.05.10 |
[JAVA]패키지 (0) | 2024.05.10 |