본문 바로가기

백엔드개발자 준비하기

[백엔드개발자 준비하기] 오브젝트클래스

오브젝트
-최상위 클래스
-생성된 모든 클래스들은 항상 최상위 클래스로 오브젝트 클래스를 가진다

생략되어있던 기본 생성자가 다른 생성자를 만들면 사라지듯 오브젝트 클래스 상속도 생략되어있다가 다른 클래스 상속으로 바꾸면 사라진다
-생략되어있던 것들이 다른것으로 명시하는 순간 대체된다
-어차피 상속받은 부모 클래스도 상위클래스는 오브젝트라 단일상속 가능

인스턴스.getClass()
-생성된 객체의 클래스 정보를 가져옴

클래스.class
-해당 클래스의 정보를 가져옴

 

[ToString]

public class ToString {

	public static void main(String[] args) {
		
		Student student1 = new Student("조병철", 34);
		Student student2 = new Student("김동민", 28);
		Student student3 = new Student("김두영", 28);
		Student student4 = new Student("강의진", 29);
		
		System.out.println(student1);
		System.out.println(student2);
		System.out.println(student3);
		System.out.println(student4);

		System.out.println();
		
		System.out.println(student1.toString());
		System.out.println(student2.toString());
		System.out.println(student3.toString());
		System.out.println(student4.toString());
	}
}

[GetClass]

public class ObjectGetClass {

	public static void main(String[] args) {
		Student student = new Student("김재영", 29);
		
		System.out.println(student.getClass());
		System.out.println(Student.class);
		
		System.out.println(student.getClass().getName());
		
		Class studentClass = student.getClass();
		
		String className = studentClass.getName();
		System.out.println("클래스 이름 : " + className);
		
		String simpleName = studentClass.getSimpleName();
		System.out.println("클래스 이름만 : " + simpleName);
	
		Field[] fields = studentClass.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field);
		}
		
		Method[] methods = studentClass.getDeclaredMethods();
		for (Method method : methods) {
			System.out.println(method);
		}
	}
}

[Equals]

public class ObjectEquals {

	public static void main(String[] args) {
		// String은 
		String name1 = "김준일";
		String name2 = "김준일";
		String name3 = new String("김준일");
		
		System.out.println(name1);
		System.out.println(name2);
		
		System.out.println(name1 == name2);
		System.out.println(name1.equals(name3));
		
////////////////////////////////////////////////////////////////////////////////		
		
		Student s1 = new Student("박은빈", 24);
		Student s2 = new Student("박은빈", 24);
		SubStudent s3 = new SubStudent("박은빈", 24);
		
		System.out.println(s1.equals(s3));
	}
}

[HashCode]

public class ObjectHashCode {

	public static void main(String[] args) {
		Student s1 = new Student("김수현", 31);
		Student s2 = new Student("김수현", 31);
		SubStudent s3 = new SubStudent("김수현", 31);
		
		System.out.println(s1.hashCode());
		System.out.println(s2.hashCode());
		
		System.out.println(s1.hashCode() == s2.hashCode());
		System.out.println(s1.hashCode() == s3.hashCode());
	}
}

[finalize]

class Test {
	private int num;

	public Test(int num) {
		super();
		this.num = num;
		System.out.println(num + "생성");
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println(num + "객체 소멸");
	}
	
}

public class Objectfinalize {

	public static void main(String[] args) {
		Test test = null;
		
			
			test = new Test(i);
			
			test = null;
			
			System.gc();
		}
	}
}