Java2 - accidentlywoo/sec GitHub Wiki
Java2 λΆν° μ 리νλ λ΄μ©
- Object ν΄λμ€
- Class ν΄λμ€
- String, Wrapper ν΄λμ€
λͺ¨λ ν΄λμ€μ μ΅μμ ν΄λμ€
java.lang.Object ν΄λμ€
- λͺ¨λ ν΄λμ€λ Objectν΄λμ€μμ μμ λ°μ
- λͺ¨λ ν΄λμ€λ Object ν΄λμ€μ λ©μλλ₯Ό μ¬μ©ν μ μμ
- λͺ¨λ ν΄λμ€λ Object ν΄λμ€μ μΌλΆ λ©μλλ₯Ό μ¬μ μνμ¬ μ¬μ©ν μ μμ
κ°μ§κ³ μλ λ©μλλ€ : clone(), equals(), finalize(), getClass(), hashCode(), notify(),notifyAll(), toString(), wait(),..
class Book{
  String title;
  String author;
  public Book(String title, String author){
    this.title = title;
    this.author = author;
  }
  @Override
  public String toString(){
    return author + ","+title;
  }
}
public class ToStringTest{
  psvm(String[] args){
    Book book = new Book("ν μ§","λ°κ²½λ¦¬");
    syso(book);
    String str = new String("ν μ§");
    syso(str.toString());
  }
}
getClass().getName() + '@' + Integer.toHexString(hashCode()) κ°μ²΄μ μ 보λ₯Ό String μΌλ‘ λ°κΎΈμ΄ μ¬μ©ν λ μ μ©ν¨. μλ° ν΄λμ€μ€μλ μ΄λ―Έ μ μλ ν΄λμ€κ° λ§μ.
ex) String, Integer, Calendar λ±
λ§μ ν΄λμ€μμ μ¬μ μνμ¬ μ¬μ©
λ κ°μ²΄μ λμΌν¨μ λ Όλ¦¬μ μΌλ‘ μ¬μ μ ν μ μμ
λ¬Όλ¦¬μ  λμΌν¨ : κ°μ μ£Όμλ₯Ό κ°μ§λ κ°μ²΄
λ Όλ¦¬μ  λμΌν¨ : κ°μ νλ²μ νμ, κ°μ μ£Όλ¬Έ λ²νΈμ μ£Όλ¬Έ
물리μ μΌλ‘ λ€λ₯Έ λ©λͺ¨λ¦¬μ μμΉν κ°μ²΄λΌλ λ Όλ¦¬μ μΌλ‘ λμΌνμ ꡬνκΈ° μν λ©μλ
class Student{
  int studentNum;
  String studentName;
  public Student(int studentNum, String studentName){
    this.studentNum = studentNum;
    this.studentName = studentName;
  }
  @Override
  public boolean equals(Object obj){
    if(obj instance of Student){
       Student std = (Student)obj;
       if(this.studentNum == std.studentNum)
          return true;
       else return false;
    }
  }
}
psvm(String[] args){
  String str1 = new String("abc");
  String str1 = new String("abc");
  syso(str1 == str2); // false
  Student lee = new Student(100, "Lee");
  Student lee2 = new Student(100, "Lee");
  syso(lee == lee2);
  syso(lee.equals(lee2);
}
hashCode() λ©μλμ λ°ν κ° : μΈμ€ν΄μ€κ° μ μ₯λ κ°μλ¨Έμ μ μ£Όμλ₯Ό 10μ§μλ‘ λ°ν
λ κ°μ μλ‘ λ€λ₯Έ λ©λͺ¨λ¦¬μ μμΉν μΈμ€ν΄μ€κ° λμΌνλ€λ κ²μ?
- 
λ Όλ¦¬μ μΌλ‘ λμΌ : equals()μ λ°νκ°μ΄ true 
- 
λμΌν hashCode κ°μ κ°μ§ : hashCode()μ λ°νκ°μ΄ λμΌ 
Integer i1 = new Integer(100);
Integer i2 = new Integer(100);
syso(i1.equals(i2));
syso(i1.hashCode());
syso(i2.hashCode());
// equals -> true // κ°, hashcodeeκ°λ€.
syso(System.identityHashCode(i1));
syso(System.identityHashCode(i2));//μ€μ  λ©λͺ¨λ¦¬ κ°
κ°μ²΄μ 볡μ¬λ³Έμ λ§λ¦
κΈ°λ³Έ ν(Prototype)μΌλ‘ λΆν° κ°μ μμ± κ°μ κ°μ§ κ°μ²΄μ 볡μ¬λ³Έμ μμ±ν μ μμ
κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ μ 보μλμ μλ°°λλ κ°λ₯μ±μ΄ μμΌλ―λ‘ λ³΅μ ν κ°μ²΄λ cloneable μΈν°νμ΄μ€λ₯Ό λͺ μν΄μΌ ν¨
class Book implements Cloneable{//mark interface
  String title;
  String author;
  public Book(String title, String author){
    this.title = title;
    this.author = author;
  }
  @Override
  public String toString(){
    return author + ","+title;
  }
  @Override
  protected Object clone() throws CloneNotSupportedException{
    return super.clone();
  }
}
public class ToStringTest{
  psvm(String[] args){
    Book book = new Book("ν μ§","λ°κ²½λ¦¬");
    syso(book);
    Book book2 = book.clone();
    syso(book2);
  }
}
κ°λΉμ§ μ½λ ν°κ° νΈμΆ, 리μμ€μ ν΄μ 
μΈμ€ν΄μ€κ° νλ©λͺ¨λ¦¬μ ν΄μ§λ λ, κ°λΉμ§μ»¬λ ν°μ μν΄μ νΈμΆλλ€
Classν΄λμ€μ λ€μ μ€λͺ ~
μλ°μ λͺ¨λ ν΄λμ€μ μΈν°νμ΄μ€λ μ»΄νμΌ ν class νμΌλ‘ μμ±λ¨
class νμΌμλ κ°μ²΄μ μ 보 (λ©€λ²λ³μ, λ©μλ, μμ±μλ±)κ° ν¬ν¨λμ΄ μμ
Classν΄λμ€λ μ»΄νμΌλ class νμΌμμ κ°μ²΄μ μ 보λ₯Ό κ°μ Έμ¬ μ μμ
// 1.
String s = new String();
Class c = s.getClass();//getClassλ Objectμ λ©μλ
// 2.
Class c = String.Class;
// 3.
Class c = Class.forName("java.lang.String"); //λ°νμ μμ μμ λ°μΈλ© λμ  λ‘λ©(<-> μ»΄νμΌ μμ μμ λ°μΈλ©λλ μ μ  λ‘λ©)
Class ν΄λμ€λ‘λΆν° κ°μ²΄μ μ 보λ₯Ό κ°μ Έμ νλ‘κ·Έλλ° νλ λ°©μ
λ‘컬μ κ°μ²΄κ° μκ³ μλ£νμ μ μ μλ κ²½μ° μ μ©ν νλ‘κ·Έλλ°
java.lang.reflect ν¨ν€μ§μ μλ ν΄λμ€ νμ©
Class ν΄λμ€ λ©μλ
new ν€μλλ₯Ό μ¬μ©νμ§ μκ³ μΈμ€ν΄μ€λ₯Ό μμ±
psvm(String[] args) throws ClassNotFoundException{
  Class c1 = String.class;
  String str = new String();
  Class c2 = str.getClass();
  Class c3 = Class.forName("java.lang.String");
  Constructor[] cons = c3.getConstructors();
  for(Constructor con : cons){
     sout(con);
  }
  Method[] methods = c3.getMethods();
  for(Method method : methods){
     syso(method);
  }
}
package classex
public class Person{
  private String name;
  private int age;
  public Person(){}
  public Person(String name){
    this.name = name;
  }
  public Person(String name, int age){
    this.name = name;
    this.age = age;
  }
  ~getter/setter
  ~toString 
}
psvm(String[] arg){
  Person person = new Person("james");
  Class c1 = Class.forName("classex.Person");
  Person person1 = (Person)c1.newInstance();
  sout(person1); // null
  Class[] parameterTypes = {String.class};
  Constructor cons = c1.getConstructor(PrameterTypes);
}
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Person person = new Person("james");
        System.out.println(person);
        //λ§€κ°λ³μ μλ κΈ°λ³Έ μμ±μ
        Class c1 = Class.forName("step1.Person");
        Person person1 = (Person) c1.newInstance(); //return type Object, κΈ°λ³Έ μμ±μ νΈμΆ
        System.out.println("Class : "+person1);
        // λ§€κ°λ³μκ° μλ μμ±μ
        Class[] parameterTypes= {String.class};
        Constructor cons = c1.getConstructor(parameterTypes);
        Object[] initargs = {"κΉμ μ "};
        Person person2 = (Person) cons.newInstance(initargs);
        System.out.println(person2);
    }
