ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 221219. 자바 12. 제네릭 <>
    스타터스 백엔드 3기 2022. 12. 19. 16:12

    하루입니다.

     

     

     


     

     

    제네릭 <>

    • <>를 사용하여 동일 타입 데이터만 저장하도록 강제하였다. 
    • ArrayList에 Employee타입의 매개변수를 전달하는 것과 유사하다.
    • generic의 장점
              1. 컴파일 타입을 체크해서 미리 오류를 발견할 수 있다.
              2. 형변환 코드가 사라진다. add()할 때도 Employee, get()할 때도 Employee이다.

     

     

     

    	식별자 - 데이터
    HashMap<String, String[]> map = new HashMap();
    HashSet<Integer> set = new HashSet(5);
    ArrayList<Employee> list = new ArrayList(5);

     

     


     

     

    제네릭 정의하기

    class Apple {
    	String origin;
    	public Apple(String origin) {
    		super();
    		this.origin = origin;
    	}
    }
    
    class Paper {
    	String size;
    	public Paper(String size) {
    		super();
    		this.size = size;
    	}
    }
    
    // T : type 매개변수
    // BOX : 원시타입
    class Box<T> {
    	T contents;
    	
    	public Box(T contents) {
    		super();
    		this.contents = contents;
    	}
    
    	public T getContents() {
    		return contents;
    	}
    
    	public void setContents(T contents) {
    		this.contents = contents;
    	}
    }
    
    public class NonGenericTest {
    	public static void main(String[] args) {
    		Apple a = new Apple("아산");
    		Paper p = new Paper("A4");
    		Box<Apple> aBox = new Box(a);		
    		Box<Paper> pBox = new Box(p);	
    		System.out.println(aBox.contents.origin);
    		System.out.println(pBox.contents.size);
    	}
    }
    
    ----------------------------------------------------------
    
    아산
    A4

     

     

    제네릭으로 타입 한정하기

    • <T extends Solid> 를 사용해서 제네릭으로 타입을 한정할 수 있다.
    • Solid의 자식 클래스만 생성 가능하다.
    • Wine은 Solid를 extends 하지 않았고, 컴파일 오류가 발생한다.
    class Solid {
    	public String tellName() {
    		return "고체";
    	}
    }
    
    class Apple3 extends Solid{
    	String origin;
    	public Apple3(String origin) {
    		super();
    		this.origin = origin;
    	}
    }
    
    class Wine3 {
    	String kind;
    	public Wine3(String kind) {
    		super();
    		this.kind = kind;
    	}
    }
    // type으로 가능한 것을 한정한다.
    class Box3<T extends Solid>  {	// Solid와 자식 클래스만 가능하도록 할 것이다.
    	T contents;
    	
    	public Box3(T contents) {
    		super();
    		this.contents = contents;
    	}
    
    	public T getContents() {
    		return contents;
    	}
    
    	public void setContents(T contents) {
    		this.contents = contents;
    	}
    }
    
    public class NonGenericTest3 {
    
    	public static void main(String[] args) {
    		Apple3 a = new Apple3("아산");
    		Box3<Apple3> aBox = new Box3(a);		
    		Box3<Solid> sBox = new Box3(new Solid());	
    		System.out.println(aBox.contents.tellName());
    //		Box3<Wine3> wBox = new Box3(new Wine3("로제와인"));	
    	}
    }

     

     

     

     

     

     


     

    ArrayListTest HashMapTest HashSetTest NonGenericTest NonGenericTest2 NonGenericTest3

     

    스레드

    • 프로그램 : 컴퓨터 cpu가 실행할 수 있는 이해 가능한 이진코드의 집합체
    • 프로세스 : 현재 cpu가 실행중인 프로그램
    • window / mac / linux : 1번에여러개 프로그램 동시실행 가능
    • OS가 멀티프로세스 환경을 지원한다.

     

    싱글스레드

    멀티스레드 : 1번에 여러개의 스레드를 동시에 실행할 수 있는 환경ㅁ.

    1개 cpu 3개 시간 분할 실행 방식 + 우서순위

    자바웹서버 - 멀티스레드 환경 구현

Designed by Tistory.