Sawon 객체 생성!!
사원의 이름은 김입니다.
name = 김
Sawon 객체 메모리 해제
class Sawon
{
String name; // null
public Sawon(String name) // 초기화
{
this.name = name;
System.out.println("Sawon 객체 생성!!");
}
public void display()
{
System.out.println("name = " + name);
}
@Override
public String toString() {
return "사원의 이름은 " + name + "입니다.";
}
@Override
protected void finalize() throws Throwable {
System.out.println("Sawon 객체 메모리 해제");
}
}
public class MainClass3 {
public static void main(String[] args) {
Sawon s = new Sawon("김");
System.out.println(s); // .toString() 생략
s.display();
s = null; // 메모리 해제
System.gc(); // garbage collection 직접 호출
}
}
equals()
s1과 s2 다르다.
s1과 s2 같다.
import com.sun.jdi.Value;
class Student
{
int id;
@Override
public boolean equals(Object obj) {
boolean bCheck = false;
if(obj instanceof Student)
{
bCheck= id == ((Student)obj).id;
}
else
{
bCheck=false;
}
return bCheck;
}
// 초기값
public Student(int id)
{
this.id = id;
}
}
public class MainClass4 {
public static void main(String[] args) {
Student s1 = new Student(100);
Student s2 = new Student(100);
if(s1==s2) // 주소값 비교
System.out.println("s1과 s2 같다.");
else
System.out.println("s1과 s2 다르다.");
if(s1.equals(s2)) // 값 비교
System.out.println("s1과 s2 같다.");
else
System.out.println("s1과 s2 다르다.");
}
}
형 변환
-하위 클래스에 값 가져올 때 형 변환 필요
name = 김
class Member
{
String name; // null 값 초기화
// 초기화
public Member(String name)
{
this.name = name;
}
// 값을 출력
public void display()
{
System.out.println("name = " + name);
}
}
public class MainClass5 {
public static void main(String[] args) {
Object obj = new Member("김"); // obj는 name이나 display 없음
Member m = (Member)obj;
m.display(); // void라서 println 생략
}
}
clone()
-메모리 복제
box1.width=640
box1.height=480
box2.width=640
box2.height=480
box1.width=1024
box1.height=768
box2.width=1024
box2.height=768
box1=Box@626b2d4a
box2=Box@626b2d4a
ㅡㅡㅡㅡㅡㅡㅡㅡㅡ복제ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
box1.width=1024
box1.height=768
box3.width=1024
box3.height=768
box1.width=1024
box1.height=768
box3.width=1000
box3.height=500
box1=Box@626b2d4a
box3=Box@5e91993f
box4.width=640
box4.height=480
class Box implements Cloneable
{
int width;
int height;
// 초기화
public Box()
{
width = 640;
height = 480;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class MainClass6 {
public static void main(String[] args) {
Box box1 = new Box();
Box box2 = box1;
System.out.println("box1.width=" + box1.width);
System.out.println("box1.height=" + box1.height);
System.out.println();
System.out.println("box2.width=" + box2.width);
System.out.println("box2.height=" + box2.height);
System.out.println();
box2.width = 1024;
box2.height = 768;
System.out.println("box1.width=" + box1.width);
System.out.println("box1.height=" + box1.height);
System.out.println("box2.width=" + box2.width);
System.out.println("box2.height=" + box2.height);
System.out.println("box1=" + box1);
System.out.println("box2=" + box2);
System.out.println("ㅡㅡㅡㅡㅡㅡㅡㅡㅡ복제ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
try
{
Box box3 = (Box)box1.clone(); // 형변환(리턴형, 매개변수 > Object)
// 값은 그대로 카피하고 > 새로운 메모리 생성
System.out.println("box1.width=" + box1.width);
System.out.println("box1.height=" + box1.height);
System.out.println("box3.width=" + box3.width);
System.out.println("box3.height=" + box3.height);
System.out.println();
box3.width = 1000;
box3.height = 500;
System.out.println("box1.width=" + box1.width);
System.out.println("box1.height=" + box1.height);
System.out.println("box3.width=" + box3.width);
System.out.println("box3.height=" + box3.height);
System.out.println("box1=" + box1);
System.out.println("box3=" + box3);
Box box4 = new Box();
System.out.println("box4.width=" + box4.width);
System.out.println("box4.height=" + box4.height);
} catch(Exception ex) {}
}
}
알아서 참고하자...
'수업' 카테고리의 다른 글
+26 라이브러리 : java.util, java.util.regex, java.util.StringTokenizer (0) | 2022.06.09 |
---|---|
+25 라이브러리 (0) | 2022.06.08 |
+24 예외처리(throw, throws, finally) (0) | 2022.06.07 |
+23 내부클래스, 예외처리 (0) | 2022.06.03 |
+22 super, this, 형변환, 추상클래스, 인터페이스 (0) | 2022.06.02 |