티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/84
class Book {
private String title;
private String author;
public Book (String title, String author) {
this.title = title;
this.author = author;
}
/* @Override
public String toString() {
return title + "," + author;
} */
}
public class BookTest {
public static void main(String[] args) {
Book book = new Book("러브라이브", "리코");
System.out.println(book);
String str = new String("test");
System.out.println(str); // str.toString() 이 오버라이딩이 되어 있음
}
}
@Override
public String toString() {
return title + "," + author;
}
index = hash(key)
// index = 저장 위치
// hash = 해시 함수
// key = 객체 정보
public class Student implements Cloneable {
private int studentNum;
private String studentName;
public Student(int studentNum, String studentName) {
this.studentNum = studentNum;
this.studentName = studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String toString() {
return studentNum + "," + studentName;
}
@Override
public int hashCode() {
return studentNum;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) { // 다운캐스팅 전 instanceof 로 형 확인
Student std = (Student) obj; // 다운 캐스팅
if (this.studentNum == std.studentNum) { // 학번이 같은지
return true;
}
}
return false;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
public class EqualTest {
public static void main(String[] args) throws CloneNotSupportedException {
Student std1 = new Student(100, "리코");
Student std2 = new Student(100, "리코");
Student std3 = std1; // 주소가 같음
System.out.println("std1 == std2 : " + (std1 == std2));
System.out.println("std1 == std3 : " + (std1 == std3)); // true 반환
System.out.println(std1.hashCode()); // hashCode() 재정의를 통해 조작 가능
System.out.println(std2.hashCode()); // hashCode() 재정의를 통해 조작 가능
System.out.println(System.identityHashCode(std1)); // 실제 물리적 위치
System.out.println(System.identityHashCode(std2)); // 실제 물리적 위치
System.out.println("std1 == std2 : " + (std1.equals(std2))); // 재정의를 통해 논리적 구조가 같을 경우 같다고 조작 가능
std1.setStudentName("마리");
Student copyStudent = (Student)std1.clone();
System.out.println(copyStudent);
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
String 선언 방식에 따라 차이가 있음
String str1 = new String("abc");
= 힙 메모리에 인스턴스로 생성되는 경우String str2 = "abc";
= 상수 풀(constrant pool) 에 있는 주소를 참조하는 경우힙 메모리는 생성 시마다 다른 주소 값을 가지지만 상수 풀의 문자열은 모두 같은 주소 값을 가짐
String str1 = new String("abc");
String str2 = new String("abc");
str1 == str2; // false (다른 주소를 가지므로 다른 객체로 판별)
String str3 = "abc";
String str4 = "abc";
str3 == str4; // true (두 변수 모두 "abc" 를 참조하고 있으므로)
public class StringTest {
public static void main(String[] args) {
String java = new String("java");
String android = new String("android");
System.out.println(System.identityHashCode(java));
java = java.concat(android); // 연결을 하더라도
System.out.println(java);
System.out.println(System.identityHashCode(java)); // 기존 java에 연결된 것이 아니라 아예 새로 생성되어 해시값이 다름
}
}
public class StringBuilderTest {
public static void main(String[] args) {
String java = new String("java");
String android = new String("android");
System.out.println(System.identityHashCode(java));
StringBuilder buffer = new StringBuilder(java);
buffer.append(android);
System.out.println(System.identityHashCode(java));
String test = buffer.toString(); // string 인자로 사용하기 위해서는 toString() 필요
System.out.println(test);
}
}
Class c = Class.forName("java.lang.String"); << 클래스명은 클래스의 전체 주소를 적을 것
Class c = 클래스명.Class
Class c = String.Class
String s = new String();
Class c = s.getClass(); // Object 메소드
public class StringTest {
public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName("java.lang.String"); // String 클래스 동적 로드
Constructor[] cons = c.getConstructors(); // Constructor 배열 리스트 생성, String 클래스의 Constructor
for(Constructor co : cons) {
System.out.println(co);
}
Method[] m = c.getMethods(); // Method 배열 리스트 생성, String 클래스의 Method
for (Method mth : m) {
System.out.println(mth);
}
}
}
public class Person {
private String name;
private int age;
public Person() {}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
public class ClassTest {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class c1 = Class.forName("ch04_Class.Person"); // Person 클래스 동적 로딩
Person person = (Person) c1.newInstance(); // 다운 캐스팅한 후 newInstance() 로 객체 생성
person.setName("리코");
System.out.println(person);
Class c2 = person.getClass(); // 이미 인스턴스가 있어야 함
Person p = (Person) c2.newInstance();
System.out.println(p);
Class[] parameterTypes = {String.class};
Constructor cons = c2.getConstructor(parameterTypes);
Object[] initargs = {"마리"};
Person mariPerson = (Person)cons.newInstance(initargs);
System.out.println(mariPerson);
// 위의 코드와 동일한 기능
Person mariPerson2 = new Person("마리");
}
}