Class ν΄λμ€ static λ©μλ
λμ  λ‘λ©μ΄λ? μ»΄νμΌ μμ λ°μ΄ν° νμ μ΄ λͺ¨λ bidingλμ΄ μλ£νμ΄ λ‘λ©λλ κ²(static loading)μ΄ μλλΌ μ€ν μ€μ λ°μ΄ν° νμ μ μκ³ bidingλλ λ°©μ
μ€ν μμ λ‘λ©λλ―λ‘ κ²½μ°μ λ°λΌ λ€λ₯Έ ν΄λμ€κ° μ¬μ©λ μ μμ΄ μ μ©ν¨
μ»΄νμΌ νμμ 체ν¬ν μ μμΌλ―λ‘ ν΄λΉ λ¬Έμμ΄μ λν ν΄λμ€κ° μλ κ²½μ° μμΈ(ClassNotFoundException)μ΄ λ°μν μ μμ
Stringμ Immutable
public final class String implements java.io.Serializable, Comparable<String>, CharSequence{
  private final char value[]; // μ΅μ΄ νλ² ν λΉ
}
String ν΄λμ€ μ μΈνκΈ°
String str1 = new String("abc"); //μΈμ€ν΄μ€λ‘ μμ±λ¨
String str2 = "abc"; // μμνμ μλ λ¬Έμμ΄μ κ°λ¦¬ν΄
Wrapper ν΄λμ€λ λκ°μ΄ μ μ©μ΄ λλ λ΄μ©
νλ² μ μΈλκ±°λ μμ±λ λ¬Έμμ΄μ λ³κ²½ν μ μμ
String ν΄λμ€μ concat() λ©μλ νΉμ "+"λ₯Ό μ΄μ©νμ¬ Stringμ μ°κ²°νλ κ²½μ° λ¬Έμμ΄μ μλ‘ μμ±λ¨.
κ°λ³μ μΈ char[] λ°°μ΄μ λ©€λ²λ³μλΌ κ°μ§κ³ μλ ν΄λμ€
λ¬Έμμ΄μ λ³κ²½νκ±°λ μ°κ²°νλ κ²½μ° μ¬μ©νλ©΄ νΈλ¦¬ν ν΄λμ€
StringBufferλ λ©ν° μ°λ λ νλ‘κ·Έλλ°μμ λκΈ°ν(Synchronization)μ΄ λ³΄μ₯λ¨ λ¨μΌ μ°λ λ νλ‘κ·Έλλ°μμλ StringBuilderλ₯Ό μ¬μ©νλ κ²μ΄ μ±λ₯μ΄ μ’μ
toString() λ©μλλ‘ Stringλ°ν
primitive -> C/C++μμ λμ΄μ΄ : μ΄κ±Έ μ°λ κ²μ κ°μ²΄μ§ν₯μ μ΄κΈλλ€.λΌκ³ λ§νλ μ¬λλ€μ΄μλ€.
Pythonμ κ²½μ° Primitive typeκ°μκ² μμ. λͺ¨λ κ²μ΄ κ°μ²΄