Java2 - accidentlywoo/sec GitHub Wiki

Java2 λΆ€ν„° μ •λ¦¬ν•˜λŠ” λ‚΄μš©

  • Object 클래슀
  • Class 클래슀
  • String, Wrapper 클래슀

Object 클래슀

λͺ¨λ“  클래슀의 μ΅œμƒμœ„ 클래슀

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());
  }
}

toString() λ©”μ„œλ“œμ˜ μ›ν˜•

getClass().getName() + '@' + Integer.toHexString(hashCode()) 객체의 정보λ₯Ό String 으둜 λ°”κΎΈμ–΄ μ‚¬μš©ν•  λ•Œ μœ μš©ν•¨. μžλ°” ν΄λž˜μŠ€μ€‘μ—λŠ” 이미 μ •μ˜λœ ν΄λž˜μŠ€κ°€ 많음.

ex) String, Integer, Calendar λ“±

λ§Žμ€ ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ν•˜μ—¬ μ‚¬μš©

equals() λ©”μ„œλ“œ

두 객체의 동일함을 λ…Όλ¦¬μ μœΌλ‘œ μž¬μ •μ˜ ν•  수 있음

물리적 동일함 : 같은 μ£Όμ†Œλ₯Ό κ°€μ§€λŠ” 객체

논리적 동일함 : 같은 ν•™λ²ˆμ˜ 학생, 같은 μ£Όλ¬Έ 번호의 μ£Όλ¬Έ

물리적으둜 λ‹€λ₯Έ λ©”λͺ¨λ¦¬μ— μœ„μΉ˜ν•œ 객체라도 λ…Όλ¦¬μ μœΌλ‘œ λ™μΌν•œμ„ κ΅¬ν•˜κΈ° μœ„ν•œ λ©”μ„œλ“œ

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() λ©”μ„œλ“œ

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));//μ‹€μ œ λ©”λͺ¨λ¦¬ κ°’

clone()λ©”μ„œλ“œ

객체의 볡사본을 λ§Œλ“¦

κΈ°λ³Έ ν‹€(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);
  }
}

finalize()

가비지 μ½œλ ‰ν„°κ°€ 호좜, λ¦¬μ†ŒμŠ€μ˜ ν•΄μ œ

μΈμŠ€ν„΄μŠ€κ°€ νž™λ©”λͺ¨λ¦¬μ— ν•΄μ§€λ λ•Œ, 가비지컬렉터에 μ˜ν•΄μ„œ ν˜ΈμΆœλœλ‹€

getClass()

Classν΄λž˜μŠ€μ™€ λ‹€μ‹œ μ„€λͺ…~

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"); //λŸ°νƒ€μž„ μ‹œμ μ—μ„œ 바인딩 동적 λ‘œλ”©(<-> 컴파일 μ‹œμ μ—μ„œ λ°”μΈλ”©λ˜λŠ” 정적 λ‘œλ”©)

reflection ν”„λ‘œκ·Έλž˜λ°

Class ν΄λž˜μŠ€λ‘œλΆ€ν„° 객체의 정보λ₯Ό 가져와 ν”„λ‘œκ·Έλž˜λ° ν•˜λŠ” 방식

λ‘œμ»¬μ— 객체가 μ—†κ³  μžλ£Œν˜•μ„ μ•Œ 수 μ—†λŠ” 경우 μœ μš©ν•œ ν”„λ‘œκ·Έλž˜λ°

java.lang.reflect νŒ¨ν‚€μ§€μ— μžˆλŠ” 클래슀 ν™œμš©

newInstance() λ©”μ†Œλ“œ

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);
    }

forName() λ©”μ†Œλ“œμ™€ 동적 λ‘œλ”©

Class 클래슀 static λ©”μ†Œλ“œ

동적 λ‘œλ”©μ΄λž€? 컴파일 μ‹œμ— 데이터 νƒ€μž…μ΄ λͺ¨λ‘ bidingλ˜μ–΄ μžλ£Œν˜•μ΄ λ‘œλ”©λ˜λŠ” 것(static loading)이 μ•„λ‹ˆλΌ μ‹€ν–‰ 쀑에 데이터 νƒ€μž…μ„ μ•Œκ³  bidingλ˜λŠ” 방식

μ‹€ν–‰ μ‹œμ— λ‘œλ”©λ˜λ―€λ‘œ κ²½μš°μ— 따라 λ‹€λ₯Έ ν΄λž˜μŠ€κ°€ μ‚¬μš©λ  수 μžˆμ–΄ μœ μš©ν•¨

컴파일 νƒ€μž„μ— 체크할 수 μ—†μœΌλ―€λ‘œ ν•΄λ‹Ή λ¬Έμžμ—΄μ— λŒ€ν•œ ν΄λž˜μŠ€κ°€ μ—†λŠ” 경우 μ˜ˆμ™Έ(ClassNotFoundException)이 λ°œμƒν•  수 있음

String

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을 μ—°κ²°ν•˜λŠ” 경우 λ¬Έμžμ—΄μ€ μƒˆλ‘œ 생성됨.

StringBuilder와 StringBuffer

가변적인 char[] 배열을 λ©€λ²„λ³€μˆ˜λΌ 가지고 μžˆλŠ” 클래슀

λ¬Έμžμ—΄μ„ λ³€κ²½ν•˜κ±°λ‚˜ μ—°κ²°ν•˜λŠ” 경우 μ‚¬μš©ν•˜λ©΄ νŽΈλ¦¬ν•œ 클래슀

StringBufferλŠ” λ©€ν‹° μ“°λ ˆλ“œ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ 동기화(Synchronization)이 보μž₯됨 단일 μ“°λ ˆλ“œ ν”„λ‘œκ·Έλž˜λ°μ—μ„œλŠ” StringBuilderλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ„±λŠ₯이 μ’‹μŒ

toString() λ©”μ„œλ“œλ‘œ Stringλ°˜ν™˜

Wrapper

primitive -> C/C++μ—μ„œ λ„˜μ–΄μ˜΄ : 이걸 μ“°λŠ” 것은 객체지ν–₯에 μ–΄κΈ‹λ‚œλ‹€.라고 λ§ν•˜λŠ” μ‚¬λžŒλ“€μ΄μžˆλ‹€.

Python의 경우 Primitive typeκ°™μ€κ²Œ μ—†μŒ. λͺ¨λ“ κ²ƒμ΄ 객체

⚠️ **GitHub.com Fallback** ⚠️